diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.js new file mode 100644 index 0000000000000000000000000000000000000000..38ae97ab95643be5f5386cbd1ed371decf6dea22 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-keys.js @@ -0,0 +1,36 @@ +// @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!'}, + ], + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cafa06298a40ae17b932aa5e61ef9817fe400218 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +import { + createHookWrapper, + identity, + CONST_STRING0, + CONST_STRING1, +} from 'shared-runtime'; + +function useHook({value}) { + return { + getValue() { + return identity(value); + }, + }.getValue() + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { + createHookWrapper, + identity, + CONST_STRING0, + CONST_STRING1, +} from "shared-runtime"; + +function useHook(t0) { + const { value } = t0; + return { + getValue() { + return identity(value); + }, + }.getValue() + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":"global string 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/object-literal-method-call-in-ternary-test.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.js new file mode 100644 index 0000000000000000000000000000000000000000..7f96298511a1f12fa2f18e4265c33e3d16fa3e07 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-call-in-ternary-test.js @@ -0,0 +1,21 @@ +import { + createHookWrapper, + identity, + CONST_STRING0, + CONST_STRING1, +} from 'shared-runtime'; + +function useHook({value}) { + return { + getValue() { + return identity(value); + }, + }.getValue() + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ef8b8ef430fffffa3e8ee1ad4053f7e7414cc10c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {identity, createHookWrapper} from 'shared-runtime'; + +function useHook({isCond, value}) { + return isCond + ? identity({ + getValue() { + return value; + }, + }) + : 42; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{isCond: true, value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity, createHookWrapper } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(3); + const { isCond, value } = t0; + let t1; + if ($[0] !== isCond || $[1] !== value) { + t1 = isCond + ? identity({ + getValue() { + return value; + }, + }) + : 42; + $[0] = isCond; + $[1] = value; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ isCond: true, value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getValue":{"kind":"Function","result":0}},"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/object-literal-method-derived-in-ternary-consequent.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.js new file mode 100644 index 0000000000000000000000000000000000000000..e2e2a0bffccbbee60339ef4ef8bbf8feb5f47faf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-derived-in-ternary-consequent.js @@ -0,0 +1,16 @@ +import {identity, createHookWrapper} from 'shared-runtime'; + +function useHook({isCond, value}) { + return isCond + ? identity({ + getValue() { + return value; + }, + }) + : 42; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{isCond: true, value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..41b8d802d3cfe5cfcc34a5d6c5dbbe3e1037a63d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {createHookWrapper} from 'shared-runtime'; + +function useHook({isCond, value}) { + return isCond + ? { + getValue() { + return value; + }, + } + : 42; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{isCond: true, value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(3); + const { isCond, value } = t0; + let t1; + if ($[0] !== isCond || $[1] !== value) { + t1 = isCond + ? { + getValue() { + return value; + }, + } + : 42; + $[0] = isCond; + $[1] = value; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ isCond: true, value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getValue":{"kind":"Function","result":0}},"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/object-literal-method-in-ternary-consequent.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.js new file mode 100644 index 0000000000000000000000000000000000000000..84617e7c8bcdc29380812c8cae26d19b2415edec --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-consequent.js @@ -0,0 +1,16 @@ +import {createHookWrapper} from 'shared-runtime'; + +function useHook({isCond, value}) { + return isCond + ? { + getValue() { + return value; + }, + } + : 42; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{isCond: true, value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..51463b326d354089633faea9c0e9eca971d7f732 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +import {createHookWrapper, CONST_STRING0, CONST_STRING1} from 'shared-runtime'; + +function useHook({value}) { + return { + getValue() { + return identity(value); + }, + } + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { + createHookWrapper, + CONST_STRING0, + CONST_STRING1, +} from "shared-runtime"; + +function useHook(t0) { + const { value } = t0; + return { + getValue() { + return identity(value); + }, + } + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":"global string 0","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/object-literal-method-in-ternary-test.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.js new file mode 100644 index 0000000000000000000000000000000000000000..c414f4a946dea79aa9d5c1d82f438e07512b449e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-method-in-ternary-test.js @@ -0,0 +1,16 @@ +import {createHookWrapper, CONST_STRING0, CONST_STRING1} from 'shared-runtime'; + +function useHook({value}) { + return { + getValue() { + return identity(value); + }, + } + ? CONST_STRING0 + : CONST_STRING1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-spread-element.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-spread-element.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6900cd5b82189b0a47878623f5255d9b9510ed85 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-spread-element.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +function Component(props) { + const x = {...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 = { ...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-literal-spread-element.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-spread-element.js new file mode 100644 index 0000000000000000000000000000000000000000..d3a2d2846e7e492c758b79c915268dbf11764819 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-literal-spread-element.js @@ -0,0 +1,10 @@ +function Component(props) { + const x = {...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-method-maybe-alias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-maybe-alias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..74c0dcdc14f59e884471f6e5a7ad9450285c6c55 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-maybe-alias.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {createHookWrapper, setProperty} from 'shared-runtime'; +function useHook(props) { + const x = { + getX() { + return props; + }, + }; + const y = { + getY() { + return 'y'; + }, + }; + return setProperty(x, y); +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper, setProperty } from "shared-runtime"; +function useHook(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = { + getX() { + return props; + }, + }; + const y = { + getY() { + return "y"; + }, + }; + t0 = setProperty(x, y); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getX":{"kind":"Function","result":{"value":0}},"wat0":{"getY":{"kind":"Function","result":"y"}}},"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/object-method-maybe-alias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-maybe-alias.js new file mode 100644 index 0000000000000000000000000000000000000000..dbb1800c97785cd1a281a919aa3dbc844cc5644c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-maybe-alias.js @@ -0,0 +1,19 @@ +import {createHookWrapper, setProperty} from 'shared-runtime'; +function useHook(props) { + const x = { + getX() { + return props; + }, + }; + const y = { + getY() { + return 'y'; + }, + }; + return setProperty(x, y); +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..886eb119d36ba50445773a9e5e84e84a4e42797a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-3.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {createHookWrapper, mutate} from 'shared-runtime'; + +function useHook(a) { + const x = {a}; + let obj = { + method() { + mutate(x); + return x; + }, + }; + return obj.method(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{x: 1}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper, mutate } from "shared-runtime"; + +function useHook(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + const x = { a }; + const obj = { + method() { + mutate(x); + return x; + }, + }; + t0 = obj.method(); + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ x: 1 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"a":{"x":1},"wat0":"joe"},"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/object-method-shorthand-3.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-3.js new file mode 100644 index 0000000000000000000000000000000000000000..ef8b122bbb4f186d2541e0df2b631652da98e3f0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-3.js @@ -0,0 +1,17 @@ +import {createHookWrapper, mutate} from 'shared-runtime'; + +function useHook(a) { + const x = {a}; + let obj = { + method() { + mutate(x); + return x; + }, + }; + return obj.method(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{x: 1}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1674c16e88b3cb222bc1e7cbc8af48ecfb755fd6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {createHookWrapper, mutate, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return value; + }, + }; + mutate(x); + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime"; +function useHook(t0) { + const $ = _c(2); + const { value } = t0; + let obj; + if ($[0] !== value) { + const x = mutateAndReturn({ value }); + obj = { + getValue() { + return value; + }, + }; + mutate(x); + $[0] = value; + $[1] = obj; + } else { + obj = $[1]; + } + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getValue":{"kind":"Function","result":0}},"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/object-method-shorthand-aliased-mutate-after.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.js new file mode 100644 index 0000000000000000000000000000000000000000..03058d857bfbf13c6737cf0796b44285accdab93 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-aliased-mutate-after.js @@ -0,0 +1,16 @@ +import {createHookWrapper, mutate, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return value; + }, + }; + mutate(x); + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6c135bb0ebf9e29ba62f0fc7a4c0c139b7cc7923 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {createHookWrapper, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return x; + }, + }; + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper, mutateAndReturn } from "shared-runtime"; +function useHook(t0) { + const $ = _c(4); + const { value } = t0; + let t1; + if ($[0] !== value) { + t1 = mutateAndReturn({ value }); + $[0] = value; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + let t2; + if ($[2] !== x) { + t2 = { + getValue() { + return x; + }, + }; + $[2] = x; + $[3] = t2; + } else { + t2 = $[3]; + } + const obj = t2; + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getValue":{"kind":"Function","result":{"value":0,"wat0":"joe"}}},"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/object-method-shorthand-derived-value.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.js new file mode 100644 index 0000000000000000000000000000000000000000..a5c919984d0b3dcdf59ebd358f6611dfa0ff1d65 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-derived-value.js @@ -0,0 +1,15 @@ +import {createHookWrapper, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return x; + }, + }; + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2f130f32c74a8598d9d1da36f3807deacc584b79 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +import {createHookWrapper} from 'shared-runtime'; +import {useState} from 'react'; +function useFoo() { + const [state, _setState] = useState(false); + return { + func() { + return state; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useFoo), + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper } from "shared-runtime"; +import { useState } from "react"; +function useFoo() { + const $ = _c(2); + const [state] = useState(false); + let t0; + if ($[0] !== state) { + t0 = { + func() { + return state; + }, + }; + $[0] = state; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useFoo), + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"func":{"kind":"Function","result":false}},"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/object-method-shorthand-hook-dep.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.js new file mode 100644 index 0000000000000000000000000000000000000000..da9a8ba1394775923f5e14ea60c9c0b738835f1c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-hook-dep.js @@ -0,0 +1,15 @@ +import {createHookWrapper} from 'shared-runtime'; +import {useState} from 'react'; +function useFoo() { + const [state, _setState] = useState(false); + return { + func() { + return state; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useFoo), + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9c73dd6f99251d232ef80bca8f0925a5325eff48 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {createHookWrapper, mutate, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return x; + }, + }; + mutate(obj); + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper, mutate, mutateAndReturn } from "shared-runtime"; +function useHook(t0) { + const $ = _c(2); + const { value } = t0; + let obj; + if ($[0] !== value) { + const x = mutateAndReturn({ value }); + obj = { + getValue() { + return x; + }, + }; + mutate(obj); + $[0] = value; + $[1] = obj; + } else { + obj = $[1]; + } + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getValue":{"kind":"Function","result":{"value":0,"wat0":"joe"}},"wat0":"joe"},"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/object-method-shorthand-mutated-after.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.js new file mode 100644 index 0000000000000000000000000000000000000000..29928376bd03a41cdc2b7514cad3b126247834c0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-mutated-after.js @@ -0,0 +1,16 @@ +import {createHookWrapper, mutate, mutateAndReturn} from 'shared-runtime'; +function useHook({value}) { + const x = mutateAndReturn({value}); + const obj = { + getValue() { + return x; + }, + }; + mutate(obj); + return obj; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ebfa1d8d316203a66819db9205e8ab14e59d6d15 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function Component() { + let obj = { + method() { + return 1; + }, + }; + return obj.method(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 1}, {a: 2}, {b: 2}], +}; + +``` + +## 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")) { + const obj = { + method() { + return 1; + }, + }; + t0 = obj.method(); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: 1 }, { a: 2 }, { b: 2 }], +}; + +``` + +### 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/object-method-shorthand.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand.js new file mode 100644 index 0000000000000000000000000000000000000000..f7bf87e26e0f0a9cb0353b9d9d86c8cd8b9a8812 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand.js @@ -0,0 +1,13 @@ +function Component() { + let obj = { + method() { + return 1; + }, + }; + return obj.method(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 1}, {a: 2}, {b: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-mutated-in-consequent-alternate-both-return.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-mutated-in-consequent-alternate-both-return.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b5534114c08a131a1faa5b6f13646408ea34ef6e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-mutated-in-consequent-alternate-both-return.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +import {makeObject_Primitives} from 'shared-runtime'; + +function Component(props) { + const object = makeObject_Primitives(); + if (props.cond) { + object.value = 1; + return object; + } else { + object.value = props.value; + return object; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: false, value: [0, 1, 2]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeObject_Primitives } from "shared-runtime"; + +function Component(props) { + const $ = _c(3); + let t0; + if ($[0] !== props.cond || $[1] !== props.value) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + const object = makeObject_Primitives(); + if (props.cond) { + object.value = 1; + t0 = object; + break bb0; + } else { + object.value = props.value; + t0 = object; + break bb0; + } + } + $[0] = props.cond; + $[1] = props.value; + $[2] = t0; + } else { + t0 = $[2]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: false, value: [0, 1, 2] }], +}; + +``` + +### Eval output +(kind: ok) {"a":0,"b":"value1","c":true,"value":[0,1,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-mutated-in-consequent-alternate-both-return.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-mutated-in-consequent-alternate-both-return.js new file mode 100644 index 0000000000000000000000000000000000000000..94a142d0337084faac6df8134a277db51dd67d73 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-mutated-in-consequent-alternate-both-return.js @@ -0,0 +1,17 @@ +import {makeObject_Primitives} from 'shared-runtime'; + +function Component(props) { + const object = makeObject_Primitives(); + if (props.cond) { + object.value = 1; + return object; + } else { + object.value = props.value; + return object; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: false, value: [0, 1, 2]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-pattern-params.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-pattern-params.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ad8828770dfc75650cf4ca4d6e7d641d61239290 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-pattern-params.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +function component({a, b}) { + let y = {a}; + let z = {b}; + return {y, z}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-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 y = t1; + let t2; + if ($[2] !== b) { + t2 = { b }; + $[2] = b; + $[3] = t2; + } else { + t2 = $[3]; + } + const z = t2; + let t3; + if ($[4] !== y || $[5] !== z) { + t3 = { y, z }; + $[4] = y; + $[5] = z; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +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-pattern-params.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-pattern-params.js new file mode 100644 index 0000000000000000000000000000000000000000..6bebf0633cdd33017c7dec0d53ec92e5fd645b85 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-pattern-params.js @@ -0,0 +1,11 @@ +function component({a, b}) { + let y = {a}; + let z = {b}; + return {y, z}; +} + +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-properties.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-properties.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..290901a4d1340ab2b47db2b6e60fc73e25656539 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-properties.expect.md @@ -0,0 +1,25 @@ + +## Input + +```javascript +function foo(a, b, c) { + const x = a.x; + const y = {...b.c.d}; + y.z = c.d.e; + foo(a.b.c); + [a.b.c]; +} + +``` + +## Code + +```javascript +function foo(a, b, c) { + const y = { ...b.c.d }; + y.z = c.d.e; + foo(a.b.c); +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-properties.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-properties.js new file mode 100644 index 0000000000000000000000000000000000000000..4aa21f7c7f661d4210f66dfda433520ff0f197fe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-properties.js @@ -0,0 +1,7 @@ +function foo(a, b, c) { + const x = a.x; + const y = {...b.c.d}; + y.z = c.d.e; + foo(a.b.c); + [a.b.c]; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-1.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-1.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6b9ca0c2317b32cd5f42923d6437a249c9643a01 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-1.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +import {createHookWrapper} from 'shared-runtime'; +function useHook({a, b}) { + return { + x: function () { + return [a]; + }, + y() { + return [b]; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{a: 1, b: 2}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper } from "shared-runtime"; +function useHook(t0) { + const $ = _c(5); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = function () { + return [a]; + }; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] !== b || $[3] !== t1) { + t2 = { + x: t1, + y() { + return [b]; + }, + }; + $[2] = b; + $[3] = t1; + $[4] = t2; + } else { + t2 = $[4]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ a: 1, b: 2 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"x":{"kind":"Function","result":[1]},"y":{"kind":"Function","result":[2]}},"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/object-shorthand-method-1.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-1.js new file mode 100644 index 0000000000000000000000000000000000000000..7ec6b24bb4f0858547c1360e2184ea244376f84a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-1.js @@ -0,0 +1,16 @@ +import {createHookWrapper} from 'shared-runtime'; +function useHook({a, b}) { + return { + x: function () { + return [a]; + }, + y() { + return [b]; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{a: 1, b: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-2.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-2.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0a0383e93a52e8b9a8ab842cd7d01ed09e50b922 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-2.expect.md @@ -0,0 +1,76 @@ + +## Input + +```javascript +import {createHookWrapper} from 'shared-runtime'; + +function useHook({a, b, c}) { + return { + x: [a], + y() { + return [b]; + }, + z: {c}, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{a: 1, b: 2, c: 2}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { createHookWrapper } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(8); + const { a, b, c } = t0; + let t1; + if ($[0] !== a) { + t1 = [a]; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] !== b || $[3] !== c || $[4] !== t1) { + let t3; + if ($[6] !== c) { + t3 = { c }; + $[6] = c; + $[7] = t3; + } else { + t3 = $[7]; + } + t2 = { + x: t1, + y() { + return [b]; + }, + z: t3, + }; + $[2] = b; + $[3] = c; + $[4] = t1; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ a: 1, b: 2, c: 2 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"x":[1],"y":{"kind":"Function","result":[2]},"z":{"c":2}},"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/object-shorthand-method-2.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-2.js new file mode 100644 index 0000000000000000000000000000000000000000..faf33dfeedffb38307a42f345d5253a41b7e105e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-2.js @@ -0,0 +1,16 @@ +import {createHookWrapper} from 'shared-runtime'; + +function useHook({a, b, c}) { + return { + x: [a], + y() { + return [b]; + }, + z: {c}, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{a: 1, b: 2, c: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-nested.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-nested.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..dc1cd699d295c2dc27cd06ec8634b4dbffcb3c60 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-nested.expect.md @@ -0,0 +1,72 @@ + +## Input + +```javascript +import {useState} from 'react'; +import {createHookWrapper} from 'shared-runtime'; + +function useHook({value}) { + const [state] = useState(false); + + return { + getX() { + return { + a: [], + getY() { + return value; + }, + state, + }; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; +import { createHookWrapper } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(3); + const { value } = t0; + const [state] = useState(false); + let t1; + if ($[0] !== state || $[1] !== value) { + t1 = { + getX() { + return { + a: [], + getY() { + return value; + }, + state, + }; + }, + }; + $[0] = state; + $[1] = value; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{ value: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"result":{"getX":{"kind":"Function","result":{"a":[],"getY":{"kind":"Function","result":0},"state":false}}},"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/object-shorthand-method-nested.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-nested.js new file mode 100644 index 0000000000000000000000000000000000000000..64cbac9ea5e1e5f25909168009fed154ee8de41f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-shorthand-method-nested.js @@ -0,0 +1,23 @@ +import {useState} from 'react'; +import {createHookWrapper} from 'shared-runtime'; + +function useHook({value}) { + const [state] = useState(false); + + return { + getX() { + return { + a: [], + getY() { + return value; + }, + state, + }; + }, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: createHookWrapper(useHook), + params: [{value: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values-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-values-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-values-mutation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values-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-values-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-values.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..900399066413c8b898fa42d8ee85accde5b11ae0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values.expect.md @@ -0,0 +1,103 @@ + +## 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, + {id: item.id, render: ref => }, + ]) + ), + [items] + ); + + // Without a declaration for Object.entries(), this would be assumed to mutate + // `record`, meaning existing memoization couldn't be preserved + return ( +
+ {Object.values(record).map(({id, render}) => ( + + ))} +
+ ); +} + +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(4); + 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.values(record).map(_temp2)}
; + $[2] = record; + $[3] = t2; + } else { + t2 = $[3]; + } + return t2; +} +function _temp2(t0) { + const { id, render } = t0; + return ; +} +function _temp(item) { + return [ + item.id, + { id: item.id, render: (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 diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values.js new file mode 100644 index 0000000000000000000000000000000000000000..4cf229c379a56d3741b60c1c4d15e46d71dbc097 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-values.js @@ -0,0 +1,39 @@ +// @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, + {id: item.id, render: ref => }, + ]) + ), + [items] + ); + + // Without a declaration for Object.entries(), this would be assumed to mutate + // `record`, meaning existing memoization couldn't be preserved + return ( +
+ {Object.values(record).map(({id, render}) => ( + + ))} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + {id: '0', name: 'Hello'}, + {id: '1', name: 'World!'}, + ], + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-logical-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-logical-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..60c1a427cd50852b1e4547627e85ccda4fd521d2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-logical-expr.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +import {useNoAlias} from 'shared-runtime'; + +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ?? {}; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; + +``` + +## Code + +```javascript +import { useNoAlias } from "shared-runtime"; + +function useFoo(props) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ?? {}; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{ value: null }], +}; + +``` + +### 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/optional-call-chain-in-logical-expr.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-logical-expr.ts new file mode 100644 index 0000000000000000000000000000000000000000..b96f6aefad5e557d9aefb36a254b655b4880fad6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-logical-expr.ts @@ -0,0 +1,11 @@ +import {useNoAlias} from 'shared-runtime'; + +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ?? {}; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-ternary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-ternary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c8c2a77fff6a042b749278771977a7ba48f71439 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-ternary.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +import {useNoAlias} from 'shared-runtime'; + +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ? {} : null; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; + +``` + +## Code + +```javascript +import { useNoAlias } from "shared-runtime"; + +function useFoo(props) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ? {} : null; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{ value: null }], +}; + +``` + +### 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/optional-call-chain-in-ternary.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-ternary.ts new file mode 100644 index 0000000000000000000000000000000000000000..ac5302da00c536ed1edf16745c065503474add6f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chain-in-ternary.ts @@ -0,0 +1,11 @@ +import {useNoAlias} from 'shared-runtime'; + +function useFoo(props: {value: {x: string; y: string} | null}) { + const value = props.value; + return useNoAlias(value?.x, value?.y) ? {} : null; +} + +export const FIXTURE_ENTRYPONT = { + fn: useFoo, + props: [{value: null}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5f90a126d31abaf38fe47c59e4e83b0a72b8bfaf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.expect.md @@ -0,0 +1,29 @@ + +## Input + +```javascript +function Component(props) { + return call?.(props.a)?.(props.b)?.(props.c); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = call?.(props.a)?.(props.b)?.(props.c); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.js new file mode 100644 index 0000000000000000000000000000000000000000..c4fbcff906d9b9a4458a60b8c1a625c08e9b1c58 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-chained.js @@ -0,0 +1,3 @@ +function Component(props) { + return call?.(props.a)?.(props.b)?.(props.c); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fa6f8cd9bc66c2c43808ae77c1b0033d04d7bd6b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +import {useFragment} from 'shared-runtime'; + +function Component(props) { + const item = useFragment( + graphql` + fragment F on T { + id + } + `, + props.item + ); + return item.items?.map(item => renderItem(item)) ?? []; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useFragment } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + const item = useFragment( + graphql` + fragment F on T { + id + } + `, + props.item, + ); + let t0; + if ($[0] !== item.items) { + t0 = item.items?.map(_temp) ?? []; + $[0] = item.items; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} +function _temp(item_0) { + return renderItem(item_0); +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.js new file mode 100644 index 0000000000000000000000000000000000000000..6fa11a2dab494f2b739565d26d9f29137575b181 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-logical.js @@ -0,0 +1,13 @@ +import {useFragment} from 'shared-runtime'; + +function Component(props) { + const item = useFragment( + graphql` + fragment F on T { + id + } + `, + props.item + ); + return item.items?.map(item => renderItem(item)) ?? []; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..00338b7f60c604e7794cb2043436d58260688312 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.expect.md @@ -0,0 +1,29 @@ + +## Input + +```javascript +function Component(props) { + return foo?.(props); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = foo?.(props); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.js new file mode 100644 index 0000000000000000000000000000000000000000..6590cb5b78f04b76e5b1c4ad4474ab72ed9e8500 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-simple.js @@ -0,0 +1,3 @@ +function Component(props) { + return foo?.(props); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-independently-memoizable-arg.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-independently-memoizable-arg.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6d25c5ee7140337eb8a082c50454cb889b41e663 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-independently-memoizable-arg.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +function Component(props) { + const x = makeOptionalFunction(props); + // for a regular call, the JSX element could be independently memoized + // since it is an immutable value. however, because the call is optional, + // we can't extract out independent memoization for the element w/o + // forcing that argument to evaluate unconditionally + const y = x?.( +
+ {props.text} +
+ ); + return y; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = makeOptionalFunction(props); + t0 = x?.( +
+ {props.text} +
, + ); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const y = t0; + + 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/optional-call-with-independently-memoizable-arg.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-independently-memoizable-arg.js new file mode 100644 index 0000000000000000000000000000000000000000..9d9abf887f51eab7a9e06953f37d958a23051485 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-independently-memoizable-arg.js @@ -0,0 +1,13 @@ +function Component(props) { + const x = makeOptionalFunction(props); + // for a regular call, the JSX element could be independently memoized + // since it is an immutable value. however, because the call is optional, + // we can't extract out independent memoization for the element w/o + // forcing that argument to evaluate unconditionally + const y = x?.( +
+ {props.text} +
+ ); + return y; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d95461adf90e645537b70f9a7b2a85ad7c54a6a8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.expect.md @@ -0,0 +1,29 @@ + +## Input + +```javascript +function Component(props) { + return props?.items?.map?.(render)?.filter(Boolean) ?? []; +} + +``` + +## 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?.map?.(render)?.filter(Boolean) ?? []; + $[0] = props?.items; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.js new file mode 100644 index 0000000000000000000000000000000000000000..400ec94c5c0c98df563364d2e4e21beaf8e3c8dc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.js @@ -0,0 +1,3 @@ +function Component(props) { + return props?.items?.map?.(render)?.filter(Boolean) ?? []; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..856d235ae6c50c632f16f18108327d79d53e84e0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + const x = makeOptionalFunction(props); + const y = makeObject(props); + const z = x?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = makeOptionalFunction(props); + const y = makeObject(props); + t0 = x?.(y.a, props.a, foo(y.b), bar(props.b)); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + 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/optional-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call.js new file mode 100644 index 0000000000000000000000000000000000000000..f4610a49f3abf9d9dde556ac982e0a3875dffb4e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call.js @@ -0,0 +1,6 @@ +function Component(props) { + const x = makeOptionalFunction(props); + const y = makeObject(props); + const z = x?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-chain-on-known-nonnull.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-chain-on-known-nonnull.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a1cc75f53630a700277a50af29c0eee89e37fc0c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-chain-on-known-nonnull.expect.md @@ -0,0 +1,141 @@ + +## Input + +```javascript +// @enablePropagateDepsInHIR +function Component({data}) { + const geography = nullthrows(data.details); + const section = geography.sections?.nodes[0]; + const geoCountry = geography?.country ?? geography?.stateCountry; + const getTriggerType = () => { + if (geoCountry != null) { + return 'COUNTRY'; + } + if (geography?.region != null) { + return 'REGION'; + } + return 'GLOBAL'; + }; + const conditions = geography?.conditions?.nodes ?? []; + const [name, setName] = useState(section?.name); + const [isSaving, setIsSaving] = useState(false); + const triggerType = getTriggerType(); + + const controls = + geography?.controls?.nodes.map(c => ({ + title: c.name ?? '', + uniqueID: c.id ?? '', + })) ?? []; + + const handleSubmit = () => { + setIsSaving(true); + mutate({ + geography_id: nullthrows(geography?.id), + section_id: section?.id, + name, + triggerType, + controls, + }); + }; + + return ( +
+ {name} + {geoCountry} + {conditions} +
+ ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR +function Component(t0) { + const $ = _c(12); + const { data } = t0; + const geography = nullthrows(data.details); + const section = geography.sections?.nodes[0]; + const geoCountry = geography?.country ?? geography?.stateCountry; + const getTriggerType = () => { + if (geoCountry != null) { + return "COUNTRY"; + } + + if (geography?.region != null) { + return "REGION"; + } + + return "GLOBAL"; + }; + + const conditions = geography?.conditions?.nodes ?? []; + const [name] = useState(section?.name); + const [, setIsSaving] = useState(false); + const triggerType = getTriggerType(); + + const controls = geography?.controls?.nodes.map(_temp) ?? []; + let t1; + if ( + $[0] !== controls || + $[1] !== geography?.id || + $[2] !== name || + $[3] !== section?.id || + $[4] !== setIsSaving || + $[5] !== triggerType + ) { + t1 = () => { + setIsSaving(true); + mutate({ + geography_id: nullthrows(geography?.id), + section_id: section?.id, + name, + triggerType, + controls, + }); + }; + $[0] = controls; + $[1] = geography?.id; + $[2] = name; + $[3] = section?.id; + $[4] = setIsSaving; + $[5] = triggerType; + $[6] = t1; + } else { + t1 = $[6]; + } + const handleSubmit = t1; + let t2; + if ( + $[7] !== conditions || + $[8] !== geoCountry || + $[9] !== handleSubmit || + $[10] !== name + ) { + t2 = ( +
+ {name} + {geoCountry} + {conditions} +
+ ); + $[7] = conditions; + $[8] = geoCountry; + $[9] = handleSubmit; + $[10] = name; + $[11] = t2; + } else { + t2 = $[11]; + } + return t2; +} +function _temp(c) { + return { title: c.name ?? "", uniqueID: c.id ?? "" }; +} + +``` + +### 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/optional-chain-on-known-nonnull.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-chain-on-known-nonnull.js new file mode 100644 index 0000000000000000000000000000000000000000..4a19e446d7a928689714c2e362201febc2ce8458 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-chain-on-known-nonnull.js @@ -0,0 +1,44 @@ +// @enablePropagateDepsInHIR +function Component({data}) { + const geography = nullthrows(data.details); + const section = geography.sections?.nodes[0]; + const geoCountry = geography?.country ?? geography?.stateCountry; + const getTriggerType = () => { + if (geoCountry != null) { + return 'COUNTRY'; + } + if (geography?.region != null) { + return 'REGION'; + } + return 'GLOBAL'; + }; + const conditions = geography?.conditions?.nodes ?? []; + const [name, setName] = useState(section?.name); + const [isSaving, setIsSaving] = useState(false); + const triggerType = getTriggerType(); + + const controls = + geography?.controls?.nodes.map(c => ({ + title: c.name ?? '', + uniqueID: c.id ?? '', + })) ?? []; + + const handleSubmit = () => { + setIsSaving(true); + mutate({ + geography_id: nullthrows(geography?.id), + section_id: section?.id, + name, + triggerType, + controls, + }); + }; + + return ( +
+ {name} + {geoCountry} + {conditions} +
+ ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-load-static.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-load-static.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cd520d13e11cc77121c2ee3f93fdb1a611420389 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-load-static.expect.md @@ -0,0 +1,21 @@ + +## Input + +```javascript +function Component(props) { + let x = a?.b.c[0]; + return x; +} + +``` + +## Code + +```javascript +function Component(props) { + const x = a?.b.c[0]; + 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/optional-computed-load-static.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-load-static.js new file mode 100644 index 0000000000000000000000000000000000000000..aa192bc82dcf9e704d59a3437298e504f90eaf13 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-load-static.js @@ -0,0 +1,4 @@ +function Component(props) { + let x = a?.b.c[0]; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c641fb9a248e513c04d380a04c7142db9b2c874f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.expect.md @@ -0,0 +1,31 @@ + +## Input + +```javascript +function Component(props) { + const object = makeObject(props); + return object?.[props.key]; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = makeObject(props); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const object = t0; + return object?.[props.key]; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..5e8a408e10070801a321e92b0f8b5e4637971260 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-computed-member-expression.js @@ -0,0 +1,4 @@ +function Component(props) { + const object = makeObject(props); + return object?.[props.key]; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4c2bced216ff4e8d0a916edf831926ace3b4df6c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.expect.md @@ -0,0 +1,96 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {identity, ValidateMemoization} from 'shared-runtime'; +import {useMemo} from 'react'; + +function Component({arg}) { + const data = useMemo(() => { + return arg?.items.edges?.nodes.map(identity); + }, [arg?.items.edges?.nodes]); + return ( + + ); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{arg: null}], + sequentialRenders: [ + {arg: null}, + {arg: null}, + {arg: {items: {edges: null}}}, + {arg: {items: {edges: null}}}, + {arg: {items: {edges: {nodes: [1, 2, 'hello']}}}}, + {arg: {items: {edges: {nodes: [1, 2, 'hello']}}}}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import { identity, ValidateMemoization } from "shared-runtime"; +import { useMemo } from "react"; + +function Component(t0) { + const $ = _c(7); + const { arg } = t0; + + arg?.items.edges?.nodes; + let t1; + if ($[0] !== arg?.items.edges?.nodes) { + t1 = arg?.items.edges?.nodes.map(identity); + $[0] = arg?.items.edges?.nodes; + $[1] = t1; + } else { + t1 = $[1]; + } + const data = t1; + + const t2 = arg?.items.edges?.nodes; + let t3; + if ($[2] !== t2) { + t3 = [t2]; + $[2] = t2; + $[3] = t3; + } else { + t3 = $[3]; + } + let t4; + if ($[4] !== data || $[5] !== t3) { + t4 = ; + $[4] = data; + $[5] = t3; + $[6] = t4; + } else { + t4 = $[6]; + } + return t4; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ arg: null }], + sequentialRenders: [ + { arg: null }, + { arg: null }, + { arg: { items: { edges: null } } }, + { arg: { items: { edges: null } } }, + { arg: { items: { edges: { nodes: [1, 2, "hello"] } } } }, + { arg: { items: { edges: { nodes: [1, 2, "hello"] } } } }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[null]}
+
{"inputs":[null]}
+
{"inputs":[null]}
+
{"inputs":[null]}
+
{"inputs":[[1,2,"hello"]],"output":[1,2,"hello"]}
+
{"inputs":[[1,2,"hello"]],"output":[1,2,"hello"]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.js new file mode 100644 index 0000000000000000000000000000000000000000..73f0f4d421a4c4dafaa9a9d0941b8bece546e2de --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-as-memo-dep.js @@ -0,0 +1,24 @@ +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {identity, ValidateMemoization} from 'shared-runtime'; +import {useMemo} from 'react'; + +function Component({arg}) { + const data = useMemo(() => { + return arg?.items.edges?.nodes.map(identity); + }, [arg?.items.edges?.nodes]); + return ( + + ); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{arg: null}], + sequentialRenders: [ + {arg: null}, + {arg: null}, + {arg: {items: {edges: null}}}, + {arg: {items: {edges: null}}}, + {arg: {items: {edges: {nodes: [1, 2, 'hello']}}}}, + {arg: {items: {edges: {nodes: [1, 2, 'hello']}}}}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6632545eeebd05527b790d9fa117b60cca51a792 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.expect.md @@ -0,0 +1,38 @@ + +## Input + +```javascript +function Component(props) { + const x = makeObject(); + return x?.[foo(props.value)]; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(3); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = makeObject(); + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + let t1; + if ($[1] !== props) { + t1 = x?.[foo(props.value)]; + $[1] = props; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.js new file mode 100644 index 0000000000000000000000000000000000000000..ad51e5ec62862547cdc0952ac4abccdcb6f69231 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-call-as-property.js @@ -0,0 +1,4 @@ +function Component(props) { + const x = makeObject(); + return x?.[foo(props.value)]; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-chain.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-chain.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8c705f6174929bb3534e108e397115d5028e46aa --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-chain.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +// Note that `a?.b.c` is semantically different from `(a?.b).c` +// We should codegen the correct member expressions +function Component(props) { + let x = props?.b.c; + let y = props?.b.c.d?.e.f.g?.h; + return {x, y}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Note that `a?.b.c` is semantically different from `(a?.b).c` +// We should codegen the correct member expressions +function Component(props) { + const $ = _c(3); + const x = props?.b.c; + const y = props?.b.c.d?.e.f.g?.h; + let t0; + if ($[0] !== x || $[1] !== y) { + t0 = { x, y }; + $[0] = x; + $[1] = y; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +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/optional-member-expression-chain.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-chain.js new file mode 100644 index 0000000000000000000000000000000000000000..065bc894d96d24399709a6124b4e2055c6013c26 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-chain.js @@ -0,0 +1,13 @@ +// Note that `a?.b.c` is semantically different from `(a?.b).c` +// We should codegen the correct member expressions +function Component(props) { + let x = props?.b.c; + let y = props?.b.c.d?.e.f.g?.h; + return {x, y}; +} + +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/optional-member-expression-inverted-optionals-parallel-paths.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-inverted-optionals-parallel-paths.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..73c88e2c493c1de91f0793fbc53c11ffbfee62c9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-inverted-optionals-parallel-paths.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +function Component(props) { + const data = useMemo(() => { + const x = []; + x.push(props?.a.b?.c.d?.e); + x.push(props.a?.b.c?.d.e); + return x; + }, [props.a.b.c.d.e]); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import { ValidateMemoization } from "shared-runtime"; +function Component(props) { + const $ = _c(2); + + const x$0 = []; + x$0.push(props?.a.b?.c.d?.e); + x$0.push(props.a?.b.c?.d.e); + let t0; + if ($[0] !== props.a.b.c.d.e) { + t0 = ; + $[0] = props.a.b.c.d.e; + $[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/optional-member-expression-inverted-optionals-parallel-paths.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-inverted-optionals-parallel-paths.js new file mode 100644 index 0000000000000000000000000000000000000000..563b0bbf0f418ef808c3ee691247157bddf897cc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-inverted-optionals-parallel-paths.js @@ -0,0 +1,11 @@ +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +function Component(props) { + const data = useMemo(() => { + const x = []; + x.push(props?.a.b?.c.d?.e); + x.push(props.a?.b.c?.d.e); + return x; + }, [props.a.b.c.d.e]); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single-with-unconditional.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single-with-unconditional.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..95ebf3f95f9e7c6571b418ef37cd040284c11638 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single-with-unconditional.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +function Component(props) { + const data = useMemo(() => { + const x = []; + x.push(props?.items); + x.push(props.items); + return x; + }, [props.items]); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import { ValidateMemoization } from "shared-runtime"; +function Component(props) { + const $ = _c(7); + let x; + if ($[0] !== props.items) { + x = []; + x.push(props?.items); + x.push(props.items); + $[0] = props.items; + $[1] = x; + } else { + x = $[1]; + } + const data = x; + let t0; + if ($[2] !== props.items) { + t0 = [props.items]; + $[2] = props.items; + $[3] = t0; + } else { + t0 = $[3]; + } + let t1; + if ($[4] !== data || $[5] !== t0) { + t1 = ; + $[4] = data; + $[5] = t0; + $[6] = t1; + } else { + t1 = $[6]; + } + 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/optional-member-expression-single-with-unconditional.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single-with-unconditional.js new file mode 100644 index 0000000000000000000000000000000000000000..8e6275bf921eb88d5075ca3ae063b2ab189c62d7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single-with-unconditional.js @@ -0,0 +1,11 @@ +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +function Component(props) { + const data = useMemo(() => { + const x = []; + x.push(props?.items); + x.push(props.items); + return x; + }, [props.items]); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..43476f1604b49fe5fe032122b64c1fbf7f010b56 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.expect.md @@ -0,0 +1,89 @@ + +## Input + +```javascript +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +import {useMemo} from 'react'; +function Component({arg}) { + const data = useMemo(() => { + const x = []; + x.push(arg?.items); + return x; + }, [arg?.items]); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{arg: {items: 2}}], + sequentialRenders: [ + {arg: {items: 2}}, + {arg: {items: 2}}, + {arg: null}, + {arg: null}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import { ValidateMemoization } from "shared-runtime"; +import { useMemo } from "react"; +function Component(t0) { + const $ = _c(7); + const { arg } = t0; + + arg?.items; + let x; + if ($[0] !== arg?.items) { + x = []; + x.push(arg?.items); + $[0] = arg?.items; + $[1] = x; + } else { + x = $[1]; + } + const data = x; + const t1 = arg?.items; + let t2; + if ($[2] !== t1) { + t2 = [t1]; + $[2] = t1; + $[3] = t2; + } else { + t2 = $[3]; + } + let t3; + if ($[4] !== data || $[5] !== t2) { + t3 = ; + $[4] = data; + $[5] = t2; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ arg: { items: 2 } }], + sequentialRenders: [ + { arg: { items: 2 } }, + { arg: { items: 2 } }, + { arg: null }, + { arg: null }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[2],"output":[2]}
+
{"inputs":[2],"output":[2]}
+
{"inputs":[null],"output":[null]}
+
{"inputs":[null],"output":[null]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.js new file mode 100644 index 0000000000000000000000000000000000000000..62ac31dd6d1b1198c2dc7eee9a3ae29f2c9cba55 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-single.js @@ -0,0 +1,22 @@ +// @validatePreserveExistingMemoizationGuarantees @enableOptionalDependencies +import {ValidateMemoization} from 'shared-runtime'; +import {useMemo} from 'react'; +function Component({arg}) { + const data = useMemo(() => { + const x = []; + x.push(arg?.items); + return x; + }, [arg?.items]); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{arg: {items: 2}}], + sequentialRenders: [ + {arg: {items: 2}}, + {arg: {items: 2}}, + {arg: null}, + {arg: null}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b30a76a1c041ca9255f0ace8deab3cbef16b09e5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.expect.md @@ -0,0 +1,30 @@ + +## Input + +```javascript +function Component(props) { + const x = makeObject(); + return x.y?.[props.a?.[props.b?.[props.c]]]; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = makeObject(); + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + return x.y?.[props.a?.[props.b?.[props.c]]]; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.js new file mode 100644 index 0000000000000000000000000000000000000000..d5ced1ec06fc78205682a5465a0767f4859c2ca0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression-with-optional-member-expr-as-property.js @@ -0,0 +1,4 @@ +function Component(props) { + const x = makeObject(); + return x.y?.[props.a?.[props.b?.[props.c]]]; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4029117ea6ea99d921131dad34d24b1e6ee39818 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function Foo(props) { + let x = bar(props.a); + let y = x?.b; + + let z = useBar(y); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.a) { + t0 = bar(props.a); + $[0] = props.a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + const y = x?.b; + + const z = useBar(y); + 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/optional-member-expression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression.js new file mode 100644 index 0000000000000000000000000000000000000000..f2af4518ee240d6214b1db694ca23efaac2c7891 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-member-expression.js @@ -0,0 +1,7 @@ +function Foo(props) { + let x = bar(props.a); + let y = x?.b; + + let z = useBar(y); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-method-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-method-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6da3dea15073130e399f9d218b2a663bf2168e87 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-method-call.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + const x = makeObject(props); + const y = makeObject(props); + const z = x.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = makeObject(props); + const y = makeObject(props); + t0 = x.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + 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/optional-method-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-method-call.js new file mode 100644 index 0000000000000000000000000000000000000000..25145d61edc68ea26e9322607f293a35f5ea93e7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-method-call.js @@ -0,0 +1,6 @@ +function Component(props) { + const x = makeObject(props); + const y = makeObject(props); + const z = x.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-method-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-method-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1ce6250e10256869b6d33ffe2bdb02589ddfa096 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-method-call.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + const z = x?.method(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + t0 = x?.method(y.a, props.a, foo(y.b), bar(props.b)); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + 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/optional-receiver-method-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-method-call.js new file mode 100644 index 0000000000000000000000000000000000000000..698437dcd8c1eab379974c24e60d906245e224f9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-method-call.js @@ -0,0 +1,6 @@ +function Component(props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + const z = x?.method(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-optional-method.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-optional-method.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ac3d19960876770ddcc2d2d0409908d5385a73eb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-optional-method.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + const z = x?.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + t0 = x?.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + 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/optional-receiver-optional-method.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-optional-method.js new file mode 100644 index 0000000000000000000000000000000000000000..0d62a3c78dbe5f49493305f185b3fe440520ffb8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-receiver-optional-method.js @@ -0,0 +1,6 @@ +function Component(props) { + const x = makeOptionalObject(props); + const y = makeObject(props); + const z = x?.optionalMethod?.(y.a, props.a, foo(y.b), bar(props.b)); + return z; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7e79b2d0437b00261eb2a1c3c5c71b23f4c70eee --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +// @enableReactiveScopesInHIR:false +import {useRef} from 'react'; +import {addOne} from 'shared-runtime'; + +function useKeyCommand() { + const currentPosition = useRef(0); + const handleKey = direction => () => { + const position = currentPosition.current; + const nextPosition = direction === 'left' ? addOne(position) : position; + currentPosition.current = nextPosition; + }; + const moveLeft = { + handler: handleKey('left'), + }; + const moveRight = { + handler: handleKey('right'), + }; + return [moveLeft, moveRight]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useKeyCommand, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enableReactiveScopesInHIR:false +import { useRef } from "react"; +import { addOne } from "shared-runtime"; + +function useKeyCommand() { + const $ = _c(1); + const currentPosition = useRef(0); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const handleKey = (direction) => () => { + const position = currentPosition.current; + const nextPosition = direction === "left" ? addOne(position) : position; + currentPosition.current = nextPosition; + }; + const moveLeft = { handler: handleKey("left") }; + const moveRight = { handler: handleKey("right") }; + t0 = [moveLeft, moveRight]; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useKeyCommand, + params: [], +}; + +``` + +### Eval output +(kind: ok) [{"handler":"[[ function params=0 ]]"},{"handler":"[[ 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/original-reactive-scopes-fork/capture-ref-for-later-mutation.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.tsx new file mode 100644 index 0000000000000000000000000000000000000000..6f27dfe07fb3337147936cd410227dc75d095eb3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/original-reactive-scopes-fork/capture-ref-for-later-mutation.tsx @@ -0,0 +1,24 @@ +// @enableReactiveScopesInHIR:false +import {useRef} from 'react'; +import {addOne} from 'shared-runtime'; + +function useKeyCommand() { + const currentPosition = useRef(0); + const handleKey = direction => () => { + const position = currentPosition.current; + const nextPosition = direction === 'left' ? addOne(position) : position; + currentPosition.current = nextPosition; + }; + const moveLeft = { + handler: handleKey('left'), + }; + const moveRight = { + handler: handleKey('right'), + }; + return [moveLeft, moveRight]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useKeyCommand, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0f01d77d256806b48abb8e687a7af63aee209e5d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function Component(props) { + // test outlined functions with destructured parameters - the + // temporary for the destructured param must be promoted + return ( + <> + {props.items.map(({id, name}) => ( + + ))} + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{items: [{id: 1, name: 'one'}]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.items) { + t0 = props.items.map(_temp); + $[0] = props.items; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 = <>{t0}; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} +function _temp(t0) { + const { id, name } = t0; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ items: [{ id: 1, name: "one" }] }], +}; + +``` + +### Eval output +(kind: ok)
{"name":"one"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.js new file mode 100644 index 0000000000000000000000000000000000000000..d185c40f0621fd54fcea37d8153569862fbd7408 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-destructured-params.js @@ -0,0 +1,18 @@ +import {Stringify} from 'shared-runtime'; + +function Component(props) { + // test outlined functions with destructured parameters - the + // temporary for the destructured param must be promoted + return ( + <> + {props.items.map(({id, name}) => ( + + ))} + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{items: [{id: 1, name: 'one'}]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-in-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-in-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..59aed5cdfb57719369bc07559bc036b8534cf673 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-in-iife.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +function Component() { + return (function () { + function Inner() { + return
null} />; + } + 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")) { + const Inner = function Inner() { + return
; + }; + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() { + return null; +} + +``` + +### 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/outlined-function-in-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-in-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..2d2c53fad795a96450365772ace5a2c28704ee92 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-in-iife.js @@ -0,0 +1,8 @@ +function Component() { + return (function () { + function Inner() { + return
null} />; + } + return ; + })(); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-param-shadowing.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-param-shadowing.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..44b997797dd55ebb1d5c2a1a0d28f871806a0502 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-param-shadowing.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +function Component(props) { + const {value} = props; + const items = props.list.filter(value => value > 0); + return ( +
+ {items.length} + {value} +
+ ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(5); + const { value } = props; + let t0; + if ($[0] !== props.list) { + t0 = props.list.filter(_temp); + $[0] = props.list; + $[1] = t0; + } else { + t0 = $[1]; + } + const items = t0; + let t1; + if ($[2] !== items.length || $[3] !== value) { + t1 = ( +
+ {items.length} + {value} +
+ ); + $[2] = items.length; + $[3] = value; + $[4] = t1; + } else { + t1 = $[4]; + } + return t1; +} +function _temp(value_0) { + return value_0 > 0; +} + +``` + +### 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/outlined-function-param-shadowing.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-param-shadowing.js new file mode 100644 index 0000000000000000000000000000000000000000..86a5fd39cce192522431b259db3826906f97ffde --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-function-param-shadowing.js @@ -0,0 +1,10 @@ +function Component(props) { + const {value} = props; + const items = props.list.filter(value => value > 0); + return ( +
+ {items.length} + {value} +
+ ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..dec1928cf89c2778d9165e2bacf473ab409d7c24 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function Component(props) { + return ( +
+ {props.items.map(item => ( + + ))} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{items: [{id: 1, name: 'one'}]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.items) { + t0 = props.items.map(_temp); + $[0] = props.items; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 =
{t0}
; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} +function _temp(item) { + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ items: [{ id: 1, name: "one" }] }], +}; + +``` + +### Eval output +(kind: ok)
{"item":"one"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.js new file mode 100644 index 0000000000000000000000000000000000000000..71c93afe2387506ee325a665f7be66ff1651888f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlined-helper.js @@ -0,0 +1,16 @@ +import {Stringify} from 'shared-runtime'; + +function Component(props) { + return ( +
+ {props.items.map(item => ( + + ))} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{items: [{id: 1, name: 'one'}]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d97c06481c2e18830110badfe53a5cba48c7d8fb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.expect.md @@ -0,0 +1,72 @@ + +## Input + +```javascript +const Component2 = props => { + return ( +
    + {props.items.map(item => ( +
  • {item.name}
  • + ))} +
+ ); +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component2, + params: [ + { + items: [ + {id: 2, name: 'foo'}, + {id: 3, name: 'bar'}, + ], + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +const Component2 = (props) => { + const $ = _c(4); + let t0; + if ($[0] !== props.items) { + t0 = props.items.map(_temp); + $[0] = props.items; + $[1] = t0; + } else { + t0 = $[1]; + } + let t1; + if ($[2] !== t0) { + t1 =
    {t0}
; + $[2] = t0; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component2, + params: [ + { + items: [ + { id: 2, name: "foo" }, + { id: 3, name: "bar" }, + ], + }, + ], +}; +function _temp(item) { + return
  • {item.name}
  • ; +} + +``` + +### Eval output +(kind: ok)
    • foo
    • bar
    \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..3a6ac59d40973c91e7fa29e83900b3f8c0f1f020 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-func-expr.js @@ -0,0 +1,21 @@ +const Component2 = props => { + return ( +
      + {props.items.map(item => ( +
    • {item.name}
    • + ))} +
    + ); +}; + +export const FIXTURE_ENTRYPOINT = { + fn: Component2, + params: [ + { + items: [ + {id: 2, name: 'foo'}, + {id: 3, name: 'bar'}, + ], + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..11d4646c6553870325e09208197d868c94f1c029 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.expect.md @@ -0,0 +1,90 @@ + +## Input + +```javascript +function Component(props) { + return ; +} + +const View = React.memo(({items}) => { + return ( +
      + {items.map(item => ( +
    • {item.name}
    • + ))} +
    + ); +}); + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + {id: 2, name: 'foo'}, + {id: 3, name: 'bar'}, + ], + }, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = ; + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +const View = React.memo((t0) => { + const $ = _c(4); + const { items } = t0; + let t1; + if ($[0] !== items) { + t1 = items.map(_temp); + $[0] = items; + $[1] = t1; + } else { + t1 = $[1]; + } + let t2; + if ($[2] !== t1) { + t2 =
      {t1}
    ; + $[2] = t1; + $[3] = t2; + } else { + t2 = $[3]; + } + return t2; +}); + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + { id: 2, name: "foo" }, + { id: 3, name: "bar" }, + ], + }, + ], +}; +function _temp(item) { + return
  • {item.name}
  • ; +} + +``` + +### Eval output +(kind: ok)
    • foo
    • bar
    \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.js new file mode 100644 index 0000000000000000000000000000000000000000..cc106300a39f17599eddb99d6e7f3f8207b9fc74 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/outlining-in-react-memo.js @@ -0,0 +1,25 @@ +function Component(props) { + return ; +} + +const View = React.memo(({items}) => { + return ( +
      + {items.map(item => ( +
    • {item.name}
    • + ))} +
    + ); +}); + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [ + { + items: [ + {id: 2, name: 'foo'}, + {id: 3, name: 'bar'}, + ], + }, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved-by-terminal.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved-by-terminal.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..894dea7ce89b3b75e4755ba217e391adadfce2f7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved-by-terminal.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +function foo(a, b, c) { + const x = []; + const y = []; + + if (x) { + } + + y.push(a); + x.push(b); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b, c) { + const x = []; + const y = []; + + if (x) { + } + + y.push(a); + x.push(b); +} + +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/overlapping-scopes-interleaved-by-terminal.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved-by-terminal.js new file mode 100644 index 0000000000000000000000000000000000000000..de49b8b7597c76e90dbf2bb4641480403b0eeea9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved-by-terminal.js @@ -0,0 +1,16 @@ +function foo(a, b, c) { + const x = []; + const y = []; + + if (x) { + } + + y.push(a); + x.push(b); +} + +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/overlapping-scopes-interleaved.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6c425bcd88b8dbe165de85411c425f12b11148b4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function foo(a, b) { + let x = []; + let y = []; + x.push(a); + y.push(b); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b) { + const x = []; + const y = []; + x.push(a); + y.push(b); +} + +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/overlapping-scopes-interleaved.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved.js new file mode 100644 index 0000000000000000000000000000000000000000..b219006e2d1bcf62d4952e28359856588f83c7ba --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-interleaved.js @@ -0,0 +1,12 @@ +function foo(a, b) { + let x = []; + let y = []; + x.push(a); + y.push(b); +} + +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/overlapping-scopes-shadowed.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowed.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..de68c96a38c682f36225f232890e69fa27394613 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowed.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function foo(a, b) { + let x = []; + let y = []; + y.push(b); + x.push(a); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b) { + const x = []; + const y = []; + y.push(b); + x.push(a); +} + +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/overlapping-scopes-shadowed.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowed.js new file mode 100644 index 0000000000000000000000000000000000000000..6253f2fd07483744137266594341ec79a233bb4f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowed.js @@ -0,0 +1,12 @@ +function foo(a, b) { + let x = []; + let y = []; + y.push(b); + x.push(a); +} + +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/overlapping-scopes-shadowing-within-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowing-within-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a9d23720ae6ddcf295f333cefad67b0b9f838a16 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowing-within-block.expect.md @@ -0,0 +1,76 @@ + +## Input + +```javascript +function foo(a, b, c) { + let x = []; + if (a) { + let y = []; + if (b) { + y.push(c); + } + + x.push(
    {y}
    ); + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c) { + const $ = _c(9); + let x; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + x = []; + if (a) { + let y; + if ($[4] !== b || $[5] !== c) { + y = []; + if (b) { + y.push(c); + } + $[4] = b; + $[5] = c; + $[6] = y; + } else { + y = $[6]; + } + let t0; + if ($[7] !== y) { + t0 =
    {y}
    ; + $[7] = y; + $[8] = t0; + } else { + t0 = $[8]; + } + x.push(t0); + } + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + } else { + x = $[3]; + } + + return x; +} + +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/overlapping-scopes-shadowing-within-block.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowing-within-block.js new file mode 100644 index 0000000000000000000000000000000000000000..134aa185fa05666664b8908398acdfa7f79c725f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-shadowing-within-block.js @@ -0,0 +1,18 @@ +function foo(a, b, c) { + let x = []; + if (a) { + let y = []; + if (b) { + y.push(c); + } + + x.push(
    {y}
    ); + } + return x; +} + +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/overlapping-scopes-while.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-while.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7d14ce7e0d91aa29644a62f270b4d1ee3976ad12 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-while.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +function foo(a, b, c) { + let x = []; + let y = []; + while (c) { + y.push(b); + x.push(a); + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b, c) { + const x = []; + const y = []; + while (c) { + y.push(b); + x.push(a); + } +} + +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/overlapping-scopes-while.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-while.js new file mode 100644 index 0000000000000000000000000000000000000000..4f6b2939cc798057a721d45d1b672f53e1d8dd2a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-while.js @@ -0,0 +1,14 @@ +function foo(a, b, c) { + let x = []; + let y = []; + while (c) { + y.push(b); + x.push(a); + } +} + +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/overlapping-scopes-within-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-within-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1737be20de66c754f3ef2bbcb130110664511d8a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-within-block.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +function foo(a, b, c) { + let x = []; + if (a) { + let y = []; + if (b) { + y.push(c); + } + + x.push(y); + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b, c) { + const $ = _c(7); + let x; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + x = []; + if (a) { + let y; + if ($[4] !== b || $[5] !== c) { + y = []; + if (b) { + y.push(c); + } + $[4] = b; + $[5] = c; + $[6] = y; + } else { + y = $[6]; + } + + x.push(y); + } + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + } else { + x = $[3]; + } + + return x; +} + +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/overlapping-scopes-within-block.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-within-block.js new file mode 100644 index 0000000000000000000000000000000000000000..330eb3d4de84c9d197caeb22bb79f2eca6ea50ef --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/overlapping-scopes-within-block.js @@ -0,0 +1,18 @@ +function foo(a, b, c) { + let x = []; + if (a) { + let y = []; + if (b) { + y.push(c); + } + + x.push(y); + } + return x; +} + +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/panresponder-ref-in-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..92831f06594338f867829d72204d12c03616be59 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.expect.md @@ -0,0 +1,77 @@ + +## Input + +```javascript +// @flow +import {PanResponder, Stringify} from 'shared-runtime'; + +export default component Playground() { + const onDragEndRef = useRef(() => {}); + useEffect(() => { + onDragEndRef.current = () => { + console.log('drag ended'); + }; + }); + const panResponder = useMemo( + () => + PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }), + [] + ); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { PanResponder, Stringify } from "shared-runtime"; + +export default function Playground() { + const $ = _c(3); + const onDragEndRef = useRef(_temp); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = () => { + onDragEndRef.current = _temp2; + }; + $[0] = t0; + } else { + t0 = $[0]; + } + useEffect(t0); + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }); + $[1] = t1; + } else { + t1 = $[1]; + } + const panResponder = t1; + let t2; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t2 = ; + $[2] = t2; + } else { + t2 = $[2]; + } + return t2; +} +function _temp2() { + console.log("drag ended"); +} +function _temp() {} + +``` + +### 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/panresponder-ref-in-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..93614bfeec0b844a1c01b4cf0232e2da8a6a5325 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/panresponder-ref-in-callback.js @@ -0,0 +1,21 @@ +// @flow +import {PanResponder, Stringify} from 'shared-runtime'; + +export default component Playground() { + const onDragEndRef = useRef(() => {}); + useEffect(() => { + onDragEndRef.current = () => { + console.log('drag ended'); + }; + }); + const panResponder = useMemo( + () => + PanResponder.create({ + onPanResponderTerminate: () => { + onDragEndRef.current(); + }, + }), + [] + ); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0b37c7c204377674d60f8db8609859fdd07a4728 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md @@ -0,0 +1,83 @@ + +## Input + +```javascript +function Component(props) { + let x = []; + let y = null; + if (props.cond) { + x.push(props.a); + // oops no memo! + return x; + } else { + y = foo(); + if (props.b) { + return; + } + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, a: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(6); + let t0; + let y; + if ($[0] !== props.a || $[1] !== props.b || $[2] !== props.cond) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + const x = []; + if (props.cond) { + x.push(props.a); + t0 = x; + break bb0; + } else { + let t1; + if ($[5] === Symbol.for("react.memo_cache_sentinel")) { + t1 = foo(); + $[5] = t1; + } else { + t1 = $[5]; + } + y = t1; + if (props.b) { + t0 = undefined; + break bb0; + } + } + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.cond; + $[3] = t0; + $[4] = y; + } else { + t0 = $[3]; + y = $[4]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true, a: 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/partial-early-return-within-reactive-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..66d68242b8bb657519972ee5c8be480c3addce24 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.js @@ -0,0 +1,20 @@ +function Component(props) { + let x = []; + let y = null; + if (props.cond) { + x.push(props.a); + // oops no memo! + return x; + } else { + y = foo(); + if (props.b) { + return; + } + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, a: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern1a_type_to_primitive.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern1a_type_to_primitive.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9bcfaf2bf65b41ba2586fc7f6fac37bc023f768c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern1a_type_to_primitive.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +// Pattern 1a: shapeId null→generated + return Type→Primitive +// Arrow function with forEach callback +// Divergence: TS has shapeId:null, return:Type(22); Rust has shapeId:"", return:Primitive + +const getItemKeyMap = (): Map => { + const itemKeys = []; + itemKeys.forEach((itemKey: string) => {}); +}; + +``` + +## Code + +```javascript +// Pattern 1a: shapeId null→generated + return Type→Primitive +// Arrow function with forEach callback +// Divergence: TS has shapeId:null, return:Type(22); Rust has shapeId:"", return:Primitive + +const getItemKeyMap = () => { + const itemKeys = []; + itemKeys.forEach(_temp); +}; +function _temp(itemKey) {} + +``` + +### 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/pattern1a_type_to_primitive.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern1a_type_to_primitive.js new file mode 100644 index 0000000000000000000000000000000000000000..7129b7c797a963ae3164986c88ed84f5c99ccd95 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern1a_type_to_primitive.js @@ -0,0 +1,8 @@ +// Pattern 1a: shapeId null→generated + return Type→Primitive +// Arrow function with forEach callback +// Divergence: TS has shapeId:null, return:Type(22); Rust has shapeId:"", return:Primitive + +const getItemKeyMap = (): Map => { + const itemKeys = []; + itemKeys.forEach((itemKey: string) => {}); +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern4_bare_type.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern4_bare_type.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0a14af7d049a87b5949a669ee9be7fa30b2f7762 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern4_bare_type.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +// Pattern 4: Bare Type(N) vs Primitive (no shapeId difference) +// Object method with sort/filter chain +// Divergence: TS has type:Type(21); Rust has type:Primitive + +const formatNumber = function (x: number, y: number): number { + return Math.round((x - y) * 1000); +}; +const PerfHelper = { + formatMetrics(): Metrics { + const sortedLogs = logs.sort((entry1, entry2) => {}).filter(); + if (sortedLogs[0].begin < traceBeginTimeSec) { + } + sortedLogs.forEach((entry: TimeSliceEntry) => {}); + }, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Pattern 4: Bare Type(N) vs Primitive (no shapeId difference) +// Object method with sort/filter chain +// Divergence: TS has type:Type(21); Rust has type:Primitive + +const formatNumber = function (x, y) { + const $ = _c(3); + let t0; + if ($[0] !== x || $[1] !== y) { + t0 = Math.round((x - y) * 1000); + $[0] = x; + $[1] = y; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +}; + +const PerfHelper = { + formatMetrics(): Metrics { + const sortedLogs = logs.sort((entry1, entry2) => {}).filter(); + if (sortedLogs[0].begin < traceBeginTimeSec) { + } + sortedLogs.forEach((entry: TimeSliceEntry) => {}); + }, +}; + +``` + +### 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/pattern4_bare_type.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern4_bare_type.js new file mode 100644 index 0000000000000000000000000000000000000000..ada66e9f0972af3c03c66993b449f8288be96865 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/pattern4_bare_type.js @@ -0,0 +1,15 @@ +// Pattern 4: Bare Type(N) vs Primitive (no shapeId difference) +// Object method with sort/filter chain +// Divergence: TS has type:Type(21); Rust has type:Primitive + +const formatNumber = function (x: number, y: number): number { + return Math.round((x - y) * 1000); +}; +const PerfHelper = { + formatMetrics(): Metrics { + const sortedLogs = logs.sort((entry1, entry2) => {}).filter(); + if (sortedLogs[0].begin < traceBeginTimeSec) { + } + sortedLogs.forEach((entry: TimeSliceEntry) => {}); + }, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..36f3175a15a282ff340dd995da4cf3ea515dceea --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +import {arrayPush} from 'shared-runtime'; + +function Foo(cond) { + let x = null; + if (cond) { + x = []; + } else { + } + // Here, x = phi(x$null, x$[]) should receive a ValueKind of Mutable + arrayPush(x, 2); + + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{cond: true}], + sequentialRenders: [{cond: true}, {cond: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { arrayPush } from "shared-runtime"; + +function Foo(cond) { + const $ = _c(2); + let x; + if ($[0] !== cond) { + x = null; + if (cond) { + x = []; + } + + arrayPush(x, 2); + $[0] = cond; + $[1] = x; + } else { + x = $[1]; + } + + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ cond: true }], + sequentialRenders: [{ cond: true }, { cond: true }], +}; + +``` + +### Eval output +(kind: ok) [2] +[2] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.ts new file mode 100644 index 0000000000000000000000000000000000000000..3bf259216c7fbc9737b655dfef3e2a60a1944586 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-reference-effects.ts @@ -0,0 +1,19 @@ +import {arrayPush} from 'shared-runtime'; + +function Foo(cond) { + let x = null; + if (cond) { + x = []; + } else { + } + // Here, x = phi(x$null, x$[]) should receive a ValueKind of Mutable + arrayPush(x, 2); + + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{cond: true}], + sequentialRenders: [{cond: true}, {cond: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2bfb21bc6739fd3de7c9be036ca450f4e5e77b74 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.expect.md @@ -0,0 +1,116 @@ + +## Input + +```javascript +import {makeArray} from 'shared-runtime'; + +function Component(props) { + const x = {}; + let y; + if (props.cond) { + if (props.cond2) { + y = [props.value]; + } else { + y = [props.value2]; + } + } else { + y = []; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the array literals in the + // if/else branches + y.push(x); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, cond2: true, value: 42}], + sequentialRenders: [ + {cond: true, cond2: true, value: 3.14}, + {cond: true, cond2: true, value: 42}, + {cond: true, cond2: true, value: 3.14}, + {cond: true, cond2: false, value2: 3.14}, + {cond: true, cond2: false, value2: 42}, + {cond: true, cond2: false, value2: 3.14}, + {cond: false}, + {cond: false}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray } from "shared-runtime"; + +function Component(props) { + const $ = _c(6); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = {}; + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + let t1; + if ( + $[1] !== props.cond || + $[2] !== props.cond2 || + $[3] !== props.value || + $[4] !== props.value2 + ) { + let y; + if (props.cond) { + if (props.cond2) { + y = [props.value]; + } else { + y = [props.value2]; + } + } else { + y = []; + } + y.push(x); + t1 = [x, y]; + $[1] = props.cond; + $[2] = props.cond2; + $[3] = props.value; + $[4] = props.value2; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true, cond2: true, value: 42 }], + sequentialRenders: [ + { cond: true, cond2: true, value: 3.14 }, + { cond: true, cond2: true, value: 42 }, + { cond: true, cond2: true, value: 3.14 }, + { cond: true, cond2: false, value2: 3.14 }, + { cond: true, cond2: false, value2: 42 }, + { cond: true, cond2: false, value2: 3.14 }, + { cond: false }, + { cond: false }, + ], +}; + +``` + +### Eval output +(kind: ok) [{},[3.14,"[[ cyclic ref *1 ]]"]] +[{},[42,"[[ cyclic ref *1 ]]"]] +[{},[3.14,"[[ cyclic ref *1 ]]"]] +[{},[3.14,"[[ cyclic ref *1 ]]"]] +[{},[42,"[[ cyclic ref *1 ]]"]] +[{},[3.14,"[[ cyclic ref *1 ]]"]] +[{},["[[ cyclic ref *1 ]]"]] +[{},["[[ cyclic ref *1 ]]"]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.js new file mode 100644 index 0000000000000000000000000000000000000000..0a9aa39defea444d8fb08cf6f1d9a8c19ec988ad --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push-consecutive-phis.js @@ -0,0 +1,37 @@ +import {makeArray} from 'shared-runtime'; + +function Component(props) { + const x = {}; + let y; + if (props.cond) { + if (props.cond2) { + y = [props.value]; + } else { + y = [props.value2]; + } + } else { + y = []; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the array literals in the + // if/else branches + y.push(x); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, cond2: true, value: 42}], + sequentialRenders: [ + {cond: true, cond2: true, value: 3.14}, + {cond: true, cond2: true, value: 42}, + {cond: true, cond2: true, value: 3.14}, + {cond: true, cond2: false, value2: 3.14}, + {cond: true, cond2: false, value2: 42}, + {cond: true, cond2: false, value2: 3.14}, + {cond: false}, + {cond: false}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9fc02ca3b905f997fa3e2f7e3bfe755291facc2b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.expect.md @@ -0,0 +1,82 @@ + +## Input + +```javascript +function Component(props) { + const x = {}; + let y; + if (props.cond) { + y = [props.value]; + } else { + y = []; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the array literals in the + // if/else branches + y.push(x); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, value: 42}], + sequentialRenders: [ + {cond: true, value: 3.14}, + {cond: false, value: 3.14}, + {cond: true, value: 42}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = {}; + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + let t1; + if ($[1] !== props.cond || $[2] !== props.value) { + let y; + if (props.cond) { + y = [props.value]; + } else { + y = []; + } + y.push(x); + t1 = [x, y]; + $[1] = props.cond; + $[2] = props.value; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true, value: 42 }], + sequentialRenders: [ + { cond: true, value: 3.14 }, + { cond: false, value: 3.14 }, + { cond: true, value: 42 }, + ], +}; + +``` + +### Eval output +(kind: ok) [{},[3.14,"[[ cyclic ref *1 ]]"]] +[{},["[[ cyclic ref *1 ]]"]] +[{},[42,"[[ cyclic ref *1 ]]"]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js new file mode 100644 index 0000000000000000000000000000000000000000..732a1524cba2de5c81469b3c3d32a92d58b50f6b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-array-push.js @@ -0,0 +1,26 @@ +function Component(props) { + const x = {}; + let y; + if (props.cond) { + y = [props.value]; + } else { + y = []; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the array literals in the + // if/else branches + y.push(x); + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, value: 42}], + sequentialRenders: [ + {cond: true, value: 3.14}, + {cond: false, value: 3.14}, + {cond: true, value: 42}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f0a4ad368ff9b5468921235d0df8f647b52c5f4b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.expect.md @@ -0,0 +1,71 @@ + +## Input + +```javascript +// @debug +function Component(props) { + const x = {}; + let y; + if (props.cond) { + y = {}; + } else { + y = {a: props.a}; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the object literals in the + // if/else branches + y.x = x; + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: false, a: 'a!'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @debug +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = {}; + $[0] = t0; + } else { + t0 = $[0]; + } + const x = t0; + let t1; + if ($[1] !== props.a || $[2] !== props.cond) { + let y; + if (props.cond) { + y = {}; + } else { + y = { a: props.a }; + } + y.x = x; + t1 = [x, y]; + $[1] = props.a; + $[2] = props.cond; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: false, a: "a!" }], +}; + +``` + +### Eval output +(kind: ok) [{},{"a":"a!","x":"[[ cyclic ref *1 ]]"}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.js new file mode 100644 index 0000000000000000000000000000000000000000..ba7133ff808df4bba71528505ea5074aba08d62a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/phi-type-inference-property-store.js @@ -0,0 +1,22 @@ +// @debug +function Component(props) { + const x = {}; + let y; + if (props.cond) { + y = {}; + } else { + y = {a: props.a}; + } + // This should be inferred as ` y` s.t. `x` can still + // be independently memoized. *But* this also must properly + // extend the mutable range of the object literals in the + // if/else branches + y.x = x; + + return [x, y]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: false, a: 'a!'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-existing-memoization-guarantees/lambda-with-fbt-preserve-memoization.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-existing-memoization-guarantees/lambda-with-fbt-preserve-memoization.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bafbb5c5ef37ac698cac72469025b8ed997c5e56 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-existing-memoization-guarantees/lambda-with-fbt-preserve-memoization.expect.md @@ -0,0 +1,86 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees +import {fbt} from 'fbt'; + +function Component() { + const buttonLabel = () => { + if (!someCondition) { + return {'Purchase as a gift'}; + } else if ( + !iconOnly && + showPrice && + item?.current_gift_offer?.price?.formatted != null + ) { + return ( + + {'Gift | '} + + {item?.current_gift_offer?.price?.formatted} + + + ); + } else if (!iconOnly && !showPrice) { + return {'Gift'}; + } + }; + + return ( + +