|
| 1 | + |
| 2 | +## Input |
| 3 | + |
| 4 | +```javascript |
| 5 | +import {useIdentity, Stringify} from 'shared-runtime'; |
| 6 | + |
| 7 | +/** |
| 8 | + * TODO: Note that this `Array.from` is inferred to be mutating its first |
| 9 | + * argument. This is because React Compiler's typing system does not yet support |
| 10 | + * annotating a function with a set of argument match cases + distinct |
| 11 | + * definitions (polymorphism). |
| 12 | + * |
| 13 | + * In this case, we should be able to infer that the `Array.from` call is |
| 14 | + * not mutating its 0th argument. |
| 15 | + * The 0th argument should be typed as having `effect:Mutate` only when |
| 16 | + * (1) it might be a mutable iterable or |
| 17 | + * (2) the 1st argument might mutate its callee |
| 18 | + */ |
| 19 | +function Component({value}) { |
| 20 | + const arr = [{value: 'foo'}, {value: 'bar'}, {value}]; |
| 21 | + useIdentity(); |
| 22 | + const derived = Array.from(arr, (x, idx) => ({...x, id: idx})); |
| 23 | + return <Stringify>{derived.at(-1)}</Stringify>; |
| 24 | +} |
| 25 | + |
| 26 | +export const FIXTURE_ENTRYPOINT = { |
| 27 | + fn: Component, |
| 28 | + params: [{value: 5}], |
| 29 | + sequentialRenders: [{value: 5}, {value: 6}, {value: 6}], |
| 30 | +}; |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +## Code |
| 35 | + |
| 36 | +```javascript |
| 37 | +import { c as _c } from "react/compiler-runtime"; |
| 38 | +import { useIdentity, Stringify } from "shared-runtime"; |
| 39 | + |
| 40 | +/** |
| 41 | + * TODO: Note that this `Array.from` is inferred to be mutating its first |
| 42 | + * argument. This is because React Compiler's typing system does not yet support |
| 43 | + * annotating a function with a set of argument match cases + distinct |
| 44 | + * definitions (polymorphism). |
| 45 | + * |
| 46 | + * In this case, we should be able to infer that the `Array.from` call is |
| 47 | + * not mutating its 0th argument. |
| 48 | + * The 0th argument should be typed as having `effect:Mutate` only when |
| 49 | + * (1) it might be a mutable iterable or |
| 50 | + * (2) the 1st argument might mutate its callee |
| 51 | + */ |
| 52 | +function Component(t0) { |
| 53 | + const $ = _c(4); |
| 54 | + const { value } = t0; |
| 55 | + const arr = [{ value: "foo" }, { value: "bar" }, { value }]; |
| 56 | + useIdentity(); |
| 57 | + const derived = Array.from(arr, _temp); |
| 58 | + let t1; |
| 59 | + if ($[0] !== derived) { |
| 60 | + t1 = derived.at(-1); |
| 61 | + $[0] = derived; |
| 62 | + $[1] = t1; |
| 63 | + } else { |
| 64 | + t1 = $[1]; |
| 65 | + } |
| 66 | + let t2; |
| 67 | + if ($[2] !== t1) { |
| 68 | + t2 = <Stringify>{t1}</Stringify>; |
| 69 | + $[2] = t1; |
| 70 | + $[3] = t2; |
| 71 | + } else { |
| 72 | + t2 = $[3]; |
| 73 | + } |
| 74 | + return t2; |
| 75 | +} |
| 76 | +function _temp(x, idx) { |
| 77 | + return { ...x, id: idx }; |
| 78 | +} |
| 79 | + |
| 80 | +export const FIXTURE_ENTRYPOINT = { |
| 81 | + fn: Component, |
| 82 | + params: [{ value: 5 }], |
| 83 | + sequentialRenders: [{ value: 5 }, { value: 6 }, { value: 6 }], |
| 84 | +}; |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +### Eval output |
| 89 | +(kind: ok) <div>{"children":{"value":5,"id":2}}</div> |
| 90 | +<div>{"children":{"value":6,"id":2}}</div> |
| 91 | +<div>{"children":{"value":6,"id":2}}</div> |
0 commit comments