Copy a JSX props spread before later props that need statements
Why
The props {...base, b: v} of <Foo {...base} b={v} /> copy base where the spread stands, before v is evaluated. sequence_arguments got the bare spread as an argument. A variable is safe to delay, so it stayed in the object literal, after the statements of any later prop, child or key that needs them (a try, or a switch that is not one expression). A mutation of the base in those statements showed up in the props. The spread is now the copy {...base} while the arguments are sequenced, so it is held in a temporary when later code can change it, and otherwise stays in place and is merged with the other props as before. Copying a record runs no code, so statements that only define pure values don't need the temporary.
modifiedJS JSX props spread with a prop that needs statements
@@config({flags: ["-bs-jsx", "4"]})@inline(never) let id = x => xmodule Foo = { type props = {mutable a: int, b?: int} @react.componentWithProps let make = (props: props) => React.int(props.a)}let f = (base: Foo.props) => <Foo {...base} b={try { base.a = 100; id(5) } catch { | _ => 0 }} />
js
Before
After
function f(base) {function f(base) { let tmp; let tmp = {...base}; let tmp$1; try { try { base.a = 100; base.a = 100; tmp = id(5); tmp$1 = id(5); } catch (exn) { } catch (exn) { tmp = 0; tmp$1 = 0; } } return JsxRuntime.jsx(Example$Foo, { return JsxRuntime.jsx(Example$Foo, { ...base, b: tmp ...tmp, b: tmp$1 }); });}}
Review
- Is a lone spread copied? No,
<Foo {...base} />, with or without a key, still passesbaseitself. - Why spread the copy again instead of assigning the props to it? The element stays a
Jsxnode, which JSX preserve mode can print as JSX. - Which checked-in outputs change? Only the new test's.