diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call-within-lambda.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call-within-lambda.js new file mode 100644 index 0000000000000000000000000000000000000000..f3cd8932c763d4cd6303ebc311955838207ab901 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call-within-lambda.js @@ -0,0 +1,17 @@ +function Foo({}) { + const outer = val => { + const fact = x => { + if (x <= 0) { + return 1; + } + return x * fact(x - 1); + }; + return fact(val); + }; + return outer(3); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..61fbfc5efec51e0f0de881d3808289abb9c44ba8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +function Foo({value}: {value: number}) { + const factorial = (x: number) => { + if (x <= 1) { + return 1; + } else { + return x * factorial(x - 1); + } + }; + + return factorial(value); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{value: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo(t0) { + const $ = _c(2); + const { value } = t0; + let t1; + if ($[0] !== value) { + const factorial = (x) => { + if (x <= 1) { + return 1; + } else { + return x * factorial(x - 1); + } + }; + t1 = factorial(value); + $[0] = value; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ value: 3 }], +}; + +``` + +### Eval output +(kind: ok) 6 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.ts new file mode 100644 index 0000000000000000000000000000000000000000..21d693ac25d0b7f04345314943c6fef7d4715507 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-recursive-call.ts @@ -0,0 +1,16 @@ +function Foo({value}: {value: number}) { + const factorial = (x: number) => { + if (x <= 1) { + return 1; + } else { + return x * factorial(x - 1); + } + }; + + return factorial(value); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{value: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a4775bc45bea99a288fa29483038764aa03c5eb9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function get2() { + const callbk = () => { + const copy = x; + return copy; + }; + const x = 2; + return callbk(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: get2, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function get2() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const callbk = () => { + const copy = x; + return copy; + }; + const x = 2; + t0 = callbk(); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: get2, + params: [], +}; + +``` + +### Eval output +(kind: ok) 2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.js new file mode 100644 index 0000000000000000000000000000000000000000..5970998a9d7309a54678b8652824a67fdbd38e4e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.js @@ -0,0 +1,13 @@ +function get2() { + const callbk = () => { + const copy = x; + return copy; + }; + const x = 2; + return callbk(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: get2, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9397518d2d116424f2d52e3a38d8f21c7443de80 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.expect.md @@ -0,0 +1,73 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees +function useFoo() { + const onClick = response => { + setState(DISABLED_FORM); + }; + + const [state, setState] = useState(); + const handleLogout = useCallback(() => { + setState(DISABLED_FORM); + }, [setState]); + const getComponent = () => { + return handleLogout()} />; + }; + + // this `getComponent` call should not be inferred as mutating setState + return [getComponent(), onClick]; // pass onClick to avoid dce +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees +function useFoo() { + const $ = _c(7); + const onClick = (response) => { + setState(DISABLED_FORM); + }; + + const [, t0] = useState(); + const setState = t0; + let t1; + if ($[0] !== setState) { + t1 = () => { + setState(DISABLED_FORM); + }; + $[0] = setState; + $[1] = t1; + } else { + t1 = $[1]; + } + setState; + const handleLogout = t1; + let t2; + if ($[2] !== handleLogout) { + const getComponent = () => handleLogout()} />; + t2 = getComponent(); + $[2] = handleLogout; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== onClick || $[5] !== t2) { + t3 = [t2, onClick]; + $[4] = onClick; + $[5] = t2; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.js new file mode 100644 index 0000000000000000000000000000000000000000..4a44679390a80145c4ab80f0c1396119776eee19 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-setstate-captured-indirectly-jsx.js @@ -0,0 +1,17 @@ +// @validatePreserveExistingMemoizationGuarantees +function useFoo() { + const onClick = response => { + setState(DISABLED_FORM); + }; + + const [state, setState] = useState(); + const handleLogout = useCallback(() => { + setState(DISABLED_FORM); + }, [setState]); + const getComponent = () => { + return handleLogout()} />; + }; + + // this `getComponent` call should not be inferred as mutating setState + return [getComponent(), onClick]; // pass onClick to avoid dce +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7939c9143d1d765d72be4f5c8bbe02aac642f774 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function hoisting() { + const foo = () => { + return bar + baz; + }; + const bar = 3; + const baz = 2; + return foo(); // OK: called outside of TDZ for bar/baz +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function hoisting() { + const $ = _c(1); + let foo; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + foo = () => bar + baz; + + const bar = 3; + const baz = 2; + $[0] = foo; + } else { + foo = $[0]; + } + return foo(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 5 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.js new file mode 100644 index 0000000000000000000000000000000000000000..dd4aea4c555fc970b126f5847e2f2b7ae31a7508 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-const-declaration.js @@ -0,0 +1,14 @@ +function hoisting() { + const foo = () => { + return bar + baz; + }; + const bar = 3; + const baz = 2; + return foo(); // OK: called outside of TDZ for bar/baz +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2df5c8ec44ef6bad4b338de2502fe6cd0a05a598 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +function hoisting() { + const foo = () => { + return bar(); + }; + const bar = () => { + return 1; + }; + + return foo(); // OK: bar's value is only accessed outside of its TDZ +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function hoisting() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const foo = () => bar(); + const bar = _temp; + t0 = foo(); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() { + return 1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 1 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..785c4181e718f57bf93d51302bb9c1ab7ddb7695 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-function-expression.js @@ -0,0 +1,16 @@ +function hoisting() { + const foo = () => { + return bar(); + }; + const bar = () => { + return 1; + }; + + return foo(); // OK: bar's value is only accessed outside of its TDZ +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8d694a984aed539e8e7ac9fdb4331b829e1f4faf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function hoisting() { + let foo = () => { + return bar + baz; + }; + let bar = 3; + let baz = 2; + return foo(); // OK: called outside of TDZ for bar/baz +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function hoisting() { + const $ = _c(1); + let foo; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + foo = () => bar + baz; + + let bar = 3; + let baz = 2; + $[0] = foo; + } else { + foo = $[0]; + } + return foo(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 5 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.js new file mode 100644 index 0000000000000000000000000000000000000000..b9a02619f5c9aab203a71de1845881af42b94d56 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-simple-let-declaration.js @@ -0,0 +1,14 @@ +function hoisting() { + let foo = () => { + return bar + baz; + }; + let bar = 3; + let baz = 2; + return foo(); // OK: called outside of TDZ for bar/baz +} + +export const FIXTURE_ENTRYPOINT = { + fn: hoisting, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-skip-type-alias-declarations.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-skip-type-alias-declarations.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..114923a07f939a05fd6ee404f0837b2905c46e34 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hoisting-skip-type-alias-declarations.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +// @flow +type Item = {id: string, name: string}; + +function Component({items}: {items: Array}) { + const onClick = () => { + const mapped: Array = items.map(item => ({ + id: item.id, + name: item.name.toUpperCase(), + })); + submit(mapped); + }; + return + {/** + * The scope for the + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; + +function Component() { + const $ = _c(2); + const [count, setCount] = useState(0); + let t0; + if ($[0] !== count) { + t0 = ( +
+ + + +
+ ); + $[0] = count; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js new file mode 100644 index 0000000000000000000000000000000000000000..0eb93a2a460b35d74be927089573b023519dbe5f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-deps-subset-of-decls.js @@ -0,0 +1,21 @@ +import {useState} from 'react'; + +function Component() { + const [count, setCount] = useState(0); + return ( +
+ + {/** + * The scope for the +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a1f70cea197d977e6f981a5e81b9b53265cfe3fc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +const {getNumber} = require('shared-runtime'); + +function Component(props) { + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + return {session_id: getNumber()}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +const { getNumber } = require("shared-runtime"); + +function Component(props) { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { session_id: getNumber() }; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) {"session_id":4} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.js new file mode 100644 index 0000000000000000000000000000000000000000..9d63fc8165a9a36c8c4631f073c9ff6d8903c499 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-no-deps.js @@ -0,0 +1,12 @@ +const {getNumber} = require('shared-runtime'); + +function Component(props) { + // Two scopes: one for `getNumber()`, one for the object literal. + // Neither has dependencies so they should merge + return {session_id: getNumber()}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d31d366b1e0d7b9d92da4630e513946a0500cf67 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.expect.md @@ -0,0 +1,110 @@ + +## Input + +```javascript +import {useState} from 'react'; +import {Stringify} from 'shared-runtime'; + +// This is a translation of the original merge-consecutive-scopes which uses plain objects +// to describe the UI instead of JSX. The JSXText elements in that fixture happen to +// prevent scome scopes from merging, which concealed a bug with the merging logic. +// By avoiding JSX we eliminate extraneous instructions and more accurately test the merging. +function Component(props) { + let [state, setState] = useState(0); + return [ + {component: Stringify, props: {text: 'Counter'}}, + {component: 'span', props: {children: [state]}}, + { + component: 'button', + props: { + 'data-testid': 'button', + onClick: () => setState(state + 1), + children: ['increment'], + }, + }, + ]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; +import { Stringify } from "shared-runtime"; + +// This is a translation of the original merge-consecutive-scopes which uses plain objects +// to describe the UI instead of JSX. The JSXText elements in that fixture happen to +// prevent scome scopes from merging, which concealed a bug with the merging logic. +// By avoiding JSX we eliminate extraneous instructions and more accurately test the merging. +function Component(props) { + const $ = _c(11); + const [state, setState] = useState(0); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { component: Stringify, props: { text: "Counter" } }; + $[0] = t0; + } else { + t0 = $[0]; + } + let t1; + if ($[1] !== state) { + t1 = { component: "span", props: { children: [state] } }; + $[1] = state; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] !== state) { + t2 = () => setState(state + 1); + $[3] = state; + $[4] = t2; + } else { + t2 = $[4]; + } + let t3; + if ($[5] === Symbol.for("react.memo_cache_sentinel")) { + t3 = ["increment"]; + $[5] = t3; + } else { + t3 = $[5]; + } + let t4; + if ($[6] !== t2) { + t4 = { + component: "button", + props: { "data-testid": "button", onClick: t2, children: t3 }, + }; + $[6] = t2; + $[7] = t4; + } else { + t4 = $[7]; + } + let t5; + if ($[8] !== t1 || $[9] !== t4) { + t5 = [t0, t1, t4]; + $[8] = t1; + $[9] = t4; + $[10] = t5; + } else { + t5 = $[10]; + } + return t5; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) [{"component":"[[ function params=1 ]]","props":{"text":"Counter"}},{"component":"span","props":{"children":[0]}},{"component":"button","props":{"data-testid":"button","onClick":"[[ function params=0 ]]","children":["increment"]}}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.js new file mode 100644 index 0000000000000000000000000000000000000000..c229d6b480a1c6cb60b5eb1216b01859d95ff457 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes-objects.js @@ -0,0 +1,27 @@ +import {useState} from 'react'; +import {Stringify} from 'shared-runtime'; + +// This is a translation of the original merge-consecutive-scopes which uses plain objects +// to describe the UI instead of JSX. The JSXText elements in that fixture happen to +// prevent scome scopes from merging, which concealed a bug with the merging logic. +// By avoiding JSX we eliminate extraneous instructions and more accurately test the merging. +function Component(props) { + let [state, setState] = useState(0); + return [ + {component: Stringify, props: {text: 'Counter'}}, + {component: 'span', props: {children: [state]}}, + { + component: 'button', + props: { + 'data-testid': 'button', + onClick: () => setState(state + 1), + children: ['increment'], + }, + }, + ]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2e246c0e1c041fb69f7f3cd6aeb473a4ac95f00d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.expect.md @@ -0,0 +1,91 @@ + +## Input + +```javascript +import {useState} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Component() { + let [state, setState] = useState(0); + return ( +
+ + {state} + +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; +import { Stringify } from "shared-runtime"; + +function Component() { + const $ = _c(8); + const [state, setState] = useState(0); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + let t1; + if ($[1] !== state) { + t1 = {state}; + $[1] = state; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] !== state) { + t2 = ( + + ); + $[3] = state; + $[4] = t2; + } else { + t2 = $[4]; + } + let t3; + if ($[5] !== t1 || $[6] !== t2) { + t3 = ( +
+ {t0} + {t1} + {t2} +
+ ); + $[5] = t1; + $[6] = t2; + $[7] = t3; + } else { + t3 = $[7]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok)
{"text":"Counter"}
0
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.js new file mode 100644 index 0000000000000000000000000000000000000000..b0a72c2a389ba19d31f41580b7d2db072a6fb81e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-consecutive-scopes.js @@ -0,0 +1,20 @@ +import {useState} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Component() { + let [state, setState] = useState(0); + return ( +
+ + {state} + +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..16ac0b2966e58588633d974bfc4fe632bbf14117 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {setProperty} from 'shared-runtime'; + +function Component(props) { + // start of scope for y, depend on props.a + let y = {}; + + // nested scope for x, dependent on props.a + const x = {}; + setProperty(x, props.a); + // end of scope for x + + y.a = props.a; + y.x = x; + // end of scope for y + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { setProperty } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let y; + if ($[0] !== props.a) { + y = {}; + + const x = {}; + setProperty(x, props.a); + + y.a = props.a; + y.x = x; + $[0] = props.a; + $[1] = y; + } else { + y = $[1]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"a":42,"x":{"wat0":42}} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.js new file mode 100644 index 0000000000000000000000000000000000000000..39db609334d2481aa718c86b44824d484d6380ac --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/merge-nested-scopes-with-same-inputs.js @@ -0,0 +1,22 @@ +import {setProperty} from 'shared-runtime'; + +function Component(props) { + // start of scope for y, depend on props.a + let y = {}; + + // nested scope for x, dependent on props.a + const x = {}; + setProperty(x, props.a); + // end of scope for x + + y.a = props.a; + y.x = x; + // end of scope for y + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..08a3ccb87f93f36416e4fbced6fb1b480afbd378 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.expect.md @@ -0,0 +1,100 @@ + +## Input + +```javascript +// @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros:"cx" +import {identity} from 'shared-runtime'; + +const DARK = 'dark'; + +function Component() { + const theme = useTheme(); + return ( +
+ ); +} + +function cx(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(' '); +} + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros:"cx" +import { identity } from "shared-runtime"; + +const DARK = "dark"; + +function Component() { + const $ = _c(2); + const theme = useTheme(); + + const t0 = cx({ + "styles/light": true, + "styles/dark": theme.getTheme() === DARK, + }); + let t1; + if ($[0] !== t0) { + t1 =
; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +function cx(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(" "); +} + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.js new file mode 100644 index 0000000000000000000000000000000000000000..c72f69375443236e666e1061a485a2b02e556890 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-assigned-to-temporary.js @@ -0,0 +1,39 @@ +// @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros:"cx" +import {identity} from 'shared-runtime'; + +const DARK = 'dark'; + +function Component() { + const theme = useTheme(); + return ( +
+ ); +} + +function cx(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(' '); +} + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c99b05a11338cd40b41aae200cc3823ba8dc54d8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.expect.md @@ -0,0 +1,104 @@ + +## Input + +```javascript +// @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) +import {identity} from 'shared-runtime'; + +const DARK = 'dark'; + +function Component() { + const theme = useTheme(); + return ( +
+ ); +} + +const cx = { + foo(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(' '); + }, +}; + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) +import { identity } from "shared-runtime"; + +const DARK = "dark"; + +function Component() { + const $ = _c(2); + const theme = useTheme(); + + const t0 = cx.foo({ + "styles/light": true, + "styles/dark": identity([theme.getTheme()]), + }); + let t1; + if ($[0] !== t0) { + t1 =
; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +const cx = { + foo(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(" "); + }, +}; + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.js new file mode 100644 index 0000000000000000000000000000000000000000..4254c5a4428b012c28734fbc0e3cf71da93b9813 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-assigned-to-temporary.js @@ -0,0 +1,41 @@ +// @compilationMode:"infer" @enableAssumeHooksFollowRulesOfReact:false @customMacros(cx) +import {identity} from 'shared-runtime'; + +const DARK = 'dark'; + +function Component() { + const theme = useTheme(); + return ( +
+ ); +} + +const cx = { + foo(obj) { + const classes = []; + for (const [key, value] of Object.entries(obj)) { + if (value) { + classes.push(key); + } + } + return classes.join(' '); + }, +}; + +function useTheme() { + return { + getTheme() { + return DARK; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..90de08f333520d9ba1908912819bac4f2b0d0d9c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +// @compilationMode:"infer" +import {makeArray} from 'shared-runtime'; + +function Component() { + const items = makeArray('foo', 'bar', '', null, 'baz', false, 'merp'); + const classname = cx.namespace(...items.filter(isNonEmptyString)); + return
Ok
; +} + +function isNonEmptyString(s) { + return typeof s === 'string' && s.trim().length !== 0; +} + +const cx = { + namespace(...items) { + return items.join(' '); + }, +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"infer" +import { makeArray } from "shared-runtime"; + +function Component() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const items = makeArray("foo", "bar", "", null, "baz", false, "merp"); + const classname = cx.namespace(...items.filter(isNonEmptyString)); + t0 =
Ok
; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +function isNonEmptyString(s) { + return typeof s === "string" && s.trim().length !== 0; +} + +const cx = { + namespace(...items) { + return items.join(" "); + }, +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
Ok
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.js new file mode 100644 index 0000000000000000000000000000000000000000..41aebae7e335d74b6405f99e9dc9d745ddc8ad2b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-isms/repro-cx-namespace-nesting.js @@ -0,0 +1,23 @@ +// @compilationMode:"infer" +import {makeArray} from 'shared-runtime'; + +function Component() { + const items = makeArray('foo', 'bar', '', null, 'baz', false, 'merp'); + const classname = cx.namespace(...items.filter(isNonEmptyString)); + return
Ok
; +} + +function isNonEmptyString(s) { + return typeof s === 'string' && s.trim().length !== 0; +} + +const cx = { + namespace(...items) { + return items.join(' '); + }, +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..65b2fd6ac8b40f0de311bf35448d521c614d057e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.expect.md @@ -0,0 +1,70 @@ + +## Input + +```javascript +function a() { + return import.meta.url; +} + +function b() { + let a = 0; + if (import.meta.url) { + a = 1; + } + return a; +} + +function c() { + let a = 0; + if (import.meta.foo) { + a = 1; + } + return a; +} + +function d() { + let a = 0; + if (import.meta) { + a = 1; + } + return a; +} + +``` + +## Code + +```javascript +function a() { + return import.meta.url; +} + +function b() { + let a = 0; + if (import.meta.url) { + a = 1; + } + + return a; +} + +function c() { + let a = 0; + if (import.meta.foo) { + a = 1; + } + + return a; +} + +function d() { + let a = 0; + if (import.meta) { + a = 1; + } + + return a; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.mjs b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.mjs new file mode 100644 index 0000000000000000000000000000000000000000..3f387b2699f1e28a54bc56da908e2c5e376c42f0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/meta-property.mjs @@ -0,0 +1,27 @@ +function a() { + return import.meta.url; +} + +function b() { + let a = 0; + if (import.meta.url) { + a = 1; + } + return a; +} + +function c() { + let a = 0; + if (import.meta.foo) { + a = 1; + } + return a; +} + +function d() { + let a = 0; + if (import.meta) { + a = 1; + } + return a; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2fc302c8b42878a153bf35c80a63e84acab7f881 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +function foo(a, b, c) { + // Construct and freeze x, y + const x = makeObject(a); + const y = makeObject(a); +
+ {x} + {y} +
; + + // z should depend on `x`, `y.method`, and `b` + const z = x[y.method](b); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c) { + const $ = _c(8); + let t0; + if ($[0] !== a) { + t0 = makeObject(a); + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let t1; + if ($[2] !== a) { + t1 = makeObject(a); + $[2] = a; + $[3] = t1; + } else { + t1 = $[3]; + } + const y = t1; + let t2; + if ($[4] !== b || $[5] !== x || $[6] !== y.method) { + t2 = x[y.method](b); + $[4] = b; + $[5] = x; + $[6] = y.method; + $[7] = t2; + } else { + t2 = $[7]; + } + const z = t2; + return z; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.js new file mode 100644 index 0000000000000000000000000000000000000000..5aaf78027ee911ea4121d648d44ec8ee1de37aa5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-computed.js @@ -0,0 +1,13 @@ +function foo(a, b, c) { + // Construct and freeze x, y + const x = makeObject(a); + const y = makeObject(a); +
+ {x} + {y} +
; + + // z should depend on `x`, `y.method`, and `b` + const z = x[y.method](b); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..58c06101d38eb2ad91d9ffb74ceb9e943980fd13 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function foo(a, b, c) { + // Construct and freeze x + const x = makeObject(a); +
{x}
; + + // y should depend on `x` and `b` + const method = x.method; + const y = method.call(x, b); + return y; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c) { + const $ = _c(6); + let t0; + if ($[0] !== a) { + t0 = makeObject(a); + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + + const method = x.method; + let t1; + if ($[2] !== b || $[3] !== method || $[4] !== x) { + t1 = method.call(x, b); + $[2] = b; + $[3] = method; + $[4] = x; + $[5] = t1; + } else { + t1 = $[5]; + } + const y = t1; + return y; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.js new file mode 100644 index 0000000000000000000000000000000000000000..5289caae269e4f476a007f1acb08f3103a05b330 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-fn-call.js @@ -0,0 +1,10 @@ +function foo(a, b, c) { + // Construct and freeze x + const x = makeObject(a); +
{x}
; + + // y should depend on `x` and `b` + const method = x.method; + const y = method.call(x, b); + return y; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..128d8cd87ef9ee9d782662ef337ad30e07807d75 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.expect.md @@ -0,0 +1,323 @@ + +## Input + +```javascript +import fbt from 'fbt'; +import * as React from 'react'; +import {useState} from 'react'; + +// Minimized from MusicPartnerManagerMusicWorkDocumentStoreRenderer.react.js +// Tests AlignMethodCallScopes mutable_range sync after scope merging. + +function nullthrows(x) { + if (x != null) return x; + throw new Error('nullthrows'); +} + +function Component({store}) { + const [isShown, setIsShown] = useState(false); + const id = nullthrows(store?.id); + const storeUri = nullthrows(store?.store_uri); + const licensedGeos = nullthrows(store?.licensed_geos); + const documentSchema = nullthrows(store?.document_schema); + const ingestionEnabled = nullthrows(store?.ingestion_enabled); + const cwrSenderIds = store?.cwr_sender_ids; + + if (!store.hasOwnProperty('store_uri')) { + return; + } + + return ( +
+ + + {fbt( + `Directory: ${fbt.param( + 'store URI', + storeUri.replace('sftp://sftp.fb.com', '') + )}`, + 'directory info' + )} + + + {fbt(`Schema: ${fbt.param('schema', documentSchema)}`, 'schema')},{' '} + {fbt(`Geos: ${fbt.param('geos', licensedGeos.length)}`, 'geos count')} + ,{' '} + + {cwrSenderIds != null && Sender ID: {cwrSenderIds[0]}} + Ingestion: {ingestionEnabled ? 'Enabled' : 'Disabled'} + + {[...licensedGeos].sort().join(',')} + + + + +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + store: { + id: '1', + store_uri: 'sftp://sftp.fb.com/Music', + licensed_geos: ['US', 'GB'], + document_schema: 'CWR', + ingestion_enabled: true, + cwr_sender_ids: ['123'], + }, + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import fbt from "fbt"; +import * as React from "react"; +import { useState } from "react"; + +// Minimized from MusicPartnerManagerMusicWorkDocumentStoreRenderer.react.js +// Tests AlignMethodCallScopes mutable_range sync after scope merging. + +function nullthrows(x) { + if (x != null) { + return x; + } + throw new Error("nullthrows"); +} + +function Component(t0) { + const $ = _c(37); + const { store } = t0; + const [isShown, setIsShown] = useState(false); + const t1 = store?.id; + let t2; + if ($[0] !== t1) { + t2 = nullthrows(t1); + $[0] = t1; + $[1] = t2; + } else { + t2 = $[1]; + } + const id = t2; + let t3; + let t4; + let t5; + let t6; + let t7; + let t8; + let t9; + if ($[2] !== store) { + t9 = Symbol.for("react.early_return_sentinel"); + bb0: { + const storeUri = nullthrows(store?.store_uri); + const licensedGeos = nullthrows(store?.licensed_geos); + const documentSchema = nullthrows(store?.document_schema); + const ingestionEnabled = nullthrows(store?.ingestion_enabled); + const cwrSenderIds = store?.cwr_sender_ids; + if (!store.hasOwnProperty("store_uri")) { + t9 = undefined; + break bb0; + } + t8 = store.id; + const t10 = fbt._( + "Directory: {store URI}", + [fbt._param("store URI", storeUri.replace("sftp://sftp.fb.com", ""))], + { hk: "y1DI6" }, + ); + if ($[10] !== t10) { + t4 = {t10}; + $[10] = t10; + $[11] = t4; + } else { + t4 = $[11]; + } + t5 = ( + + {fbt._("Schema: {schema}", [fbt._param("schema", documentSchema)], { + hk: "1Ua9F9", + })} + ,{" "} + {fbt._("Geos: {geos}", [fbt._param("geos", licensedGeos.length)], { + hk: "1PRSzD", + })} + ,{" "} + + ); + if ($[12] !== cwrSenderIds) { + t6 = cwrSenderIds != null && Sender ID: {cwrSenderIds[0]}; + $[12] = cwrSenderIds; + $[13] = t6; + } else { + t6 = $[13]; + } + const t11 = ingestionEnabled ? "Enabled" : "Disabled"; + if ($[14] !== t11) { + t7 = Ingestion: {t11}; + $[14] = t11; + $[15] = t7; + } else { + t7 = $[15]; + } + t3 = [...licensedGeos].sort().join(","); + } + $[2] = store; + $[3] = t3; + $[4] = t4; + $[5] = t5; + $[6] = t6; + $[7] = t7; + $[8] = t8; + $[9] = t9; + } else { + t3 = $[3]; + t4 = $[4]; + t5 = $[5]; + t6 = $[6]; + t7 = $[7]; + t8 = $[8]; + t9 = $[9]; + } + if (t9 !== Symbol.for("react.early_return_sentinel")) { + return t9; + } + let t10; + if ($[16] === Symbol.for("react.memo_cache_sentinel")) { + t10 = ; + $[16] = t10; + } else { + t10 = $[16]; + } + let t11; + if ($[17] !== t3) { + t11 = ( + + {t3} + {t10} + + ); + $[17] = t3; + $[18] = t11; + } else { + t11 = $[18]; + } + let t12; + if ( + $[19] !== t11 || + $[20] !== t4 || + $[21] !== t5 || + $[22] !== t6 || + $[23] !== t7 + ) { + t12 = ( + + {t4} + {t5} + {t6} + {t7} + {t11} + + ); + $[19] = t11; + $[20] = t4; + $[21] = t5; + $[22] = t6; + $[23] = t7; + $[24] = t12; + } else { + t12 = $[24]; + } + let t13; + if ($[25] === Symbol.for("react.memo_cache_sentinel")) { + t13 =
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.js new file mode 100644 index 0000000000000000000000000000000000000000..92cb36c8a83f0dfe9f4b6d3f7f17bbfe051ca129 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call-scope-merge-mutable-range-sync.js @@ -0,0 +1,78 @@ +import fbt from 'fbt'; +import * as React from 'react'; +import {useState} from 'react'; + +// Minimized from MusicPartnerManagerMusicWorkDocumentStoreRenderer.react.js +// Tests AlignMethodCallScopes mutable_range sync after scope merging. + +function nullthrows(x) { + if (x != null) return x; + throw new Error('nullthrows'); +} + +function Component({store}) { + const [isShown, setIsShown] = useState(false); + const id = nullthrows(store?.id); + const storeUri = nullthrows(store?.store_uri); + const licensedGeos = nullthrows(store?.licensed_geos); + const documentSchema = nullthrows(store?.document_schema); + const ingestionEnabled = nullthrows(store?.ingestion_enabled); + const cwrSenderIds = store?.cwr_sender_ids; + + if (!store.hasOwnProperty('store_uri')) { + return; + } + + return ( +
+ + + {fbt( + `Directory: ${fbt.param( + 'store URI', + storeUri.replace('sftp://sftp.fb.com', '') + )}`, + 'directory info' + )} + + + {fbt(`Schema: ${fbt.param('schema', documentSchema)}`, 'schema')},{' '} + {fbt(`Geos: ${fbt.param('geos', licensedGeos.length)}`, 'geos count')} + ,{' '} + + {cwrSenderIds != null && Sender ID: {cwrSenderIds[0]}} + Ingestion: {ingestionEnabled ? 'Enabled' : 'Disabled'} + + {[...licensedGeos].sort().join(',')} + + + + +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + store: { + id: '1', + store_uri: 'sftp://sftp.fb.com/Music', + licensed_geos: ['US', 'GB'], + document_schema: 'CWR', + ingestion_enabled: true, + cwr_sender_ids: ['123'], + }, + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fd8a4935a8253962294a1f80026837dc9af082c0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {addOne, shallowCopy} from 'shared-runtime'; + +function foo(a, b, c) { + // Construct and freeze x + const x = shallowCopy(a); +
{x}
; + + // y should depend on `x` and `b` + const y = x.foo(b); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{foo: addOne}, 3], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { addOne, shallowCopy } from "shared-runtime"; + +function foo(a, b, c) { + const $ = _c(5); + let t0; + if ($[0] !== a) { + t0 = shallowCopy(a); + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let t1; + if ($[2] !== b || $[3] !== x) { + t1 = x.foo(b); + $[2] = b; + $[3] = x; + $[4] = t1; + } else { + t1 = $[4]; + } + const y = t1; + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{ foo: addOne }, 3], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 4 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.js new file mode 100644 index 0000000000000000000000000000000000000000..b106b95a984261feb6873b2d0421ef55f13cb3a6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/method-call.js @@ -0,0 +1,17 @@ +import {addOne, shallowCopy} from 'shared-runtime'; + +function foo(a, b, c) { + // Construct and freeze x + const x = shallowCopy(a); +
{x}
; + + // y should depend on `x` and `b` + const y = x.foo(b); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{foo: addOne}, 3], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6dd05cbe7c619357f912df57ddd9dce52feeb773 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.expect.md @@ -0,0 +1,155 @@ + +## Input + +```javascript +import { + arrayPush, + identity, + makeArray, + Stringify, + useFragment, +} from 'shared-runtime'; + +/** + * Bug repro showing why it's invalid for function references to be annotated + * with a `Read` effect when that reference might lead to the function being + * invoked. + * + * Note that currently, `Array.map` is annotated to have `Read` effects on its + * operands. This is incorrect as function effects must be replayed when `map` + * is called + * - Read: non-aliasing data dependency + * - Capture: maybe-aliasing data dependency + * - ConditionallyMutate: maybe-aliasing data dependency; maybe-write / invoke + * but only if the value is mutable + * + * Invalid evaluator result: Found differences in evaluator results Non-forget + * (expected): (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2],"count":4}
{"item":1}
+ * Forget: + * (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2,2,2,2],"count":4}
{"item":1}
+ */ + +function Component({extraJsx}) { + const x = makeArray(); + const items = useFragment(); + // This closure has the following effects that must be replayed: + // - MaybeFreeze / Capture of `items` + // - ConditionalMutate of x + const jsx = items.a.map((item, i) => { + arrayPush(x, 2); + return ; + }); + const offset = jsx.length; + for (let i = 0; i < extraJsx; i++) { + jsx.push(); + } + const count = jsx.length; + identity(count); + return ( + <> + + {jsx[0]} + + ); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{extraJsx: 0}], + sequentialRenders: [{extraJsx: 0}, {extraJsx: 1}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { + arrayPush, + identity, + makeArray, + Stringify, + useFragment, +} from "shared-runtime"; + +/** + * Bug repro showing why it's invalid for function references to be annotated + * with a `Read` effect when that reference might lead to the function being + * invoked. + * + * Note that currently, `Array.map` is annotated to have `Read` effects on its + * operands. This is incorrect as function effects must be replayed when `map` + * is called + * - Read: non-aliasing data dependency + * - Capture: maybe-aliasing data dependency + * - ConditionallyMutate: maybe-aliasing data dependency; maybe-write / invoke + * but only if the value is mutable + * + * Invalid evaluator result: Found differences in evaluator results Non-forget + * (expected): (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2],"count":4}
{"item":1}
+ * Forget: + * (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2,2,2,2],"count":4}
{"item":1}
+ */ + +function Component(t0) { + const $ = _c(6); + const { extraJsx } = t0; + const x = makeArray(); + const items = useFragment(); + + const jsx = items.a.map((item, i) => { + arrayPush(x, 2); + return ; + }); + const offset = jsx.length; + for (let i_0 = 0; i_0 < extraJsx; i_0++) { + jsx.push(); + } + + const count = jsx.length; + identity(count); + let t1; + if ($[0] !== count || $[1] !== x) { + t1 = ; + $[0] = count; + $[1] = x; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] !== jsx[0] || $[4] !== t1) { + t2 = ( + <> + {t1} + {jsx[0]} + + ); + $[3] = jsx[0]; + $[4] = t1; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ extraJsx: 0 }], + sequentialRenders: [{ extraJsx: 0 }, { extraJsx: 1 }], +}; + +``` + +### Eval output +(kind: ok)
{"x":[2,2,2],"count":3}
{"item":1}
+
{"x":[2,2,2],"count":4}
{"item":1}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.js new file mode 100644 index 0000000000000000000000000000000000000000..858a4ab3dc6935a954ba16b5551f9c534d6446d2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mixedreadonly-mutating-map.js @@ -0,0 +1,59 @@ +import { + arrayPush, + identity, + makeArray, + Stringify, + useFragment, +} from 'shared-runtime'; + +/** + * Bug repro showing why it's invalid for function references to be annotated + * with a `Read` effect when that reference might lead to the function being + * invoked. + * + * Note that currently, `Array.map` is annotated to have `Read` effects on its + * operands. This is incorrect as function effects must be replayed when `map` + * is called + * - Read: non-aliasing data dependency + * - Capture: maybe-aliasing data dependency + * - ConditionallyMutate: maybe-aliasing data dependency; maybe-write / invoke + * but only if the value is mutable + * + * Invalid evaluator result: Found differences in evaluator results Non-forget + * (expected): (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2],"count":4}
{"item":1}
+ * Forget: + * (kind: ok) + *
{"x":[2,2,2],"count":3}
{"item":1}
+ *
{"x":[2,2,2,2,2,2],"count":4}
{"item":1}
+ */ + +function Component({extraJsx}) { + const x = makeArray(); + const items = useFragment(); + // This closure has the following effects that must be replayed: + // - MaybeFreeze / Capture of `items` + // - ConditionalMutate of x + const jsx = items.a.map((item, i) => { + arrayPush(x, 2); + return ; + }); + const offset = jsx.length; + for (let i = 0; i < extraJsx; i++) { + jsx.push(); + } + const count = jsx.length; + identity(count); + return ( + <> + + {jsx[0]} + + ); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{extraJsx: 0}], + sequentialRenders: [{extraJsx: 0}, {extraJsx: 1}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8e96f49417b2b5e44912009477ae061a93c3af08 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.expect.md @@ -0,0 +1,103 @@ + +## Input + +```javascript +import React from 'react'; +import {useState} from 'react'; + +const CONST = true; + +let NON_REASSIGNED_LET = true; + +let REASSIGNED_LET = false; +REASSIGNED_LET = true; + +function reassignedFunction() {} +reassignedFunction = true; + +function nonReassignedFunction() {} + +class ReassignedClass {} +ReassignedClass = true; + +class NonReassignedClass {} + +function Component() { + const [state] = useState(null); + return [ + React, + state, + CONST, + NON_REASSIGNED_LET, + REASSIGNED_LET, + reassignedFunction, + nonReassignedFunction, + ReassignedClass, + NonReassignedClass, + ]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import React from "react"; +import { useState } from "react"; + +const CONST = true; + +let NON_REASSIGNED_LET = true; + +let REASSIGNED_LET = false; +REASSIGNED_LET = true; + +function reassignedFunction() {} +reassignedFunction = true; + +function nonReassignedFunction() {} + +class ReassignedClass {} +ReassignedClass = true; + +class NonReassignedClass {} + +function Component() { + const $ = _c(2); + const [state] = useState(null); + let t0; + if ($[0] !== state) { + t0 = [ + React, + state, + CONST, + NON_REASSIGNED_LET, + REASSIGNED_LET, + reassignedFunction, + nonReassignedFunction, + ReassignedClass, + NonReassignedClass, + ]; + $[0] = state; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) [{"Children":{"map":"[[ function params=3 ]]","forEach":"[[ function params=3 ]]","count":"[[ function params=1 ]]","toArray":"[[ function params=1 ]]","only":"[[ function params=1 ]]"},"Component":"[[ function params=3 ]]","PureComponent":"[[ function params=3 ]]","__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE":{"H":{"readContext":"[[ function params=1 ]]","use":"[[ function params=1 ]]","useCallback":"[[ function params=2 ]]","useContext":"[[ function params=1 ]]","useEffect":"[[ function params=2 ]]","useImperativeHandle":"[[ function params=3 ]]","useInsertionEffect":"[[ function params=2 ]]","useLayoutEffect":"[[ function params=2 ]]","useMemo":"[[ function params=2 ]]","useReducer":"[[ function params=3 ]]","useRef":"[[ function params=1 ]]","useState":"[[ function params=1 ]]","useDebugValue":"[[ function params=0 ]]","useDeferredValue":"[[ function params=2 ]]","useTransition":"[[ function params=0 ]]","useSyncExternalStore":"[[ function params=3 ]]","useId":"[[ function params=0 ]]","useCacheRefresh":"[[ function params=0 ]]","useMemoCache":"[[ function params=1 ]]","useEffectEvent":"[[ function params=1 ]]","useHostTransitionStatus":"[[ function params=0 ]]","useFormState":"[[ function params=2 ]]","useActionState":"[[ function params=2 ]]","useOptimistic":"[[ function params=1 ]]"},"A":{"getCacheForType":"[[ function params=1 ]]","getOwner":"[[ function params=0 ]]"},"T":null,"S":"[[ function params=2 ]]","actQueue":["[[ function params=0 ]]","[[ function params=1 ]]"],"isBatchingLegacy":false,"didScheduleLegacyUpdate":false,"didUsePromise":false,"thrownErrors":[],"getCurrentStack":"[[ function params=0 ]]"},"__COMPILER_RUNTIME":{"c":"[[ function params=1 ]]"},"act":"[[ function params=1 ]]","cache":"[[ function params=1 ]]","captureOwnerStack":"[[ function params=0 ]]","cloneElement":"[[ function params=3 ]]","createContext":"[[ function params=1 ]]","createElement":"[[ function params=3 ]]","createRef":"[[ function params=0 ]]","experimental_useEffectEvent":"[[ function params=1 ]]","experimental_useOptimistic":"[[ function params=2 ]]","experimental_useResourceEffect":"[[ function params=0 ]]","forwardRef":"[[ function params=1 ]]","isValidElement":"[[ function params=1 ]]","lazy":"[[ function params=1 ]]","memo":"[[ function params=2 ]]","startTransition":"[[ function params=1 ]]","unstable_getCacheForType":"[[ function params=1 ]]","unstable_postpone":"[[ function params=1 ]]","unstable_useCacheRefresh":"[[ function params=0 ]]","use":"[[ function params=1 ]]","useActionState":"[[ function params=3 ]]","useCallback":"[[ function params=2 ]]","useContext":"[[ function params=1 ]]","useDebugValue":"[[ function params=2 ]]","useDeferredValue":"[[ function params=2 ]]","useEffect":"[[ function params=2 ]]","useId":"[[ function params=0 ]]","useImperativeHandle":"[[ function params=3 ]]","useInsertionEffect":"[[ function params=2 ]]","useLayoutEffect":"[[ function params=2 ]]","useMemo":"[[ function params=2 ]]","useOptimistic":"[[ function params=2 ]]","useReducer":"[[ function params=3 ]]","useRef":"[[ function params=1 ]]","useState":"[[ function params=1 ]]","useSyncExternalStore":"[[ function params=3 ]]","useTransition":"[[ function params=0 ]]","version":"19.0.0-experimental-4beb1fd8-20241118"},"[[ cyclic ref *6 ]]",true,true,true,true,"[[ function params=0 ]]",true,"[[ function params=0 ]]"] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.js new file mode 100644 index 0000000000000000000000000000000000000000..6190eb9e528115b1c263c837e30bc73fed713b15 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/module-scoped-bindings.js @@ -0,0 +1,39 @@ +import React from 'react'; +import {useState} from 'react'; + +const CONST = true; + +let NON_REASSIGNED_LET = true; + +let REASSIGNED_LET = false; +REASSIGNED_LET = true; + +function reassignedFunction() {} +reassignedFunction = true; + +function nonReassignedFunction() {} + +class ReassignedClass {} +ReassignedClass = true; + +class NonReassignedClass {} + +function Component() { + const [state] = useState(null); + return [ + React, + state, + CONST, + NON_REASSIGNED_LET, + REASSIGNED_LET, + reassignedFunction, + nonReassignedFunction, + ReassignedClass, + NonReassignedClass, + ]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0a1d0eeeefa48e5489222fccc29f7572a370fbe6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +function Component() { + 'use foo'; + 'use bar'; + return
"foo"
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: true, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component() { + "use foo"; + "use bar"; + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 =
"foo"
; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: true, +}; + +``` + +### Eval output +(kind: ok)
"foo"
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.js new file mode 100644 index 0000000000000000000000000000000000000000..5a269d2471a07bd9c63166b06d4bdbb784fb938f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multi-directive.js @@ -0,0 +1,11 @@ +function Component() { + 'use foo'; + 'use bar'; + return
"foo"
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: true, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ba9b6684c9d55ab3708be9b3e53346333d5f6a78 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.expect.md @@ -0,0 +1,71 @@ + +## Input + +```javascript +import {useState} from 'react'; + +function Component(props) { + const [_state, setState] = useState(); + const a = () => { + return b(); + }; + const b = () => { + return ( + <> +
onClick(true)}>a
+
onClick(false)}>b
+ + ); + }; + const onClick = value => { + setState(value); + }; + + return
{a()}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; + +function Component(props) { + const $ = _c(1); + const [, setState] = useState(); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const a = () => b(); + const b = () => ( + <> +
onClick(true)}>a
+
onClick(false)}>b
+ + ); + const onClick = (value) => { + setState(value); + }; + t0 =
{a()}
; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
a
b
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..8d7c9fc1138eebbc1308518917f7c4e6fcc28b1f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-calls-to-hoisted-callback-from-other-callback.js @@ -0,0 +1,26 @@ +import {useState} from 'react'; + +function Component(props) { + const [_state, setState] = useState(); + const a = () => { + return b(); + }; + const b = () => { + return ( + <> +
onClick(true)}>a
+
onClick(false)}>b
+ + ); + }; + const onClick = value => { + setState(value); + }; + + return
{a()}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..13e67f0e4909cdb4c1c25f21bfd693739de05fcd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +// @panicThreshold:"none" +import {useHook} from 'shared-runtime'; + +function InvalidComponent(props) { + if (props.cond) { + useHook(); + } + return
Hello World!
; +} + +function ValidComponent(props) { + return
{props.greeting}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @panicThreshold:"none" +import { useHook } from "shared-runtime"; + +function InvalidComponent(props) { + if (props.cond) { + useHook(); + } + return
Hello World!
; +} + +function ValidComponent(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.greeting) { + t0 =
{props.greeting}
; + $[0] = props.greeting; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.js new file mode 100644 index 0000000000000000000000000000000000000000..80f9bdd1092f4d8ea80ab081a7604161a6125c9b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/multiple-components-first-is-invalid.js @@ -0,0 +1,13 @@ +// @panicThreshold:"none" +import {useHook} from 'shared-runtime'; + +function InvalidComponent(props) { + if (props.cond) { + useHook(); + } + return
Hello World!
; +} + +function ValidComponent(props) { + return
{props.greeting}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..dacee742440823d2a8d155ab3c7aef5bdc279460 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.expect.md @@ -0,0 +1,123 @@ + +## Input + +```javascript +function mutate(x, y) { + 'use no forget'; + if (x != null) { + x.value = (x.value ?? 0) + 1; + } + if (y != null) { + y.value = (y.value ?? 0) + 1; + } +} +function cond(x) { + 'use no forget'; + return x.value > 5; +} + +function testFunction(props) { + let a = {}; + let b = {}; + let c = {}; + let d = {}; + while (true) { + let z = a; + a = b; + b = c; + c = d; + d = z; + mutate(a, b); + if (cond(a)) { + break; + } + } + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `d`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + + mutate(d, null); + return {a, b, c, d}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: testFunction, + params: [{}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function mutate(x, y) { + "use no forget"; + if (x != null) { + x.value = (x.value ?? 0) + 1; + } + if (y != null) { + y.value = (y.value ?? 0) + 1; + } +} +function cond(x) { + "use no forget"; + return x.value > 5; +} + +function testFunction(props) { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + let a = {}; + let b = {}; + let c = {}; + let d = {}; + while (true) { + const z = a; + a = b; + b = c; + c = d; + d = z; + mutate(a, b); + if (cond(a)) { + break; + } + } + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + mutate(d, null); + t0 = { a, b, c, d }; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: testFunction, + params: [{}], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"a":{"value":6},"b":{"value":5},"c":{"value":4},"d":{"value":6}} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.js new file mode 100644 index 0000000000000000000000000000000000000000..7e4e32813fadd4ea52869b942d5f2d7b8d626b8c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-loops.js @@ -0,0 +1,52 @@ +function mutate(x, y) { + 'use no forget'; + if (x != null) { + x.value = (x.value ?? 0) + 1; + } + if (y != null) { + y.value = (y.value ?? 0) + 1; + } +} +function cond(x) { + 'use no forget'; + return x.value > 5; +} + +function testFunction(props) { + let a = {}; + let b = {}; + let c = {}; + let d = {}; + while (true) { + let z = a; + a = b; + b = c; + c = d; + d = z; + mutate(a, b); + if (cond(a)) { + break; + } + } + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `d`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + + mutate(d, null); + return {a, b, c, d}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: testFunction, + params: [{}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8510bd2ecc8b65e3c0c16193f3a0c0cf5e69677b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.expect.md @@ -0,0 +1,113 @@ + +## Input + +```javascript +function mutate(x, y) { + 'use no forget'; + if (!Array.isArray(x.value)) { + x.value = []; + } + x.value.push(y); + if (y != null) { + y.value = x; + } +} + +function Component(props) { + const a = {}; + const b = [a]; // array elements alias + const c = {}; + const d = {c}; // object values alias + + // capture all the values into this object + const x = {}; + x.b = b; + const y = mutate(x, d); // mutation aliases the arg and return value + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `x`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + if (y) { + } + + // could in theory mutate any of a/b/c/x/z, so the above should be inferred as mutable + mutate(x, null); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function mutate(x, y) { + "use no forget"; + if (!Array.isArray(x.value)) { + x.value = []; + } + x.value.push(y); + if (y != null) { + y.value = x; + } +} + +function Component(props) { + const $ = _c(1); + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const a = {}; + const b = [a]; + const c = {}; + const d = { c }; + x = {}; + x.b = b; + const y = mutate(x, d); + + if (a) { + } + + if (b) { + } + + if (c) { + } + + if (d) { + } + + if (y) { + } + + mutate(x, null); + $[0] = x; + } else { + x = $[0]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"b":[{}],"value":[{"c":{},"value":"[[ cyclic ref *0 ]]"},null]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.js new file mode 100644 index 0000000000000000000000000000000000000000..8b1d3e8fa57dbe047cc4b59bba75ca4b4b39c598 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-lifetime-with-aliasing.js @@ -0,0 +1,46 @@ +function mutate(x, y) { + 'use no forget'; + if (!Array.isArray(x.value)) { + x.value = []; + } + x.value.push(y); + if (y != null) { + y.value = x; + } +} + +function Component(props) { + const a = {}; + const b = [a]; // array elements alias + const c = {}; + const d = {c}; // object values alias + + // capture all the values into this object + const x = {}; + x.b = b; + const y = mutate(x, d); // mutation aliases the arg and return value + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `x`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + if (y) { + } + + // could in theory mutate any of a/b/c/x/z, so the above should be inferred as mutable + mutate(x, null); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b28a96361b8c446ab5325bb486ed264f734a1d96 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.expect.md @@ -0,0 +1,71 @@ + +## Input + +```javascript +function mutate() {} +function cond() {} + +function Component(props) { + let a = {}; + let b = {}; + let c = {}; + let d = {}; + while (true) { + mutate(a, b); + if (cond(a)) { + break; + } + } + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `d`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + + mutate(d, null); +} + +``` + +## Code + +```javascript +function mutate() {} +function cond() {} + +function Component(props) { + const a = {}; + const b = {}; + const c = {}; + const d = {}; + while (true) { + mutate(a, b); + if (cond(a)) { + break; + } + } + + if (a) { + } + + if (b) { + } + + if (c) { + } + + if (d) { + } + + mutate(d, null); +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.js new file mode 100644 index 0000000000000000000000000000000000000000..5b42db68310a8c92cd2c0d0268daaf7509c250c9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutable-liverange-loop.js @@ -0,0 +1,29 @@ +function mutate() {} +function cond() {} + +function Component(props) { + let a = {}; + let b = {}; + let c = {}; + let d = {}; + while (true) { + mutate(a, b); + if (cond(a)) { + break; + } + } + + // all of these tests are seemingly readonly, since the values are never directly + // mutated again. but they are all aliased by `d`, which is later modified, and + // these are therefore mutable references: + if (a) { + } + if (b) { + } + if (c) { + } + if (d) { + } + + mutate(d, null); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f3f21f8f6222b7c8c95c8e4cc015d4a295b6702f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +function component(a) { + let y = function () { + m(x); + }; + + let x = {a}; + m(x); + return y; +} + +function m(x) {} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{name: 'Jason'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + y = function () { + m(x); + }; + + let x = { a }; + m(x); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +function m(x) {} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{ name: "Jason" }], +}; + +``` + +### Eval output +(kind: ok) "[[ function params=0 ]]" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.js new file mode 100644 index 0000000000000000000000000000000000000000..9985aea7f2fc5094587dd6c444e67ae1cacddb07 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-captured-arg-separately.js @@ -0,0 +1,16 @@ +function component(a) { + let y = function () { + m(x); + }; + + let x = {a}; + m(x); + return y; +} + +function m(x) {} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{name: 'Jason'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4633a1f807294d446889f8bb1ee37efbda366490 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.expect.md @@ -0,0 +1,97 @@ + +## Input + +```javascript +import {CONST_TRUE, identity, shallowCopy} from 'shared-runtime'; + +function mutate(_: unknown) {} + +/** + * There are three values with their own scopes in this fixture. + * - arr, whose mutable range extends to the `mutate(...)` call + * - cond, which has a mutable range of exactly 1 (e.g. created but not + * mutated) + * - { val: CONST_TRUE }, which is also not mutated after creation. However, + * its scope range becomes extended to the value block. + * + * After AlignScopesToBlockScopes, our scopes look roughly like this + * ```js + * [1] arr = shallowCopy() ⌝@0 + * [2] cond = identity() <- @1 | + * [3] $0 = Ternary test=cond ⌝@2 | + * [4] {val : CONST_TRUE} | | + * [5] mutate(arr) | | + * [6] return $0 ⌟ ⌟ + * ``` + * + * Observe that instruction 5 mutates scope 0, which means that scopes 0 and 2 + * should be merged. + */ +function useFoo({input}) { + const arr = shallowCopy(input); + + const cond = identity(false); + return cond ? {val: CONST_TRUE} : mutate(arr); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{input: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { CONST_TRUE, identity, shallowCopy } from "shared-runtime"; + +function mutate(_) {} + +/** + * There are three values with their own scopes in this fixture. + * - arr, whose mutable range extends to the `mutate(...)` call + * - cond, which has a mutable range of exactly 1 (e.g. created but not + * mutated) + * - { val: CONST_TRUE }, which is also not mutated after creation. However, + * its scope range becomes extended to the value block. + * + * After AlignScopesToBlockScopes, our scopes look roughly like this + * ```js + * [1] arr = shallowCopy() ⌝@0 + * [2] cond = identity() <- @1 | + * [3] $0 = Ternary test=cond ⌝@2 | + * [4] {val : CONST_TRUE} | | + * [5] mutate(arr) | | + * [6] return $0 ⌟ ⌟ + * ``` + * + * Observe that instruction 5 mutates scope 0, which means that scopes 0 and 2 + * should be merged. + */ +function useFoo(t0) { + const $ = _c(2); + const { input } = t0; + let t1; + if ($[0] !== input) { + const arr = shallowCopy(input); + const cond = identity(false); + t1 = cond ? { val: CONST_TRUE } : mutate(arr); + $[0] = input; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ input: 3 }], +}; + +``` + +### Eval output +(kind: ok) \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.ts new file mode 100644 index 0000000000000000000000000000000000000000..fd4452bebbd865660f7942986e045edb23608e08 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutate-outer-scope-within-value-block.ts @@ -0,0 +1,36 @@ +import {CONST_TRUE, identity, shallowCopy} from 'shared-runtime'; + +function mutate(_: unknown) {} + +/** + * There are three values with their own scopes in this fixture. + * - arr, whose mutable range extends to the `mutate(...)` call + * - cond, which has a mutable range of exactly 1 (e.g. created but not + * mutated) + * - { val: CONST_TRUE }, which is also not mutated after creation. However, + * its scope range becomes extended to the value block. + * + * After AlignScopesToBlockScopes, our scopes look roughly like this + * ```js + * [1] arr = shallowCopy() ⌝@0 + * [2] cond = identity() <- @1 | + * [3] $0 = Ternary test=cond ⌝@2 | + * [4] {val : CONST_TRUE} | | + * [5] mutate(arr) | | + * [6] return $0 ⌟ ⌟ + * ``` + * + * Observe that instruction 5 mutates scope 0, which means that scopes 0 and 2 + * should be merged. + */ +function useFoo({input}) { + const arr = shallowCopy(input); + + const cond = identity(false); + return cond ? {val: CONST_TRUE} : mutate(arr); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{input: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b60832af667688a13ef5a4dd3f6bcb34b330ae2b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturnNewValue} from 'shared-runtime'; + +function Component(props) { + const key = {}; + // Key is modified by the function, but key itself is not frozen + const element =
{props.value}
; + // Key is later mutated here: this mutation must be grouped with the + // jsx construction above + mutate(key); + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturnNewValue } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let element; + if ($[0] !== props.value) { + const key = {}; + element =
{props.value}
; + + mutate(key); + $[0] = props.value; + $[1] = element; + } else { + element = $[1]; + } + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok)
42
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.js new file mode 100644 index 0000000000000000000000000000000000000000..3bb6eeae45ce09cf881520f812123377506e0d08 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-during-jsx-construction.js @@ -0,0 +1,16 @@ +import {identity, mutate, mutateAndReturnNewValue} from 'shared-runtime'; + +function Component(props) { + const key = {}; + // Key is modified by the function, but key itself is not frozen + const element =
{props.value}
; + // Key is later mutated here: this mutation must be grouped with the + // jsx construction above + mutate(key); + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..408dd932854cde8712200df4e9c4b6729ef8fc7d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.expect.md @@ -0,0 +1,82 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +/** + * This test fixture is similar to mutation-within-jsx. The only difference + * is that there is no `freeze` effect here, which means that `z` may be + * mutated after its memo block through mutating `y`. + * + * While this is technically correct (as `z` is a nested memo block), it + * is an edge case as we believe that values are not mutated after their + * memo blocks (which may lead to 'tearing', i.e. mutating one render's + * values in a subsequent render. + */ +function useFoo({a, b}) { + // x and y's scopes start here + const x = {a}; + const y = [b]; + mutate(x); + // z captures the result of `mutate(y)`, which may be aliased to `y`. + const z = [mutate(y)]; + // the following line may also mutate z + mutate(y); + // and end here + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{a: 2, b: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +/** + * This test fixture is similar to mutation-within-jsx. The only difference + * is that there is no `freeze` effect here, which means that `z` may be + * mutated after its memo block through mutating `y`. + * + * While this is technically correct (as `z` is a nested memo block), it + * is an edge case as we believe that values are not mutated after their + * memo blocks (which may lead to 'tearing', i.e. mutating one render's + * values in a subsequent render. + */ +function useFoo(t0) { + const $ = _c(3); + const { a, b } = t0; + let z; + if ($[0] !== a || $[1] !== b) { + const x = { a }; + const y = [b]; + mutate(x); + z = [mutate(y)]; + + mutate(y); + $[0] = a; + $[1] = b; + $[2] = z; + } else { + z = $[2]; + } + + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ a: 2, b: 3 }], +}; + +``` + +### Eval output +(kind: ok) [null] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.tsx new file mode 100644 index 0000000000000000000000000000000000000000..52aae3a78b415c4b0ab31a7825256653b81cc0f3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-capture-and-mutablerange.tsx @@ -0,0 +1,29 @@ +import {mutate} from 'shared-runtime'; + +/** + * This test fixture is similar to mutation-within-jsx. The only difference + * is that there is no `freeze` effect here, which means that `z` may be + * mutated after its memo block through mutating `y`. + * + * While this is technically correct (as `z` is a nested memo block), it + * is an edge case as we believe that values are not mutated after their + * memo blocks (which may lead to 'tearing', i.e. mutating one render's + * values in a subsequent render. + */ +function useFoo({a, b}) { + // x and y's scopes start here + const x = {a}; + const y = [b]; + mutate(x); + // z captures the result of `mutate(y)`, which may be aliased to `y`. + const z = [mutate(y)]; + // the following line may also mutate z + mutate(y); + // and end here + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{a: 2, b: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..90884a477ebbf73714713b9cb1258a77116dc9e5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.expect.md @@ -0,0 +1,91 @@ + +## Input + +```javascript +import { + Stringify, + makeObject_Primitives, + mutate, + mutateAndReturn, +} from 'shared-runtime'; + +function useFoo({data}) { + let obj = null; + let myDiv = null; + label: { + if (data.cond) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + break label; + } + mutate(obj); + } + } + + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{data: {cond: true, cond1: true}}], + sequentialRenders: [ + {data: {cond: true, cond1: true}}, + {data: {cond: true, cond1: true}}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { + Stringify, + makeObject_Primitives, + mutate, + mutateAndReturn, +} from "shared-runtime"; + +function useFoo(t0) { + const $ = _c(4); + const { data } = t0; + let obj; + let myDiv = null; + if ($[0] !== data.cond || $[1] !== data.cond1) { + bb0: if (data.cond) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + break bb0; + } + + mutate(obj); + } + $[0] = data.cond; + $[1] = data.cond1; + $[2] = obj; + $[3] = myDiv; + } else { + obj = $[2]; + myDiv = $[3]; + } + + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ data: { cond: true, cond1: true } }], + sequentialRenders: [ + { data: { cond: true, cond1: true } }, + { data: { cond: true, cond1: true } }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"value":{"a":0,"b":"value1","c":true,"wat0":"joe"}}
+
{"value":{"a":0,"b":"value1","c":true,"wat0":"joe"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.tsx new file mode 100644 index 0000000000000000000000000000000000000000..c3f6a778e2db3614687440f79c224448d7a6f6cc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx-and-break.tsx @@ -0,0 +1,32 @@ +import { + Stringify, + makeObject_Primitives, + mutate, + mutateAndReturn, +} from 'shared-runtime'; + +function useFoo({data}) { + let obj = null; + let myDiv = null; + label: { + if (data.cond) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + break label; + } + mutate(obj); + } + } + + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{data: {cond: true, cond1: true}}], + sequentialRenders: [ + {data: {cond: true, cond1: true}}, + {data: {cond: true, cond1: true}}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..61f2b13c11e57bccdf08c1a21b1e377fe799f3ea --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.expect.md @@ -0,0 +1,132 @@ + +## Input + +```javascript +import { + Stringify, + makeObject_Primitives, + mutateAndReturn, +} from 'shared-runtime'; + +/** + * In this example, the `` JSX block mutates then captures obj. + * As JSX expressions freeze their values, we know that `obj` and `myDiv` cannot + * be mutated past this. + * This set of mutable range + scopes is an edge case because the JSX expression + * references values in two scopes. + * - (freeze) the result of `mutateAndReturn` + * this is a mutable value with a mutable range starting at `makeObject()` + * - (mutate) the lvalue storing the result of `` + * this is a immutable value and so gets assigned a different scope + * + * obj@0 = makeObj(); ⌝ scope@0 + * if (cond) { | + * $1@0 = mutate(obj@0); | + * myDiv@1 = JSX $1@0 <- scope@1 | + * } ⌟ + * + * Coincidentally, the range of `obj` is extended by alignScopesToBlocks to *past* + * the end of the JSX instruction. As we currently alias identifier mutableRanges to + * scope ranges, this `freeze` reference is perceived as occurring during the mutable + * range of `obj` (even though it is after the last mutating reference). + * + * This case is technically safe as `myDiv` correctly takes `obj` as a dependency. As + * a result, developers can never observe myDiv can aliasing a different value generation + * than `obj` (e.g. the invariant `myDiv.props.value === obj` always holds). + */ +function useFoo({data}) { + let obj = null; + let myDiv = null; + if (data.cond) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + } + } + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{data: {cond: true, cond1: true}}], + sequentialRenders: [ + {data: {cond: true, cond1: true}}, + {data: {cond: true, cond1: true}}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { + Stringify, + makeObject_Primitives, + mutateAndReturn, +} from "shared-runtime"; + +/** + * In this example, the `` JSX block mutates then captures obj. + * As JSX expressions freeze their values, we know that `obj` and `myDiv` cannot + * be mutated past this. + * This set of mutable range + scopes is an edge case because the JSX expression + * references values in two scopes. + * - (freeze) the result of `mutateAndReturn` + * this is a mutable value with a mutable range starting at `makeObject()` + * - (mutate) the lvalue storing the result of `` + * this is a immutable value and so gets assigned a different scope + * + * obj@0 = makeObj(); ⌝ scope@0 + * if (cond) { | + * $1@0 = mutate(obj@0); | + * myDiv@1 = JSX $1@0 <- scope@1 | + * } ⌟ + * + * Coincidentally, the range of `obj` is extended by alignScopesToBlocks to *past* + * the end of the JSX instruction. As we currently alias identifier mutableRanges to + * scope ranges, this `freeze` reference is perceived as occurring during the mutable + * range of `obj` (even though it is after the last mutating reference). + * + * This case is technically safe as `myDiv` correctly takes `obj` as a dependency. As + * a result, developers can never observe myDiv can aliasing a different value generation + * than `obj` (e.g. the invariant `myDiv.props.value === obj` always holds). + */ +function useFoo(t0) { + const $ = _c(3); + const { data } = t0; + let obj; + let myDiv = null; + if (data.cond) { + if ($[0] !== data.cond1) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + } + $[0] = data.cond1; + $[1] = obj; + $[2] = myDiv; + } else { + obj = $[1]; + myDiv = $[2]; + } + } + + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ data: { cond: true, cond1: true } }], + sequentialRenders: [ + { data: { cond: true, cond1: true } }, + { data: { cond: true, cond1: true } }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"value":{"a":0,"b":"value1","c":true,"wat0":"joe"}}
+
{"value":{"a":0,"b":"value1","c":true,"wat0":"joe"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.tsx new file mode 100644 index 0000000000000000000000000000000000000000..48a817e13ffdaa2dbf2d8fe604d106083e42a2b5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/mutation-within-jsx.tsx @@ -0,0 +1,52 @@ +import { + Stringify, + makeObject_Primitives, + mutateAndReturn, +} from 'shared-runtime'; + +/** + * In this example, the `` JSX block mutates then captures obj. + * As JSX expressions freeze their values, we know that `obj` and `myDiv` cannot + * be mutated past this. + * This set of mutable range + scopes is an edge case because the JSX expression + * references values in two scopes. + * - (freeze) the result of `mutateAndReturn` + * this is a mutable value with a mutable range starting at `makeObject()` + * - (mutate) the lvalue storing the result of `` + * this is a immutable value and so gets assigned a different scope + * + * obj@0 = makeObj(); ⌝ scope@0 + * if (cond) { | + * $1@0 = mutate(obj@0); | + * myDiv@1 = JSX $1@0 <- scope@1 | + * } ⌟ + * + * Coincidentally, the range of `obj` is extended by alignScopesToBlocks to *past* + * the end of the JSX instruction. As we currently alias identifier mutableRanges to + * scope ranges, this `freeze` reference is perceived as occurring during the mutable + * range of `obj` (even though it is after the last mutating reference). + * + * This case is technically safe as `myDiv` correctly takes `obj` as a dependency. As + * a result, developers can never observe myDiv can aliasing a different value generation + * than `obj` (e.g. the invariant `myDiv.props.value === obj` always holds). + */ +function useFoo({data}) { + let obj = null; + let myDiv = null; + if (data.cond) { + obj = makeObject_Primitives(); + if (data.cond1) { + myDiv = ; + } + } + return myDiv; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{data: {cond: true, cond1: true}}], + sequentialRenders: [ + {data: {cond: true, cond1: true}}, + {data: {cond: true, cond1: true}}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a1267c6f8295b32182e2ed2ac2beb290d12348a8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +// @enableNameAnonymousFunctions +import {Stringify} from 'shared-runtime'; + +function Component(props) { + const onClick = () => { + console.log('hello!'); + }; + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNameAnonymousFunctions +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(1); + const onClick = _ComponentOnClick; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 =
; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _ComponentOnClick() { + console.log("hello!"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.js new file mode 100644 index 0000000000000000000000000000000000000000..0906cb928a8c3429922fcbf1f7107526851fed2a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions-outline.js @@ -0,0 +1,14 @@ +// @enableNameAnonymousFunctions +import {Stringify} from 'shared-runtime'; + +function Component(props) { + const onClick = () => { + console.log('hello!'); + }; + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6fccad7685b20ec9c104e1eb78b3090e806a90cc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.expect.md @@ -0,0 +1,289 @@ + +## Input + +```javascript +// @enableNameAnonymousFunctions + +import {useCallback, useEffect} from 'react'; +import {identity, Stringify, useIdentity} from 'shared-runtime'; +import * as SharedRuntime from 'shared-runtime'; + +function Component(props) { + function named() { + const inner = () => props.named; + const innerIdentity = identity(() => props.named); + return inner(innerIdentity()); + } + const callback = useCallback(() => { + return 'ok'; + }, []); + const namedVariable = function () { + return props.namedVariable; + }; + const methodCall = SharedRuntime.identity(() => props.methodCall); + const call = identity(() => props.call); + const builtinElementAttr =
props.builtinElementAttr} />; + const namedElementAttr = props.namedElementAttr} />; + const hookArgument = useIdentity(() => props.hookArgument); + useEffect(() => { + console.log(props.useEffect); + JSON.stringify(null, null, () => props.useEffect); + const g = () => props.useEffect; + console.log(g()); + }, [props.useEffect]); + return ( + <> + {named()} + {callback()} + {namedVariable()} + {methodCall()} + {call()} + {builtinElementAttr} + {namedElementAttr} + {hookArgument()} + + ); +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + named: '', + namedVariable: '', + methodCall: '', + call: '', + builtinElementAttr: '', + namedElementAttr: '', + hookArgument: '', + useEffect: '', + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNameAnonymousFunctions + +import { useCallback, useEffect } from "react"; +import { identity, Stringify, useIdentity } from "shared-runtime"; +import * as SharedRuntime from "shared-runtime"; + +function Component(props) { + const $ = _c(31); + let t0; + if ($[0] !== props.named) { + t0 = function named() { + const inner = { "Component[named > inner]": () => props.named }[ + "Component[named > inner]" + ]; + const innerIdentity = identity( + { "Component[named > identity()]": () => props.named }[ + "Component[named > identity()]" + ], + ); + return inner(innerIdentity()); + }; + $[0] = props.named; + $[1] = t0; + } else { + t0 = $[1]; + } + const named = t0; + + const callback = _ComponentCallback; + let t1; + if ($[2] !== props.namedVariable) { + t1 = { + "Component[namedVariable]": function () { + return props.namedVariable; + }, + }["Component[namedVariable]"]; + $[2] = props.namedVariable; + $[3] = t1; + } else { + t1 = $[3]; + } + const namedVariable = t1; + let t2; + if ($[4] !== props.methodCall) { + t2 = { "Component[SharedRuntime.identity()]": () => props.methodCall }[ + "Component[SharedRuntime.identity()]" + ]; + $[4] = props.methodCall; + $[5] = t2; + } else { + t2 = $[5]; + } + const methodCall = SharedRuntime.identity(t2); + let t3; + if ($[6] !== props.call) { + t3 = { "Component[identity()]": () => props.call }["Component[identity()]"]; + $[6] = props.call; + $[7] = t3; + } else { + t3 = $[7]; + } + const call = identity(t3); + let t4; + if ($[8] !== props.builtinElementAttr) { + t4 = ( +
.onClick]": () => props.builtinElementAttr }[ + "Component[
.onClick]" + ] + } + /> + ); + $[8] = props.builtinElementAttr; + $[9] = t4; + } else { + t4 = $[9]; + } + const builtinElementAttr = t4; + let t5; + if ($[10] !== props.namedElementAttr) { + t5 = ( + .onClick]": () => props.namedElementAttr }[ + "Component[.onClick]" + ] + } + /> + ); + $[10] = props.namedElementAttr; + $[11] = t5; + } else { + t5 = $[11]; + } + const namedElementAttr = t5; + let t6; + if ($[12] !== props.hookArgument) { + t6 = { "Component[useIdentity()]": () => props.hookArgument }[ + "Component[useIdentity()]" + ]; + $[12] = props.hookArgument; + $[13] = t6; + } else { + t6 = $[13]; + } + const hookArgument = useIdentity(t6); + let t7; + let t8; + if ($[14] !== props.useEffect) { + t7 = { + "Component[useEffect()]": () => { + console.log(props.useEffect); + JSON.stringify( + null, + null, + { + "Component[useEffect() > JSON.stringify()]": () => props.useEffect, + }["Component[useEffect() > JSON.stringify()]"], + ); + const g = { "Component[useEffect() > g]": () => props.useEffect }[ + "Component[useEffect() > g]" + ]; + console.log(g()); + }, + }["Component[useEffect()]"]; + t8 = [props.useEffect]; + $[14] = props.useEffect; + $[15] = t7; + $[16] = t8; + } else { + t7 = $[15]; + t8 = $[16]; + } + useEffect(t7, t8); + let t9; + if ($[17] !== named) { + t9 = named(); + $[17] = named; + $[18] = t9; + } else { + t9 = $[18]; + } + const t10 = callback(); + let t11; + if ($[19] !== namedVariable) { + t11 = namedVariable(); + $[19] = namedVariable; + $[20] = t11; + } else { + t11 = $[20]; + } + const t12 = methodCall(); + const t13 = call(); + let t14; + if ($[21] !== hookArgument) { + t14 = hookArgument(); + $[21] = hookArgument; + $[22] = t14; + } else { + t14 = $[22]; + } + let t15; + if ( + $[23] !== builtinElementAttr || + $[24] !== namedElementAttr || + $[25] !== t11 || + $[26] !== t12 || + $[27] !== t13 || + $[28] !== t14 || + $[29] !== t9 + ) { + t15 = ( + <> + {t9} + {t10} + {t11} + {t12} + {t13} + {builtinElementAttr} + {namedElementAttr} + {t14} + + ); + $[23] = builtinElementAttr; + $[24] = namedElementAttr; + $[25] = t11; + $[26] = t12; + $[27] = t13; + $[28] = t14; + $[29] = t9; + $[30] = t15; + } else { + t15 = $[30]; + } + return t15; +} +function _ComponentCallback() { + return "ok"; +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + named: "", + namedVariable: "", + methodCall: "", + call: "", + builtinElementAttr: "", + namedElementAttr: "", + hookArgument: "", + useEffect: "", + }, + ], +}; + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.js new file mode 100644 index 0000000000000000000000000000000000000000..963bee9ea6f7e738c0b4f8ca91816fe8b5f08bfc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/name-anonymous-functions.js @@ -0,0 +1,58 @@ +// @enableNameAnonymousFunctions + +import {useCallback, useEffect} from 'react'; +import {identity, Stringify, useIdentity} from 'shared-runtime'; +import * as SharedRuntime from 'shared-runtime'; + +function Component(props) { + function named() { + const inner = () => props.named; + const innerIdentity = identity(() => props.named); + return inner(innerIdentity()); + } + const callback = useCallback(() => { + return 'ok'; + }, []); + const namedVariable = function () { + return props.namedVariable; + }; + const methodCall = SharedRuntime.identity(() => props.methodCall); + const call = identity(() => props.call); + const builtinElementAttr =
props.builtinElementAttr} />; + const namedElementAttr = props.namedElementAttr} />; + const hookArgument = useIdentity(() => props.hookArgument); + useEffect(() => { + console.log(props.useEffect); + JSON.stringify(null, null, () => props.useEffect); + const g = () => props.useEffect; + console.log(g()); + }, [props.useEffect]); + return ( + <> + {named()} + {callback()} + {namedVariable()} + {methodCall()} + {call()} + {builtinElementAttr} + {namedElementAttr} + {hookArgument()} + + ); +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + named: '', + namedVariable: '', + methodCall: '', + call: '', + builtinElementAttr: '', + namedElementAttr: '', + hookArgument: '', + useEffect: '', + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..edf34a1da4fde04cbb174d19e9901d0aa37cbd2a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +// @compilationMode:"annotation" +if (globalThis.__DEV__) { + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"annotation" +if (globalThis.__DEV__) { + function useFoo() { + "use memo"; + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = [1, 2, 3]; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; + } +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.js new file mode 100644 index 0000000000000000000000000000000000000000..e7cbda31c9300ffec794c07ae35b7493497719d1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-annotation-mode.js @@ -0,0 +1,7 @@ +// @compilationMode:"annotation" +if (globalThis.__DEV__) { + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2ed9bb851851ec54df5ccb69175a20f5a68b405c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +// @compilationMode:"annotation" +for ( + var useFoo = function useFoo() { + 'use memo'; + return [1, 2, 3]; + }; + false; + +) {} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"annotation" +for ( + var useFoo = function useFoo() { + "use memo"; + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = [1, 2, 3]; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; + }; + false; + +) {} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.js new file mode 100644 index 0000000000000000000000000000000000000000..b885a4ce84d80ce4dbc217771af21699a4a8c5e8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-for-init.js @@ -0,0 +1,9 @@ +// @compilationMode:"annotation" +for ( + var useFoo = function useFoo() { + 'use memo'; + return [1, 2, 3]; + }; + false; + +) {} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..00ae113da61a87e965978f5e923cdb16a0e7e915 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +// @compilationMode:"annotation" +if ( + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +) { +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"annotation" +if ( + function useFoo() { + "use memo"; + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = [1, 2, 3]; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; + } +) { +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..84c84e8ce0201c441f9834bf556a6ef6105f76cb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-if-test-expr.js @@ -0,0 +1,8 @@ +// @compilationMode:"annotation" +if ( + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +) { +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..e0866bd902a7483cf0f4eb70b84ae9f28d3d6564 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +// @compilationMode:"annotation" +while ( + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +) { + break; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @compilationMode:"annotation" +while ( + function useFoo() { + "use memo"; + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = [1, 2, 3]; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; + } +) { + break; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..3e3e9aeeb21bbd906411343484ce89f53eb0d54e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-discovery-while-test-expr.js @@ -0,0 +1,9 @@ +// @compilationMode:"annotation" +while ( + function useFoo() { + 'use memo'; + return [1, 2, 3]; + } +) { + break; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3c624de9ebe57ee6206244db0056f0de8d7b494d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function Component(props) { + const [x, setX] = useState(null); + + const onChange = e => { + let x = null; // intentionally shadow the original x + setX(currentX => currentX + x); // intentionally refer to shadowed x + }; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(3); + const [x, setX] = useState(null); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = (e) => { + setX(_temp); + }; + $[0] = t0; + } else { + t0 = $[0]; + } + const onChange = t0; + let t1; + if ($[1] !== x) { + t1 = ; + $[1] = x; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} +function _temp(currentX) { + return currentX + null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ["TodoAdd"], + isComponent: "TodoAdd", +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.js new file mode 100644 index 0000000000000000000000000000000000000000..9f8385a82fe91825264dd54dce64f613e742a0a1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-shadowed-identifiers.js @@ -0,0 +1,16 @@ +function Component(props) { + const [x, setX] = useState(null); + + const onChange = e => { + let x = null; // intentionally shadow the original x + setX(currentX => currentX + x); // intentionally refer to shadowed x + }; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2ab19c3f2c53e367041f4888f7569dbf05170996 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +function Foo() { + return (function t() { + let x = {}; + return function a(x = () => {}) { + return x; + }; + })(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = function a(t1) { + const x_0 = t1 === undefined ? _temp : t1; + return x_0; + }; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], + isComponent: false, +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.ts new file mode 100644 index 0000000000000000000000000000000000000000..746df1cb40889678f7c26e6ef1635a0900ae1646 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-function-with-param-as-captured-dep.ts @@ -0,0 +1,14 @@ +function Foo() { + return (function t() { + let x = {}; + return function a(x = () => {}) { + return x; + }; + })(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..92a24194a359d64100d36767c5a4ca4c19d51a32 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.expect.md @@ -0,0 +1,229 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +/** + * identity(...)?.toString() is the outer optional, and prop?.value is the inner + * one. + * Note that prop?. + */ +function useFoo({ + prop1, + prop2, + prop3, + prop4, + prop5, + prop6, +}: { + prop1: null | {value: number}; + prop2: null | {inner: {value: number}}; + prop3: null | {fn: (val: any) => NonNullable}; + prop4: null | {inner: {value: number}}; + prop5: null | {fn: (val: any) => NonNullable}; + prop6: null | {inner: {value: number}}; +}) { + // prop1?.value should be hoisted as the dependency of x + const x = identity(prop1?.value)?.toString(); + + // prop2?.inner.value should be hoisted as the dependency of y + const y = identity(prop2?.inner.value)?.toString(); + + // prop3 and prop4?.inner should be hoisted as the dependency of z + const z = prop3?.fn(prop4?.inner.value).toString(); + + // prop5 and prop6?.inner should be hoisted as the dependency of zz + const zz = prop5?.fn(prop6?.inner.value)?.toString(); + return [x, y, z, zz]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + ], + sequentialRenders: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: 3}}, + prop3: {fn: identity}, + prop4: {inner: {value: 4}}, + prop5: {fn: identity}, + prop6: {inner: {value: 4}}, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: 3}}, + prop3: {fn: identity}, + prop4: {inner: {value: 4}}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: undefined}}, + prop3: {fn: identity}, + prop4: {inner: {value: undefined}}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + { + prop1: {value: 2}, + prop2: {}, + prop3: {fn: identity}, + prop4: {}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +/** + * identity(...)?.toString() is the outer optional, and prop?.value is the inner + * one. + * Note that prop?. + */ +function useFoo(t0) { + const $ = _c(15); + const { prop1, prop2, prop3, prop4, prop5, prop6 } = t0; + let t1; + if ($[0] !== prop1?.value) { + t1 = identity(prop1?.value)?.toString(); + $[0] = prop1?.value; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + let t2; + if ($[2] !== prop2?.inner.value) { + t2 = identity(prop2?.inner.value)?.toString(); + $[2] = prop2?.inner.value; + $[3] = t2; + } else { + t2 = $[3]; + } + const y = t2; + let t3; + if ($[4] !== prop3 || $[5] !== prop4?.inner) { + t3 = prop3?.fn(prop4?.inner.value).toString(); + $[4] = prop3; + $[5] = prop4?.inner; + $[6] = t3; + } else { + t3 = $[6]; + } + const z = t3; + let t4; + if ($[7] !== prop5 || $[8] !== prop6?.inner) { + t4 = prop5?.fn(prop6?.inner.value)?.toString(); + $[7] = prop5; + $[8] = prop6?.inner; + $[9] = t4; + } else { + t4 = $[9]; + } + const zz = t4; + let t5; + if ($[10] !== x || $[11] !== y || $[12] !== z || $[13] !== zz) { + t5 = [x, y, z, zz]; + $[10] = x; + $[11] = y; + $[12] = z; + $[13] = zz; + $[14] = t5; + } else { + t5 = $[14]; + } + return t5; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + ], + + sequentialRenders: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + { + prop1: { value: 2 }, + prop2: { inner: { value: 3 } }, + prop3: { fn: identity }, + prop4: { inner: { value: 4 } }, + prop5: { fn: identity }, + prop6: { inner: { value: 4 } }, + }, + { + prop1: { value: 2 }, + prop2: { inner: { value: 3 } }, + prop3: { fn: identity }, + prop4: { inner: { value: 4 } }, + prop5: { fn: identity }, + prop6: { inner: { value: undefined } }, + }, + { + prop1: { value: 2 }, + prop2: { inner: { value: undefined } }, + prop3: { fn: identity }, + prop4: { inner: { value: undefined } }, + prop5: { fn: identity }, + prop6: { inner: { value: undefined } }, + }, + { + prop1: { value: 2 }, + prop2: {}, + prop3: { fn: identity }, + prop4: {}, + prop5: { fn: identity }, + prop6: { inner: { value: undefined } }, + }, + ], +}; + +``` + +### Eval output +(kind: ok) [null,null,null,null] +["2","3","4","4"] +["2","3","4",null] +[[ (exception in render) TypeError: Cannot read properties of undefined (reading 'toString') ]] +[[ (exception in render) TypeError: Cannot read properties of undefined (reading 'value') ]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.ts new file mode 100644 index 0000000000000000000000000000000000000000..d00cb4fee698319b721662163b29b6531d38eaeb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-chains.ts @@ -0,0 +1,91 @@ +import {identity} from 'shared-runtime'; + +/** + * identity(...)?.toString() is the outer optional, and prop?.value is the inner + * one. + * Note that prop?. + */ +function useFoo({ + prop1, + prop2, + prop3, + prop4, + prop5, + prop6, +}: { + prop1: null | {value: number}; + prop2: null | {inner: {value: number}}; + prop3: null | {fn: (val: any) => NonNullable}; + prop4: null | {inner: {value: number}}; + prop5: null | {fn: (val: any) => NonNullable}; + prop6: null | {inner: {value: number}}; +}) { + // prop1?.value should be hoisted as the dependency of x + const x = identity(prop1?.value)?.toString(); + + // prop2?.inner.value should be hoisted as the dependency of y + const y = identity(prop2?.inner.value)?.toString(); + + // prop3 and prop4?.inner should be hoisted as the dependency of z + const z = prop3?.fn(prop4?.inner.value).toString(); + + // prop5 and prop6?.inner should be hoisted as the dependency of zz + const zz = prop5?.fn(prop6?.inner.value)?.toString(); + return [x, y, z, zz]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + ], + sequentialRenders: [ + { + prop1: null, + prop2: null, + prop3: null, + prop4: null, + prop5: null, + prop6: null, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: 3}}, + prop3: {fn: identity}, + prop4: {inner: {value: 4}}, + prop5: {fn: identity}, + prop6: {inner: {value: 4}}, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: 3}}, + prop3: {fn: identity}, + prop4: {inner: {value: 4}}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + { + prop1: {value: 2}, + prop2: {inner: {value: undefined}}, + prop3: {fn: identity}, + prop4: {inner: {value: undefined}}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + { + prop1: {value: 2}, + prop2: {}, + prop3: {fn: identity}, + prop4: {}, + prop5: {fn: identity}, + prop6: {inner: {value: undefined}}, + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c2c1cc30c0d71a6a22f19138c11e4acc10feae56 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +// We should codegen nested optional properties correctly +// (i.e. placing `?` in the correct PropertyLoad) +function Component(props) { + let x = foo(props.a?.b.c.d); + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // We should codegen nested optional properties correctly +// (i.e. placing `?` in the correct PropertyLoad) +function Component(props) { + const $ = _c(2); + const t0 = props.a?.b.c.d; + let t1; + if ($[0] !== t0) { + t1 = foo(t0); + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..e9f800af8d456a20251d6e1c1d0e4b1c08d936cd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-optional-member-expr.js @@ -0,0 +1,6 @@ +// We should codegen nested optional properties correctly +// (i.e. placing `?` in the correct PropertyLoad) +function Component(props) { + let x = foo(props.a?.b.c.d); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..37d41b69f32c3efd0e6e86b65aa53655858883f1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +import {identity, mutate} from 'shared-runtime'; + +function Foo({cond}) { + const x = identity(identity(cond)) ? {a: 2} : {b: 2}; + + mutate(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{cond: false}], + sequentialRenders: [{cond: false}, {cond: false}, {cond: true}, {cond: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate } from "shared-runtime"; + +function Foo(t0) { + const $ = _c(2); + const { cond } = t0; + let x; + if ($[0] !== cond) { + x = identity(identity(cond)) ? { a: 2 } : { b: 2 }; + + mutate(x); + $[0] = cond; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ cond: false }], + sequentialRenders: [ + { cond: false }, + { cond: false }, + { cond: true }, + { cond: true }, + ], +}; + +``` + +### Eval output +(kind: ok) {"b":2,"wat0":"joe"} +{"b":2,"wat0":"joe"} +{"a":2,"wat0":"joe"} +{"a":2,"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f90511d05be9c7d8f69c51b3c8bbbb9b22eba7e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-begin-same-instr-valueblock.ts @@ -0,0 +1,14 @@ +import {identity, mutate} from 'shared-runtime'; + +function Foo({cond}) { + const x = identity(identity(cond)) ? {a: 2} : {b: 2}; + + mutate(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{cond: false}], + sequentialRenders: [{cond: false}, {cond: false}, {cond: true}, {cond: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..600ff19f0fe7c06c00a2d94eeffd338b7815b672 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.expect.md @@ -0,0 +1,27 @@ + +## Input + +```javascript +function component(props) { + let x = []; + let y = []; + y.push(useHook(props.foo)); + x.push(y); + return x; +} + +``` + +## Code + +```javascript +function component(props) { + const x = []; + const y = []; + y.push(useHook(props.foo)); + x.push(y); + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.js new file mode 100644 index 0000000000000000000000000000000000000000..a1b0858fb3e5e0f228e6fb33baa2a4cc1f7cdcdc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nested-scopes-hook-call.js @@ -0,0 +1,7 @@ +function component(props) { + let x = []; + let y = []; + y.push(useHook(props.foo)); + x.push(y); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1221ce01b9391bb67b754f31c7496e7bc6ed9bc8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.expect.md @@ -0,0 +1,77 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +class Foo {} +function Component({val}) { + const MyClass = identity(Foo); + const x = [val]; + const y = new MyClass(); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{val: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +class Foo {} +function Component(t0) { + const $ = _c(6); + const { val } = t0; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = identity(Foo); + $[0] = t1; + } else { + t1 = $[0]; + } + const MyClass = t1; + let t2; + if ($[1] !== val) { + t2 = [val]; + $[1] = val; + $[2] = t2; + } else { + t2 = $[2]; + } + const x = t2; + let t3; + if ($[3] === Symbol.for("react.memo_cache_sentinel")) { + t3 = new MyClass(); + $[3] = t3; + } else { + t3 = $[3]; + } + const y = t3; + let t4; + if ($[4] !== x) { + t4 = [x, y]; + $[4] = x; + $[5] = t4; + } else { + t4 = $[5]; + } + return t4; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ val: 0 }], +}; + +``` + +### Eval output +(kind: ok) [[0],{}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.ts new file mode 100644 index 0000000000000000000000000000000000000000..a25040a28560e88f15b3dd5ee530163d84c7358f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-does-not-mutate-class.ts @@ -0,0 +1,15 @@ +import {identity} from 'shared-runtime'; + +class Foo {} +function Component({val}) { + const MyClass = identity(Foo); + const x = [val]; + const y = new MyClass(); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{val: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..64b08ebd4622552f4336c149ae01f6bab2cd54b3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.expect.md @@ -0,0 +1,212 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import { + Stringify, + mutate, + identity, + shallowCopy, + setPropertyByKey, +} from 'shared-runtime'; + +/** + * This fixture is similar to `bug-aliased-capture-aliased-mutate` and + * `nonmutating-capture-in-unsplittable-memo-block`, but with a focus on + * dependency extraction. + * + * NOTE: this fixture is currently valid, but will break with optimizations: + * - Scope and mutable-range based reordering may move the array creation + * *after* the `mutate(aliasedObj)` call. This is invalid if mutate + * reassigns inner properties. + * - RecycleInto or other deeper-equality optimizations may produce invalid + * output -- it may compare the array's contents / dependencies too early. + * - Runtime validation for immutable values will break if `mutate` does + * interior mutation of the value captured into the array. + * + * Before scope block creation, HIR looks like this: + * // + * // $1 is unscoped as obj's mutable range will be + * // extended in a later pass + * // + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * // + * // $3 gets assigned a scope as Array is an allocating + * // instruction, but this does *not* get extended or + * // merged into the later mutation site. + * // (explained in `bug-aliased-capture-aliased-mutate`) + * // + * $3@1 = Array[$2] + * ... + * $10@0 = LoadLocal shallowCopy@0[0, 12] + * $11 = LoadGlobal mutate + * $12 = $11($10@0[0, 12]) + * + * When filling in scope dependencies, we find that it's incorrect to depend on + * PropertyLoads from obj as it hasn't completed its mutable range. Following + * the immutable / mutable-new typing system, we check the identity of obj to + * detect whether it was newly created (and thus mutable) in this render pass. + * + * HIR with scopes looks like this. + * bb0: + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * scopeTerminal deps=[obj@0] block=bb1 fallt=bb2 + * bb1: + * $3@1 = Array[$2] + * goto bb2 + * bb2: + * ... + * + * This is surprising as deps now is entirely decoupled from temporaries used + * by the block itself. scope @1's instructions now reference a value (1) + * produced outside its scope range and (2) not represented in its dependencies + * + * The right thing to do is to ensure that all Loads from a value get assigned + * the value's reactive scope. This also requires track mutating and aliasing + * separately from scope range. In this example, that would correctly merge + * the scopes of $3 with obj. + * Runtime validation and optimizations such as ReactiveGraph-based reordering + * require this as well. + * + * A tempting fix is to instead extend $3's ReactiveScope range up to include + * $2 (the PropertyLoad). This fixes dependency deduping but not reordering + * and mutability. + */ +function Component({prop}) { + let obj = shallowCopy(prop); + const aliasedObj = identity(obj); + + // [obj.id] currently is assigned its own reactive scope + const id = [obj.id]; + + // Writing to the alias may reassign to previously captured references. + // The compiler currently produces valid output, but this breaks with + // reordering, recycleInto, and other potential optimizations. + mutate(aliasedObj); + setPropertyByKey(aliasedObj, 'id', prop.id + 1); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prop: {id: 1}}], + sequentialRenders: [{prop: {id: 1}}, {prop: {id: 1}}, {prop: {id: 2}}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { + Stringify, + mutate, + identity, + shallowCopy, + setPropertyByKey, +} from "shared-runtime"; + +/** + * This fixture is similar to `bug-aliased-capture-aliased-mutate` and + * `nonmutating-capture-in-unsplittable-memo-block`, but with a focus on + * dependency extraction. + * + * NOTE: this fixture is currently valid, but will break with optimizations: + * - Scope and mutable-range based reordering may move the array creation + * *after* the `mutate(aliasedObj)` call. This is invalid if mutate + * reassigns inner properties. + * - RecycleInto or other deeper-equality optimizations may produce invalid + * output -- it may compare the array's contents / dependencies too early. + * - Runtime validation for immutable values will break if `mutate` does + * interior mutation of the value captured into the array. + * + * Before scope block creation, HIR looks like this: + * // + * // $1 is unscoped as obj's mutable range will be + * // extended in a later pass + * // + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * // + * // $3 gets assigned a scope as Array is an allocating + * // instruction, but this does *not* get extended or + * // merged into the later mutation site. + * // (explained in `bug-aliased-capture-aliased-mutate`) + * // + * $3@1 = Array[$2] + * ... + * $10@0 = LoadLocal shallowCopy@0[0, 12] + * $11 = LoadGlobal mutate + * $12 = $11($10@0[0, 12]) + * + * When filling in scope dependencies, we find that it's incorrect to depend on + * PropertyLoads from obj as it hasn't completed its mutable range. Following + * the immutable / mutable-new typing system, we check the identity of obj to + * detect whether it was newly created (and thus mutable) in this render pass. + * + * HIR with scopes looks like this. + * bb0: + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * scopeTerminal deps=[obj@0] block=bb1 fallt=bb2 + * bb1: + * $3@1 = Array[$2] + * goto bb2 + * bb2: + * ... + * + * This is surprising as deps now is entirely decoupled from temporaries used + * by the block itself. scope @1's instructions now reference a value (1) + * produced outside its scope range and (2) not represented in its dependencies + * + * The right thing to do is to ensure that all Loads from a value get assigned + * the value's reactive scope. This also requires track mutating and aliasing + * separately from scope range. In this example, that would correctly merge + * the scopes of $3 with obj. + * Runtime validation and optimizations such as ReactiveGraph-based reordering + * require this as well. + * + * A tempting fix is to instead extend $3's ReactiveScope range up to include + * $2 (the PropertyLoad). This fixes dependency deduping but not reordering + * and mutability. + */ +function Component(t0) { + const $ = _c(2); + const { prop } = t0; + let t1; + if ($[0] !== prop) { + const obj = shallowCopy(prop); + const aliasedObj = identity(obj); + const id = [obj.id]; + mutate(aliasedObj); + setPropertyByKey(aliasedObj, "id", prop.id + 1); + t1 = ; + $[0] = prop; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ prop: { id: 1 } }], + sequentialRenders: [ + { prop: { id: 1 } }, + { prop: { id: 1 } }, + { prop: { id: 2 } }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"id":[1]}
+
{"id":[1]}
+
{"id":[2]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ecd5598cb091386a2000f5274c48983bbc851c17 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/aliased-nested-scope-truncated-dep.tsx @@ -0,0 +1,94 @@ +// @enableNewMutationAliasingModel +import { + Stringify, + mutate, + identity, + shallowCopy, + setPropertyByKey, +} from 'shared-runtime'; + +/** + * This fixture is similar to `bug-aliased-capture-aliased-mutate` and + * `nonmutating-capture-in-unsplittable-memo-block`, but with a focus on + * dependency extraction. + * + * NOTE: this fixture is currently valid, but will break with optimizations: + * - Scope and mutable-range based reordering may move the array creation + * *after* the `mutate(aliasedObj)` call. This is invalid if mutate + * reassigns inner properties. + * - RecycleInto or other deeper-equality optimizations may produce invalid + * output -- it may compare the array's contents / dependencies too early. + * - Runtime validation for immutable values will break if `mutate` does + * interior mutation of the value captured into the array. + * + * Before scope block creation, HIR looks like this: + * // + * // $1 is unscoped as obj's mutable range will be + * // extended in a later pass + * // + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * // + * // $3 gets assigned a scope as Array is an allocating + * // instruction, but this does *not* get extended or + * // merged into the later mutation site. + * // (explained in `bug-aliased-capture-aliased-mutate`) + * // + * $3@1 = Array[$2] + * ... + * $10@0 = LoadLocal shallowCopy@0[0, 12] + * $11 = LoadGlobal mutate + * $12 = $11($10@0[0, 12]) + * + * When filling in scope dependencies, we find that it's incorrect to depend on + * PropertyLoads from obj as it hasn't completed its mutable range. Following + * the immutable / mutable-new typing system, we check the identity of obj to + * detect whether it was newly created (and thus mutable) in this render pass. + * + * HIR with scopes looks like this. + * bb0: + * $1 = LoadLocal obj@0[0:12] + * $2 = PropertyLoad $1.id + * scopeTerminal deps=[obj@0] block=bb1 fallt=bb2 + * bb1: + * $3@1 = Array[$2] + * goto bb2 + * bb2: + * ... + * + * This is surprising as deps now is entirely decoupled from temporaries used + * by the block itself. scope @1's instructions now reference a value (1) + * produced outside its scope range and (2) not represented in its dependencies + * + * The right thing to do is to ensure that all Loads from a value get assigned + * the value's reactive scope. This also requires track mutating and aliasing + * separately from scope range. In this example, that would correctly merge + * the scopes of $3 with obj. + * Runtime validation and optimizations such as ReactiveGraph-based reordering + * require this as well. + * + * A tempting fix is to instead extend $3's ReactiveScope range up to include + * $2 (the PropertyLoad). This fixes dependency deduping but not reordering + * and mutability. + */ +function Component({prop}) { + let obj = shallowCopy(prop); + const aliasedObj = identity(obj); + + // [obj.id] currently is assigned its own reactive scope + const id = [obj.id]; + + // Writing to the alias may reassign to previously captured references. + // The compiler currently produces valid output, but this breaks with + // reordering, recycleInto, and other potential optimizations. + mutate(aliasedObj); + setPropertyByKey(aliasedObj, 'id', prop.id + 1); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prop: {id: 1}}], + sequentialRenders: [{prop: {id: 1}}, {prop: {id: 1}}, {prop: {id: 2}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b3531c225d9ead6bad1323269338ee136993bb1c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.expect.md @@ -0,0 +1,93 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({value}) { + const arr = [{value: 'foo'}, {value: 'bar'}, {value}]; + useIdentity(null); + const derived = arr.filter(Boolean); + return ( + + {derived.at(0)} + {derived.at(-1)} + + ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(13); + const { value } = t0; + let t1; + let t2; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = { value: "foo" }; + t2 = { value: "bar" }; + $[0] = t1; + $[1] = t2; + } else { + t1 = $[0]; + t2 = $[1]; + } + let t3; + if ($[2] !== value) { + t3 = [t1, t2, { value }]; + $[2] = value; + $[3] = t3; + } else { + t3 = $[3]; + } + const arr = t3; + useIdentity(null); + let t4; + if ($[4] !== arr) { + t4 = arr.filter(Boolean); + $[4] = arr; + $[5] = t4; + } else { + t4 = $[5]; + } + const derived = t4; + let t5; + if ($[6] !== derived) { + t5 = derived.at(0); + $[6] = derived; + $[7] = t5; + } else { + t5 = $[7]; + } + let t6; + if ($[8] !== derived) { + t6 = derived.at(-1); + $[8] = derived; + $[9] = t6; + } else { + t6 = $[9]; + } + let t7; + if ($[10] !== t5 || $[11] !== t6) { + t7 = ( + + {t5} + {t6} + + ); + $[10] = t5; + $[11] = t6; + $[12] = t7; + } else { + t7 = $[12]; + } + return t7; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.js new file mode 100644 index 0000000000000000000000000000000000000000..3229088e1da90d886c2020ac24e32d5d6921492f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-filter.js @@ -0,0 +1,12 @@ +// @enableNewMutationAliasingModel +function Component({value}) { + const arr = [{value: 'foo'}, {value: 'bar'}, {value}]; + useIdentity(null); + const derived = arr.filter(Boolean); + return ( + + {derived.at(0)} + {derived.at(-1)} + + ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..db5ce79eab94591f48c45a64cc6e54bc1690c4fb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component(props) { + // This item is part of the receiver, should be memoized + const item = {a: props.a}; + const items = [item]; + const mapped = items.map(item => item); + // mapped[0].a = null; + return mapped; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: {id: 42}}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.a) { + const item = { a: props.a }; + const items = [item]; + t0 = items.map(_temp); + $[0] = props.a; + $[1] = t0; + } else { + t0 = $[1]; + } + const mapped = t0; + + return mapped; +} +function _temp(item_0) { + return item_0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: { id: 42 } }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) [{"a":{"id":42}}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.js new file mode 100644 index 0000000000000000000000000000000000000000..42e32b3e38b3f557a3b7374451b489a9c2ef9227 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-captures-receiver-noAlias.js @@ -0,0 +1,15 @@ +// @enableNewMutationAliasingModel +function Component(props) { + // This item is part of the receiver, should be memoized + const item = {a: props.a}; + const items = [item]; + const mapped = items.map(item => item); + // mapped[0].a = null; + return mapped; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: {id: 42}}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7bc2c193cf7cc3698dc344924ecb8621331e24f8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.expect.md @@ -0,0 +1,134 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +/** + * Forked from array-map-simple.js + * + * Named lambdas (e.g. cb1) may be defined in the top scope of a function and + * used in a different lambda (getArrMap1). + * + * Here, we should try to determine if cb1 is actually called. In this case: + * - getArrMap1 is assumed to be called as it's passed to JSX + * - cb1 is not assumed to be called since it's only used as a call operand + */ +function useFoo({arr1, arr2}) { + const cb1 = e => arr1[0].value + e.value; + const getArrMap1 = () => arr1.map(cb1); + const cb2 = e => arr2[0].value + e.value; + const getArrMap2 = () => arr1.map(cb2); + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{arr1: [], arr2: []}], + sequentialRenders: [ + {arr1: [], arr2: []}, + {arr1: [], arr2: null}, + {arr1: [{value: 1}, {value: 2}], arr2: [{value: -1}]}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { Stringify } from "shared-runtime"; + +/** + * Forked from array-map-simple.js + * + * Named lambdas (e.g. cb1) may be defined in the top scope of a function and + * used in a different lambda (getArrMap1). + * + * Here, we should try to determine if cb1 is actually called. In this case: + * - getArrMap1 is assumed to be called as it's passed to JSX + * - cb1 is not assumed to be called since it's only used as a call operand + */ +function useFoo(t0) { + const $ = _c(13); + const { arr1, arr2 } = t0; + let t1; + if ($[0] !== arr1[0]) { + t1 = (e) => arr1[0].value + e.value; + $[0] = arr1[0]; + $[1] = t1; + } else { + t1 = $[1]; + } + const cb1 = t1; + let t2; + if ($[2] !== arr1 || $[3] !== cb1) { + t2 = () => arr1.map(cb1); + $[2] = arr1; + $[3] = cb1; + $[4] = t2; + } else { + t2 = $[4]; + } + const getArrMap1 = t2; + let t3; + if ($[5] !== arr2) { + t3 = (e_0) => arr2[0].value + e_0.value; + $[5] = arr2; + $[6] = t3; + } else { + t3 = $[6]; + } + const cb2 = t3; + let t4; + if ($[7] !== arr1 || $[8] !== cb2) { + t4 = () => arr1.map(cb2); + $[7] = arr1; + $[8] = cb2; + $[9] = t4; + } else { + t4 = $[9]; + } + const getArrMap2 = t4; + let t5; + if ($[10] !== getArrMap1 || $[11] !== getArrMap2) { + t5 = ( + + ); + $[10] = getArrMap1; + $[11] = getArrMap2; + $[12] = t5; + } else { + t5 = $[12]; + } + return t5; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ arr1: [], arr2: [] }], + sequentialRenders: [ + { arr1: [], arr2: [] }, + { arr1: [], arr2: null }, + { arr1: [{ value: 1 }, { value: 2 }], arr2: [{ value: -1 }] }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"getArrMap1":{"kind":"Function","result":[]},"getArrMap2":{"kind":"Function","result":[]},"shouldInvokeFns":true}
+
{"getArrMap1":{"kind":"Function","result":[]},"getArrMap2":{"kind":"Function","result":[]},"shouldInvokeFns":true}
+
{"getArrMap1":{"kind":"Function","result":[2,3]},"getArrMap2":{"kind":"Function","result":[0,1]},"shouldInvokeFns":true}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.js new file mode 100644 index 0000000000000000000000000000000000000000..faa34747da18844f9dce468a504134e3609b296a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-map-named-callback-cross-context.js @@ -0,0 +1,36 @@ +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +/** + * Forked from array-map-simple.js + * + * Named lambdas (e.g. cb1) may be defined in the top scope of a function and + * used in a different lambda (getArrMap1). + * + * Here, we should try to determine if cb1 is actually called. In this case: + * - getArrMap1 is assumed to be called as it's passed to JSX + * - cb1 is not assumed to be called since it's only used as a call operand + */ +function useFoo({arr1, arr2}) { + const cb1 = e => arr1[0].value + e.value; + const getArrMap1 = () => arr1.map(cb1); + const cb2 = e => arr2[0].value + e.value; + const getArrMap2 = () => arr1.map(cb2); + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{arr1: [], arr2: []}], + sequentialRenders: [ + {arr1: [], arr2: []}, + {arr1: [], arr2: null}, + {arr1: [{value: 1}, {value: 2}], arr2: [{value: -1}]}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b2564a7a90d1be26314d99811da4d5696af189e3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b, c}) { + const x = []; + x.push(a); + const merged = {b}; // could be mutated by mutate(x) below + x.push(merged); + mutate(x); + const independent = {c}; // can't be later mutated + x.push(independent); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(6); + const { a, b, c } = t0; + let t1; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + const x = []; + x.push(a); + const merged = { b }; + x.push(merged); + mutate(x); + let t2; + if ($[4] !== c) { + t2 = { c }; + $[4] = c; + $[5] = t2; + } else { + t2 = $[5]; + } + const independent = t2; + x.push(independent); + t1 = ; + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.js new file mode 100644 index 0000000000000000000000000000000000000000..eb7f31bff631ebf7989f51ae71896545c0a450a2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/array-push.js @@ -0,0 +1,11 @@ +// @enableNewMutationAliasingModel +function Component({a, b, c}) { + const x = []; + x.push(a); + const merged = {b}; // could be mutated by mutate(x) below + x.push(merged); + mutate(x); + const independent = {c}; // can't be later mutated + x.push(independent); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..72f0e7fb28ef37e1e339552aab3b2b8f7a469b1a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + const y = [b]; + const f = () => { + y.x = x; + mutate(y); + }; + f(); + return
{x}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const x = { a }; + const y = [b]; + const f = () => { + y.x = x; + mutate(y); + }; + f(); + t1 =
{x}
; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..8d4bb23742b2b596daef0b27925d878d5aa1f826 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation-via-function-expression.js @@ -0,0 +1,11 @@ +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + const y = [b]; + const f = () => { + y.x = x; + mutate(y); + }; + f(); + return
{x}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0753f007b7b32a6f078783d0d9c06db73683a9e6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + const y = [b]; + y.x = x; + mutate(y); + return
{x}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const x = { a }; + const y = [b]; + y.x = x; + mutate(y); + t1 =
{x}
; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.js new file mode 100644 index 0000000000000000000000000000000000000000..480221fef4dab7c5bed4b4ec1830b18422107f2c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/basic-mutation.js @@ -0,0 +1,8 @@ +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + const y = [b]; + y.x = x; + mutate(y); + return
{x}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..99e9a66924ac23b3406cb9bdd00ee67e40584fcc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.expect.md @@ -0,0 +1,103 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {arrayPush, Stringify} from 'shared-runtime'; + +function Component({prop1, prop2}) { + 'use memo'; + + let x = [{value: prop1}]; + let z; + while (x.length < 2) { + // there's a phi here for x (value before the loop and the reassignment later) + + // this mutation occurs before the reassigned value + arrayPush(x, {value: prop2}); + + if (x[0].value === prop1) { + x = [{value: prop2}]; + const y = x; + z = y[0]; + } + } + z.other = true; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prop1: 0, prop2: 'a'}], + sequentialRenders: [ + {prop1: 0, prop2: 'a'}, + {prop1: 1, prop2: 'a'}, + {prop1: 1, prop2: 'b'}, + {prop1: 0, prop2: 'b'}, + {prop1: 0, prop2: 'a'}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { arrayPush, Stringify } from "shared-runtime"; + +function Component(t0) { + "use memo"; + const $ = _c(5); + const { prop1, prop2 } = t0; + let z; + if ($[0] !== prop1 || $[1] !== prop2) { + let x = [{ value: prop1 }]; + while (x.length < 2) { + arrayPush(x, { value: prop2 }); + + if (x[0].value === prop1) { + x = [{ value: prop2 }]; + const y = x; + z = y[0]; + } + } + + z.other = true; + $[0] = prop1; + $[1] = prop2; + $[2] = z; + } else { + z = $[2]; + } + let t1; + if ($[3] !== z) { + t1 = ; + $[3] = z; + $[4] = t1; + } else { + t1 = $[4]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ prop1: 0, prop2: "a" }], + sequentialRenders: [ + { prop1: 0, prop2: "a" }, + { prop1: 1, prop2: "a" }, + { prop1: 1, prop2: "b" }, + { prop1: 0, prop2: "b" }, + { prop1: 0, prop2: "a" }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"z":{"value":"a","other":true}}
+
{"z":{"value":"a","other":true}}
+
{"z":{"value":"b","other":true}}
+
{"z":{"value":"b","other":true}}
+
{"z":{"value":"a","other":true}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.js new file mode 100644 index 0000000000000000000000000000000000000000..042cae823f7f3b0c68a95904caba8773eb939dfe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-backedge-phi-with-later-mutation.js @@ -0,0 +1,35 @@ +// @enableNewMutationAliasingModel +import {arrayPush, Stringify} from 'shared-runtime'; + +function Component({prop1, prop2}) { + 'use memo'; + + let x = [{value: prop1}]; + let z; + while (x.length < 2) { + // there's a phi here for x (value before the loop and the reassignment later) + + // this mutation occurs before the reassigned value + arrayPush(x, {value: prop2}); + + if (x[0].value === prop1) { + x = [{value: prop2}]; + const y = x; + z = y[0]; + } + } + z.other = true; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prop1: 0, prop2: 'a'}], + sequentialRenders: [ + {prop1: 0, prop2: 'a'}, + {prop1: 1, prop2: 'a'}, + {prop1: 1, prop2: 'b'}, + {prop1: 0, prop2: 'b'}, + {prop1: 0, prop2: 'a'}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..34c41ff54cf93f7bb48c2863a0bd87139238d64a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.expect.md @@ -0,0 +1,80 @@ + +## Input + +```javascript +import {Stringify, mutate} from 'shared-runtime'; + +function Component({foo, bar}) { + let x = {foo}; + let y = {bar}; + const f0 = function () { + let a = {y}; + let b = {x}; + a.y.x = b; + }; + f0(); + mutate(y); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{foo: 2, bar: 3}], + sequentialRenders: [ + {foo: 2, bar: 3}, + {foo: 2, bar: 3}, + {foo: 2, bar: 4}, + {foo: 3, bar: 4}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify, mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { foo, bar } = t0; + let t1; + if ($[0] !== bar || $[1] !== foo) { + const x = { foo }; + const y = { bar }; + const f0 = function () { + const a = { y }; + const b = { x }; + a.y.x = b; + }; + f0(); + mutate(y); + t1 = ; + $[0] = bar; + $[1] = foo; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ foo: 2, bar: 3 }], + sequentialRenders: [ + { foo: 2, bar: 3 }, + { foo: 2, bar: 3 }, + { foo: 2, bar: 4 }, + { foo: 3, bar: 4 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"x":{"bar":3,"x":{"x":{"foo":2}},"wat0":"joe"}}
+
{"x":{"bar":3,"x":{"x":{"foo":2}},"wat0":"joe"}}
+
{"x":{"bar":4,"x":{"x":{"foo":2}},"wat0":"joe"}}
+
{"x":{"bar":4,"x":{"x":{"foo":3}},"wat0":"joe"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.js new file mode 100644 index 0000000000000000000000000000000000000000..5aa39d3ffb36bb1dae0c6a6f7062c9c6fc6da428 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capture-in-function-expression-indirect.js @@ -0,0 +1,25 @@ +import {Stringify, mutate} from 'shared-runtime'; + +function Component({foo, bar}) { + let x = {foo}; + let y = {bar}; + const f0 = function () { + let a = {y}; + let b = {x}; + a.y.x = b; + }; + f0(); + mutate(y); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{foo: 2, bar: 3}], + sequentialRenders: [ + {foo: 2, bar: 3}, + {foo: 2, bar: 3}, + {foo: 2, bar: 4}, + {foo: 3, bar: 4}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..45c8687b33bf6624eb225681eb11e287a552f863 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0][1]) { + y = {}; + + y = x[0][1]; + $[2] = x[0][1]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [["val1", "val2"]], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..a77287910a419e397b880c0718ede1a88f81ee26 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-2-iife.js @@ -0,0 +1,16 @@ +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7483ed4e06a93904c20ca7e817b121bf5565e92d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.expect.md @@ -0,0 +1,72 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function bar(a, b) { + const $ = _c(6); + let t0; + if ($[0] !== a || $[1] !== b) { + t0 = [a, b]; + $[0] = a; + $[1] = b; + $[2] = t0; + } else { + t0 = $[2]; + } + const x = t0; + let y; + if ($[3] !== x[0][1] || $[4] !== x[1][0]) { + y = {}; + let t = {}; + + y = x[0][1]; + t = x[1][0]; + $[3] = x[0][1]; + $[4] = x[1][0]; + $[5] = y; + } else { + y = $[5]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +### Eval output +(kind: ok) 2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..9afe5994b210b73d014e1c31daa03b5d41ee0232 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-3-iife.js @@ -0,0 +1,20 @@ +// @enableNewMutationAliasingModel +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..69de576fdfbaeb2e55a03295ed4a55e456a7b2a3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0].a[1]) { + y = {}; + + y = x[0].a[1]; + $[2] = x[0].a[1]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{ a: ["val1", "val2"] }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..5a3cb878485d40b7483facb10159d5113ecac197 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-4-iife.js @@ -0,0 +1,16 @@ +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2ff5714010cb1e9808f9cf5b7717d6e78b024258 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0]) { + y = {}; + + y = x[0]; + $[2] = x[0]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ["TodoAdd"], +}; + +``` + +### Eval output +(kind: ok) "TodoAdd" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..0b95fc02a2b5871b9716993bc4995ee5631877e3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/capturing-function-alias-computed-load-iife.js @@ -0,0 +1,15 @@ +// @enableNewMutationAliasingModel +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1241971d827d5e74d3f0eb5a4ee424d905c61479 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @validateNoImpureFunctionsInRender @enableNewMutationAliasingModel + +function Component() { + const date = Date.now(); + const now = performance.now(); + const rand = Math.random(); + return ; +} + +``` + + +## Error + +``` +Found 3 errors: + +Error: Cannot call impure function during render + +`Date.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). + +error.invalid-impure-functions-in-render.ts:4:15 + 2 | + 3 | function Component() { +> 4 | const date = Date.now(); + | ^^^^^^^^^^ Cannot call impure function + 5 | const now = performance.now(); + 6 | const rand = Math.random(); + 7 | return ; + +Error: Cannot call impure function during render + +`performance.now` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). + +error.invalid-impure-functions-in-render.ts:5:14 + 3 | function Component() { + 4 | const date = Date.now(); +> 5 | const now = performance.now(); + | ^^^^^^^^^^^^^^^^^ Cannot call impure function + 6 | const rand = Math.random(); + 7 | return ; + 8 | } + +Error: Cannot call impure function during render + +`Math.random` is an impure function. Calling an impure function can produce unstable results that update unpredictably when the component happens to re-render. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#components-and-hooks-must-be-idempotent). + +error.invalid-impure-functions-in-render.ts:6:15 + 4 | const date = Date.now(); + 5 | const now = performance.now(); +> 6 | const rand = Math.random(); + | ^^^^^^^^^^^^^ Cannot call impure function + 7 | return ; + 8 | } + 9 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.js new file mode 100644 index 0000000000000000000000000000000000000000..83cf3e04f2b6ad5e1d416d246091d81e4509f783 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-impure-functions-in-render.js @@ -0,0 +1,8 @@ +// @validateNoImpureFunctionsInRender @enableNewMutationAliasingModel + +function Component() { + const date = Date.now(); + const now = performance.now(); + const rand = Math.random(); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5cd2cf7b9798684274fca8a96fe7150ceb8ce3e6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.expect.md @@ -0,0 +1,82 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component() { + let local; + + const reassignLocal = newValue => { + local = newValue; + }; + + const onClick = newValue => { + reassignLocal('hello'); + + if (local === newValue) { + // Without React Compiler, `reassignLocal` is freshly created + // on each render, capturing a binding to the latest `local`, + // such that invoking reassignLocal will reassign the same + // binding that we are observing in the if condition, and + // we reach this branch + console.log('`local` was updated!'); + } else { + // With React Compiler enabled, `reassignLocal` is only created + // once, capturing a binding to `local` in that render pass. + // Therefore, calling `reassignLocal` will reassign the wrong + // version of `local`, and not update the binding we are checking + // in the if condition. + // + // To protect against this, we disallow reassigning locals from + // functions that escape + throw new Error('`local` not updated!'); + } + }; + + return ; +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: Cannot reassign variable after render completes + +Reassigning `local` after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-jsx-callback.ts:6:4 + 4 | + 5 | const reassignLocal = newValue => { +> 6 | local = newValue; + | ^^^^^ Cannot reassign `local` after render completes + 7 | }; + 8 | + 9 | const onClick = newValue => { + +Error: Cannot modify local variables after render completes + +This argument is a function which may reassign or mutate `local` after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead. + +error.invalid-reassign-local-variable-in-jsx-callback.ts:32:26 + 30 | }; + 31 | +> 32 | return ; + | ^^^^^^^ This function may (indirectly) reassign or modify `local` after render + 33 | } + 34 | + +error.invalid-reassign-local-variable-in-jsx-callback.ts:6:4 + 4 | + 5 | const reassignLocal = newValue => { +> 6 | local = newValue; + | ^^^^^ This modifies `local` + 7 | }; + 8 | + 9 | const onClick = newValue => { +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..2cfb336bcf5e396c29ae203c6cae4c7b0cf21a12 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-reassign-local-variable-in-jsx-callback.js @@ -0,0 +1,33 @@ +// @enableNewMutationAliasingModel +function Component() { + let local; + + const reassignLocal = newValue => { + local = newValue; + }; + + const onClick = newValue => { + reassignLocal('hello'); + + if (local === newValue) { + // Without React Compiler, `reassignLocal` is freshly created + // on each render, capturing a binding to the latest `local`, + // such that invoking reassignLocal will reassign the same + // binding that we are observing in the if condition, and + // we reach this branch + console.log('`local` was updated!'); + } else { + // With React Compiler enabled, `reassignLocal` is only created + // once, capturing a binding to `local` in that render pass. + // Therefore, calling `reassignLocal` will reassign the wrong + // version of `local`, and not update the binding we are checking + // in the if condition. + // + // To protect against this, we disallow reassigning locals from + // functions that escape + throw new Error('`local` not updated!'); + } + }; + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d50943f677840057b8b8c039da796708a2bb5504 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +//@flow @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel + +import {useCallback} from 'react'; +import {useIdentity} from 'shared-runtime'; + +function Component({content, refetch}) { + // This callback function accesses a hoisted const as a dependency, + // but it cannot reference it as a dependency since that would be a + // TDZ violation! + const onRefetch = useCallback(() => { + refetch(data); + }, [refetch]); + + // The context variable gets frozen here since it's passed to a hook + const onSubmit = useIdentity(onRefetch); + + // This has to error: onRefetch needs to memoize with `content` as a + // dependency, but the dependency comes later + const {data = null} = content; + + return ; +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: Cannot access variable before it is declared + +`data` is accessed before it is declared, which prevents the earlier access from updating when this value changes over time. + + 9 | // TDZ violation! + 10 | const onRefetch = useCallback(() => { +> 11 | refetch(data); + | ^^^^ `data` accessed before it is declared + 12 | }, [refetch]); + 13 | + 14 | // The context variable gets frozen here since it's passed to a hook + + 17 | // This has to error: onRefetch needs to memoize with `content` as a + 18 | // dependency, but the dependency comes later +> 19 | const {data = null} = content; + | ^^^^^^^^^^^ `data` is declared here + 20 | + 21 | return ; + 22 | } + +Error: Found missing memoization dependencies + +Missing dependencies can cause a value to update less often than it should, resulting in stale UI. + + 9 | // TDZ violation! + 10 | const onRefetch = useCallback(() => { +> 11 | refetch(data); + | ^^^^ Missing dependency `data` + 12 | }, [refetch]); + 13 | + 14 | // The context variable gets frozen here since it's passed to a hook +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.js new file mode 100644 index 0000000000000000000000000000000000000000..30d1e0e35e3ff4678532b2990e41b2aa2d5407c1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-referencing-frozen-hoisted-storecontext-const.js @@ -0,0 +1,22 @@ +//@flow @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel + +import {useCallback} from 'react'; +import {useIdentity} from 'shared-runtime'; + +function Component({content, refetch}) { + // This callback function accesses a hoisted const as a dependency, + // but it cannot reference it as a dependency since that would be a + // TDZ violation! + const onRefetch = useCallback(() => { + refetch(data); + }, [refetch]); + + // The context variable gets frozen here since it's passed to a hook + const onSubmit = useIdentity(onRefetch); + + // This has to error: onRefetch needs to memoize with `content` as a + // dependency, but the dependency comes later + const {data = null} = content; + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f3ebad71dfca1b592152c8076f959096773ab6ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false +import {useCallback} from 'react'; +import {makeArray} from 'shared-runtime'; + +// This case is already unsound in source, so we can safely bailout +function Foo(props) { + let x = []; + x.push(props); + + // makeArray() is captured, but depsList contains [props] + const cb = useCallback(() => [x], [x]); + + x = makeArray(); + + return cb; +} +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; + +``` + + +## Error + +``` +Found 2 errors: + +Compilation Skipped: Existing memoization could not be preserved + +React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This dependency may be mutated later, which could cause the value to change unexpectedly. + +error.invalid-useCallback-captures-reassigned-context.ts:11:37 + 9 | + 10 | // makeArray() is captured, but depsList contains [props] +> 11 | const cb = useCallback(() => [x], [x]); + | ^ This dependency may be modified later + 12 | + 13 | x = makeArray(); + 14 | + +Compilation Skipped: Existing memoization could not be preserved + +React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output. + +error.invalid-useCallback-captures-reassigned-context.ts:11:25 + 9 | + 10 | // makeArray() is captured, but depsList contains [props] +> 11 | const cb = useCallback(() => [x], [x]); + | ^^^^^^^^^ Could not preserve existing memoization + 12 | + 13 | x = makeArray(); + 14 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.js new file mode 100644 index 0000000000000000000000000000000000000000..d084df5e7a19157eefdbf14ebf0855560aa97eaa --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.invalid-useCallback-captures-reassigned-context.js @@ -0,0 +1,20 @@ +// @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false +import {useCallback} from 'react'; +import {makeArray} from 'shared-runtime'; + +// This case is already unsound in source, so we can safely bailout +function Foo(props) { + let x = []; + x.push(props); + + // makeArray() is captured, but depsList contains [props] + const cb = useCallback(() => [x], [x]); + + x = makeArray(); + + return cb; +} +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f73f23b262d94e9589b87ee1dcc4c1a85899c4fb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + useFreeze(x); + x.y = true; + return
error
; +} + +``` + + +## Error + +``` +Found 1 error: + +Error: This value cannot be modified + +Modifying a value previously passed as an argument to a hook is not allowed. Consider moving the modification before calling the hook. + +error.mutate-frozen-value.ts:5:2 + 3 | const x = {a}; + 4 | useFreeze(x); +> 5 | x.y = true; + | ^ value cannot be modified + 6 | return
error
; + 7 | } + 8 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.js new file mode 100644 index 0000000000000000000000000000000000000000..4964f2304912a6e40acc8d0d5c5ea64a17ee1ff7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-frozen-value.js @@ -0,0 +1,7 @@ +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {a}; + useFreeze(x); + x.y = true; + return
error
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3de6acb91ba47e6fe8bd37d73564168efdd3cf39 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function useHook(a, b) { + b.test = 1; + a.test = 2; +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.mutate-hook-argument.ts:3:2 + 1 | // @enableNewMutationAliasingModel + 2 | function useHook(a, b) { +> 3 | b.test = 1; + | ^ value cannot be modified + 4 | a.test = 2; + 5 | } + 6 | + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.mutate-hook-argument.ts:4:2 + 2 | function useHook(a, b) { + 3 | b.test = 1; +> 4 | a.test = 2; + | ^ value cannot be modified + 5 | } + 6 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.js new file mode 100644 index 0000000000000000000000000000000000000000..41c5b99132460fadc5157433a40123e3dbbaa94a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.mutate-hook-argument.js @@ -0,0 +1,5 @@ +// @enableNewMutationAliasingModel +function useHook(a, b) { + b.test = 1; + a.test = 2; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..80a12e5d409faf62469cd1667e3b70a065c5e200 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +let x = {a: 42}; + +function Component(props) { + foo(() => { + x.a = 10; + x.a = 20; + }); +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: This value cannot be modified + +Modifying a variable defined outside a component or hook is not allowed. Consider using an effect. + +error.not-useEffect-external-mutate.ts:6:4 + 4 | function Component(props) { + 5 | foo(() => { +> 6 | x.a = 10; + | ^ value cannot be modified + 7 | x.a = 20; + 8 | }); + 9 | } + +Error: This value cannot be modified + +Modifying a variable defined outside a component or hook is not allowed. Consider using an effect. + +error.not-useEffect-external-mutate.ts:7:4 + 5 | foo(() => { + 6 | x.a = 10; +> 7 | x.a = 20; + | ^ value cannot be modified + 8 | }); + 9 | } + 10 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..ed51080726b5a80470fd802400715121f64bb487 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.not-useEffect-external-mutate.js @@ -0,0 +1,9 @@ +// @enableNewMutationAliasingModel +let x = {a: 42}; + +function Component(props) { + foo(() => { + x.a = 10; + x.a = 20; + }); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9d106cd4f4ef662234fb54d1fb1ff80ff6096692 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component() { + const foo = () => { + // Cannot assign to globals + someUnknownGlobal = true; + moduleLocal = true; + }; + foo(); +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `someUnknownGlobal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.reassignment-to-global-indirect.ts:5:4 + 3 | const foo = () => { + 4 | // Cannot assign to globals +> 5 | someUnknownGlobal = true; + | ^^^^^^^^^^^^^^^^^ `someUnknownGlobal` cannot be reassigned + 6 | moduleLocal = true; + 7 | }; + 8 | foo(); + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `moduleLocal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.reassignment-to-global-indirect.ts:6:4 + 4 | // Cannot assign to globals + 5 | someUnknownGlobal = true; +> 6 | moduleLocal = true; + | ^^^^^^^^^^^ `moduleLocal` cannot be reassigned + 7 | }; + 8 | foo(); + 9 | } +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.js new file mode 100644 index 0000000000000000000000000000000000000000..6d6681e60ad340f97e39d97f753c1251e7251bf4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global-indirect.js @@ -0,0 +1,9 @@ +// @enableNewMutationAliasingModel +function Component() { + const foo = () => { + // Cannot assign to globals + someUnknownGlobal = true; + moduleLocal = true; + }; + foo(); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..01ff60852138267eb1e4544d86f633fc9ad8638d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component() { + // Cannot assign to globals + someUnknownGlobal = true; + moduleLocal = true; +} + +``` + + +## Error + +``` +Found 2 errors: + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `someUnknownGlobal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.reassignment-to-global.ts:4:2 + 2 | function Component() { + 3 | // Cannot assign to globals +> 4 | someUnknownGlobal = true; + | ^^^^^^^^^^^^^^^^^ `someUnknownGlobal` cannot be reassigned + 5 | moduleLocal = true; + 6 | } + 7 | + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `moduleLocal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.reassignment-to-global.ts:5:2 + 3 | // Cannot assign to globals + 4 | someUnknownGlobal = true; +> 5 | moduleLocal = true; + | ^^^^^^^^^^^ `moduleLocal` cannot be reassigned + 6 | } + 7 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.js new file mode 100644 index 0000000000000000000000000000000000000000..41b706866bf7c40b3b26a5e2402d536bed76ddf0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.reassignment-to-global.js @@ -0,0 +1,6 @@ +// @enableNewMutationAliasingModel +function Component() { + // Cannot assign to globals + someUnknownGlobal = true; + moduleLocal = true; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..44f358caade01c546cf6d022a024493efb58d54d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.expect.md @@ -0,0 +1,38 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component(props) { + function hasErrors() { + let hasErrors = false; + if (props.items == null) { + hasErrors = true; + } + return hasErrors; + } + return hasErrors(); +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized + + hasErrors_0$15:TFunction. + +error.todo-repro-named-function-with-shadowed-local-same-name.ts:10:9 + 8 | return hasErrors; + 9 | } +> 10 | return hasErrors(); + | ^^^^^^^^^ this is uninitialized + 11 | } + 12 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.js new file mode 100644 index 0000000000000000000000000000000000000000..b58c0aea7daf7f2ea99b32c6934bf6311c9a74ed --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/error.todo-repro-named-function-with-shadowed-local-same-name.js @@ -0,0 +1,11 @@ +// @enableNewMutationAliasingModel +function Component(props) { + function hasErrors() { + let hasErrors = false; + if (props.items == null) { + hasErrors = true; + } + return hasErrors; + } + return hasErrors(); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..22f967883b09a5520da2389ec53a7d4f455fd285 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +function Component(props) { + const items = (() => { + if (props.cond) { + return []; + } else { + return null; + } + })(); + items?.push(props.a); + return items; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: {}}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(3); + let items; + if ($[0] !== props.a || $[1] !== props.cond) { + let t0; + if (props.cond) { + t0 = []; + } else { + t0 = null; + } + items = t0; + + items?.push(props.a); + $[0] = props.a; + $[1] = props.cond; + $[2] = items; + } else { + items = $[2]; + } + return items; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: {} }], +}; + +``` + +### Eval output +(kind: ok) null \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.js new file mode 100644 index 0000000000000000000000000000000000000000..f4f953d294e6f9ded20f86a437c7122edc4cafcf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/iife-return-modified-later-phi.js @@ -0,0 +1,16 @@ +function Component(props) { + const items = (() => { + if (props.cond) { + return []; + } else { + return null; + } + })(); + items?.push(props.a); + return items; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: {}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b42bfb001eecab516b5c334cf4833f28e3a43b68 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const f = () => { + const y = [x]; + return y[0]; + }; + const x0 = f(); + const z = [x0]; + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const x = { a, b }; + const f = () => { + const y = [x]; + return y[0]; + }; + const x0 = f(); + const z = [x0]; + const x1 = z[0]; + x1.key = "value"; + t1 = ; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 1 }], +}; + +``` + +### Eval output +(kind: ok)
{"x":{"a":0,"b":1,"key":"value"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.js new file mode 100644 index 0000000000000000000000000000000000000000..6a981e840891c217d172f01f2f875bcbe34c9859 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections-2.js @@ -0,0 +1,20 @@ +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const f = () => { + const y = [x]; + return y[0]; + }; + const x0 = f(); + const z = [x0]; + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4dcb9110ab05f14d7b4939639333ee3fde5ab59d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const y = [x]; + const f = () => { + const x0 = y[0]; + return [x0]; + }; + const z = f(); + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const x = { a, b }; + const y = [x]; + const f = () => { + const x0 = y[0]; + return [x0]; + }; + const z = f(); + const x1 = z[0]; + x1.key = "value"; + t1 = ; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 1 }], +}; + +``` + +### Eval output +(kind: ok)
{"x":{"a":0,"b":1,"key":"value"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.js new file mode 100644 index 0000000000000000000000000000000000000000..aecd27a0930943a2b5fabcc969c4550a69246c9f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-function-call-indirections.js @@ -0,0 +1,20 @@ +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const y = [x]; + const f = () => { + const x0 = y[0]; + return [x0]; + }; + const z = f(); + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5f14dd1fe0770603ea90b92862a33a5f9c528716 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const y = [x]; + const x0 = y[0]; + const z = [x0]; + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const x = { a, b }; + const y = [x]; + const x0 = y[0]; + const z = [x0]; + const x1 = z[0]; + x1.key = "value"; + t1 = ; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 1 }], +}; + +``` + +### Eval output +(kind: ok)
{"x":{"a":0,"b":1,"key":"value"}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.js new file mode 100644 index 0000000000000000000000000000000000000000..ba8808eedfe8c90a5150e25343f2803b1b736c9a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-boxing-unboxing-indirections.js @@ -0,0 +1,17 @@ +// @enableNewMutationAliasingModel +import {Stringify} from 'shared-runtime'; + +function Component({a, b}) { + const x = {a, b}; + const y = [x]; + const x0 = y[0]; + const z = [x0]; + const x1 = z[0]; + x1.key = 'value'; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 1}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..e7b0fe8e75ee39e7cdd9470e4841795aa0a78949 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.expect.md @@ -0,0 +1,94 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import {identity, ValidateMemoization} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => ({a}), [a, b]); + const f = () => { + return identity(x); + }; + const x2 = f(); + x2.b = b; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { identity, ValidateMemoization } from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = { a }; + const f = () => identity(x); + + const x2 = f(); + x2.b = b; + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":{"a":0,"b":0}}
+
{"inputs":[0,1],"output":{"a":0,"b":1}}
+
{"inputs":[1,1],"output":{"a":1,"b":1}}
+
{"inputs":[0,0],"output":{"a":0,"b":0}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..2ac414570d8d26305bf0ddf32ff6395e31238d16 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity-function-expression.js @@ -0,0 +1,25 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import {identity, ValidateMemoization} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => ({a}), [a, b]); + const f = () => { + return identity(x); + }; + const x2 = f(); + x2.b = b; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a392cb1137f97f2d51d5d45b0d4fb1feb30a5f7b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.expect.md @@ -0,0 +1,89 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import {identity, ValidateMemoization} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => ({a}), [a, b]); + const x2 = identity(x); + x2.b = b; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { identity, ValidateMemoization } from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = { a }; + const x2 = identity(x); + x2.b = b; + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":{"a":0,"b":0}}
+
{"inputs":[0,1],"output":{"a":0,"b":1}}
+
{"inputs":[1,1],"output":{"a":1,"b":1}}
+
{"inputs":[0,0],"output":{"a":0,"b":0}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.js new file mode 100644 index 0000000000000000000000000000000000000000..33ba3a106ff1fceb2d5a76fb36337c704af8e090 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-identity.js @@ -0,0 +1,22 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import {identity, ValidateMemoization} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => ({a}), [a, b]); + const x2 = identity(x); + x2.b = b; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..34345951ed7fa6b50f14ea10d3377a6a230f8e99 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {}; + const y = {x}; + const z = y.x; + z.true = false; + return
{z}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(1); + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const x = {}; + const y = { x }; + const z = y.x; + z.true = false; + t1 =
{z}
; + $[0] = t1; + } else { + t1 = $[0]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.js new file mode 100644 index 0000000000000000000000000000000000000000..bff1ea4c3504655b74563fc3079a4460438aeeb5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/mutate-through-propertyload.js @@ -0,0 +1,8 @@ +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = {}; + const y = {x}; + const z = y.x; + z.true = false; + return
{z}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5033da8eac440bf57022db88407fd3f0677d92de --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.expect.md @@ -0,0 +1,75 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {useState} from 'react'; +import {useIdentity} from 'shared-runtime'; + +function useMakeCallback({obj}: {obj: {value: number}}) { + const [state, setState] = useState(0); + const cb = () => { + if (obj.value !== state) setState(obj.value); + }; + useIdentity(); + cb(); + return [cb]; +} +export const FIXTURE_ENTRYPOINT = { + fn: useMakeCallback, + params: [{obj: {value: 1}}], + sequentialRenders: [{obj: {value: 1}}, {obj: {value: 2}}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { useState } from "react"; +import { useIdentity } from "shared-runtime"; + +function useMakeCallback(t0) { + const $ = _c(5); + const { obj } = t0; + const [state, setState] = useState(0); + let t1; + if ($[0] !== obj.value || $[1] !== state) { + t1 = () => { + if (obj.value !== state) { + setState(obj.value); + } + }; + $[0] = obj.value; + $[1] = state; + $[2] = t1; + } else { + t1 = $[2]; + } + const cb = t1; + + useIdentity(); + cb(); + let t2; + if ($[3] !== cb) { + t2 = [cb]; + $[3] = cb; + $[4] = t2; + } else { + t2 = $[4]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useMakeCallback, + params: [{ obj: { value: 1 } }], + sequentialRenders: [{ obj: { value: 1 } }, { obj: { value: 2 } }], +}; + +``` + +### Eval output +(kind: ok) ["[[ function params=0 ]]"] +["[[ function params=0 ]]"] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.js new file mode 100644 index 0000000000000000000000000000000000000000..1f2d69d93193fc495764e5a660fdd0a97de02269 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/nullable-objects-assume-invoked-direct-call.js @@ -0,0 +1,18 @@ +// @enableNewMutationAliasingModel +import {useState} from 'react'; +import {useIdentity} from 'shared-runtime'; + +function useMakeCallback({obj}: {obj: {value: number}}) { + const [state, setState] = useState(0); + const cb = () => { + if (obj.value !== state) setState(obj.value); + }; + useIdentity(); + cb(); + return [cb]; +} +export const FIXTURE_ENTRYPOINT = { + fn: useMakeCallback, + params: [{obj: {value: 1}}], + sequentialRenders: [{obj: {value: 1}}, {obj: {value: 2}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f9f881c72af5419ba40de7898259af9366c38392 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {identity, mutate} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [key]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { identity, mutate } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let context; + if ($[0] !== props.value) { + const key = {}; + context = { [key]: identity([props.value]) }; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"[object Object]":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.js new file mode 100644 index 0000000000000000000000000000000000000000..923733b9c238d88473c41a4b1b94f800296d2f5d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-key-object-mutated-later.js @@ -0,0 +1,16 @@ +// @enableNewMutationAliasingModel +import {identity, mutate} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [key]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..713a3112fe25a89072a87be47a910a4907aac2ce --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {a: 'key'}; + const context = { + [key.a]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let context; + if ($[0] !== props.value) { + const key = { a: "key" }; + const t0 = key.a; + const t1 = identity([props.value]); + let t2; + if ($[2] !== t1) { + t2 = { [t0]: t1 }; + $[2] = t1; + $[3] = t2; + } else { + t2 = $[3]; + } + context = t2; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"key":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.js new file mode 100644 index 0000000000000000000000000000000000000000..516fdc1dbcf4118f78e73038dab1d78d2307fa7f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/object-expression-computed-member.js @@ -0,0 +1,16 @@ +// @enableNewMutationAliasingModel +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {a: 'key'}; + const context = { + [key.a]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a5cfc790ebc06c7373f2c3ef28ec0780d7c690f1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b, c}) { + const x = [a, b]; + const f = () => { + maybeMutate(x); + // different dependency to force this not to merge with x's scope + console.log(c); + }; + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(9); + const { a, b, c } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + t1 = [a, b]; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + const x = t1; + let t2; + if ($[3] !== c || $[4] !== x) { + t2 = () => { + maybeMutate(x); + + console.log(c); + }; + $[3] = c; + $[4] = x; + $[5] = t2; + } else { + t2 = $[5]; + } + const f = t2; + let t3; + if ($[6] !== f || $[7] !== x) { + t3 = ; + $[6] = f; + $[7] = x; + $[8] = t3; + } else { + t3 = $[8]; + } + return t3; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..096f4f17ea54542ae6c8eeaaed288f2887593ba8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/potential-mutation-in-function-expression.js @@ -0,0 +1,10 @@ +// @enableNewMutationAliasingModel +function Component({a, b, c}) { + const x = [a, b]; + const f = () => { + maybeMutate(x); + // different dependency to force this not to merge with x's scope + console.log(c); + }; + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..26757db1a3c28c0ebf132057c1592d1abf8625ed --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function ReactiveRefInEffect(props) { + const ref1 = useRef('initial value'); + const ref2 = useRef('initial value'); + let ref; + if (props.foo) { + ref = ref1; + } else { + ref = ref2; + } + useEffect(() => print(ref)); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function ReactiveRefInEffect(props) { + const $ = _c(4); + const ref1 = useRef("initial value"); + const ref2 = useRef("initial value"); + let ref; + if ($[0] !== props.foo) { + if (props.foo) { + ref = ref1; + } else { + ref = ref2; + } + $[0] = props.foo; + $[1] = ref; + } else { + ref = $[1]; + } + let t0; + if ($[2] !== ref) { + t0 = () => print(ref); + $[2] = ref; + $[3] = t0; + } else { + t0 = $[3]; + } + useEffect(t0); +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.js new file mode 100644 index 0000000000000000000000000000000000000000..3ae653c96203412d84ddd39b673b4388a161ad44 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/reactive-ref.js @@ -0,0 +1,12 @@ +// @enableNewMutationAliasingModel +function ReactiveRefInEffect(props) { + const ref1 = useRef('initial value'); + const ref2 = useRef('initial value'); + let ref; + if (props.foo) { + ref = ref1; + } else { + ref = ref2; + } + useEffect(() => print(ref)); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a402713f869392c0af5d51a11343b629f7845a8b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +// @flow @enableNewMutationAliasingModel + +import fbt from 'fbt'; + +component Component() { + const sections = Object.keys(items); + + for (let i = 0; i < sections.length; i += 3) { + chunks.push( + sections.slice(i, i + 3).map(section => { + return ; + }) + ); + } + + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; + +import fbt from "fbt"; + +function Component() { + const $ = _c(1); + const sections = Object.keys(items); + + for (let i = 0; i < sections.length; i = i + 3, i) { + chunks.push(sections.slice(i, i + 3).map(_temp)); + } + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp(section) { + return ; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.js new file mode 100644 index 0000000000000000000000000000000000000000..d03a44618ea01326aa1b1079f5bddac903648f15 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-compiler-infinite-loop.js @@ -0,0 +1,17 @@ +// @flow @enableNewMutationAliasingModel + +import fbt from 'fbt'; + +component Component() { + const sections = Object.keys(items); + + for (let i = 0; i < sections.length; i += 3) { + chunks.push( + sections.slice(i, i + 3).map(section => { + return ; + }) + ); + } + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..25e4a3843cf39fb22c0118b0df771dcd79cc8863 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +// @validateExhaustiveMemoizationDependencies:false +export function useFormatRelativeTime(opts = {}) { + const {timeZone, minimal} = opts; + const format = useCallback(function formatWithUnit() {}, [minimal]); + // We previously recorded `{timeZone}` as capturing timeZone into the object, + // then assumed that dateTimeFormat() mutates that object, + // which in turn could mutate timeZone and the object it came from, + // which meanteans that the value `minimal` is derived from can change. + // + // The fix was to record a Capture from a maybefrozen value as an ImmutableCapture + // which doesn't propagate mutations + dateTimeFormat({timeZone}); + return format; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validateExhaustiveMemoizationDependencies:false +export function useFormatRelativeTime(t0) { + const $ = _c(1); + const opts = t0 === undefined ? {} : t0; + const { timeZone, minimal } = opts; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = function formatWithUnit() {}; + $[0] = t1; + } else { + t1 = $[0]; + } + const format = t1; + + dateTimeFormat({ timeZone }); + return format; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.js new file mode 100644 index 0000000000000000000000000000000000000000..bd2548f672b15134c7ccbdf3b17d0986740f3e13 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-destructure-from-prop-with-default-value.js @@ -0,0 +1,14 @@ +// @validateExhaustiveMemoizationDependencies:false +export function useFormatRelativeTime(opts = {}) { + const {timeZone, minimal} = opts; + const format = useCallback(function formatWithUnit() {}, [minimal]); + // We previously recorded `{timeZone}` as capturing timeZone into the object, + // then assumed that dateTimeFormat() mutates that object, + // which in turn could mutate timeZone and the object it came from, + // which meanteans that the value `minimal` is derived from can change. + // + // The fix was to record a Capture from a maybefrozen value as an ImmutableCapture + // which doesn't propagate mutations + dateTimeFormat({timeZone}); + return format; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fe47e6b0a026f5bd910951412b67c57f0c32ff59 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +function Component() { + const x = {}; + const fn = () => { + new Object() + .build(x) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}); + }; + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component() { + const $ = _c(2); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = {}; + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + const fn = () => { + new Object() + .build(x) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}); + }; + t1 = ; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.js new file mode 100644 index 0000000000000000000000000000000000000000..6e67ed7bab695a0c010b1c92f0308228fe6e4229 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-function-expression-effects-stack-overflow.js @@ -0,0 +1,14 @@ +function Component() { + const x = {}; + const fn = () => { + new Object() + .build(x) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}) + .build({}); + }; + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9a0c82a3ccca004ae5d0db89bb87cb0a5c185a80 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.expect.md @@ -0,0 +1,80 @@ + +## Input + +```javascript +//@flow @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel +component Component( + onAsyncSubmit?: (() => void) => void, + onClose: (isConfirmed: boolean) => void +) { + // When running inferReactiveScopeVariables, + // onAsyncSubmit and onClose update to share + // a mutableRange instance. + const onSubmit = useCallback(() => { + if (onAsyncSubmit) { + onAsyncSubmit(() => { + onClose(true); + }); + return; + } + }, [onAsyncSubmit, onClose]); + // When running inferReactiveScopeVariables here, + // first the existing range gets updated (affecting + // onAsyncSubmit) and then onClose gets assigned a + // different mutable range instance, which is the + // one reset after AnalyzeFunctions. + // The fix is to fully reset mutable ranges *instances* + // after AnalyzeFunctions visit a function expression + return onClose(false)} />; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(8); + const { onAsyncSubmit, onClose } = t0; + let t1; + if ($[0] !== onAsyncSubmit || $[1] !== onClose) { + t1 = () => { + if (onAsyncSubmit) { + onAsyncSubmit(() => { + onClose(true); + }); + return; + } + }; + $[0] = onAsyncSubmit; + $[1] = onClose; + $[2] = t1; + } else { + t1 = $[2]; + } + const onSubmit = t1; + let t2; + if ($[3] !== onClose) { + t2 = () => onClose(false); + $[3] = onClose; + $[4] = t2; + } else { + t2 = $[4]; + } + let t3; + if ($[5] !== onSubmit || $[6] !== t2) { + t3 = ; + $[5] = onSubmit; + $[6] = t2; + $[7] = t3; + } else { + t3 = $[7]; + } + return t3; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.js new file mode 100644 index 0000000000000000000000000000000000000000..20cad06e97e2e73cd6d55e60a2a83af851c22470 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-internal-compiler-shared-mutablerange-bug.js @@ -0,0 +1,25 @@ +//@flow @validatePreserveExistingMemoizationGuarantees @enableNewMutationAliasingModel +component Component( + onAsyncSubmit?: (() => void) => void, + onClose: (isConfirmed: boolean) => void +) { + // When running inferReactiveScopeVariables, + // onAsyncSubmit and onClose update to share + // a mutableRange instance. + const onSubmit = useCallback(() => { + if (onAsyncSubmit) { + onAsyncSubmit(() => { + onClose(true); + }); + return; + } + }, [onAsyncSubmit, onClose]); + // When running inferReactiveScopeVariables here, + // first the existing range gets updated (affecting + // onAsyncSubmit) and then onClose gets assigned a + // different mutable range instance, which is the + // one reset after AnalyzeFunctions. + // The fix is to fully reset mutable ranges *instances* + // after AnalyzeFunctions visit a function expression + return onClose(false)} />; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6043495bc19130c30dbdd5676b44630bb067ba4d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +function Component({a, b}) { + const y = {a}; + const x = {b}; + const f = () => { + let z = null; + while (z == null) { + z = x; + } + // z is a phi with a backedge, and we don't realize it could be x, + // and therefore fail to record a Capture x <- y effect for this + // function expression + z.y = y; + }; + f(); + mutate(x); + return
{x}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const y = { a }; + const x = { b }; + const f = () => { + let z = null; + while (z == null) { + z = x; + } + z.y = y; + }; + f(); + mutate(x); + t1 =
{x}
; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.js new file mode 100644 index 0000000000000000000000000000000000000000..31a51b45aa384f822c700b75a9745e371012bf9e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-invalid-function-expression-effects-phi.js @@ -0,0 +1,17 @@ +function Component({a, b}) { + const y = {a}; + const x = {b}; + const f = () => { + let z = null; + while (z == null) { + z = x; + } + // z is a phi with a backedge, and we don't realize it could be x, + // and therefore fail to record a Capture x <- y effect for this + // function expression + z.y = y; + }; + f(); + mutate(x); + return
{x}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ef8a2dc1757ee910cd1a498e6c97ba9e3105b4df --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +// @flow @enableNewMutationAliasingModel + +import {identity, Stringify, useFragment} from 'shared-runtime'; + +component Example() { + const data = useFragment(); + + const {a, b} = identity(data); + + const el = ; + + identity(a.at(0)); + + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; + +import { identity, Stringify, useFragment } from "shared-runtime"; + +function Example() { + const $ = _c(2); + const data = useFragment(); + let t0; + if ($[0] !== data) { + const { a, b } = identity(data); + const el = ; + identity(a.at(0)); + t0 = ; + $[0] = data; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.js new file mode 100644 index 0000000000000000000000000000000000000000..7ab6dbc30ab2c77f34190b50f817d6f495284002 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-jsx-captures-value-mutated-later.js @@ -0,0 +1,15 @@ +// @flow @enableNewMutationAliasingModel + +import {identity, Stringify, useFragment} from 'shared-runtime'; + +component Example() { + const data = useFragment(); + + const {a, b} = identity(data); + + const el = ; + + identity(a.at(0)); + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..28fc8b601f7ffa6ab48f0cdedb48bd2f6e12af1b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.expect.md @@ -0,0 +1,74 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel:true + +export const App = () => { + const [selected, setSelected] = useState(new Set()); + const onSelectedChange = (value: string) => { + const newSelected = new Set(selected); + if (newSelected.has(value)) { + // This should not count as a mutation of `selected` + newSelected.delete(value); + } else { + // This should not count as a mutation of `selected` + newSelected.add(value); + } + setSelected(newSelected); + }; + + return ; +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel:true + +export const App = () => { + const $ = _c(6); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = new Set(); + $[0] = t0; + } else { + t0 = $[0]; + } + const [selected, setSelected] = useState(t0); + let t1; + if ($[1] !== selected) { + t1 = (value) => { + const newSelected = new Set(selected); + if (newSelected.has(value)) { + newSelected.delete(value); + } else { + newSelected.add(value); + } + + setSelected(newSelected); + }; + $[1] = selected; + $[2] = t1; + } else { + t1 = $[2]; + } + const onSelectedChange = t1; + let t2; + if ($[3] !== onSelectedChange || $[4] !== selected) { + t2 = ; + $[3] = onSelectedChange; + $[4] = selected; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +}; + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..c5a404a66c371958bd57e9857db7acf426810a45 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/repro-mutate-new-set-of-frozen-items-in-callback.js @@ -0,0 +1,18 @@ +// @enableNewMutationAliasingModel:true + +export const App = () => { + const [selected, setSelected] = useState(new Set()); + const onSelectedChange = (value: string) => { + const newSelected = new Set(selected); + if (newSelected.has(value)) { + // This should not count as a mutation of `selected` + newSelected.delete(value); + } else { + // This should not count as a mutation of `selected` + newSelected.add(value); + } + setSelected(newSelected); + }; + + return ; +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..955c4e0705797bcabb9adac18d8a6a123da37309 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function useHook({el1, el2}) { + const s = new Set(); + const arr = makeArray(el1); + s.add(arr); + // Mutate after store + arr.push(el2); + + s.add(makeArray(el2)); + return s.size; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function useHook(t0) { + const $ = _c(5); + const { el1, el2 } = t0; + let s; + if ($[0] !== el1 || $[1] !== el2) { + s = new Set(); + const arr = makeArray(el1); + s.add(arr); + + arr.push(el2); + let t1; + if ($[3] !== el2) { + t1 = makeArray(el2); + $[3] = el2; + $[4] = t1; + } else { + t1 = $[4]; + } + s.add(t1); + $[0] = el1; + $[1] = el2; + $[2] = s; + } else { + s = $[2]; + } + return s.size; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..3afbd93f84b17e5dc63153b4f25535c73f3e82b4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/set-add-mutate.js @@ -0,0 +1,11 @@ +// @enableNewMutationAliasingModel +function useHook({el1, el2}) { + const s = new Set(); + const arr = makeArray(el1); + s.add(arr); + // Mutate after store + arr.push(el2); + + s.add(makeArray(el2)); + return s.size; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4c04ae197292ff5631a0e4a23acbfc9bfef68e45 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.expect.md @@ -0,0 +1,70 @@ + +## Input + +```javascript +// @enablePropagateDepsInHIR @enableNewMutationAliasingModel +function useFoo(props) { + let x = []; + x.push(props.bar); + // todo: the below should memoize separately from the above + // my guess is that the phi causes the different `x` identifiers + // to get added to an alias group. this is where we need to track + // the actual state of the alias groups at the time of the mutation + props.cond ? (({x} = {x: {}}), ([x] = [[]]), x.push(props.foo)) : null; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{cond: false, foo: 2, bar: 55}], + sequentialRenders: [ + {cond: false, foo: 2, bar: 55}, + {cond: false, foo: 3, bar: 55}, + {cond: true, foo: 3, bar: 55}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR @enableNewMutationAliasingModel +function useFoo(props) { + const $ = _c(5); + let x; + if ($[0] !== props.bar) { + x = []; + x.push(props.bar); + $[0] = props.bar; + $[1] = x; + } else { + x = $[1]; + } + if ($[2] !== props.cond || $[3] !== props.foo) { + props.cond ? (([x] = [[]]), x.push(props.foo)) : null; + $[2] = props.cond; + $[3] = props.foo; + $[4] = x; + } else { + x = $[4]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ cond: false, foo: 2, bar: 55 }], + sequentialRenders: [ + { cond: false, foo: 2, bar: 55 }, + { cond: false, foo: 3, bar: 55 }, + { cond: true, foo: 3, bar: 55 }, + ], +}; + +``` + +### Eval output +(kind: ok) [55] +[55] +[3] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.js new file mode 100644 index 0000000000000000000000000000000000000000..923d0b59bb810f69e12de15b30c9f2d7afb773dd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/ssa-renaming-ternary-destruction.js @@ -0,0 +1,21 @@ +// @enablePropagateDepsInHIR @enableNewMutationAliasingModel +function useFoo(props) { + let x = []; + x.push(props.bar); + // todo: the below should memoize separately from the above + // my guess is that the phi causes the different `x` identifiers + // to get added to an alias group. this is where we need to track + // the actual state of the alias groups at the time of the mutation + props.cond ? (({x} = {x: {}}), ([x] = [[]]), x.push(props.foo)) : null; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{cond: false, foo: 2, bar: 55}], + sequentialRenders: [ + {cond: false, foo: 2, bar: 55}, + {cond: false, foo: 3, bar: 55}, + {cond: true, foo: 3, bar: 55}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..92e1a1c3b8ecf9b6eb8b94aea45076a92ac6c0d4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.expect.md @@ -0,0 +1,158 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + mutate, + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b, c}: {a: number; b: number; c: number}) { + const x = useMemo(() => [{value: a}], [a, b, c]); + if (b === 0) { + // This object should only depend on c, it cannot be affected by the later mutation + x.push({value: c}); + } else { + // This mutation shouldn't affect the object in the consequent + mutate(x); + } + + return ( + <> + ; + {/* TODO: should only depend on c */} + ; + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0, c: 0}], + sequentialRenders: [ + {a: 0, b: 0, c: 0}, + {a: 0, b: 1, c: 0}, + {a: 1, b: 1, c: 0}, + {a: 1, b: 1, c: 1}, + {a: 1, b: 1, c: 0}, + {a: 1, b: 0, c: 0}, + {a: 0, b: 0, c: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { + mutate, + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(21); + const { a, b, c } = t0; + let x; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + x = [{ value: a }]; + if (b === 0) { + x.push({ value: c }); + } else { + mutate(x); + } + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + } else { + x = $[3]; + } + let t1; + if ($[4] !== a || $[5] !== b || $[6] !== c) { + t1 = [a, b, c]; + $[4] = a; + $[5] = b; + $[6] = c; + $[7] = t1; + } else { + t1 = $[7]; + } + let t2; + if ($[8] !== t1 || $[9] !== x) { + t2 = ; + $[8] = t1; + $[9] = x; + $[10] = t2; + } else { + t2 = $[10]; + } + let t3; + if ($[11] !== a || $[12] !== b || $[13] !== c) { + t3 = [a, b, c]; + $[11] = a; + $[12] = b; + $[13] = c; + $[14] = t3; + } else { + t3 = $[14]; + } + let t4; + if ($[15] !== t3 || $[16] !== x[0]) { + t4 = ; + $[15] = t3; + $[16] = x[0]; + $[17] = t4; + } else { + t4 = $[17]; + } + let t5; + if ($[18] !== t2 || $[19] !== t4) { + t5 = ( + <> + {t2};{t4}; + + ); + $[18] = t2; + $[19] = t4; + $[20] = t5; + } else { + t5 = $[20]; + } + return t5; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0, c: 0 }], + sequentialRenders: [ + { a: 0, b: 0, c: 0 }, + { a: 0, b: 1, c: 0 }, + { a: 1, b: 1, c: 0 }, + { a: 1, b: 1, c: 1 }, + { a: 1, b: 1, c: 0 }, + { a: 1, b: 0, c: 0 }, + { a: 0, b: 0, c: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0,0],"output":[{"value":0},{"value":0}]}
;
{"inputs":[0,0,0],"output":{"value":0}}
; +
{"inputs":[0,1,0],"output":[{"value":0},"joe"]}
;
{"inputs":[0,1,0],"output":{"value":0}}
; +
{"inputs":[1,1,0],"output":[{"value":1},"joe"]}
;
{"inputs":[1,1,0],"output":{"value":1}}
; +
{"inputs":[1,1,1],"output":[{"value":1},"joe"]}
;
{"inputs":[1,1,1],"output":{"value":1}}
; +
{"inputs":[1,1,0],"output":[{"value":1},"joe"]}
;
{"inputs":[1,1,0],"output":{"value":1}}
; +
{"inputs":[1,0,0],"output":[{"value":1},{"value":0}]}
;
{"inputs":[1,0,0],"output":{"value":1}}
; +
{"inputs":[0,0,0],"output":[{"value":0},{"value":0}]}
;
{"inputs":[0,0,0],"output":{"value":0}}
; \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.tsx new file mode 100644 index 0000000000000000000000000000000000000000..c40d192468eba17d318d38faa12f86c4e4367f95 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-control-flow-sensitive-mutation.tsx @@ -0,0 +1,42 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + mutate, + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b, c}: {a: number; b: number; c: number}) { + const x = useMemo(() => [{value: a}], [a, b, c]); + if (b === 0) { + // This object should only depend on c, it cannot be affected by the later mutation + x.push({value: c}); + } else { + // This mutation shouldn't affect the object in the consequent + mutate(x); + } + + return ( + <> + ; + {/* TODO: should only depend on c */} + ; + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0, c: 0}], + sequentialRenders: [ + {a: 0, b: 0, c: 0}, + {a: 0, b: 1, c: 0}, + {a: 1, b: 1, c: 0}, + {a: 1, b: 1, c: 1}, + {a: 1, b: 1, c: 0}, + {a: 1, b: 0, c: 0}, + {a: 0, b: 0, c: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..25f37285237508ad689fe873ff58cc94ec88b036 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.expect.md @@ -0,0 +1,113 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a]); + const f = () => { + const y = typedCreateFrom(x); + const z = typedCapture(y); + return z; + }; + const z = f(); + // does not mutate x, so x should not depend on b + typedMutate(z, b); + + // TODO: this *should* only depend on `a` + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = [{ a }]; + const f = () => { + const y = typedCreateFrom(x); + const z = typedCapture(y); + return z; + }; + + const z_0 = f(); + + typedMutate(z_0, b); + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":[{"a":0}]}
+
{"inputs":[0,1],"output":[{"a":0}]}
+
{"inputs":[1,1],"output":[{"a":1}]}
+
{"inputs":[0,0],"output":[{"a":0}]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.tsx new file mode 100644 index 0000000000000000000000000000000000000000..c6bd016280d23a5aae5bd160b8ad8f49b890fe65 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/todo-transitivity-createfrom-capture-lambda.tsx @@ -0,0 +1,34 @@ +// @enablePreserveExistingMemoizationGuarantees:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a]); + const f = () => { + const y = typedCreateFrom(x); + const z = typedCapture(y); + return z; + }; + const z = f(); + // does not mutate x, so x should not depend on b + typedMutate(z, b); + + // TODO: this *should* only depend on `a` + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..09c4e3eaf3332728f00c60cf53680e3e7c573178 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = [a]; + const y = {b}; + mutate(y); + y.x = x; + return
{y}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel +function Component(t0) { + const $ = _c(5); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = [a]; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + let t2; + if ($[2] !== b || $[3] !== x) { + const y = { b }; + mutate(y); + y.x = x; + t2 =
{y}
; + $[2] = b; + $[3] = x; + $[4] = t2; + } else { + t2 = $[4]; + } + return t2; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.js new file mode 100644 index 0000000000000000000000000000000000000000..e6e2e17bc0b967d0c2a8bde96a9821aa38b97b1f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitive-mutation-before-capturing-value-created-earlier.js @@ -0,0 +1,8 @@ +// @enableNewMutationAliasingModel +function Component({a, b}) { + const x = [a]; + const y = {b}; + mutate(y); + y.x = x; + return
{y}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0f7eebb877c4e6a145b06ca18d6c33afaacfc869 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.expect.md @@ -0,0 +1,148 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const o: any = useMemo(() => ({a}), [a]); + const x: Array = useMemo(() => [o], [o, b]); + const y = typedCapture(x); + const z = typedCapture(y); + x.push(z); + x.push(b); + + return ( + <> + ; + ; + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(19); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = { a }; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const o = t1; + let x; + if ($[2] !== b || $[3] !== o) { + x = [o]; + const y = typedCapture(x); + const z = typedCapture(y); + x.push(z); + x.push(b); + $[2] = b; + $[3] = o; + $[4] = x; + } else { + x = $[4]; + } + let t2; + if ($[5] !== a) { + t2 = [a]; + $[5] = a; + $[6] = t2; + } else { + t2 = $[6]; + } + let t3; + if ($[7] !== o || $[8] !== t2) { + t3 = ; + $[7] = o; + $[8] = t2; + $[9] = t3; + } else { + t3 = $[9]; + } + let t4; + if ($[10] !== a || $[11] !== b) { + t4 = [a, b]; + $[10] = a; + $[11] = b; + $[12] = t4; + } else { + t4 = $[12]; + } + let t5; + if ($[13] !== t4 || $[14] !== x) { + t5 = ; + $[13] = t4; + $[14] = x; + $[15] = t5; + } else { + t5 = $[15]; + } + let t6; + if ($[16] !== t3 || $[17] !== t5) { + t6 = ( + <> + {t3};{t5}; + + ); + $[16] = t3; + $[17] = t5; + $[18] = t6; + } else { + t6 = $[18]; + } + return t6; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0],"output":{"a":0}}
;
{"inputs":[0,0],"output":[{"a":0},[["[[ cyclic ref *2 ]]"]],0]}
; +
{"inputs":[0],"output":{"a":0}}
;
{"inputs":[0,1],"output":[{"a":0},[["[[ cyclic ref *2 ]]"]],1]}
; +
{"inputs":[1],"output":{"a":1}}
;
{"inputs":[1,1],"output":[{"a":1},[["[[ cyclic ref *2 ]]"]],1]}
; +
{"inputs":[0],"output":{"a":0}}
;
{"inputs":[0,0],"output":[{"a":0},[["[[ cyclic ref *2 ]]"]],0]}
; \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ada8679f25104d72f89e4ad59b99e5bc4085bf70 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-add-captured-array-to-itself.tsx @@ -0,0 +1,35 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const o: any = useMemo(() => ({a}), [a]); + const x: Array = useMemo(() => [o], [o, b]); + const y = typedCapture(x); + const z = typedCapture(y); + x.push(z); + x.push(b); + + return ( + <> + ; + ; + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5a3388fd8e83c4dfdc1e17680d7b4b22683c8cb9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.expect.md @@ -0,0 +1,112 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}: {a: number; b: number}) { + const x = useMemo(() => ({a}), [a, b]); + const f = () => { + const y = typedCapture(x); + const z = typedCreateFrom(y); + return z; + }; + const z = f(); + // mutates x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = { a }; + const f = () => { + const y = typedCapture(x); + const z = typedCreateFrom(y); + return z; + }; + + const z_0 = f(); + + typedMutate(z_0, b); + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":{"a":0,"property":0}}
+
{"inputs":[0,1],"output":{"a":0,"property":1}}
+
{"inputs":[1,1],"output":{"a":1,"property":1}}
+
{"inputs":[0,0],"output":{"a":0,"property":0}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.tsx new file mode 100644 index 0000000000000000000000000000000000000000..a4a2279810ccb401d5cf7db670c838de637db498 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom-lambda.tsx @@ -0,0 +1,33 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}: {a: number; b: number}) { + const x = useMemo(() => ({a}), [a, b]); + const f = () => { + const y = typedCapture(x); + const z = typedCreateFrom(y); + return z; + }; + const z = f(); + // mutates x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9885ef17c104b94996d728cff71b1e2fb73bf843 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.expect.md @@ -0,0 +1,103 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}: {a: number; b: number}) { + const x = useMemo(() => ({a}), [a, b]); + const y = typedCapture(x); + const z = typedCreateFrom(y); + // mutates x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = { a }; + const y = typedCapture(x); + const z = typedCreateFrom(y); + + typedMutate(z, b); + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":{"a":0,"property":0}}
+
{"inputs":[0,1],"output":{"a":0,"property":1}}
+
{"inputs":[1,1],"output":{"a":1,"property":1}}
+
{"inputs":[0,0],"output":{"a":0,"property":0}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.tsx new file mode 100644 index 0000000000000000000000000000000000000000..f343cc2afa27bf9ef56a32f68ddf4e339314aa5b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-capture-createfrom.tsx @@ -0,0 +1,29 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}: {a: number; b: number}) { + const x = useMemo(() => ({a}), [a, b]); + const y = typedCapture(x); + const z = typedCreateFrom(y); + // mutates x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..42f34bff4144a4da1af4ab3fa2f1d612fbaf5cbc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.expect.md @@ -0,0 +1,101 @@ + +## Input + +```javascript +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a]); + const y = typedCreateFrom(x); + const z = typedCapture(y); + // does not mutate x, so x should not depend on b + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(7); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = [{ a }]; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + const y = typedCreateFrom(x); + const z = typedCapture(y); + + typedMutate(z, b); + let t2; + if ($[2] !== a) { + t2 = [a]; + $[2] = a; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== t2 || $[5] !== x) { + t3 = ; + $[4] = t2; + $[5] = x; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0],"output":[{"a":0}]}
+
{"inputs":[0],"output":[{"a":0}]}
+
{"inputs":[1],"output":[{"a":1}]}
+
{"inputs":[0],"output":[{"a":0}]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.tsx new file mode 100644 index 0000000000000000000000000000000000000000..32d65e61e01ec771491e879db87c7c8eff3834cc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-createfrom-capture.tsx @@ -0,0 +1,28 @@ +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a]); + const y = typedCreateFrom(x); + const z = typedCapture(y); + // does not mutate x, so x should not depend on b + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4222c0dfe2cb2369699f28150c0fd91589bd4834 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.expect.md @@ -0,0 +1,119 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a, b]); + let z: any; + if (b) { + z = x; + } else { + z = typedCapture(x); + } + // could mutate x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(11); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = { a }; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + let x; + if ($[2] !== b || $[3] !== t1) { + x = [t1]; + let z; + if (b) { + z = x; + } else { + z = typedCapture(x); + } + + typedMutate(z, b); + $[2] = b; + $[3] = t1; + $[4] = x; + } else { + x = $[4]; + } + let t2; + if ($[5] !== a || $[6] !== b) { + t2 = [a, b]; + $[5] = a; + $[6] = b; + $[7] = t2; + } else { + t2 = $[7]; + } + let t3; + if ($[8] !== t2 || $[9] !== x) { + t3 = ; + $[8] = t2; + $[9] = x; + $[10] = t3; + } else { + t3 = $[10]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 0, b: 1 }, + { a: 1, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":[{"a":0}]}
+
{"inputs":[0,1],"output":[{"a":0}]}
+
{"inputs":[1,1],"output":[{"a":1}]}
+
{"inputs":[0,0],"output":[{"a":0}]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.tsx new file mode 100644 index 0000000000000000000000000000000000000000..ece6a2dc135149f19e026bb5c82e530799b5e682 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/transitivity-phi-assign-or-capture.tsx @@ -0,0 +1,33 @@ +// @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; +import { + typedCapture, + typedCreateFrom, + typedMutate, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + const x = useMemo(() => [{a}], [a, b]); + let z: any; + if (b) { + z = x; + } else { + z = typedCapture(x); + } + // could mutate x + typedMutate(z, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 0, b: 1}, + {a: 1, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d3378b4d482bb849881283a3d34354ebdbdbebba --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.expect.md @@ -0,0 +1,119 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel + +import {useMemo} from 'react'; +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + // create a mutable value with input `a` + const x = useMemo(() => makeObject_Primitives(a), [a]); + + // freeze the value + useIdentity(x); + + // known to pass-through via aliasing signature + const x2 = typedIdentity(x); + + // Unknown function so we assume it conditionally mutates, + // but x2 is frozen so this downgrades to a read. + // x should *not* take b as a dependency + identity(x2, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 1, b: 0}, + {a: 1, b: 1}, + {a: 0, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel + +import { useMemo } from "react"; +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(7); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = makeObject_Primitives(a); + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + + useIdentity(x); + + const x2 = typedIdentity(x); + + identity(x2, b); + let t2; + if ($[2] !== a) { + t2 = [a]; + $[2] = a; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== t2 || $[5] !== x) { + t3 = ; + $[4] = t2; + $[5] = x; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 1, b: 0 }, + { a: 1, b: 1 }, + { a: 0, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[1],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[1],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[0],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[0],"output":{"a":0,"b":"value1","c":true}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.js new file mode 100644 index 0000000000000000000000000000000000000000..d0f677ee4df177c829040104d3776d6f19bb47f8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-frozen-input.js @@ -0,0 +1,40 @@ +// @enableNewMutationAliasingModel + +import {useMemo} from 'react'; +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + // create a mutable value with input `a` + const x = useMemo(() => makeObject_Primitives(a), [a]); + + // freeze the value + useIdentity(x); + + // known to pass-through via aliasing signature + const x2 = typedIdentity(x); + + // Unknown function so we assume it conditionally mutates, + // but x2 is frozen so this downgrades to a read. + // x should *not* take b as a dependency + identity(x2, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 1, b: 0}, + {a: 1, b: 1}, + {a: 0, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..17fed05d93d4dd7f4ec8cb6aca563088d7a369cf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.expect.md @@ -0,0 +1,112 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel + +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + // create a mutable value with input `a` + const x = makeObject_Primitives(a); + + // known to pass-through via aliasing signature + const x2 = typedIdentity(x); + + // Unknown function so we assume it conditionally mutates, + // and x is still mutable so + identity(x2, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 1, b: 0}, + {a: 1, b: 1}, + {a: 0, b: 1}, + {a: 0, b: 0}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel + +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + x = makeObject_Primitives(a); + + const x2 = typedIdentity(x); + + identity(x2, b); + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + let t1; + if ($[3] !== a || $[4] !== b) { + t1 = [a, b]; + $[3] = a; + $[4] = b; + $[5] = t1; + } else { + t1 = $[5]; + } + let t2; + if ($[6] !== t1 || $[7] !== x) { + t2 = ; + $[6] = t1; + $[7] = x; + $[8] = t2; + } else { + t2 = $[8]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 0, b: 0 }], + sequentialRenders: [ + { a: 0, b: 0 }, + { a: 1, b: 0 }, + { a: 1, b: 1 }, + { a: 0, b: 1 }, + { a: 0, b: 0 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[0,0],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[1,0],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[1,1],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[0,1],"output":{"a":0,"b":"value1","c":true}}
+
{"inputs":[0,0],"output":{"a":0,"b":"value1","c":true}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.js new file mode 100644 index 0000000000000000000000000000000000000000..719c89d11de2fc8cbcadc98a57d07db1d12cbbb2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/typed-identity-function-mutable-input.js @@ -0,0 +1,35 @@ +// @enableNewMutationAliasingModel + +import { + identity, + makeObject_Primitives, + typedIdentity, + useIdentity, + ValidateMemoization, +} from 'shared-runtime'; + +function Component({a, b}) { + // create a mutable value with input `a` + const x = makeObject_Primitives(a); + + // known to pass-through via aliasing signature + const x2 = typedIdentity(x); + + // Unknown function so we assume it conditionally mutates, + // and x is still mutable so + identity(x2, b); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 0, b: 0}], + sequentialRenders: [ + {a: 0, b: 0}, + {a: 1, b: 0}, + {a: 1, b: 1}, + {a: 0, b: 1}, + {a: 0, b: 0}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0e54471a23cda1f58fae5d6af94b402f5e08bb3d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.expect.md @@ -0,0 +1,100 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useCallback} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Foo({arr1, arr2, foo}) { + const x = [arr1]; + + let y = []; + + const getVal1 = useCallback(() => { + return {x: 2}; + }, []); + + const getVal2 = useCallback(() => { + return [y]; + }, [foo ? (y = x.concat(arr2)) : y]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{arr1: [1, 2], arr2: [3, 4], foo: true}], + sequentialRenders: [ + {arr1: [1, 2], arr2: [3, 4], foo: true}, + {arr1: [1, 2], arr2: [3, 4], foo: false}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useCallback } from "react"; +import { Stringify } from "shared-runtime"; + +function Foo(t0) { + const $ = _c(10); + const { arr1, arr2, foo } = t0; + let t1; + if ($[0] !== arr1) { + t1 = [arr1]; + $[0] = arr1; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + let getVal1; + let t2; + if ($[2] !== arr2 || $[3] !== foo || $[4] !== x) { + let y = []; + getVal1 = _temp; + t2 = () => [y]; + foo ? (y = x.concat(arr2)) : y; + $[2] = arr2; + $[3] = foo; + $[4] = x; + $[5] = getVal1; + $[6] = t2; + } else { + getVal1 = $[5]; + t2 = $[6]; + } + const getVal2 = t2; + let t3; + if ($[7] !== getVal1 || $[8] !== getVal2) { + t3 = ; + $[7] = getVal1; + $[8] = getVal2; + $[9] = t3; + } else { + t3 = $[9]; + } + return t3; +} +function _temp() { + return { x: 2 }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ arr1: [1, 2], arr2: [3, 4], foo: true }], + sequentialRenders: [ + { arr1: [1, 2], arr2: [3, 4], foo: true }, + { arr1: [1, 2], arr2: [3, 4], foo: false }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"val1":{"kind":"Function","result":{"x":2}},"val2":{"kind":"Function","result":[[[1,2],3,4]]},"shouldInvokeFns":true}
+
{"val1":{"kind":"Function","result":{"x":2}},"val2":{"kind":"Function","result":[[]]},"shouldInvokeFns":true}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.tsx new file mode 100644 index 0000000000000000000000000000000000000000..861c413386dfbe9ddfc23dfd40738260b811bb8d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-deplist-controlflow.tsx @@ -0,0 +1,28 @@ +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useCallback} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Foo({arr1, arr2, foo}) { + const x = [arr1]; + + let y = []; + + const getVal1 = useCallback(() => { + return {x: 2}; + }, []); + + const getVal2 = useCallback(() => { + return [y]; + }, [foo ? (y = x.concat(arr2)) : y]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{arr1: [1, 2], arr2: [3, 4], foo: true}], + sequentialRenders: [ + {arr1: [1, 2], arr2: [3, 4], foo: true}, + {arr1: [1, 2], arr2: [3, 4], foo: false}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4a65f8a4e291d8ada4f91e24d00ee7eaa44a7c72 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.expect.md @@ -0,0 +1,84 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useCallback} from 'react'; +import {Stringify} from 'shared-runtime'; + +// We currently produce invalid output (incorrect scoping for `y` declaration) +function useFoo(arr1, arr2) { + const x = [arr1]; + + let y; + const getVal = useCallback(() => { + return {y}; + }, [((y = x.concat(arr2)), y)]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useCallback } from "react"; +import { Stringify } from "shared-runtime"; + +// We currently produce invalid output (incorrect scoping for `y` declaration) +function useFoo(arr1, arr2) { + const $ = _c(7); + let t0; + if ($[0] !== arr1) { + t0 = [arr1]; + $[0] = arr1; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let t1; + if ($[2] !== arr2 || $[3] !== x) { + let y; + t1 = () => ({ y }); + (y = x.concat(arr2)), y; + $[2] = arr2; + $[3] = x; + $[4] = t1; + } else { + t1 = $[4]; + } + const getVal = t1; + let t2; + if ($[5] !== getVal) { + t2 = ; + $[5] = getVal; + $[6] = t2; + } else { + t2 = $[6]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; + +``` + +### Eval output +(kind: ok)
{"getVal":{"kind":"Function","result":{"y":[[1,2],3,4]}},"shouldInvokeFns":true}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.tsx new file mode 100644 index 0000000000000000000000000000000000000000..01a87a22599856e492c030e356cca4f867270ef2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useCallback-reordering-depslist-assignment.tsx @@ -0,0 +1,23 @@ +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useCallback} from 'react'; +import {Stringify} from 'shared-runtime'; + +// We currently produce invalid output (incorrect scoping for `y` declaration) +function useFoo(arr1, arr2) { + const x = [arr1]; + + let y; + const getVal = useCallback(() => { + return {y}; + }, [((y = x.concat(arr2)), y)]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2d3f469986614df4860520d040cfe274b48aac3c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.expect.md @@ -0,0 +1,75 @@ + +## Input + +```javascript +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; + +function useFoo(arr1, arr2) { + const x = [arr1]; + + let y; + return useMemo(() => { + return {y}; + }, [((y = x.concat(arr2)), y)]); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import { useMemo } from "react"; + +function useFoo(arr1, arr2) { + const $ = _c(7); + let t0; + if ($[0] !== arr1) { + t0 = [arr1]; + $[0] = arr1; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== arr2 || $[3] !== x) { + (y = x.concat(arr2)), y; + $[2] = arr2; + $[3] = x; + $[4] = y; + } else { + y = $[4]; + } + let t1; + if ($[5] !== y) { + t1 = { y }; + $[5] = y; + $[6] = t1; + } else { + t1 = $[6]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; + +``` + +### Eval output +(kind: ok) {"y":[[1,2],3,4]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b9c6e54da75cfdd17777e50e07428a8cead10db --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-mutability/useMemo-reordering-depslist-assignment.ts @@ -0,0 +1,19 @@ +// @enableNewMutationAliasingModel @enablePreserveExistingMemoizationGuarantees:false @validateExhaustiveMemoizationDependencies:false +import {useMemo} from 'react'; + +function useFoo(arr1, arr2) { + const x = [arr1]; + + let y; + return useMemo(() => { + return {y}; + }, [((y = x.concat(arr2)), y)]); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [ + [1, 2], + [3, 4], + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a93d0df1e7840de8912da63a6bddb35e85861560 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +function Component(props) { + const x = new Foo(...props.foo, null, ...[props.bar]); + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(3); + let t0; + if ($[0] !== props.bar || $[1] !== props.foo) { + t0 = new Foo(...props.foo, null, ...[props.bar]); + $[0] = props.bar; + $[1] = props.foo; + $[2] = t0; + } else { + t0 = $[2]; + } + const x = t0; + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.js new file mode 100644 index 0000000000000000000000000000000000000000..f8a139d5882ee227ac0bbd71d7e44f548e8026b8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/new-spread.js @@ -0,0 +1,4 @@ +function Component(props) { + const x = new Foo(...props.foo, null, ...[props.bar]); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f2c7c5d539dfa0ad50ed4412702cb1078041d727 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +// @compilationMode(all) +function useMyHook({a, b}) { + return a + b; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useMyHook, + params: [{a: 1, b: 2}], +}; + +``` + +## Code + +```javascript +// @compilationMode(all) +function useMyHook(t0) { + const { a, b } = t0; + return a + b; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useMyHook, + params: [{ a: 1, b: 2 }], +}; + +``` + +### Eval output +(kind: ok) 3 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.js new file mode 100644 index 0000000000000000000000000000000000000000..2dbafff156ba63e6e4029e7fbedee639e766aa4c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-cache-slots-no-import.js @@ -0,0 +1,9 @@ +// @compilationMode(all) +function useMyHook({a, b}) { + return a + b; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useMyHook, + params: [{a: 1, b: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ffaa9eb74c0d575bf7155f4f5734dbe2da9a52e3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +// @enableFlowSuppressions + +function useX() {} + +function Foo(props) { + // $FlowFixMe[incompatible-type] + useX(); + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; + +``` + +## Code + +```javascript +// @enableFlowSuppressions + +function useX() {} + +function Foo(props) { + useX(); + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) null \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.js new file mode 100644 index 0000000000000000000000000000000000000000..c053580f35ad2cc986e9d154189b123ef25fdd8b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/no-flow-bailout-unrelated.js @@ -0,0 +1,14 @@ +// @enableFlowSuppressions + +function useX() {} + +function Foo(props) { + // $FlowFixMe[incompatible-type] + useX(); + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f9efa4ea23489b25006fecbf6c07c781830b06e2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +function Component(props) { + const filtered = props.items.filter(item => item != null); + return filtered; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [{a: true}, null, true, false, null, 'string', 3.14, null, [null]], + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.items) { + t0 = props.items.filter(_temp); + $[0] = props.items; + $[1] = t0; + } else { + t0 = $[1]; + } + const filtered = t0; + return filtered; +} +function _temp(item) { + return item != null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + { a: true }, + null, + true, + false, + null, + "string", + 3.14, + null, + [null], + ], + }, + ], +}; + +``` + +### Eval output +(kind: ok) [{"a":true},true,false,"string",3.14,[null]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.js new file mode 100644 index 0000000000000000000000000000000000000000..b15223e99d75cf5e3e718b81c1271ab38b7d4b33 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.js @@ -0,0 +1,13 @@ +function Component(props) { + const filtered = props.items.filter(item => item != null); + return filtered; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [{a: true}, null, true, false, null, 'string', 3.14, null, [null]], + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bf8590ec3a63cbd9c729695e2095edbe4603bed3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +interface ComponentProps { + name?: string; +} + +function Component(props: ComponentProps) { + return props.name!.toUpperCase(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Alice'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +interface ComponentProps { + name?: string; +} + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.name) { + t0 = props.name.toUpperCase(); + $[0] = props.name; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Alice" }], +}; + +``` + +### Eval output +(kind: ok) "ALICE" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c866b08eee0d747b4655812e581d75a3037abb0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/non-null-assertion.ts @@ -0,0 +1,12 @@ +interface ComponentProps { + name?: string; +} + +function Component(props: ComponentProps) { + return props.name!.toUpperCase(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Alice'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9a4f0c179f9f32c3c74a1b8446ffe8c0f9a163da --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {identity, Stringify, useIdentity} from 'shared-runtime'; + +function Component(props) { + const {x, ...rest} = useIdentity(props); + const z = rest.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, Stringify, useIdentity } from "shared-runtime"; + +function Component(props) { + const $ = _c(6); + const t0 = useIdentity(props); + let rest; + let x; + if ($[0] !== t0) { + ({ x, ...rest } = t0); + $[0] = t0; + $[1] = rest; + $[2] = x; + } else { + rest = $[1]; + x = $[2]; + } + const z = rest.z; + identity(z); + let t1; + if ($[3] !== x || $[4] !== z) { + t1 = ; + $[3] = x; + $[4] = z; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: "Hello", z: "World" }], +}; + +``` + +### Eval output +(kind: ok)
{"x":"Hello","z":"World"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.js new file mode 100644 index 0000000000000000000000000000000000000000..c4447f7be6e440d3517d9d8f633deea9a902de3f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-hook-return.js @@ -0,0 +1,13 @@ +import {identity, Stringify, useIdentity} from 'shared-runtime'; + +function Component(props) { + const {x, ...rest} = useIdentity(props); + const z = rest.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5335705c5d8279ea94dbd486406e14ba68335fca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(6); + let rest; + let x; + if ($[0] !== t0) { + ({ x, ...rest } = t0); + $[0] = t0; + $[1] = rest; + $[2] = x; + } else { + rest = $[1]; + x = $[2]; + } + let t1; + if ($[3] !== rest || $[4] !== x) { + t1 = ; + $[3] = rest; + $[4] = x; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: "Hello", z: "World" }], +}; + +``` + +### Eval output +(kind: ok)
{"z":"World","x":"Hello"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.js new file mode 100644 index 0000000000000000000000000000000000000000..d9f24264d69bcde0d9e5a7aa94401a5b0cf1acd4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-jsx.js @@ -0,0 +1,10 @@ +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7a435adcaad7641db6f215d7c3b4bda047c51c37 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + const restAlias = rest; + const z = restAlias.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(6); + let rest; + let x; + if ($[0] !== t0) { + ({ x, ...rest } = t0); + $[0] = t0; + $[1] = rest; + $[2] = x; + } else { + rest = $[1]; + x = $[2]; + } + const restAlias = rest; + const z = restAlias.z; + identity(z); + let t1; + if ($[3] !== x || $[4] !== z) { + t1 = ; + $[3] = x; + $[4] = z; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: "Hello", z: "World" }], +}; + +``` + +### Eval output +(kind: ok)
{"x":"Hello","z":"World"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.js new file mode 100644 index 0000000000000000000000000000000000000000..b1d26ab7f7206e8681a7ab6004aa878a5e0b8372 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props-local-indirection.js @@ -0,0 +1,13 @@ +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + const restAlias = rest; + const z = restAlias.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a8b1c2d747f0d045f58d39005a469fdfcf0adf36 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + const z = rest.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(6); + let rest; + let x; + if ($[0] !== t0) { + ({ x, ...rest } = t0); + $[0] = t0; + $[1] = rest; + $[2] = x; + } else { + rest = $[1]; + x = $[2]; + } + const z = rest.z; + identity(z); + let t1; + if ($[3] !== x || $[4] !== z) { + t1 = ; + $[3] = x; + $[4] = z; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: "Hello", z: "World" }], +}; + +``` + +### Eval output +(kind: ok)
{"x":"Hello","z":"World"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.js new file mode 100644 index 0000000000000000000000000000000000000000..d3e83d560c253f9fa16637ddcb8ccab4e72a1560 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutated-spread-props.js @@ -0,0 +1,12 @@ +import {identity, Stringify} from 'shared-runtime'; + +function Component({x, ...rest}) { + const z = rest.z; + identity(z); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 'Hello', z: 'World'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f397b53be1a9effc1f427fb4c7515bc1f71eefa4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.expect.md @@ -0,0 +1,113 @@ + +## Input + +```javascript +import {identity, mutate} from 'shared-runtime'; + +/** + * Currently, InferReactiveScopeVariables do not ensure that maybe-aliased + * values get assigned the same reactive scope. This is safe only when an + * already-constructed value is captured, e.g. + * ```js + * const x = makeObj(); ⌝ mutable range of x + * mutate(x); ⌟ + * <-- after this point, we can produce a canonical version + * of x for all following aliases + * const y = []; + * y.push(x); <-- y captures x + * ``` + * + * However, if a value is captured/aliased during its mutable range and the + * capturing container is separately memoized, it becomes difficult to guarantee + * that all aliases refer to the same value. + * + */ +function useFoo({a, b}) { + const x = {a}; + const y = {}; + mutate(x); + const z = [identity(y), b]; + mutate(y); + + if (z[0] !== y) { + throw new Error('oh no!'); + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 4, b: 3}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate } from "shared-runtime"; + +/** + * Currently, InferReactiveScopeVariables do not ensure that maybe-aliased + * values get assigned the same reactive scope. This is safe only when an + * already-constructed value is captured, e.g. + * ```js + * const x = makeObj(); ⌝ mutable range of x + * mutate(x); ⌟ + * <-- after this point, we can produce a canonical version + * of x for all following aliases + * const y = []; + * y.push(x); <-- y captures x + * ``` + * + * However, if a value is captured/aliased during its mutable range and the + * capturing container is separately memoized, it becomes difficult to guarantee + * that all aliases refer to the same value. + * + */ +function useFoo(t0) { + const $ = _c(4); + const { a, b } = t0; + let y; + let z; + if ($[0] !== a || $[1] !== b) { + const x = { a }; + y = {}; + mutate(x); + z = [identity(y), b]; + mutate(y); + $[0] = a; + $[1] = b; + $[2] = y; + $[3] = z; + } else { + y = $[2]; + z = $[3]; + } + + if (z[0] !== y) { + throw new Error("oh no!"); + } + + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 4, b: 3 }, + ], +}; + +``` + +### Eval output +(kind: ok) [{"wat0":"joe"},3] +[{"wat0":"joe"},3] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4a8c85ef31dd6c3582bbd9756441d5ba21f83c7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonmutating-capture-in-unsplittable-memo-block.ts @@ -0,0 +1,41 @@ +import {identity, mutate} from 'shared-runtime'; + +/** + * Currently, InferReactiveScopeVariables do not ensure that maybe-aliased + * values get assigned the same reactive scope. This is safe only when an + * already-constructed value is captured, e.g. + * ```js + * const x = makeObj(); ⌝ mutable range of x + * mutate(x); ⌟ + * <-- after this point, we can produce a canonical version + * of x for all following aliases + * const y = []; + * y.push(x); <-- y captures x + * ``` + * + * However, if a value is captured/aliased during its mutable range and the + * capturing container is separately memoized, it becomes difficult to guarantee + * that all aliases refer to the same value. + * + */ +function useFoo({a, b}) { + const x = {a}; + const y = {}; + mutate(x); + const z = [identity(y), b]; + mutate(y); + + if (z[0] !== y) { + throw new Error('oh no!'); + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 4, b: 3}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..722a19eec7bea415beea58c2fe65ebaf2e026235 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +// Note that `a?.b.c` is semantically different from `(a?.b).c` +// Here, 'props?.a` is an optional chain, and `.b` is an unconditional load +// (nullthrows if a is nullish) + +function Component(props) { + let x = (props?.a).b; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +// Note that `a?.b.c` is semantically different from `(a?.b).c` +// Here, 'props?.a` is an optional chain, and `.b` is an unconditional load +// (nullthrows if a is nullish) + +function Component(props) { + const x = (props?.a).b; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ["TodoAdd"], + isComponent: "TodoAdd", +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.js new file mode 100644 index 0000000000000000000000000000000000000000..dc789fcf67f1ffa289a47e70fd72fb4f0801fa78 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonoptional-load-from-optional-memberexpr.js @@ -0,0 +1,14 @@ +// Note that `a?.b.c` is semantically different from `(a?.b).c` +// Here, 'props?.a` is an optional chain, and `.b` is an unconditional load +// (nullthrows if a is nullish) + +function Component(props) { + let x = (props?.a).b; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..113e9104800778700aa551e51a7c71ffa6bb8b8d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +// @flow +function Component() { + return ( +
+ ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( +
+ ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..85c0ad1e7d3db1dcd66b0f4af50c4f1463a1928e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/nonreactive-noescaping-dependency-can-inline-into-consuming-scope.js @@ -0,0 +1,12 @@ +// @flow +function Component() { + return ( +
+ ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..32d2a5a259fddb04be749cf1d5d618c95598b917 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +function Test() { + const obj = { + 21: 'dimaMachina', + }; + // Destructuring assignment + const {21: myVar} = obj; + return ( +
+ {obj[21]} + {myVar} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Test() { + const $ = _c(2); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { 21: "dimaMachina" }; + $[0] = t0; + } else { + t0 = $[0]; + } + const obj = t0; + + const { 21: myVar } = obj; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = ( +
+ {obj[21]} + {myVar} +
+ ); + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
dimaMachinadimaMachina
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.js new file mode 100644 index 0000000000000000000000000000000000000000..b385417bbdd3b08342eda525ea98015a3f61583c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/numeric-literal-as-object-property-key.js @@ -0,0 +1,18 @@ +function Test() { + const obj = { + 21: 'dimaMachina', + }; + // Destructuring assignment + const {21: myVar} = obj; + return ( +
+ {obj[21]} + {myVar} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..74348c21920f85cf2fb5504b23da104c38e0f6b8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = {}; + if (someVal) { + x = {b}; + } else { + x = {c}; + } + + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c, d) { + const $ = _c(4); + let x; + if (someVal) { + let t0; + if ($[0] !== b) { + t0 = { b }; + $[0] = b; + $[1] = t0; + } else { + t0 = $[1]; + } + x = t0; + } else { + let t0; + if ($[2] !== c) { + t0 = { c }; + $[2] = c; + $[3] = t0; + } else { + t0 = $[3]; + } + x = t0; + } + + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.js new file mode 100644 index 0000000000000000000000000000000000000000..79674f6185d2dd45e40fce050916d3c2b0d0524f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-cached-in-if-else.js @@ -0,0 +1,10 @@ +function foo(a, b, c, d) { + let x = {}; + if (someVal) { + x = {b}; + } else { + x = {c}; + } + + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0f2b186c6530e677d9646d6fee7968ca08afe229 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = {}; + if (someVal) { + x = {b}; + } else { + x = {c}; + } + + x.f = 1; + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c, d) { + const $ = _c(3); + let x; + if ($[0] !== b || $[1] !== c) { + if (someVal) { + x = { b }; + } else { + x = { c }; + } + + x.f = 1; + $[0] = b; + $[1] = c; + $[2] = x; + } else { + x = $[2]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.js new file mode 100644 index 0000000000000000000000000000000000000000..81d6a415b64260a859586ca198b06ee147fd0f55 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-literal-mutated-after-if-else.js @@ -0,0 +1,11 @@ +function foo(a, b, c, d) { + let x = {}; + if (someVal) { + x = {b}; + } else { + x = {c}; + } + + x.f = 1; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..52077b6b894c1a688446fdb936ddc0014e3529ed --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + const y = someObj(); + const z = y; + x = z; + } else { + x = someObj(); + } + + x.f = 1; + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c, d) { + const $ = _c(2); + someObj(); + let x; + if ($[0] !== a) { + if (a) { + const y = someObj(); + const z = y; + x = z; + } else { + x = someObj(); + } + + x.f = 1; + $[0] = a; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.js new file mode 100644 index 0000000000000000000000000000000000000000..de4dd11684fefee30c848428c0b39dfc43f3cc79 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else-with-alias.js @@ -0,0 +1,13 @@ +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + const y = someObj(); + const z = y; + x = z; + } else { + x = someObj(); + } + + x.f = 1; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..933e05d1554ca714781a0468f651fd3a44a304ec --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + x = someObj(); + } else { + x = someObj(); + } + + x.f = 1; + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c, d) { + const $ = _c(2); + someObj(); + let x; + if ($[0] !== a) { + if (a) { + x = someObj(); + } else { + x = someObj(); + } + + x.f = 1; + $[0] = a; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.js new file mode 100644 index 0000000000000000000000000000000000000000..ed43dec122110e482a4d4dae8736a477dcd15931 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-if-else.js @@ -0,0 +1,11 @@ +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + x = someObj(); + } else { + x = someObj(); + } + + x.f = 1; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8c4a8b1a3cca02c19287317d5b42fbaea5ab9a46 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + let z; + if (b) { + const w = someObj(); + z = w; + } else { + z = someObj(); + } + const y = z; + x = z; + } else { + x = someObj(); + } + + x.f = 1; + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c, d) { + const $ = _c(3); + someObj(); + let x; + if ($[0] !== a || $[1] !== b) { + if (a) { + let z; + if (b) { + const w = someObj(); + z = w; + } else { + z = someObj(); + } + + x = z; + } else { + x = someObj(); + } + + x.f = 1; + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.js new file mode 100644 index 0000000000000000000000000000000000000000..cd2ac6fa5a6e7894d5b3d8b95a62ee33a66bc3a8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/obj-mutated-after-nested-if-else-with-alias.js @@ -0,0 +1,19 @@ +function foo(a, b, c, d) { + let x = someObj(); + if (a) { + let z; + if (b) { + const w = someObj(); + z = w; + } else { + z = someObj(); + } + const y = z; + x = z; + } else { + x = someObj(); + } + + x.f = 1; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8b4dbc8f863d670c3bacbcc544f6b4fb7bfd3c46 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md @@ -0,0 +1,83 @@ + +## Input + +```javascript +function Component({a, b, c}) { + // This is an object version of array-access-assignment.js + // Meant to confirm that object expressions and PropertyStore/PropertyLoad with strings + // works equivalently to array expressions and property accesses with numeric indices + const x = {zero: a}; + const y = {zero: null, one: b}; + const z = {zero: {}, one: {}, two: {zero: c}}; + x.zero = y.one; + z.zero.zero = x.zero; + return {zero: x, one: z}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1, b: 20, c: 300}], + sequentialRenders: [ + {a: 2, b: 20, c: 300}, + {a: 3, b: 20, c: 300}, + {a: 3, b: 21, c: 300}, + {a: 3, b: 22, c: 300}, + {a: 3, b: 22, c: 301}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(6); + const { a, b, c } = t0; + let t1; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + const x = { zero: a }; + let t2; + if ($[4] !== b) { + t2 = { zero: null, one: b }; + $[4] = b; + $[5] = t2; + } else { + t2 = $[5]; + } + const y = t2; + const z = { zero: {}, one: {}, two: { zero: c } }; + x.zero = y.one; + z.zero.zero = x.zero; + t1 = { zero: x, one: z }; + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 1, b: 20, c: 300 }], + sequentialRenders: [ + { a: 2, b: 20, c: 300 }, + { a: 3, b: 20, c: 300 }, + { a: 3, b: 21, c: 300 }, + { a: 3, b: 22, c: 300 }, + { a: 3, b: 22, c: 301 }, + ], +}; + +``` + +### Eval output +(kind: ok) {"zero":{"zero":20},"one":{"zero":{"zero":20},"one":{},"two":{"zero":300}}} +{"zero":{"zero":20},"one":{"zero":{"zero":20},"one":{},"two":{"zero":300}}} +{"zero":{"zero":21},"one":{"zero":{"zero":21},"one":{},"two":{"zero":300}}} +{"zero":{"zero":22},"one":{"zero":{"zero":22},"one":{},"two":{"zero":300}}} +{"zero":{"zero":22},"one":{"zero":{"zero":22},"one":{},"two":{"zero":301}}} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.js new file mode 100644 index 0000000000000000000000000000000000000000..ef047238e7f84f964defe5efde283c793a5c7058 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.js @@ -0,0 +1,23 @@ +function Component({a, b, c}) { + // This is an object version of array-access-assignment.js + // Meant to confirm that object expressions and PropertyStore/PropertyLoad with strings + // works equivalently to array expressions and property accesses with numeric indices + const x = {zero: a}; + const y = {zero: null, one: b}; + const z = {zero: {}, one: {}, two: {zero: c}}; + x.zero = y.one; + z.zero.zero = x.zero; + return {zero: x, one: z}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1, b: 20, c: 300}], + sequentialRenders: [ + {a: 2, b: 20, c: 300}, + {a: 3, b: 20, c: 300}, + {a: 3, b: 21, c: 300}, + {a: 3, b: 22, c: 300}, + {a: 3, b: 22, c: 301}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..399c9d1d30aae83e55334f64a2fa14ce4e800d83 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function foo(a, b, c) { + const x = {...a}; + x[b] = c[b]; + x[1 + 2] = c[b * 4]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b, c) { + const x = { ...a }; + x[b] = c[b]; + x[3] = c[b * 4]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ["TodoAdd"], + isComponent: "TodoAdd", +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.js new file mode 100644 index 0000000000000000000000000000000000000000..68efd7f4f70498725ebd348450bed3e8994bb663 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-computed-access-assignment.js @@ -0,0 +1,11 @@ +function foo(a, b, c) { + const x = {...a}; + x[b] = c[b]; + x[1 + 2] = c[b * 4]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bc541b47f1f5df36c236ecd927a7bfd1d092c891 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {makeObject_Primitives, Stringify} from 'shared-runtime'; + +function Component(props) { + const object = {object: props.object}; + const entries = Object.entries(object); + entries.map(([, value]) => { + value.updated = true; + }); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{object: {key: makeObject_Primitives()}}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeObject_Primitives, Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.object) { + const object = { object: props.object }; + const entries = Object.entries(object); + entries.map(_temp); + t0 = ; + $[0] = props.object; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} +function _temp(t0) { + const [, value] = t0; + value.updated = true; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ object: { key: makeObject_Primitives() } }], +}; + +``` + +### Eval output +(kind: ok)
{"entries":[["object",{"key":{"a":0,"b":"value1","c":true},"updated":true}]]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.js new file mode 100644 index 0000000000000000000000000000000000000000..2902cffd014fe053f8586603aa8ff2d732ee3cae --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-entries-mutation.js @@ -0,0 +1,15 @@ +import {makeObject_Primitives, Stringify} from 'shared-runtime'; + +function Component(props) { + const object = {object: props.object}; + const entries = Object.entries(object); + entries.map(([, value]) => { + value.updated = true; + }); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{object: {key: makeObject_Primitives()}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9d970ef9e6752421d536f634f5ab05fce6c9e198 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +function Foo() { + const x = () => { + window.href = 'foo'; + }; + const y = {x}; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo() { + const $ = _c(1); + const x = _temp; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const y = { x }; + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() { + window.href = "foo"; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; + +``` + +### Eval output +(kind: exception) Bar is not defined \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.js new file mode 100644 index 0000000000000000000000000000000000000000..b3c936a2a284d9553ca34104e3eb06b407c53acd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-captures-function-with-global-mutation.js @@ -0,0 +1,12 @@ +function Foo() { + const x = () => { + window.href = 'foo'; + }; + const y = {x}; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..40685f9acc231007eb93671d0c90d59a31886876 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + const key = 42; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'hello!'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.value) { + t0 = identity([props.value]); + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 = { [42]: t0 }; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + const context = t1; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "hello!" }], +}; + +``` + +### Eval output +(kind: ok) {"42":["hello!"]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.js new file mode 100644 index 0000000000000000000000000000000000000000..c76210f30e4fa344ea23f6e202d143ad55e14ab0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-number.js @@ -0,0 +1,14 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + const key = 42; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'hello!'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..be73de917e3be64069ea7a7c7b338495278bd973 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + const key = 'KeyName'; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.value) { + t0 = identity([props.value]); + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 = { ["KeyName"]: t0 }; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + const context = t1; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"KeyName":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.js new file mode 100644 index 0000000000000000000000000000000000000000..cdbe85e819df86fb0db654b25c820c65d121f631 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-constant-string.js @@ -0,0 +1,14 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + const key = 'KeyName'; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d46db9c8de1a48aa16c9f2de804a6637d912018c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [(mutate(key), key)]: identity([props.value]), + }; + mutate(key); + return [context, key]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [{value: 42}, {value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.value) { + const key = {}; + const context = { [(mutate(key), key)]: identity([props.value]) }; + mutate(key); + t0 = [context, key]; + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], + sequentialRenders: [{ value: 42 }, { value: 42 }], +}; + +``` + +### Eval output +(kind: ok) [{"[object Object]":[42]},{"wat0":"joe","wat1":"joe"}] +[{"[object Object]":[42]},{"wat0":"joe","wat1":"joe"}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..183c03cf916db7bdfc4990f194a1378dd5cdd5d2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction-sequence-expr.js @@ -0,0 +1,16 @@ +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [(mutate(key), key)]: identity([props.value]), + }; + mutate(key); + return [context, key]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [{value: 42}, {value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1d671aa36cf97c24689d7c1e24379334b446ba3b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [mutateAndReturn(key)]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [{value: 42}, {value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let context; + if ($[0] !== props.value) { + const key = {}; + context = { [mutateAndReturn(key)]: identity([props.value]) }; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], + sequentialRenders: [{ value: 42 }, { value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"[object Object]":[42]} +{"[object Object]":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.js new file mode 100644 index 0000000000000000000000000000000000000000..0176850e9814aef2170f6312a0773d17feb0308d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-modified-during-after-construction.js @@ -0,0 +1,16 @@ +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [mutateAndReturn(key)]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [{value: 42}, {value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..77d3653e0c1b59c23f0da6f90abba0b0751d150e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [mutateAndReturn(key)]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(5); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const key = {}; + t0 = mutateAndReturn(key); + $[0] = t0; + } else { + t0 = $[0]; + } + let t1; + if ($[1] !== props.value) { + t1 = identity([props.value]); + $[1] = props.value; + $[2] = t1; + } else { + t1 = $[2]; + } + let t2; + if ($[3] !== t1) { + t2 = { [t0]: t1 }; + $[3] = t1; + $[4] = t2; + } else { + t2 = $[4]; + } + const context = t2; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"[object Object]":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.js new file mode 100644 index 0000000000000000000000000000000000000000..0a35c884f3f2c828e94a3450e4153bbeacdf0a01 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-mutate-key-while-constructing-object.js @@ -0,0 +1,14 @@ +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [mutateAndReturn(key)]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..deb40abb34d7c327ff8c657de2f5cac624e26864 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +const SCALE = 2; + +function Component(props) { + const key = SCALE; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{key: 'Sathya', value: 'Compiler'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +const SCALE = 2; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.value) { + t0 = identity([props.value]); + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 = { [SCALE]: t0 }; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + const context = t1; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ key: "Sathya", value: "Compiler" }], +}; + +``` + +### Eval output +(kind: ok) {"2":["Compiler"]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.js new file mode 100644 index 0000000000000000000000000000000000000000..18a1646ef6c20245fdb0e874fbb68c3a48883d59 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-non-reactive.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +const SCALE = 2; + +function Component(props) { + const key = SCALE; + const context = { + [key]: identity([props.value]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{key: 'Sathya', value: 'Compiler'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..dfd6649095b544d60bcba85eb2b193212422969f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +import {identity, mutate} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [key]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let context; + if ($[0] !== props.value) { + const key = {}; + context = { [key]: identity([props.value]) }; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"[object Object]":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.js new file mode 100644 index 0000000000000000000000000000000000000000..1edaaaef27e4b2fbe28f528822eb3533551f16a7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key-object-mutated-later.js @@ -0,0 +1,15 @@ +import {identity, mutate} from 'shared-runtime'; + +function Component(props) { + const key = {}; + const context = { + [key]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..419e40ab5cacea37ba19422822f8504ee7eee0e2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +const SCALE = 2; + +function Component(props) { + const {key} = props; + const context = { + [key]: identity([props.value, SCALE]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{key: 'Sathya', value: 'Compiler'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +const SCALE = 2; + +function Component(props) { + const $ = _c(5); + const { key } = props; + let t0; + if ($[0] !== props.value) { + t0 = identity([props.value, SCALE]); + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== key || $[3] !== t0) { + t1 = { [key]: t0 }; + $[2] = key; + $[3] = t0; + $[4] = t1; + } else { + t1 = $[4]; + } + const context = t1; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ key: "Sathya", value: "Compiler" }], +}; + +``` + +### Eval output +(kind: ok) {"Sathya":["Compiler",2]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.js new file mode 100644 index 0000000000000000000000000000000000000000..bf5544eaa86664f75021c3b7e1f84f5f6c7126c5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-key.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +const SCALE = 2; + +function Component(props) { + const {key} = props; + const context = { + [key]: identity([props.value, SCALE]), + }; + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{key: 'Sathya', value: 'Compiler'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..49be82b146bcea1f0e6099803a5e7ec575aead35 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {a: 'key'}; + const context = { + [key.a]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let context; + if ($[0] !== props.value) { + const key = { a: "key" }; + const t0 = key.a; + const t1 = identity([props.value]); + let t2; + if ($[2] !== t1) { + t2 = { [t0]: t1 }; + $[2] = t1; + $[3] = t2; + } else { + t2 = $[3]; + } + context = t2; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"key":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.js new file mode 100644 index 0000000000000000000000000000000000000000..95a1d434624e914e799290f4156b4c90d98535a9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.js @@ -0,0 +1,15 @@ +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const key = {a: 'key'}; + const context = { + [key.a]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ac6cb97b086c0210f9e2cda9db3ee53c00b96c62 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const obj = {mutateAndReturn}; + const key = {}; + const context = { + [obj.mutateAndReturn(key)]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, mutate, mutateAndReturn } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let context; + if ($[0] !== props.value) { + const obj = { mutateAndReturn }; + const key = {}; + context = { [obj.mutateAndReturn(key)]: identity([props.value]) }; + mutate(key); + $[0] = props.value; + $[1] = context; + } else { + context = $[1]; + } + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"[object Object]":[42]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.js new file mode 100644 index 0000000000000000000000000000000000000000..5cef590ed5dab08013a945388f4cf963239181b9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-member-expr-call.js @@ -0,0 +1,16 @@ +import {identity, mutate, mutateAndReturn} from 'shared-runtime'; + +function Component(props) { + const obj = {mutateAndReturn}; + const key = {}; + const context = { + [obj.mutateAndReturn(key)]: identity([props.value]), + }; + mutate(key); + return context; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..360a8d45175f1b18c836df2fe809ddd79f58a3ab --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +function Component(props) { + const x = {['foo']: props.foo}; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.foo) { + t0 = { foo: props.foo }; + $[0] = props.foo; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ["TodoAdd"], + isComponent: "TodoAdd", +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.js new file mode 100644 index 0000000000000000000000000000000000000000..30e9855d7f8634fe602fac29d41572d430923a32 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-string-literal-key.js @@ -0,0 +1,10 @@ +function Component(props) { + const x = {['foo']: props.foo}; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f076d9032db0ea9697e93ba651f894e354dbf31e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.expect.md @@ -0,0 +1,108 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees +import {useMemo} from 'react'; +import {Stringify} from 'shared-runtime'; + +// derived from https://github.com/facebook/react/issues/32261 +function Component({items}) { + const record = useMemo( + () => + Object.fromEntries( + items.map(item => [item.id, ref => ]) + ), + [items] + ); + + // Without a declaration for Object.entries(), this would be assumed to mutate + // `record`, meaning existing memoization couldn't be preserved + return ( +
+ {Object.keys(record).map(id => ( + + ))} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + {id: '0', name: 'Hello'}, + {id: '1', name: 'World!'}, + ], + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees +import { useMemo } from "react"; +import { Stringify } from "shared-runtime"; + +// derived from https://github.com/facebook/react/issues/32261 +function Component(t0) { + const $ = _c(7); + const { items } = t0; + let t1; + if ($[0] !== items) { + t1 = Object.fromEntries(items.map(_temp)); + $[0] = items; + $[1] = t1; + } else { + t1 = $[1]; + } + const record = t1; + let t2; + if ($[2] !== record) { + t2 = Object.keys(record); + $[2] = record; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== record || $[5] !== t2) { + t3 = ( +
+ {t2.map((id) => ( + + ))} +
+ ); + $[4] = record; + $[5] = t2; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} +function _temp(item) { + return [item.id, (ref) => ]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + { id: "0", name: "Hello" }, + { id: "1", name: "World!" }, + ], + }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"render":"[[ function params=1 ]]"}
{"render":"[[ function params=1 ]]"}
\ No newline at end of file