Skip to content
Myr

fix(jsx): throw new Error instead of string (#4241)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Taku Amano · committed by GitHub · parent 54cf3fb · Browse files · 2 files+4 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+4 added−4 removed

  • tests:+2 added−2 removed
  • other:+2 added−2 removed

2 files+4 added−4 removed

modified:src/jsx/base.ts+2 added-2 removed
207 unmodified lines
208 buffer[0] += ` ${key}=""`209 }210 } else if (key === 'dangerouslySetInnerHTML') {211 if (children.length > 0) {212 throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.'212 throw new Error('Can only set one of `children` or `props.dangerouslySetInnerHTML`.')213 }214215 children = [raw(v.__html)]216 } else if (v instanceof Promise) {217 buffer[0] += ` ${key}="`218 buffer.unshift('"', v)219 } else if (typeof v === 'function') {220 if (!key.startsWith('on')) {221 throw `Invalid prop '${key}' of type 'function' supplied to '${tag}'.`221 throw new Error(`Invalid prop '${key}' of type 'function' supplied to '${tag}'.`)222 }223 // maybe event handler for client components, just ignore in server components224 } else {225 buffer[0] += ` ${key}="`
208 unmodified lines
modified:src/jsx/index.test.tsx+2 added-2 removed
186 unmodified lines
187188 it('Should get an error if both dangerouslySetInnerHTML and children are specified', () => {189 expect(() =>190 (<span dangerouslySetInnerHTML={{ __html: '" is allowed here' }}>Hello</span>).toString()191 ).toThrow()191 ).toThrow(Error)192 })193 })194195 // https://en.reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored
165 unmodified lines
361362 it('should raise an error if used in other props', () => {363 const onClick = () => {}364 const template = <button data-handler={onClick}>Click</button>365 expect(() => template.toString()).toThrow()365 expect(() => template.toString()).toThrow(Error)366 })367 })368369 // https://en.reactjs.org/docs/jsx-in-depth.html#functions-as-children
696 unmodified lines