diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..524dc8af35601c055669bfee05a8b47db529cc9b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.js @@ -0,0 +1,17 @@ +import {mutate} from 'shared-runtime'; +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + y.x = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..448078a9b5ac3da2c14b94300e67423cd4631886 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function component(a) { + let x = {a}; + let y = {}; + (function () { + let a = y; + a['x'] = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function component(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + + const a_0 = y; + a_0.x = x; + + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ["foo"], +}; + +``` + +### Eval output +(kind: ok) {"x":{"a":"foo"},"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..e212df190d329fb3a04f8049f08bbe61fcc990fb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate-iife.js @@ -0,0 +1,17 @@ +import {mutate} from 'shared-runtime'; + +function component(a) { + let x = {a}; + let y = {}; + (function () { + let a = y; + a['x'] = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d65e9cca9274009b243f1030b3e498496c4a9ce3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + let a = y; + a['x'] = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { a } = t0; + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + const f0 = function () { + const a_0 = y; + a_0.x = x; + }; + + f0(); + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2 }], + sequentialRenders: [{ a: 2 }, { a: 2 }, { a: 3 }], +}; + +``` + +### Eval output +(kind: ok) {"x":{"a":2},"wat0":"joe"} +{"x":{"a":2},"wat0":"joe"} +{"x":{"a":3},"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..b4f645846c5be065fa08a201026e0e17f4a56fbd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.js @@ -0,0 +1,19 @@ +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + let a = y; + a['x'] = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3acc6eac15dd22ce6b7a1030fa948f260895618f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +const {mutate} = require('shared-runtime'); + +function component(a) { + let x = {a}; + let y = {}; + (function () { + let a = y; + a.x = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +const { mutate } = require("shared-runtime"); + +function component(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + + const a_0 = y; + a_0.x = x; + + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ["foo"], +}; + +``` + +### Eval output +(kind: ok) {"x":{"a":"foo"},"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..fc716170f1d3cfbe09fd6c8686ee33356a40ef93 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate-iife.js @@ -0,0 +1,17 @@ +const {mutate} = require('shared-runtime'); + +function component(a) { + let x = {a}; + let y = {}; + (function () { + let a = y; + a.x = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..deb78c79e08edc0e53c304cf661a8909e96b6dd6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + let a = y; + a.x = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { a } = t0; + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + const f0 = function () { + const a_0 = y; + a_0.x = x; + }; + + f0(); + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2 }], + sequentialRenders: [{ a: 2 }, { a: 2 }, { a: 3 }], +}; + +``` + +### Eval output +(kind: ok) {"x":{"a":2},"wat0":"joe"} +{"x":{"a":2},"wat0":"joe"} +{"x":{"a":3},"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..a82df6b27fff88617d31e18a72d38c81035b56b1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.js @@ -0,0 +1,19 @@ +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + let a = y; + a.x = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6836544c5d337e1aa5e0be8ca124ca5d31f6bc47 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +function component(a, b) { + let y = {b}; + let z = {a}; + let x = function () { + z.a = 2; + y.b; + }; + x(); + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{a: 'val1', b: 'val2'}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a, b) { + const $ = _c(2); + let z; + if ($[0] !== a) { + z = { a }; + const x = function () { + z.a = 2; + }; + + x(); + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{ a: "val1", b: "val2" }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"a":2} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.js new file mode 100644 index 0000000000000000000000000000000000000000..91ef51bc7cac4acb6318734dec391ccf0d77fe0e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-2.js @@ -0,0 +1,16 @@ +function component(a, b) { + let y = {b}; + let z = {a}; + let x = function () { + z.a = 2; + y.b; + }; + x(); + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [{a: 'val1', b: 'val2'}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-3.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-3.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ca9986720c0919e3d90a61370a6e8fba59b2b0af --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-3.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +function component(a, b) { + let y = {b}; + let z = {a}; + let x = function () { + z.a = 2; + y.b; + }; + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a, b) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + + return z; +} + +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/capturing-func-mutate-3.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-3.js new file mode 100644 index 0000000000000000000000000000000000000000..2528d43d6f1401544332a62de4068a5780da806b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-3.js @@ -0,0 +1,15 @@ +function component(a, b) { + let y = {b}; + let z = {a}; + let x = function () { + z.a = 2; + y.b; + }; + return 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/capturing-func-mutate-nested.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..566b02e1719dabf1618cf3f2a39ae66c03c5299a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function component(a) { + let y = {b: {a}}; + let x = function () { + y.b.a = 2; + }; + x(); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + y = { b: { a } }; + const x = function () { + y.b.a = 2; + }; + + x(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +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/capturing-func-mutate-nested.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.js new file mode 100644 index 0000000000000000000000000000000000000000..e9230c6023569339fd1362d974333ef1ccaf2686 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.js @@ -0,0 +1,14 @@ +function component(a) { + let y = {b: {a}}; + let x = function () { + y.b.a = 2; + }; + x(); + return 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/capturing-func-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..03c0db7e971c9f7896b6d522b9ee355f92c5196c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md @@ -0,0 +1,76 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function Component({a, b}) { + let z = {a}; + let y = {b: {b}}; + let x = function () { + z.a = 2; + mutate(y.b); + }; + x(); + return [y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 4, b: 3}, + {a: 4, b: 5}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { a, b } = t0; + let t1; + if ($[0] !== a || $[1] !== b) { + const z = { a }; + const y = { b: { b } }; + const x = function () { + z.a = 2; + mutate(y.b); + }; + x(); + t1 = [y, z]; + $[0] = a; + $[1] = b; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 2, b: 3 }, + { a: 4, b: 3 }, + { a: 4, b: 5 }, + ], +}; + +``` + +### Eval output +(kind: ok) [{"b":{"b":3,"wat0":"joe"}},{"a":2}] +[{"b":{"b":3,"wat0":"joe"}},{"a":2}] +[{"b":{"b":3,"wat0":"joe"}},{"a":2}] +[{"b":{"b":5,"wat0":"joe"}},{"a":2}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..2ec7bcbe86cfd0a3d6856b1fb543d39674eb4ab2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-mutate.js @@ -0,0 +1,23 @@ +import {mutate} from 'shared-runtime'; + +function Component({a, b}) { + let z = {a}; + let y = {b: {b}}; + let x = function () { + z.a = 2; + mutate(y.b); + }; + x(); + return [y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 4, b: 3}, + {a: 4, b: 5}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..14bf94e770a6b29f9dfe9534d2fa2c77d60c9e28 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.expect.md @@ -0,0 +1,80 @@ + +## Input + +```javascript +function Component({a, b}) { + let z = {a}; + let y = {b}; + let x = function () { + z.a = 2; + return Math.max(y.b, 0); + }; + x(); + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 4, b: 3}, + {a: 4, b: 5}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(5); + const { a, b } = t0; + let z; + if ($[0] !== a || $[1] !== b) { + z = { a }; + let t1; + if ($[3] !== b) { + t1 = { b }; + $[3] = b; + $[4] = t1; + } else { + t1 = $[4]; + } + const y = t1; + const x = function () { + z.a = 2; + return Math.max(y.b, 0); + }; + + x(); + $[0] = a; + $[1] = b; + $[2] = z; + } else { + z = $[2]; + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 2, b: 3 }, + { a: 4, b: 3 }, + { a: 4, b: 5 }, + ], +}; + +``` + +### Eval output +(kind: ok) {"a":2} +{"a":2} +{"a":2} +{"a":2} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..8fe3bb3db52f610c87bc1a5dd0edec0814d98bd0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-no-mutate.js @@ -0,0 +1,21 @@ +function Component({a, b}) { + let z = {a}; + let y = {b}; + let x = function () { + z.a = 2; + return Math.max(y.b, 0); + }; + x(); + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 4, b: 3}, + {a: 4, b: 5}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a071dddba6b4c0b58d49194dc0fd62742d238f0e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +const {mutate} = require('shared-runtime'); + +function component(a) { + let x = {a}; + let y = {}; + (function () { + y = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +const { mutate } = require("shared-runtime"); + +function component(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + + y = x; + + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ["foo"], +}; + +``` + +### Eval output +(kind: ok) {"a":"foo","wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..e8b829c23c8322c5a0ca7104a5db325b358b2695 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias-iife.js @@ -0,0 +1,16 @@ +const {mutate} = require('shared-runtime'); + +function component(a) { + let x = {a}; + let y = {}; + (function () { + y = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['foo'], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0e2d77023996a8d98afc5e151481eddf51a582f8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + y = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { a } = t0; + let y; + if ($[0] !== a) { + const x = { a }; + y = {}; + const f0 = function () { + y = x; + }; + + f0(); + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2 }], + sequentialRenders: [{ a: 2 }, { a: 2 }, { a: 3 }], +}; + +``` + +### Eval output +(kind: ok) {"a":2,"wat0":"joe"} +{"a":2,"wat0":"joe"} +{"a":3,"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.js new file mode 100644 index 0000000000000000000000000000000000000000..22764fec72e6e637eb35c8f68a6e33548eabf2ff --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-func-simple-alias.js @@ -0,0 +1,18 @@ +import {mutate} from 'shared-runtime'; + +function Component({a}) { + let x = {a}; + let y = {}; + const f0 = function () { + y = x; + }; + f0(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-1.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-1.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6364d9b1647a0a5f9ac9152dc7786174c42cd837 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-1.expect.md @@ -0,0 +1,50 @@ + +## Input + +```javascript +function component(a) { + let z = {a}; + let x = function () { + console.log(z); + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + const z = { a }; + t0 = function () { + console.log(z); + }; + $[0] = a; + $[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/capturing-function-1.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-1.js new file mode 100644 index 0000000000000000000000000000000000000000..ed0f9961b13ea11b91c0f309e5d5c0c636b08688 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-1.js @@ -0,0 +1,13 @@ +function component(a) { + let z = {a}; + let x = function () { + console.log(z); + }; + 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/capturing-function-alias-computed-load-2-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..877cafa00dcf318fa99e7a563bfaf24271d503e8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0][1]) { + y = {}; + + y = x[0][1]; + $[2] = x[0][1]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [["val1", "val2"]], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..4c224e28415b153c5aa8e68d1ef75d8e0a935e33 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2-iife.js @@ -0,0 +1,15 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0][1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..935f86180983a3e4e1461aeb2355a625aa9d6222 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0][1]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = [a]; + y = {}; + const f0 = function () { + y = x[0][1]; + }; + + f0(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [["val1", "val2"]], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js new file mode 100644 index 0000000000000000000000000000000000000000..2c385ffd0db46e4f22bc3520ec6a169a2672db62 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-2.js @@ -0,0 +1,16 @@ +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0][1]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [['val1', 'val2']], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..036f70d0449e4a4822febf458299e74500b759be --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.expect.md @@ -0,0 +1,71 @@ + +## Input + +```javascript +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a, b) { + const $ = _c(6); + let t0; + if ($[0] !== a || $[1] !== b) { + t0 = [a, b]; + $[0] = a; + $[1] = b; + $[2] = t0; + } else { + t0 = $[2]; + } + const x = t0; + let y; + if ($[3] !== x[0][1] || $[4] !== x[1][0]) { + y = {}; + let t = {}; + + y = x[0][1]; + t = x[1][0]; + $[3] = x[0][1]; + $[4] = x[1][0]; + $[5] = y; + } else { + y = $[5]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +### Eval output +(kind: ok) 2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..1afc28a9922ecbbbbbce7fc81984b61866ac53bf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3-iife.js @@ -0,0 +1,19 @@ +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + (function () { + y = x[0][1]; + t = x[1][0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..e542d21cd5ef7a82510bdf3e72b79c5d132afc4e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + const f0 = function () { + y = x[0][1]; + t = x[1][0]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a, b) { + const $ = _c(3); + let y; + if ($[0] !== a || $[1] !== b) { + const x = [a, b]; + y = {}; + let t = {}; + const f0 = function () { + y = x[0][1]; + t = x[1][0]; + }; + + f0(); + $[0] = a; + $[1] = b; + $[2] = y; + } else { + y = $[2]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; + +``` + +### Eval output +(kind: ok) 2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js new file mode 100644 index 0000000000000000000000000000000000000000..3e92916e673a9c01419141a3ded4a154f22d6835 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-3.js @@ -0,0 +1,20 @@ +function bar(a, b) { + let x = [a, b]; + let y = {}; + let t = {}; + const f0 = function () { + y = x[0][1]; + t = x[1][0]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [ + [1, 2], + [2, 3], + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4019172e2aec94d8bbf551b07b952ef1027ceed0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0].a[1]) { + y = {}; + + y = x[0].a[1]; + $[2] = x[0].a[1]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{ a: ["val1", "val2"] }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..ca479a7458926317138707442e6993957206359a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4-iife.js @@ -0,0 +1,15 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0].a[1]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..97762abcc0a863554cc139c80485ad666a0fa198 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0].a[1]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = [a]; + y = {}; + const f0 = function () { + y = x[0].a[1]; + }; + + f0(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{ a: ["val1", "val2"] }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "val2" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js new file mode 100644 index 0000000000000000000000000000000000000000..861a4f3d14b2fa44409a3348423308833662230a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-4.js @@ -0,0 +1,16 @@ +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0].a[1]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: [{a: ['val1', 'val2']}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..57f809f84a94f14904e03b14292d756ce2994896 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = [a]; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + let y; + if ($[2] !== x[0]) { + y = {}; + + y = x[0]; + $[2] = x[0]; + $[3] = y; + } else { + y = $[3]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ["TodoAdd"], +}; + +``` + +### Eval output +(kind: ok) "TodoAdd" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.js new file mode 100644 index 0000000000000000000000000000000000000000..9a0c7c19aa700c1a1291dce9cb9e59cb49b81a92 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load-iife.js @@ -0,0 +1,14 @@ +function bar(a) { + let x = [a]; + let y = {}; + (function () { + y = x[0]; + })(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a3966035f80f87e16665777bf336b01fcf08b84b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function bar(a) { + const $ = _c(2); + let y; + if ($[0] !== a) { + const x = [a]; + y = {}; + const f0 = function () { + y = x[0]; + }; + + f0(); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + 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/capturing-function-alias-computed-load.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js new file mode 100644 index 0000000000000000000000000000000000000000..7abf7c093e57417a951ba95859e35da2f79504fa --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-alias-computed-load.js @@ -0,0 +1,16 @@ +function bar(a) { + let x = [a]; + let y = {}; + const f0 = function () { + y = x[0]; + }; + f0(); + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: bar, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f72e68eb9de98994c90c5a5d955fb7c98237dbf4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md @@ -0,0 +1,96 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function Component({a, b}) { + let z = {a}; + (function () { + mutate(z); + })(); + let y = z; + + { + // z is shadowed & renamed but the lambda is unaffected. + let z = {b}; + y = {y, z}; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 2, b: 4}, + {a: 3, b: 4}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function Component(t0) { + const $ = _c(7); + const { a, b } = t0; + let z; + if ($[0] !== a) { + z = { a }; + + mutate(z); + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + + let y = z; + let t1; + if ($[2] !== b) { + t1 = { b }; + $[2] = b; + $[3] = t1; + } else { + t1 = $[3]; + } + const z_0 = t1; + let t2; + if ($[4] !== y || $[5] !== z_0) { + t2 = { y, z: z_0 }; + $[4] = y; + $[5] = z_0; + $[6] = t2; + } else { + t2 = $[6]; + } + y = t2; + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 2, b: 3 }, + { a: 2, b: 4 }, + { a: 3, b: 4 }, + ], +}; + +``` + +### Eval output +(kind: ok) {"y":{"a":2,"wat0":"joe"},"z":{"b":3}} +{"y":{"a":2,"wat0":"joe"},"z":{"b":3}} +{"y":{"a":2,"wat0":"joe"},"z":{"b":4}} +{"y":{"a":3,"wat0":"joe"},"z":{"b":4}} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.js new file mode 100644 index 0000000000000000000000000000000000000000..b13e563ea820f829edb548a2ed2ef61dcc77c736 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.js @@ -0,0 +1,27 @@ +import {mutate} from 'shared-runtime'; + +function Component({a, b}) { + let z = {a}; + (function () { + mutate(z); + })(); + let y = z; + + { + // z is shadowed & renamed but the lambda is unaffected. + let z = {b}; + y = {y, z}; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 2, b: 4}, + {a: 3, b: 4}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..743d3ce9e890b8302eeda971b1043bf91ad725f6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +function useHook(a, b) { + let z = {a}; + let y = b; + let x = function () { + if (y) { + // we don't know for sure this mutates, so we should assume + // that there is no mutation so long as `x` isn't called + // during render + maybeMutate(z); + } + }; + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function useHook(a, b) { + const $ = _c(5); + let t0; + if ($[0] !== a) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + const y = b; + let t1; + if ($[2] !== y || $[3] !== z) { + t1 = function () { + if (y) { + maybeMutate(z); + } + }; + $[2] = y; + $[3] = z; + $[4] = t1; + } else { + t1 = $[4]; + } + const x = t1; + + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.js new file mode 100644 index 0000000000000000000000000000000000000000..04d6ca29e1fea1f0134087c7d549f21398a60a31 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-conditional-capture-mutate.js @@ -0,0 +1,13 @@ +function useHook(a, b) { + let z = {a}; + let y = b; + let x = function () { + if (y) { + // we don't know for sure this mutates, so we should assume + // that there is no mutation so long as `x` isn't called + // during render + maybeMutate(z); + } + }; + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a023d50236d83596c3941a9094375ff83689c5ed --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function component(a) { + let t = {a}; + function x() { + t.foo(); + } + x(t); + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let t; + if ($[0] !== a) { + t = { a }; + const x = function x() { + t.foo(); + }; + + x(t); + $[0] = a; + $[1] = t; + } else { + t = $[1]; + } + return t; +} + +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/capturing-function-decl.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-decl.js new file mode 100644 index 0000000000000000000000000000000000000000..f8d1e184b3c6fe516fd953e08157caa29cd06226 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-decl.js @@ -0,0 +1,14 @@ +function component(a) { + let t = {a}; + function x() { + t.foo(); + } + x(t); + return t; +} + +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/capturing-function-member-expr-arguments.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d9b3632a902c796742a76e9fe4187fa4f7b53904 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.expect.md @@ -0,0 +1,40 @@ + +## Input + +```javascript +function Foo(props) { + const onFoo = useCallback( + reason => { + log(props.router.location); + }, + [props.router.location] + ); + + return onFoo; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo(props) { + const $ = _c(2); + let t0; + if ($[0] !== props.router.location) { + t0 = (reason) => { + log(props.router.location); + }; + $[0] = props.router.location; + $[1] = t0; + } else { + t0 = $[1]; + } + const onFoo = t0; + + return onFoo; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js new file mode 100644 index 0000000000000000000000000000000000000000..ed8ddd804e47605845325d6e733b7d7dd79255e1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-arguments.js @@ -0,0 +1,10 @@ +function Foo(props) { + const onFoo = useCallback( + reason => { + log(props.router.location); + }, + [props.router.location] + ); + + return onFoo; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cab9c9a500b9c6c323caa1dc571d1ddd4e80af6d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.expect.md @@ -0,0 +1,61 @@ + +## Input + +```javascript +function component({mutator}) { + const poke = () => { + mutator.poke(); + }; + + const hide = () => { + mutator.user.hide(); + }; + + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(t0) { + const $ = _c(7); + const { mutator } = t0; + let t1; + if ($[0] !== mutator) { + t1 = () => { + mutator.poke(); + }; + $[0] = mutator; + $[1] = t1; + } else { + t1 = $[1]; + } + const poke = t1; + let t2; + if ($[2] !== mutator.user) { + t2 = () => { + mutator.user.hide(); + }; + $[2] = mutator.user; + $[3] = t2; + } else { + t2 = $[3]; + } + const hide = t2; + let t3; + if ($[4] !== hide || $[5] !== poke) { + t3 = ; + $[4] = hide; + $[5] = poke; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.js new file mode 100644 index 0000000000000000000000000000000000000000..f2c7e900e3ae038c06175f5a412697a94fde8967 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-member-expr-call.js @@ -0,0 +1,11 @@ +function component({mutator}) { + const poke = () => { + mutator.poke(); + }; + + const hide = () => { + mutator.user.hide(); + }; + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9d9b79ef257235067eeae83747a3fe34ac063e4d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.expect.md @@ -0,0 +1,74 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; + +function useHook({a, b}) { + let z = {a}; + { + let z = {b}; + (function () { + mutate(z); + })(); + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 2, b: 4}, + {a: 3, b: 4}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(2); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = { a }; + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const z = t1; + + const z_0 = { b }; + + mutate(z_0); + + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 2, b: 3 }, + { a: 2, b: 4 }, + { a: 3, b: 4 }, + ], +}; + +``` + +### Eval output +(kind: ok) {"a":2} +{"a":2} +{"a":2} +{"a":3} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.js new file mode 100644 index 0000000000000000000000000000000000000000..49595f19d7e6f40db9e36eb9bd5065803bd2ec21 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-renamed-ref.js @@ -0,0 +1,23 @@ +import {mutate} from 'shared-runtime'; + +function useHook({a, b}) { + let z = {a}; + { + let z = {b}; + (function () { + mutate(z); + })(); + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 2, b: 4}, + {a: 3, b: 4}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..31b80bcda3b2980db8a95f15be9625889fc08176 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; +function Component({a, b}) { + let z = {a}; + let p = () => {z}; + return p(); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1}], + sequentialRenders: [{a: 1}, {a: 1}, {a: 2}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; +function Component(t0) { + const $ = _c(2); + const { a } = t0; + let t1; + if ($[0] !== a) { + const z = { a }; + const p = () => {z}; + t1 = p(); + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 1 }], + sequentialRenders: [{ a: 1 }, { a: 1 }, { a: 2 }], +}; + +``` + +### Eval output +(kind: ok)
{"children":{"a":1}}
+
{"children":{"a":1}}
+
{"children":{"a":2}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.js new file mode 100644 index 0000000000000000000000000000000000000000..d5a4bb842f96ceea745ef75cd697b86ec41d62c2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-runs-inference.js @@ -0,0 +1,11 @@ +import {Stringify} from 'shared-runtime'; +function Component({a, b}) { + let z = {a}; + let p = () => {z}; + return p(); +} +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1}], + sequentialRenders: [{a: 1}, {a: 1}, {a: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-shadow-captured.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-shadow-captured.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d409bd32965f901f1e91b5f0af36bda22ee9248f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-shadow-captured.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {mutate, Stringify} from 'shared-runtime'; +function Component({a}) { + let z = {a}; + let x = function () { + let z; + mutate(z); + return z; + }; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1}], + sequentialRenders: [{a: 1}, {a: 1}, {a: 2}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate, Stringify } from "shared-runtime"; +function Component(t0) { + const $ = _c(1); + + const x = _temp; + let t1; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t1 = ; + $[0] = t1; + } else { + t1 = $[0]; + } + return t1; +} +function _temp() { + let z_0; + mutate(z_0); + return z_0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 1 }], + sequentialRenders: [{ a: 1 }, { a: 1 }, { a: 2 }], +}; + +``` + +### Eval output +(kind: ok)
{"fn":{"kind":"Function"},"shouldInvokeFns":true}
+
{"fn":{"kind":"Function"},"shouldInvokeFns":true}
+
{"fn":{"kind":"Function"},"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/capturing-function-shadow-captured.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-shadow-captured.js new file mode 100644 index 0000000000000000000000000000000000000000..a0ce67e4d00b653805dd16bb5c6376f6aa3d0550 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-shadow-captured.js @@ -0,0 +1,16 @@ +import {mutate, Stringify} from 'shared-runtime'; +function Component({a}) { + let z = {a}; + let x = function () { + let z; + mutate(z); + return z; + }; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1}], + sequentialRenders: [{a: 1}, {a: 1}, {a: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..89e69d0790bde975c9bc7baa7ec3b11670711a88 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +function StoreLandingUnseenGiftModalContainer(a) { + const giftsSeen = {a}; + return (gift => (gift.id ? giftsSeen[gift.id] : false))(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: StoreLandingUnseenGiftModalContainer, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function StoreLandingUnseenGiftModalContainer(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + const giftsSeen = { a }; + t0 = ((gift) => (gift.id ? giftsSeen[gift.id] : false))(); + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: StoreLandingUnseenGiftModalContainer, + 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/capturing-function-skip-computed-path.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.js new file mode 100644 index 0000000000000000000000000000000000000000..48332e6e01d64325bf06b0c78ff8c52046365718 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-skip-computed-path.js @@ -0,0 +1,10 @@ +function StoreLandingUnseenGiftModalContainer(a) { + const giftsSeen = {a}; + return (gift => (gift.id ? giftsSeen[gift.id] : false))(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: StoreLandingUnseenGiftModalContainer, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b0bbce8d87e3bd507794c0ce3c19dd8b36c6131a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-within-block.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function component(a) { + let z = {a}; + let x; + { + x = function () { + console.log(z); + }; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + let x; + let t1; + if ($[2] !== z) { + t1 = function () { + console.log(z); + }; + $[2] = z; + $[3] = t1; + } else { + t1 = $[3]; + } + x = t1; + + 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/capturing-function-within-block.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-within-block.js new file mode 100644 index 0000000000000000000000000000000000000000..72510bf9c37a7f265bb2e9c372f9aee52c2c531d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-function-within-block.js @@ -0,0 +1,16 @@ +function component(a) { + let z = {a}; + let x; + { + x = function () { + console.log(z); + }; + } + 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/capturing-member-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-member-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..943533a63d1226e2f3ce2f09af0b1106d8ce3303 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-member-expr.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +function component(a) { + let z = {a}; + let x = function () { + console.log(z.a); + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + let t1; + if ($[2] !== z.a) { + t1 = function () { + console.log(z.a); + }; + $[2] = z.a; + $[3] = t1; + } else { + t1 = $[3]; + } + const x = t1; + + 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/capturing-member-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-member-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..2305db93f9b71ea50595701a7d0608b9acfcf44c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-member-expr.js @@ -0,0 +1,13 @@ +function component(a) { + let z = {a}; + let x = function () { + console.log(z.a); + }; + 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/capturing-nested-member-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..34a663aac4a2013d3d9b35c847d338939712f359 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-call.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +function component(a) { + let z = {a: {a}}; + let x = function () { + z.a.a(); + }; + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + t0 = { a: { a } }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + + return z; +} + +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/capturing-nested-member-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-call.js new file mode 100644 index 0000000000000000000000000000000000000000..a76a4b88a97a8784a3a2c5b358fdd8dd8544fa48 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-call.js @@ -0,0 +1,13 @@ +function component(a) { + let z = {a: {a}}; + let x = function () { + z.a.a(); + }; + return 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/capturing-nested-member-expr-in-nested-func.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9e95af2b0d5900f0339f6ce78ba1836e9b27d6c1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function component(a) { + let z = {a: {a}}; + let x = function () { + (function () { + console.log(z.a.a); + })(); + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = { a: { a } }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + let t1; + if ($[2] !== z.a.a) { + t1 = function () { + (function () { + console.log(z.a.a); + })(); + }; + $[2] = z.a.a; + $[3] = t1; + } else { + t1 = $[3]; + } + const x = t1; + + 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/capturing-nested-member-expr-in-nested-func.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.js new file mode 100644 index 0000000000000000000000000000000000000000..c814d21d6afd058f756e493cfec26f0f15d0492e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr-in-nested-func.js @@ -0,0 +1,15 @@ +function component(a) { + let z = {a: {a}}; + let x = function () { + (function () { + console.log(z.a.a); + })(); + }; + 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/capturing-nested-member-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..13c122750e9bb533c1d4e9e1bc18d705133cfa15 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +function component(a) { + let z = {a: {a}}; + let x = function () { + console.log(z.a.a); + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(4); + let t0; + if ($[0] !== a) { + t0 = { a: { a } }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + let t1; + if ($[2] !== z.a.a) { + t1 = function () { + console.log(z.a.a); + }; + $[2] = z.a.a; + $[3] = t1; + } else { + t1 = $[3]; + } + const x = t1; + + 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/capturing-nested-member-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..a9cdaa155ee87f6c8a8cdf3dfc02e62047b06607 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-nested-member-expr.js @@ -0,0 +1,13 @@ +function component(a) { + let z = {a: {a}}; + let x = function () { + console.log(z.a.a); + }; + 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/capturing-reference-changes-type.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-reference-changes-type.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f5d4b94ae5410e1025f074080462749cf58b8d1d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-reference-changes-type.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; +function Component({a}) { + let x = {a}; + let y = 1; + (function () { + y = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; +function Component(t0) { + const $ = _c(2); + const { a } = t0; + let y; + if ($[0] !== a) { + const x = { a }; + y = 1; + + y = x; + + mutate(y); + $[0] = a; + $[1] = y; + } else { + y = $[1]; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 2 }], + sequentialRenders: [{ a: 2 }, { a: 2 }, { a: 3 }], +}; + +``` + +### Eval output +(kind: ok) {"a":2,"wat0":"joe"} +{"a":2,"wat0":"joe"} +{"a":3,"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-reference-changes-type.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-reference-changes-type.js new file mode 100644 index 0000000000000000000000000000000000000000..2b4b1b47ecc31c04ab3ba34336252367250d1421 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-reference-changes-type.js @@ -0,0 +1,16 @@ +import {mutate} from 'shared-runtime'; +function Component({a}) { + let x = {a}; + let y = 1; + (function () { + y = x; + })(); + mutate(y); + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 2}], + sequentialRenders: [{a: 2}, {a: 2}, {a: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-block.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-block.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..be0b64c122ac5d1a1778b74688d4e763e7fdcf49 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-block.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +function component(a) { + let z = {a}; + let x = function () { + { + console.log(z); + } + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + const z = { a }; + t0 = function () { + console.log(z); + }; + $[0] = a; + $[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/capturing-variable-in-nested-block.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-block.js new file mode 100644 index 0000000000000000000000000000000000000000..0b39d15c848aa4b0b011a151a70f594f202d2b02 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-block.js @@ -0,0 +1,15 @@ +function component(a) { + let z = {a}; + let x = function () { + { + console.log(z); + } + }; + 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/capturing-variable-in-nested-function.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-function.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cf365b495681bf7e16ef5abd0e5205f33d9a5ba3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-function.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +function component(a) { + let z = {a}; + let x = function () { + (function () { + console.log(z); + })(); + }; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + const z = { a }; + t0 = function () { + (function () { + console.log(z); + })(); + }; + $[0] = a; + $[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/capturing-variable-in-nested-function.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-function.js new file mode 100644 index 0000000000000000000000000000000000000000..63a5bf3d17c61b0bf39505109fc4d75e1eab373d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/capturing-variable-in-nested-function.js @@ -0,0 +1,15 @@ +function component(a) { + let z = {a}; + let x = function () { + (function () { + console.log(z); + })(); + }; + 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/chained-assignment-context-variable.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..796b75b2ddbe1204edcf7bbd4b44b70ed8a2a07a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +import {makeArray} from 'shared-runtime'; + +function Component() { + let x, + y = (x = {}); + const foo = () => { + x = makeArray(); + }; + foo(); + return [y, x]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray } from "shared-runtime"; + +function Component() { + const $ = _c(3); + let x; + let y; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + y = x = {}; + const foo = () => { + x = makeArray(); + }; + + foo(); + $[0] = x; + $[1] = y; + } else { + x = $[0]; + y = $[1]; + } + let t0; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + t0 = [y, x]; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) [{},[]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.js new file mode 100644 index 0000000000000000000000000000000000000000..7d1ce18444866b5e105d2343f8821b39a6c5141e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-context-variable.js @@ -0,0 +1,16 @@ +import {makeArray} from 'shared-runtime'; + +function Component() { + let x, + y = (x = {}); + const foo = () => { + x = makeArray(); + }; + foo(); + return [y, x]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d3ab894d2af1da181d46456120b180773a855ee0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function foo() { + const x = {x: 0}; + const y = {z: 0}; + const z = {z: 0}; + x.x += y.y *= 1; + z.z += y.y *= x.x &= 3; + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo() { + const $ = _c(1); + let z; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const x = { x: 0 }; + const y = { z: 0 }; + z = { z: 0 }; + x.x = x.x + (y.y = y.y * 1); + z.z = z.z + (y.y = y.y * (x.x = x.x & 3)); + $[0] = z; + } else { + z = $[0]; + } + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"z":null} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.js new file mode 100644 index 0000000000000000000000000000000000000000..1fb9bbef3c26dba32681d3d2c44a941b39bf67b9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/chained-assignment-expressions.js @@ -0,0 +1,14 @@ +function foo() { + const x = {x: 0}; + const y = {z: 0}; + const z = {z: 0}; + x.x += y.y *= 1; + z.z += y.y *= x.x &= 3; + return z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..175a590be5bd79b378f8159e6dbb2721de0340ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +// @expectNothingCompiled @compilationMode:"infer" +class Component { + _renderMessage = () => { + const Message = () => { + const message = this.state.message; + return
{message}
; + }; + return ; + }; + + render() { + return this._renderMessage(); + } +} + +``` + +## Code + +```javascript +// @expectNothingCompiled @compilationMode:"infer" +class Component { + _renderMessage = () => { + const Message = () => { + const message = this.state.message; + return
{message}
; + }; + return ; + }; + + render() { + return this._renderMessage(); + } +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.js new file mode 100644 index 0000000000000000000000000000000000000000..324945eb2a9dce523748d6568962a77f076adb41 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.js @@ -0,0 +1,14 @@ +// @expectNothingCompiled @compilationMode:"infer" +class Component { + _renderMessage = () => { + const Message = () => { + const message = this.state.message; + return
{message}
; + }; + return ; + }; + + render() { + return this._renderMessage(); + } +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0778152f341d6a76ca229af5471be5d01a92987e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + let w = {}; + return makeArray( + (w = 42), + w, + (function foo() { + w = 999; + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray, print } from "shared-runtime"; + +function useTest() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + let w = {}; + const t1 = (w = 42); + const t2 = w; + + w = 999; + t0 = makeArray(t1, t2, 2); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +### Eval output +(kind: ok) [42,42,2] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.ts new file mode 100644 index 0000000000000000000000000000000000000000..8dd93a8482b9252d1d6f5a2eba6e5118116601bf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-reassign.ts @@ -0,0 +1,18 @@ +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + let w = {}; + return makeArray( + (w = 42), + w, + (function foo() { + w = 999; + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7310e0fcd26afc5ce850c940410e30cd472bf2c1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.expect.md @@ -0,0 +1,57 @@ + +## Input + +```javascript +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + let w = {}; + return makeArray( + (w.x = 42), + w.x, + (function foo() { + w.x = 999; + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray, print } from "shared-runtime"; + +function useTest() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const w = {}; + const t1 = (w.x = 42); + const t2 = w.x; + + w.x = 999; + t0 = makeArray(t1, t2, 2); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +### Eval output +(kind: ok) [42,42,2] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b9f10856e48f916c895c987c6b18c364e45a54f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife-storeprop.ts @@ -0,0 +1,18 @@ +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + let w = {}; + return makeArray( + (w.x = 42), + w.x, + (function foo() { + w.x = 999; + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..70b23c70c093d4936a782dd00892a694f889ea40 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + return makeArray( + print(1), + (function foo() { + print(2); + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray, print } from "shared-runtime"; + +function useTest() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const t1 = print(1); + + print(2); + t0 = makeArray(t1, 2); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; + +``` + +### Eval output +(kind: ok) [null,2] +logs: [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/codegen-inline-iife.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife.ts new file mode 100644 index 0000000000000000000000000000000000000000..52b1e8a9278e17afbe51b443d281d4bff492d62e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-inline-iife.ts @@ -0,0 +1,16 @@ +import {makeArray, print} from 'shared-runtime'; + +function useTest() { + return makeArray( + print(1), + (function foo() { + print(2); + return 2; + })(), + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: useTest, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..edabebecfc4a00d64f6bb5c3536785b5f7bc78ab --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +// @enableEmitInstrumentForget @compilationMode:"annotation" + +function Bar(props) { + 'use forget'; + return
{props.bar}
; +} + +function NoForget(props) { + return {props.noForget}; +} + +function Foo(props) { + 'use forget'; + return {props.bar}; +} + +``` + +## Code + +```javascript +import { shouldInstrument, useRenderCounter } from "react-compiler-runtime"; +import { c as _c } from "react/compiler-runtime"; // @enableEmitInstrumentForget @compilationMode:"annotation" + +function Bar(props) { + "use forget"; + if (DEV && shouldInstrument) + useRenderCounter("Bar", "/codegen-instrument-forget-test.ts"); + const $ = _c(2); + let t0; + if ($[0] !== props.bar) { + t0 =
{props.bar}
; + $[0] = props.bar; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +function NoForget(props) { + return {props.noForget}; +} + +function Foo(props) { + "use forget"; + if (DEV && shouldInstrument) + useRenderCounter("Foo", "/codegen-instrument-forget-test.ts"); + const $ = _c(2); + let t0; + if ($[0] !== props.bar) { + t0 = {props.bar}; + $[0] = props.bar; + $[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/codegen-instrument-forget-test.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.js new file mode 100644 index 0000000000000000000000000000000000000000..cc5e4710548258b8709c2c1d4701bf6c5ea10120 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.js @@ -0,0 +1,15 @@ +// @enableEmitInstrumentForget @compilationMode:"annotation" + +function Bar(props) { + 'use forget'; + return
{props.bar}
; +} + +function NoForget(props) { + return {props.noForget}; +} + +function Foo(props) { + 'use forget'; + return {props.bar}; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0d18fbbd5fcd1eabaf3f2cdd03b3428bae0c899b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +// Constant propagation can produce NaN/Infinity from arithmetic. +// These must be emitted as Identifier("NaN")/Identifier("Infinity"), +// not NumericLiteral(NaN) which serializes to null in JSON. + +function Component({x}) { + const nan = 0 / 0; + const inf = 1 / 0; + const negInf = -1 / 0; + return ( +
+ {x ? nan : inf} + {negInf} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Constant propagation can produce NaN/Infinity from arithmetic. +// These must be emitted as Identifier("NaN")/Identifier("Infinity"), +// not NumericLiteral(NaN) which serializes to null in JSON. + +function Component(t0) { + const $ = _c(2); + const { x } = t0; + + const t1 = x ? NaN : Infinity; + let t2; + if ($[0] !== t1) { + t2 = ( +
+ {t1} + {-Infinity} +
+ ); + $[0] = t1; + $[1] = t2; + } else { + t2 = $[1]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: true }], +}; + +``` + +### Eval output +(kind: ok)
NaN-Infinity
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.js new file mode 100644 index 0000000000000000000000000000000000000000..9d366206d769ce93bdb225bb90e057bf9df10743 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-nan-infinity-as-identifiers.js @@ -0,0 +1,20 @@ +// Constant propagation can produce NaN/Infinity from arithmetic. +// These must be emitted as Identifier("NaN")/Identifier("Infinity"), +// not NumericLiteral(NaN) which serializes to null in JSON. + +function Component({x}) { + const nan = 0 / 0; + const inf = 1 / 0; + const negInf = -1 / 0; + return ( +
+ {x ? nan : inf} + {negInf} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bc8e8caa924679afb10846861310407d0ed5d73b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +function Component({dataSource, viewRenderer}) { + return ( + { + function useConfig() { + return {dataSource, viewRenderer}; + } + return useConfig; + })(), + }, + ]} + /> + ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(5); + const { dataSource, viewRenderer } = t0; + let t1; + if ($[0] !== dataSource || $[1] !== viewRenderer) { + t1 = function useConfig() { + return { dataSource, viewRenderer }; + }; + $[0] = dataSource; + $[1] = viewRenderer; + $[2] = t1; + } else { + t1 = $[2]; + } + const useConfig = t1; + + const t2 = useConfig; + let t3; + if ($[3] !== t2) { + t3 = ; + $[3] = t2; + $[4] = t3; + } else { + t3 = $[4]; + } + return t3; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.js new file mode 100644 index 0000000000000000000000000000000000000000..7c1e6b90996b0dd0b86dc90fa72a61f0ba3a15d2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/compiled-function-nested-in-jsx.js @@ -0,0 +1,16 @@ +function Component({dataSource, viewRenderer}) { + return ( + { + function useConfig() { + return {dataSource, viewRenderer}; + } + return useConfig; + })(), + }, + ]} + /> + ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/complex-while.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/complex-while.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..22d4634541d26634b24afcd1dfc28c3241077986 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/complex-while.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +function foo(a, b, c) { + label: if (a) { + while (b) { + if (c) { + break label; + } + } + } + return c; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b, c) { + bb0: if (a) { + while (b) { + if (c) { + break bb0; + } + } + } + + return c; +} + +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/complex-while.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/complex-while.js new file mode 100644 index 0000000000000000000000000000000000000000..c9194a39b4a167a261915052e1f3405ae5f5df29 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/complex-while.js @@ -0,0 +1,16 @@ +function foo(a, b, c) { + label: if (a) { + while (b) { + if (c) { + break label; + } + } + } + return c; +} + +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/component-declaration-basic.flow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-declaration-basic.flow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..15cba5b29a7c57cafb9141eff6e7d14095703e00 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-declaration-basic.flow.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +// @flow @compilationMode:"infer" +export default component Foo(bar: number) { + return ; +} + +component Bar(bar: number) { + return
{bar}
; +} + +function shouldNotCompile() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{bar: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +export default function Foo(t0) { + const $ = _c(2); + const { bar } = t0; + let t1; + if ($[0] !== bar) { + t1 = ; + $[0] = bar; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +function Bar(t0) { + const $ = _c(2); + const { bar } = t0; + let t1; + if ($[0] !== bar) { + t1 =
{bar}
; + $[0] = bar; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +function shouldNotCompile() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{ bar: 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/component-declaration-basic.flow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-declaration-basic.flow.js new file mode 100644 index 0000000000000000000000000000000000000000..04d419c7ce0bc8a446e981a3c48dd84c1520e972 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-declaration-basic.flow.js @@ -0,0 +1,15 @@ +// @flow @compilationMode:"infer" +export default component Foo(bar: number) { + return ; +} + +component Bar(bar: number) { + return
{bar}
; +} + +function shouldNotCompile() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{bar: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..122c083004fe667f9a8bb9b3338d9815bd343e2a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.expect.md @@ -0,0 +1,83 @@ + +## Input + +```javascript +// @flow @compilationMode:"syntax" +// Component declarations nested inside object method bodies should be found +export const examples = [ + { + title: 'Example 1', + render(): React.MixedElement { + component Demo1() { + const x = useFoo(); + return
{x}
; + } + return ; + }, + }, + { + title: 'Example 2', + render(): React.MixedElement { + component Demo2() { + const y = useBar(); + return {y}; + } + return ; + }, + }, +]; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; + +export const examples = [ + { + title: "Example 1", + render(): React.MixedElement { + function Demo1() { + const $ = _c(2); + const x = useFoo(); + let t0; + if ($[0] !== x) { + t0 =
{x}
; + $[0] = x; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; + } + + return ; + }, + }, + { + title: "Example 2", + render(): React.MixedElement { + function Demo2() { + const $ = _c(2); + const y = useBar(); + let t0; + if ($[0] !== y) { + t0 = {y}; + $[0] = y; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; + } + + return ; + }, + }, +]; + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.js new file mode 100644 index 0000000000000000000000000000000000000000..53b42f74a93b8b7c827a5679952fa7c282abb201 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-in-object-method-body.flow.js @@ -0,0 +1,24 @@ +// @flow @compilationMode:"syntax" +// Component declarations nested inside object method bodies should be found +export const examples = [ + { + title: 'Example 1', + render(): React.MixedElement { + component Demo1() { + const x = useFoo(); + return
{x}
; + } + return ; + }, + }, + { + title: 'Example 2', + render(): React.MixedElement { + component Demo2() { + const y = useBar(); + return {y}; + } + return ; + }, + }, +]; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fe3d17d120e08180a21cd7931c2665bd447cf7e1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; +function Component(props) { + const cb = (x, y, z) => x + y + z; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{id: 0}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; +function Component(props) { + const $ = _c(2); + const cb = _temp; + let t0; + if ($[0] !== props.id) { + t0 = ; + $[0] = props.id; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} +function _temp(x, y, z) { + return x + y + z; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ id: 0 }], +}; + +``` + +### Eval output +(kind: ok)
{"cb":"[[ function params=3 ]]","id":0}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.tsx new file mode 100644 index 0000000000000000000000000000000000000000..a65614f09a48c86d1f4bf0d0ee328277cc682d7d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component-inner-function-with-many-args.tsx @@ -0,0 +1,11 @@ +import {Stringify} from 'shared-runtime'; +function Component(props) { + const cb = (x, y, z) => x + y + z; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{id: 0}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c6037fd2bb3909e99e2f62ead6e484810f935357 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component.expect.md @@ -0,0 +1,105 @@ + +## Input + +```javascript +function Component(props) { + const items = props.items; + const maxItems = props.maxItems; + + const renderedItems = []; + const seen = new Set(); + const max = Math.max(0, maxItems); + for (let i = 0; i < items.length; i += 1) { + const item = items.at(i); + if (item == null || seen.has(item)) { + continue; + } + seen.add(item); + renderedItems.push(
{item}
); + if (renderedItems.length >= max) { + break; + } + } + const count = renderedItems.length; + return ( +
+

{count} Items

+ {renderedItems} +
+ ); +} + +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(8); + const items = props.items; + const maxItems = props.maxItems; + let renderedItems; + if ($[0] !== items || $[1] !== maxItems) { + renderedItems = []; + const seen = new Set(); + const max = Math.max(0, maxItems); + for (let i = 0; i < items.length; i = i + 1, i) { + const item = items.at(i); + if (item == null || seen.has(item)) { + continue; + } + + seen.add(item); + renderedItems.push(
{item}
); + if (renderedItems.length >= max) { + break; + } + } + $[0] = items; + $[1] = maxItems; + $[2] = renderedItems; + } else { + renderedItems = $[2]; + } + + const count = renderedItems.length; + let t0; + if ($[3] !== count) { + t0 =

{count} Items

; + $[3] = count; + $[4] = t0; + } else { + t0 = $[4]; + } + let t1; + if ($[5] !== renderedItems || $[6] !== t0) { + t1 = ( +
+ {t0} + {renderedItems} +
+ ); + $[5] = renderedItems; + $[6] = t0; + $[7] = t1; + } else { + t1 = $[7]; + } + return t1; +} + +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/component.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component.js new file mode 100644 index 0000000000000000000000000000000000000000..c460b9e3d35b2a428bbba0b5193ce3a8c9ec1b40 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/component.js @@ -0,0 +1,32 @@ +function Component(props) { + const items = props.items; + const maxItems = props.maxItems; + + const renderedItems = []; + const seen = new Set(); + const max = Math.max(0, maxItems); + for (let i = 0; i < items.length; i += 1) { + const item = items.at(i); + if (item == null || seen.has(item)) { + continue; + } + seen.add(item); + renderedItems.push(
{item}
); + if (renderedItems.length >= max) { + break; + } + } + const count = renderedItems.length; + return ( +
+

{count} Items

+ {renderedItems} +
+ ); +} + +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/computed-call-evaluation-order.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5ecf59e7a229072682d6cdeedf73a1cffedfbd30 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-evaluation-order.expect.md @@ -0,0 +1,68 @@ + +## Input + +```javascript +// Should print A, B, arg, original +function Component() { + const changeF = o => { + o.f = () => console.log('new'); + }; + const x = { + f: () => console.log('original'), + }; + + (console.log('A'), x)[(console.log('B'), 'f')]( + (changeF(x), console.log('arg'), 1) + ); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Should print A, B, arg, original +function Component() { + const $ = _c(1); + const changeF = _temp2; + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + x = { f: _temp3 }; + + (console.log("A"), x)[(console.log("B"), "f")]( + (changeF(x), console.log("arg"), 1), + ); + $[0] = x; + } else { + x = $[0]; + } + return x; +} +function _temp3() { + return console.log("original"); +} +function _temp2(o) { + o.f = _temp; +} +function _temp() { + return console.log("new"); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"f":"[[ function params=0 ]]"} +logs: ['A','B','arg','original'] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-evaluation-order.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-evaluation-order.js new file mode 100644 index 0000000000000000000000000000000000000000..ec08d4d3eaf6ea6e081f486a875638a10a9fd728 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-evaluation-order.js @@ -0,0 +1,20 @@ +// Should print A, B, arg, original +function Component() { + const changeF = o => { + o.f = () => console.log('new'); + }; + const x = { + f: () => console.log('original'), + }; + + (console.log('A'), x)[(console.log('B'), 'f')]( + (changeF(x), console.log('arg'), 1) + ); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0329450b13e341c8fb6ac397a0d977e524901724 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Component(props) { + const x = foo[props.method](...props.a, null, ...props.b); + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.a || $[1] !== props.b || $[2] !== props.method) { + t0 = foo[props.method](...props.a, null, ...props.b); + $[0] = props.a; + $[1] = props.b; + $[2] = props.method; + $[3] = t0; + } else { + t0 = $[3]; + } + const x = t0; + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.js new file mode 100644 index 0000000000000000000000000000000000000000..b933b65d4ea6a188ea09fde9d41eee2642f7316d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-call-spread.js @@ -0,0 +1,4 @@ +function Component(props) { + const x = foo[props.method](...props.a, null, ...props.b); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9f147194da1cd8b5495ade29cff2b78f9c0d443d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.expect.md @@ -0,0 +1,40 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +function Component(props) { + let a = foo(); + // freeze `a` so we know the next line cannot mutate it +
{a}
; + + // b should be dependent on `props.a` + let b = bar(a[props.a] + 1); + return b; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false +function Component(props) { + const $ = _c(2); + const a = foo(); + + const t0 = a[props.a] + 1; + let t1; + if ($[0] !== t0) { + t1 = bar(t0); + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const b = t1; + return b; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.js new file mode 100644 index 0000000000000000000000000000000000000000..29d6797fdd40eb87afa0a1f393b7fcbce5f2b0c1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-load-primitive-as-dependency.js @@ -0,0 +1,10 @@ +// @enablePreserveExistingMemoizationGuarantees:false +function Component(props) { + let a = foo(); + // freeze `a` so we know the next line cannot mutate it +
{a}
; + + // b should be dependent on `props.a` + let b = bar(a[props.a] + 1); + return b; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9af46bbfd988905005f6083656ff31e4bdc890ff --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {mutate} from 'shared-runtime'; +function useHook({a, b}) { + let y = {a}; + let x = {b}; + x['y'] = y; + mutate(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 3, b: 3}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { mutate } from "shared-runtime"; +function useHook(t0) { + const $ = _c(3); + const { a, b } = t0; + let x; + if ($[0] !== a || $[1] !== b) { + const y = { a }; + x = { b }; + x.y = y; + mutate(x); + $[0] = a; + $[1] = b; + $[2] = x; + } else { + x = $[2]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{ a: 2, b: 3 }], + sequentialRenders: [ + { a: 2, b: 3 }, + { a: 2, b: 3 }, + { a: 3, b: 3 }, + ], +}; + +``` + +### Eval output +(kind: ok) {"b":3,"y":{"a":2},"wat0":"joe"} +{"b":3,"y":{"a":2},"wat0":"joe"} +{"b":3,"y":{"a":3},"wat0":"joe"} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.js new file mode 100644 index 0000000000000000000000000000000000000000..85d0c398bcffcb7a6be30c4bd9e794783c57fe3f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/computed-store-alias.js @@ -0,0 +1,18 @@ +import {mutate} from 'shared-runtime'; +function useHook({a, b}) { + let y = {a}; + let x = {b}; + x['y'] = y; + mutate(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], + sequentialRenders: [ + {a: 2, b: 3}, + {a: 2, b: 3}, + {a: 3, b: 3}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..996afa1cb224bc041f7d90e3a6856121c402f596 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +function component() { + let [x, setX] = useState(0); + const handler = v => setX(v); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function component() { + const $ = _c(1); + const [, setX] = useState(0); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const handler = (v) => setX(v); + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + 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/concise-arrow-expr.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.js new file mode 100644 index 0000000000000000000000000000000000000000..e54eedaadc8d205a04cea3300d9a017211d02436 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/concise-arrow-expr.js @@ -0,0 +1,5 @@ +function component() { + let [x, setX] = useState(0); + const handler = v => setX(v); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-break-labeled.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-break-labeled.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3f795b604e382f940fb18266d2696f437e50d702 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-break-labeled.expect.md @@ -0,0 +1,73 @@ + +## Input + +```javascript +/** + * props.b *does* influence `a` + */ +function Component(props) { + const a = []; + a.push(props.a); + label: { + if (props.b) { + break label; + } + a.push(props.c); + } + a.push(props.d); + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; /** + * props.b *does* influence `a` + */ +function Component(props) { + const $ = _c(5); + let a; + if ( + $[0] !== props.a || + $[1] !== props.b || + $[2] !== props.c || + $[3] !== props.d + ) { + a = []; + a.push(props.a); + bb0: { + if (props.b) { + break bb0; + } + + a.push(props.c); + } + + a.push(props.d); + $[0] = props.a; + $[1] = props.b; + $[2] = props.c; + $[3] = props.d; + $[4] = a; + } else { + a = $[4]; + } + return a; +} + +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/conditional-break-labeled.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-break-labeled.js new file mode 100644 index 0000000000000000000000000000000000000000..ccf983189cf718f3b5f6efb23a44bf5516c4716d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-break-labeled.js @@ -0,0 +1,21 @@ +/** + * props.b *does* influence `a` + */ +function Component(props) { + const a = []; + a.push(props.a); + label: { + if (props.b) { + break label; + } + a.push(props.c); + } + a.push(props.d); + return a; +} + +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/conditional-early-return.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-early-return.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5e708b95c6fe9bd99ef8bc59eb25edf4b1b4b6aa --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-early-return.expect.md @@ -0,0 +1,223 @@ + +## Input + +```javascript +/** + * props.b does *not* influence `a` + */ +function ComponentA(props) { + const a_DEBUG = []; + a_DEBUG.push(props.a); + if (props.b) { + return null; + } + a_DEBUG.push(props.d); + return a_DEBUG; +} + +/** + * props.b *does* influence `a` + */ +function ComponentB(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + } + a.push(props.d); + return a; +} + +/** + * props.b *does* influence `a`, but only in a way that is never observable + */ +function ComponentC(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + return null; + } + a.push(props.d); + return a; +} + +/** + * props.b *does* influence `a` + */ +function ComponentD(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + return a; + } + a.push(props.d); + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: ComponentA, + params: [{a: 1, b: false, d: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; /** + * props.b does *not* influence `a` + */ +function ComponentA(props) { + const $ = _c(5); + let a_DEBUG; + let t0; + if ($[0] !== props.a || $[1] !== props.b || $[2] !== props.d) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + a_DEBUG = []; + a_DEBUG.push(props.a); + if (props.b) { + t0 = null; + break bb0; + } + + a_DEBUG.push(props.d); + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.d; + $[3] = a_DEBUG; + $[4] = t0; + } else { + a_DEBUG = $[3]; + t0 = $[4]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + return a_DEBUG; +} + +/** + * props.b *does* influence `a` + */ +function ComponentB(props) { + const $ = _c(5); + let a; + if ( + $[0] !== props.a || + $[1] !== props.b || + $[2] !== props.c || + $[3] !== props.d + ) { + a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + } + + a.push(props.d); + $[0] = props.a; + $[1] = props.b; + $[2] = props.c; + $[3] = props.d; + $[4] = a; + } else { + a = $[4]; + } + return a; +} + +/** + * props.b *does* influence `a`, but only in a way that is never observable + */ +function ComponentC(props) { + const $ = _c(6); + let a; + let t0; + if ( + $[0] !== props.a || + $[1] !== props.b || + $[2] !== props.c || + $[3] !== props.d + ) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + t0 = null; + break bb0; + } + + a.push(props.d); + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.c; + $[3] = props.d; + $[4] = a; + $[5] = t0; + } else { + a = $[4]; + t0 = $[5]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + return a; +} + +/** + * props.b *does* influence `a` + */ +function ComponentD(props) { + const $ = _c(6); + let a; + let t0; + if ( + $[0] !== props.a || + $[1] !== props.b || + $[2] !== props.c || + $[3] !== props.d + ) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + t0 = a; + break bb0; + } + + a.push(props.d); + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.c; + $[3] = props.d; + $[4] = a; + $[5] = t0; + } else { + a = $[4]; + t0 = $[5]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: ComponentA, + params: [{ a: 1, b: false, d: 3 }], +}; + +``` + +### Eval output +(kind: ok) [1,3] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-early-return.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-early-return.js new file mode 100644 index 0000000000000000000000000000000000000000..e0ed10140925219682c420e2212d4ddcc53b9274 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-early-return.js @@ -0,0 +1,58 @@ +/** + * props.b does *not* influence `a` + */ +function ComponentA(props) { + const a_DEBUG = []; + a_DEBUG.push(props.a); + if (props.b) { + return null; + } + a_DEBUG.push(props.d); + return a_DEBUG; +} + +/** + * props.b *does* influence `a` + */ +function ComponentB(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + } + a.push(props.d); + return a; +} + +/** + * props.b *does* influence `a`, but only in a way that is never observable + */ +function ComponentC(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + return null; + } + a.push(props.d); + return a; +} + +/** + * props.b *does* influence `a` + */ +function ComponentD(props) { + const a = []; + a.push(props.a); + if (props.b) { + a.push(props.c); + return a; + } + a.push(props.d); + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: ComponentA, + params: [{a: 1, b: false, d: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0bcaf60a8e67afeefc0b72a26c3d5e4860f02ca3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.expect.md @@ -0,0 +1,88 @@ + +## Input + +```javascript +function ComponentA(props) { + const a = []; + const b = []; + if (b) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + return ; +} + +function ComponentB(props) { + const a = []; + const b = []; + if (mayMutate(b)) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + return ; +} + +function Foo() {} +function mayMutate() {} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function ComponentA(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.p0 || $[1] !== props.p1 || $[2] !== props.p2) { + const a = []; + const b = []; + if (b) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + t0 = ; + $[0] = props.p0; + $[1] = props.p1; + $[2] = props.p2; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +function ComponentB(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.p0 || $[1] !== props.p1 || $[2] !== props.p2) { + const a = []; + const b = []; + if (mayMutate(b)) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + t0 = ; + $[0] = props.p0; + $[1] = props.p1; + $[2] = props.p2; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +function Foo() {} +function mayMutate() {} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.js new file mode 100644 index 0000000000000000000000000000000000000000..0378ab655c1ef0023d392f8c5c052cf6075b5eef --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-on-mutable.js @@ -0,0 +1,26 @@ +function ComponentA(props) { + const a = []; + const b = []; + if (b) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + return ; +} + +function ComponentB(props) { + const a = []; + const b = []; + if (mayMutate(b)) { + a.push(props.p0); + } + if (props.p1) { + b.push(props.p2); + } + return ; +} + +function Foo() {} +function mayMutate() {} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..31199cd5528c792ab3d15f2ff050bd08b44fe2ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +function Component(props) { + const [x, setX] = useState(0); + + const foo = () => { + setX(1); + }; + + if (props.cond) { + setX(2); + foo(); + } + + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(props) { + const [x, setX] = useState(0); + + const foo = () => { + setX(1); + }; + + if (props.cond) { + setX(2); + foo(); + } + + 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/conditional-set-state-in-render.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.js new file mode 100644 index 0000000000000000000000000000000000000000..f55201b7adcd1badf0349d437d6f526829dcbd9e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conditional-set-state-in-render.js @@ -0,0 +1,20 @@ +function Component(props) { + const [x, setX] = useState(0); + + const foo = () => { + setX(1); + }; + + if (props.cond) { + setX(2); + 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/conflict-codegen-instrument-forget.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflict-codegen-instrument-forget.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..243c4154679a3a2440322efeebe7c75ea1eccc34 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflict-codegen-instrument-forget.expect.md @@ -0,0 +1,97 @@ + +## Input + +```javascript +// @enableEmitInstrumentForget @compilationMode:"annotation" + +import {identity} from 'shared-runtime'; + +function Bar(props) { + 'use forget'; + const shouldInstrument = identity(null); + const _shouldInstrument = identity(null); + const _x2 = () => { + const _shouldInstrument2 = 'hello world'; + return identity({_shouldInstrument2}); + }; + return ( +
+ {props.bar} +
+ ); +} + +function Foo(props) { + 'use forget'; + return {props.bar}; +} + +``` + +## Code + +```javascript +import { + shouldInstrument as _shouldInstrument3, + useRenderCounter, +} from "react-compiler-runtime"; +import { c as _c } from "react/compiler-runtime"; // @enableEmitInstrumentForget @compilationMode:"annotation" + +import { identity } from "shared-runtime"; + +function Bar(props) { + "use forget"; + if (DEV && _shouldInstrument3) + useRenderCounter("Bar", "/conflict-codegen-instrument-forget.ts"); + const $ = _c(4); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = identity(null); + $[0] = t0; + } else { + t0 = $[0]; + } + const shouldInstrument = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = identity(null); + $[1] = t1; + } else { + t1 = $[1]; + } + const _shouldInstrument = t1; + let t2; + if ($[2] !== props.bar) { + t2 = ( +
+ {props.bar} +
+ ); + $[2] = props.bar; + $[3] = t2; + } else { + t2 = $[3]; + } + return t2; +} + +function Foo(props) { + "use forget"; + if (DEV && _shouldInstrument3) + useRenderCounter("Foo", "/conflict-codegen-instrument-forget.ts"); + const $ = _c(2); + let t0; + if ($[0] !== props.bar) { + t0 = {props.bar}; + $[0] = props.bar; + $[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/conflict-codegen-instrument-forget.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflict-codegen-instrument-forget.js new file mode 100644 index 0000000000000000000000000000000000000000..58729d6beeb1ee7dc96131c1aee2e7d16fd2a992 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflict-codegen-instrument-forget.js @@ -0,0 +1,23 @@ +// @enableEmitInstrumentForget @compilationMode:"annotation" + +import {identity} from 'shared-runtime'; + +function Bar(props) { + 'use forget'; + const shouldInstrument = identity(null); + const _shouldInstrument = identity(null); + const _x2 = () => { + const _shouldInstrument2 = 'hello world'; + return identity({_shouldInstrument2}); + }; + return ( +
+ {props.bar} +
+ ); +} + +function Foo(props) { + 'use forget'; + return {props.bar}; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..84a542978c567d45c5569718480cc0b31dcd91c9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + const $ = identity('jQuery'); + const t0 = identity([$]); + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $0 = _c(1); + let t0; + if ($0[0] === Symbol.for("react.memo_cache_sentinel")) { + const $ = identity("jQuery"); + t0 = identity([$]); + $0[0] = t0; + } else { + t0 = $0[0]; + } + const t0$0 = t0; + return t0$0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) ["jQuery"] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.js new file mode 100644 index 0000000000000000000000000000000000000000..b8416701bcfd7858ed5af0610ea5be0a121c3c47 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/conflicting-dollar-sign-variable.js @@ -0,0 +1,12 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + const $ = identity('jQuery'); + const t0 = identity([$]); + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b09c51a8768d7577d63cb196c2dbdd30a8e264d9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +import {useMemo} from 'react'; +import {identity} from 'shared-runtime'; + +function useHook({a, b}) { + const valA = useMemo(() => identity({a}), [a]); + const valB = useMemo(() => identity([b]), [b]); + return [valA, valB]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useMemo } from "react"; +import { identity } from "shared-runtime"; + +function useHook(t0) { + const $ = _c(7); + const { a, b } = t0; + let t1; + if ($[0] !== a) { + t1 = identity({ a }); + $[0] = a; + $[1] = t1; + } else { + t1 = $[1]; + } + const valA = t1; + let t2; + if ($[2] !== b) { + t2 = identity([b]); + $[2] = b; + $[3] = t2; + } else { + t2 = $[3]; + } + const valB = t2; + let t3; + if ($[4] !== valA || $[5] !== valB) { + t3 = [valA, valB]; + $[4] = valA; + $[5] = valB; + $[6] = t3; + } else { + t3 = $[6]; + } + return t3; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{ a: 2, b: 3 }], +}; + +``` + +### Eval output +(kind: ok) [{"a":2},[3]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.ts new file mode 100644 index 0000000000000000000000000000000000000000..d4a35a7bd4302e3ed06ebae0385742c5d50d9001 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/consecutive-use-memo.ts @@ -0,0 +1,13 @@ +import {useMemo} from 'react'; +import {identity} from 'shared-runtime'; + +function useHook({a, b}) { + const valA = useMemo(() => identity({a}), [a]); + const valB = useMemo(() => identity([b]), [b]); + return [valA, valB]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useHook, + params: [{a: 2, b: 3}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..29fd76e8363d9a46beab52eb19d0afc7abc38529 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +import {shallowCopy} from 'shared-runtime'; + +function Component(props) { + const x = shallowCopy(props); + // These calls should view x as readonly and be grouped outside of the reactive scope for x: + console.log(x); + console.info(x); + console.warn(x); + console.error(x); + console.trace(x); + console.table(x); + global.console.log(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1, b: 2}], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { shallowCopy } from "shared-runtime"; + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = shallowCopy(props); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + + console.log(x); + console.info(x); + console.warn(x); + console.error(x); + console.trace(x); + console.table(x); + global.console.log(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: 1, b: 2 }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"a":1,"b":2} +logs: [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.js new file mode 100644 index 0000000000000000000000000000000000000000..e611f40b944eb7d622873c2f4fc083af91598700 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.js @@ -0,0 +1,20 @@ +import {shallowCopy} from 'shared-runtime'; + +function Component(props) { + const x = shallowCopy(props); + // These calls should view x as readonly and be grouped outside of the reactive scope for x: + console.log(x); + console.info(x); + console.warn(x); + console.error(x); + console.trace(x); + console.table(x); + global.console.log(x); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: 1, b: 2}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..04b6c4f17f41a3584b2cb1108e88f60bfb1f0d2b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +function foo() { + const isX = GLOBAL_IS_X; + const getJSX = () => { + return ; + }; + const result = getJSX(); + return result; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo() { + const $ = _c(1); + + const getJSX = _temp; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = getJSX(); + $[0] = t0; + } else { + t0 = $[0]; + } + const result = t0; + return result; +} +function _temp() { + return ; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.js new file mode 100644 index 0000000000000000000000000000000000000000..ca1f4023ac534b12dadc29b7d885ca7d888ff088 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-global.js @@ -0,0 +1,8 @@ +function foo() { + const isX = GLOBAL_IS_X; + const getJSX = () => { + return ; + }; + const result = getJSX(); + return result; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..60fe0808d922a55842210f998a7a38a2df1b5953 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +function foo() { + const x = 42; + const f = () => { + console.log(x); + }; + f(); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo() { + const f = _temp; + + f(); + return 42; +} +function _temp() { + console.log(42); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 42 +logs: [42] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.js new file mode 100644 index 0000000000000000000000000000000000000000..7708bf254277ee1a4be17fab42680345f67f6548 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-into-function-expression-primitive.js @@ -0,0 +1,14 @@ +function foo() { + const x = 42; + const f = () => { + console.log(x); + }; + f(); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fc2a10cd3d9985080a219192558fa89a2335b25c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +function useFoo(setOne: boolean) { + let x; + let y; + let z; + if (setOne) { + x = y = z = 1; + } else { + x = 2; + y = 3; + z = 5; + } + return {x, y, z}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [true], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function useFoo(setOne) { + const $ = _c(4); + let x; + let y; + let z; + if (setOne) { + x = y = z = 1; + } else { + x = 2; + y = 3; + z = 5; + } + let t0; + if ($[0] !== x || $[1] !== y || $[2] !== z) { + t0 = { x, y, z }; + $[0] = x; + $[1] = y; + $[2] = z; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [true], +}; + +``` + +### Eval output +(kind: ok) {"x":1,"y":1,"z":1} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts new file mode 100644 index 0000000000000000000000000000000000000000..0bf17a61478702b980eb272759bb0cc7b54e133b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/const-propagation-phi-nodes.ts @@ -0,0 +1,18 @@ +function useFoo(setOne: boolean) { + let x; + let y; + let z; + if (setOne) { + x = y = z = 1; + } else { + x = 2; + y = 3; + z = 5; + } + return {x, y, z}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [true], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-computed.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-computed.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a7662e95ad8bbee9bbbf476875798bb4907a4fa6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-computed.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +function Component(props) { + const index = 'foo'; + const x = {}; + x[index] = x[index] + x['bar']; + x[index](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 x; + if ($[0] !== props.foo) { + x = {}; + x.foo = x.foo + x.bar; + x.foo(props.foo); + $[0] = props.foo; + $[1] = x; + } else { + x = $[1]; + } + 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/constant-computed.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-computed.js new file mode 100644 index 0000000000000000000000000000000000000000..075f9374b2c5d926085b7f52bfb48d98091334cf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-computed.js @@ -0,0 +1,13 @@ +function Component(props) { + const index = 'foo'; + const x = {}; + x[index] = x[index] + x['bar']; + x[index](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/constant-prop-across-objectmethod-def.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-across-objectmethod-def.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..eab467da27fce0a918a361cd3c123037801f7a0d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-across-objectmethod-def.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +// repro for context identifier scoping bug, in which x was +// inferred as a context variable. + +function Component() { + let x = 2; + const obj = { + method() {}, + }; + x = 4; + identity(obj); + // constant propagation should return 4 here + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { identity } from "shared-runtime"; + +// repro for context identifier scoping bug, in which x was +// inferred as a context variable. + +function Component() { + const obj = { method() {} }; + + identity(obj); + + return 4; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) 4 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-across-objectmethod-def.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-across-objectmethod-def.js new file mode 100644 index 0000000000000000000000000000000000000000..934bb1f0422a609dc061fdbaccb13b54514b1102 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-across-objectmethod-def.js @@ -0,0 +1,20 @@ +import {identity} from 'shared-runtime'; + +// repro for context identifier scoping bug, in which x was +// inferred as a context variable. + +function Component() { + let x = 2; + const obj = { + method() {}, + }; + x = 4; + identity(obj); + // constant propagation should return 4 here + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3dedb80ab74710ac4517ece6a0ed28705219dffe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +import {invoke} from 'shared-runtime'; + +function Component() { + let x = 2; + const fn = () => { + return {x: 'value'}; + }; + invoke(fn); + x = 3; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { invoke } from "shared-runtime"; + +function Component() { + const fn = _temp; + + invoke(fn); + + return 3; +} +function _temp() { + return { x: "value" }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok) 3 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.js new file mode 100644 index 0000000000000000000000000000000000000000..8d472d1e62bb29b845b1d48ccb6ca3d04de3e7c7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-colliding-identifier.js @@ -0,0 +1,16 @@ +import {invoke} from 'shared-runtime'; + +function Component() { + let x = 2; + const fn = () => { + return {x: 'value'}; + }; + invoke(fn); + x = 3; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-to-object-method.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-to-object-method.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cc5633a7b3f40fc4f6554b7895c672b4d5eacff8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-to-object-method.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Foo() { + const CONSTANT = 1; + const x = { + foo() { + return identity(CONSTANT); + }, + }; + return x.foo(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const x = { + foo() { + return identity(1); + }, + }; + t0 = x.foo(); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; + +``` + +### 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/constant-prop-to-object-method.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-to-object-method.js new file mode 100644 index 0000000000000000000000000000000000000000..58a2b2761058c7ad1f2359ce6dc9d9ba05a8f582 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-prop-to-object-method.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +function Foo() { + const CONSTANT = 1; + const x = { + foo() { + return identity(CONSTANT); + }, + }; + return x.foo(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7bbd8c90d7ffd13f15fb2df0dedce527ccb5274c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {CONST_STRING0, Text} from 'shared-runtime'; +function useFoo() { + 'use no forget'; + return {tab: CONST_STRING0}; +} + +function Test() { + const {tab} = useFoo(); + const currentTab = tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING0; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { CONST_STRING0, Text } from "shared-runtime"; +function useFoo() { + "use no forget"; + return { tab: CONST_STRING0 }; +} + +function Test() { + const $ = _c(1); + const { tab } = useFoo(); + tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING0; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; + +``` + +### Eval output +(kind: ok)
global string 0
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js new file mode 100644 index 0000000000000000000000000000000000000000..4f70bb58046b390069004a1729a135f08d18ec28 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis-constant.js @@ -0,0 +1,18 @@ +import {CONST_STRING0, Text} from 'shared-runtime'; +function useFoo() { + 'use no forget'; + return {tab: CONST_STRING0}; +} + +function Test() { + const {tab} = useFoo(); + const currentTab = tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING0; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a738754a1600bc8688e22313615032a5c490a11e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +import {CONST_STRING0, CONST_STRING1, Text} from 'shared-runtime'; + +function useFoo() { + 'use no forget'; + return {tab: CONST_STRING1}; +} + +function Test() { + const {tab} = useFoo(); + const currentTab = tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING1; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { CONST_STRING0, CONST_STRING1, Text } from "shared-runtime"; + +function useFoo() { + "use no forget"; + return { tab: CONST_STRING1 }; +} + +function Test() { + const $ = _c(2); + const { tab } = useFoo(); + const currentTab = tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING1; + let t0; + if ($[0] !== currentTab) { + t0 = ; + $[0] = currentTab; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; + +``` + +### Eval output +(kind: ok)
global string 1
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.js new file mode 100644 index 0000000000000000000000000000000000000000..2be7df2e4071df5a5848158d801a5e796c5f23a6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagate-global-phis.js @@ -0,0 +1,19 @@ +import {CONST_STRING0, CONST_STRING1, Text} from 'shared-runtime'; + +function useFoo() { + 'use no forget'; + return {tab: CONST_STRING1}; +} + +function Test() { + const {tab} = useFoo(); + const currentTab = tab === CONST_STRING0 ? CONST_STRING0 : CONST_STRING1; + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Test, + params: [], + isComponent: true, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a95bed470606db47b66ad1466d80c2beccbce857 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function foo() { + return ( + >> 0, + (2 ** 64) >> 4, + 1e21 << 1, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":[0,0,-559939584,559939584,-559939584,-559939584,3735027712,0,-1119879168]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.js new file mode 100644 index 0000000000000000000000000000000000000000..72b0588336b67a7b2c008c8a653252e3ff71c506 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops-large-numbers.js @@ -0,0 +1,25 @@ +import {Stringify} from 'shared-runtime'; + +function foo() { + return ( + >> 0, + (2 ** 64) >> 4, + 1e21 << 1, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b916be1357e24d7cc4e70764e3964751b25e450d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.expect.md @@ -0,0 +1,78 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function foo() { + return ( + > 0, + 123 >>> 0, + 123.45 | 1, + 123.45 & 1, + 123.45 ^ 1, + 123 << 1, + 123 >> 1, + 123 >>> 1, + 3 ** 2, + 3 ** 2.5, + 3.5 ** 2, + 2 ** (3 ** 0.5), + 4 % 2, + 4 % 2.5, + 4 % 3, + 4.5 % 2, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":[123,0,123,123,123,123,123,1,122,246,61,61,9,15.588457268119896,12.25,3.3219970854839125,0,1.5,1,0.5]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js new file mode 100644 index 0000000000000000000000000000000000000000..6f984c4c77a2c7dc9793c5ecd5e91bfb4ca74389 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-bit-ops.js @@ -0,0 +1,36 @@ +import {Stringify} from 'shared-runtime'; + +function foo() { + return ( + > 0, + 123 >>> 0, + 123.45 | 1, + 123.45 & 1, + 123.45 ^ 1, + 123 << 1, + 123 >> 1, + 123 >>> 1, + 3 ** 2, + 3 ** 2.5, + 3.5 ** 2, + 2 ** (3 ** 0.5), + 4 % 2, + 4 % 2.5, + 4 % 3, + 4.5 % 2, + ]} + /> + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b6017e4cb224f58225b43adca4c11a0faa51cd59 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +function foo() { + let y = 0; + for (const x = 100; x < 10; x) { + y = y + 1; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo() { + let y = 0; + for (const x = 100; false; 100) { + y = y + 1; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 0 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.js new file mode 100644 index 0000000000000000000000000000000000000000..5793444de207ae17aa9523a55ddfbfd6d5195fab --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-for.js @@ -0,0 +1,13 @@ +function foo() { + let y = 0; + for (const x = 100; x < 10; x) { + y = y + 1; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-into-function-expressions.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-into-function-expressions.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..503ee6d716813dc291ad51d3f9411177ead8e74b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-into-function-expressions.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +import {Stringify, identity} from 'shared-runtime'; + +function Component(props) { + const x = 42; + const onEvent = () => { + return identity(x); + }; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + sequentialRenders: [{}, {}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify, identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(1); + + const onEvent = _temp; + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() { + return identity(42); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + sequentialRenders: [{}, {}], +}; + +``` + +### Eval output +(kind: ok)
{"onEvent":{"kind":"Function","result":42},"shouldInvokeFns":true}
+
{"onEvent":{"kind":"Function","result":42},"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/constant-propagation-into-function-expressions.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-into-function-expressions.js new file mode 100644 index 0000000000000000000000000000000000000000..d1df503ad97368a2bbe68f9a4e91e31f6f8bd94c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-into-function-expressions.js @@ -0,0 +1,15 @@ +import {Stringify, identity} from 'shared-runtime'; + +function Component(props) { + const x = 42; + const onEvent = () => { + return identity(x); + }; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], + sequentialRenders: [{}, {}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..16786daf2b1fd82bb105c92543c42f5e3171523e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo(a, b, c) { + let x; + if (a) { + x = 2 - 1; + } else { + x = 0 + 1; + } + if (x === 1) { + return b; + } else { + return c; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function foo(a, b, c) { + if (a) { + } + + return 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/constant-propagation-phi.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-phi.js new file mode 100644 index 0000000000000000000000000000000000000000..1e2bee69ad29dc54ef68739a754cd3af62728d12 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-phi.js @@ -0,0 +1,19 @@ +function foo(a, b, c) { + let x; + if (a) { + x = 2 - 1; + } else { + x = 0 + 1; + } + if (x === 1) { + return b; + } else { + return c; + } +} + +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/constant-propagation-scientific-notation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-scientific-notation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2567107d45f75248ad9b0711447dbfbadc3fa1f3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-scientific-notation.expect.md @@ -0,0 +1,38 @@ + +## Input + +```javascript +// Template literal constant folding uses format_js_number to convert +// numbers to strings. Without the shared implementation, large numbers +// may format incorrectly (e.g. 1e21 as "1e21" instead of "1e+21"). + +function Component() { + const x = `value is ${1e21}`; + return
{x}
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Template literal constant folding uses format_js_number to convert +// numbers to strings. Without the shared implementation, large numbers +// may format incorrectly (e.g. 1e21 as "1e21" instead of "1e+21"). + +function Component() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 =
{"value is 1e+21"}
; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-scientific-notation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-scientific-notation.js new file mode 100644 index 0000000000000000000000000000000000000000..7b8d13c80a29e948b56fe552f022b7c43737ba44 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-scientific-notation.js @@ -0,0 +1,8 @@ +// Template literal constant folding uses format_js_number to convert +// numbers to strings. Without the shared implementation, large numbers +// may format incorrectly (e.g. 1e21 as "1e21" instead of "1e+21"). + +function Component() { + const x = `value is ${1e21}`; + return
{x}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8174bd0364e17d00600aeb99a540aa24d8407d37 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function foo() { + const a = 'a' + 'b'; + const c = 'c'; + return a + c; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo() { + return "abc"; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) "abc" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.js new file mode 100644 index 0000000000000000000000000000000000000000..d8c19b6a24ed7f199483a30682fcf3a311607afe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-string-concat.js @@ -0,0 +1,11 @@ +function foo() { + const a = 'a' + 'b'; + const c = 'c'; + return a + c; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..06a7ee3a37f0a3e03bd3831127395fb7ae3d45e8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.expect.md @@ -0,0 +1,136 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +import {Stringify, identity} from 'shared-runtime'; + +function foo() { + try { + identity(`${Symbol('0')}`); // Uncaught TypeError: Cannot convert a Symbol value to a string (leave as is) + } catch {} + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false +import { Stringify, identity } from "shared-runtime"; + +function foo() { + const $ = _c(1); + try { + identity(`${Symbol("0")}`); + } catch {} + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":[true,true,"a\nb","\n","a1b"," abc A\n\nŧ","abc1def","abc1def2","abc1def2ghi","a4bcde6f","120","NaN","Infinity","-Infinity","9007199254740991","-9007199254740991","1.7976931348623157e+308","5e-324","0","\n ","[object Object]","1,2,3","true","false","null","undefined","1234567890","0123456789","01234567890","01234567890","0123401234567890123456789067890","012340123456789067890","0","",""]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.js new file mode 100644 index 0000000000000000000000000000000000000000..199284b8caa937589ad8cf10e82b24005856a8de --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-template-literal.js @@ -0,0 +1,57 @@ +// @enablePreserveExistingMemoizationGuarantees:false +import {Stringify, identity} from 'shared-runtime'; + +function foo() { + try { + identity(`${Symbol('0')}`); // Uncaught TypeError: Cannot convert a Symbol value to a string (leave as is) + } catch {} + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b52ea1b51b4b18eed32da730d55da41527fe29ea --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.expect.md @@ -0,0 +1,73 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function foo() { + const a = -1; + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":[-2,0,true,null,null,null,null,null]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js new file mode 100644 index 0000000000000000000000000000000000000000..8ef8ec0e6c79a29c3e6940f2c3ffa6da761966ce --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary-number.js @@ -0,0 +1,25 @@ +import {Stringify} from 'shared-runtime'; + +function foo() { + const a = -1; + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..aaea767643b0605025283e2029cdcec9023babd1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.expect.md @@ -0,0 +1,86 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function foo() { + let _b; + const b = true; + if (!b) { + _b = 'bar'; + } else { + _b = 'baz'; + } + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( + + ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok)
{"value":{"_b":"baz","b0":false,"n0":true,"n1":false,"n2":false,"n3":false,"s0":true,"s1":false,"s2":false,"u":true,"n":true}}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js new file mode 100644 index 0000000000000000000000000000000000000000..e78c1bceba538beafd8f6183d44cd3b84d69b1d5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-unary.js @@ -0,0 +1,35 @@ +import {Stringify} from 'shared-runtime'; + +function foo() { + let _b; + const b = true; + if (!b) { + _b = 'bar'; + } else { + _b = 'baz'; + } + + return ( + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..deb51f9554f49ea0f48333e887e6da1a834d032c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +function foo() { + let x = 100; + let y = 0; + while (x < 10) { + y += 1; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo() { + let y = 0; + while (false) { + y = y + 1; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 0 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.js new file mode 100644 index 0000000000000000000000000000000000000000..fe63a1e7acb05dadafe3407d5c1893cf481658e8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation-while.js @@ -0,0 +1,14 @@ +function foo() { + let x = 100; + let y = 0; + while (x < 10) { + y += 1; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8b26b4601ac08822c05b9f3fdd94f3fbb2257cc2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function foo() { + const a = 1; + const b = 2; + const c = 3; + const d = a + b; + const e = d * c; + const f = e / d; + const g = f - e; + + if (g) { + console.log('foo'); + } + + const h = g; + const i = h; + const j = i; + return j; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo() { + console.log("foo"); + + return -6; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) -6 +logs: ['foo'] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.js new file mode 100644 index 0000000000000000000000000000000000000000..044e82d19ec6d53e4383431535efb9381d7c451c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constant-propagation.js @@ -0,0 +1,24 @@ +function foo() { + const a = 1; + const b = 2; + const c = 3; + const d = a + b; + const e = d * c; + const f = e / d; + const g = f - e; + + if (g) { + console.log('foo'); + } + + const h = g; + const i = h; + const j = i; + return j; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constructor.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constructor.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bfa6c8307128553f9de33f185cf66f7b2ba1eab0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constructor.expect.md @@ -0,0 +1,41 @@ + +## Input + +```javascript +function Foo() {} + +function Component(props) { + const a = []; + const b = {}; + new Foo(a, b); + let _ =
; + new Foo(b); + return
; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo() {} + +function Component(props) { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const a = []; + const b = {}; + new Foo(a, b); + new Foo(b); + t0 =
; + $[0] = t0; + } else { + t0 = $[0]; + } + 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/constructor.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constructor.js new file mode 100644 index 0000000000000000000000000000000000000000..ed1df01d2aa32d269aa5874fe9470477db2d7375 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/constructor.js @@ -0,0 +1,10 @@ +function Foo() {} + +function Component(props) { + const a = []; + const b = {}; + new Foo(a, b); + let _ =
; + new Foo(b); + return
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..636bc53a172e5cdae6b27fe7ca9d7dfbdeda032b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +import {useMemo} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let Component = Stringify; + + Component = useMemo(() => { + return Component; + }, [Component]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Sathya'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePreserveExistingMemoizationGuarantees:false +import { useMemo } from "react"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(3); + let Component; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + Component = Stringify; + + Component; + Component = Component; + $[0] = Component; + } else { + Component = $[0]; + } + let t0; + if ($[1] !== props) { + t0 = ; + $[1] = props; + $[2] = t0; + } else { + t0 = $[2]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ name: "Sathya" }], +}; + +``` + +### Eval output +(kind: ok)
{"name":"Sathya"}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js new file mode 100644 index 0000000000000000000000000000000000000000..49cf3364b1c4ab8b4b08c7399c772064f26c4d4a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-as-jsx-element-tag.js @@ -0,0 +1,18 @@ +// @enablePreserveExistingMemoizationGuarantees:false +import {useMemo} from 'react'; +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let Component = Stringify; + + Component = useMemo(() => { + return Component; + }, [Component]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{name: 'Sathya'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1e3498217230a62b02f104b0fa87f78048ea2735 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {invoke} from 'shared-runtime'; + +function Component({shouldReassign}) { + let x = null; + const reassign = () => { + if (shouldReassign) { + x = 2; + } + }; + invoke(reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{shouldReassign: true}], + sequentialRenders: [{shouldReassign: false}, {shouldReassign: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { invoke } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { shouldReassign } = t0; + let x; + if ($[0] !== shouldReassign) { + x = null; + const reassign = () => { + if (shouldReassign) { + x = 2; + } + }; + + invoke(reassign); + $[0] = shouldReassign; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ shouldReassign: true }], + sequentialRenders: [{ shouldReassign: false }, { shouldReassign: true }], +}; + +``` + +### Eval output +(kind: ok) null +2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.js new file mode 100644 index 0000000000000000000000000000000000000000..a2d103f61e57d49a37cfa4f89a6bd04de7a2e999 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-explicit-control-flow.js @@ -0,0 +1,18 @@ +import {invoke} from 'shared-runtime'; + +function Component({shouldReassign}) { + let x = null; + const reassign = () => { + if (shouldReassign) { + x = 2; + } + }; + invoke(reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{shouldReassign: true}], + sequentialRenders: [{shouldReassign: false}, {shouldReassign: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8f5da8487667f0d9276ca91695648c75ace12fc9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +import {conditionalInvoke} from 'shared-runtime'; + +// same as context-variable-reactive-explicit-control-flow.js, but make +// the control flow implicit + +function Component({shouldReassign}) { + let x = null; + const reassign = () => { + x = 2; + }; + conditionalInvoke(shouldReassign, reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{shouldReassign: true}], + sequentialRenders: [{shouldReassign: false}, {shouldReassign: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { conditionalInvoke } from "shared-runtime"; + +// same as context-variable-reactive-explicit-control-flow.js, but make +// the control flow implicit + +function Component(t0) { + const $ = _c(2); + const { shouldReassign } = t0; + let x; + if ($[0] !== shouldReassign) { + x = null; + const reassign = () => { + x = 2; + }; + + conditionalInvoke(shouldReassign, reassign); + $[0] = shouldReassign; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ shouldReassign: true }], + sequentialRenders: [{ shouldReassign: false }, { shouldReassign: true }], +}; + +``` + +### Eval output +(kind: ok) null +2 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.js new file mode 100644 index 0000000000000000000000000000000000000000..1bf1c81ca34b11d15ded51c54f65f7c4faab0c46 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reactive-implicit-control-flow.js @@ -0,0 +1,19 @@ +import {conditionalInvoke} from 'shared-runtime'; + +// same as context-variable-reactive-explicit-control-flow.js, but make +// the control flow implicit + +function Component({shouldReassign}) { + let x = null; + const reassign = () => { + x = 2; + }; + conditionalInvoke(shouldReassign, reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{shouldReassign: true}], + sequentialRenders: [{shouldReassign: false}, {shouldReassign: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cdd0269d5cf40024249ee851959f573d4983b559 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {invoke} from 'shared-runtime'; + +function Component({cond}) { + let x = 2; + const obj = { + method(cond) { + if (cond) { + x = 4; + } + }, + }; + invoke(obj.method, cond); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { invoke } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { cond } = t0; + let x; + if ($[0] !== cond) { + x = 2; + const obj = { + method(cond_0) { + if (cond_0) { + x = 4; + } + }, + }; + invoke(obj.method, cond); + $[0] = cond; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true }], +}; + +``` + +### Eval output +(kind: ok) 4 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.js new file mode 100644 index 0000000000000000000000000000000000000000..6c6408209ff014bdcc66858d4a4b214b2d286973 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-objectmethod.js @@ -0,0 +1,19 @@ +import {invoke} from 'shared-runtime'; + +function Component({cond}) { + let x = 2; + const obj = { + method(cond) { + if (cond) { + x = 4; + } + }, + }; + invoke(obj.method, cond); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..08b92e940fcb52691406adb41f473628e4d363a2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let x = null; + const callback = () => { + console.log(x); + }; + x = {}; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + let x = null; + const callback = () => { + console.log(x); + }; + x = {}; + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
{"callback":{"kind":"Function"},"shouldInvokeFns":true}
+logs: [{}] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.js new file mode 100644 index 0000000000000000000000000000000000000000..9cdecf5a44457bc429eb09491626a975de86b975 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-outside-of-lambda.js @@ -0,0 +1,15 @@ +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let x = null; + const callback = () => { + console.log(x); + }; + x = {}; + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8578d269cbe2c1a9cec0f3475a0f3cc648178fd9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import {invoke} from 'shared-runtime'; + +function Component({value}) { + let x = null; + const reassign = () => { + x = value; + }; + invoke(reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 2}], + sequentialRenders: [{value: 2}, {value: 4}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { invoke } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + const { value } = t0; + let x; + if ($[0] !== value) { + x = null; + const reassign = () => { + x = value; + }; + + invoke(reassign); + $[0] = value; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 2 }], + sequentialRenders: [{ value: 2 }, { value: 4 }], +}; + +``` + +### Eval output +(kind: ok) 2 +4 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.js new file mode 100644 index 0000000000000000000000000000000000000000..5f11db367c0616e968c527490ef3c69aad1c4f1f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-reactive-capture.js @@ -0,0 +1,16 @@ +import {invoke} from 'shared-runtime'; + +function Component({value}) { + let x = null; + const reassign = () => { + x = value; + }; + invoke(reassign); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 2}], + sequentialRenders: [{value: 2}, {value: 4}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4ab740767567ae85576a0c752a6affbf48ad0613 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.expect.md @@ -0,0 +1,78 @@ + +## Input + +```javascript +import {conditionalInvoke} from 'shared-runtime'; + +function Component({doReassign1, doReassign2}) { + let x = {}; + const reassign1 = () => { + x = 2; + }; + const reassign2 = () => { + x = 3; + }; + conditionalInvoke(doReassign1, reassign1); + conditionalInvoke(doReassign2, reassign2); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{doReassign1: true, doReassign2: true}], + sequentialRenders: [ + {doReassign1: true, doReassign2: true}, + {doReassign1: true, doReassign2: false}, + {doReassign1: false, doReassign2: false}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { conditionalInvoke } from "shared-runtime"; + +function Component(t0) { + const $ = _c(3); + const { doReassign1, doReassign2 } = t0; + let x; + if ($[0] !== doReassign1 || $[1] !== doReassign2) { + x = {}; + const reassign1 = () => { + x = 2; + }; + + const reassign2 = () => { + x = 3; + }; + + conditionalInvoke(doReassign1, reassign1); + conditionalInvoke(doReassign2, reassign2); + $[0] = doReassign1; + $[1] = doReassign2; + $[2] = x; + } else { + x = $[2]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ doReassign1: true, doReassign2: true }], + sequentialRenders: [ + { doReassign1: true, doReassign2: true }, + { doReassign1: true, doReassign2: false }, + { doReassign1: false, doReassign2: false }, + ], +}; + +``` + +### Eval output +(kind: ok) 3 +2 +{} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.js new file mode 100644 index 0000000000000000000000000000000000000000..5a7392d839ffbd48234b6f17a86854a1e81627ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/context-variable-reassigned-two-lambdas.js @@ -0,0 +1,24 @@ +import {conditionalInvoke} from 'shared-runtime'; + +function Component({doReassign1, doReassign2}) { + let x = {}; + const reassign1 = () => { + x = 2; + }; + const reassign2 = () => { + x = 3; + }; + conditionalInvoke(doReassign1, reassign1); + conditionalInvoke(doReassign2, reassign2); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{doReassign1: true, doReassign2: true}], + sequentialRenders: [ + {doReassign1: true, doReassign2: true}, + {doReassign1: true, doReassign2: false}, + {doReassign1: false, doReassign2: false}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1d7a816a95f46be1a346bf83de7951295430dd14 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +import {useState} from 'react'; +function component() { + let [x, setX] = useState(0); + const handler = event => setX(event.target.value); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [], + isComponent: true, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useState } from "react"; +function component() { + const $ = _c(3); + const [x, setX] = useState(0); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = (event) => setX(event.target.value); + $[0] = t0; + } else { + t0 = $[0]; + } + const handler = t0; + let t1; + if ($[1] !== x) { + t1 = ; + $[1] = x; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [], + isComponent: true, +}; + +``` + +### Eval output +(kind: ok) \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.js new file mode 100644 index 0000000000000000000000000000000000000000..4f477c1ab8e985ab19718ba94a49cd3c991f28e0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/controlled-input.js @@ -0,0 +1,12 @@ +import {useState} from 'react'; +function component() { + let [x, setX] = useState(0); + const handler = event => setX(event.target.value); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: [], + isComponent: true, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8f0033e086d322f53465894e2d3caa2f15cde638 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.expect.md @@ -0,0 +1,68 @@ + +## Input + +```javascript +import React from 'react'; +import {shallowCopy} from 'shared-runtime'; + +function Component(props) { + const childProps = {style: {width: props.width}}; + const element = React.createElement('div', childProps, ['hello world']); + shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import React from "react"; +import { shallowCopy } from "shared-runtime"; + +function Component(props) { + const $ = _c(5); + let t0; + if ($[0] !== props.width) { + t0 = { style: { width: props.width } }; + $[0] = props.width; + $[1] = t0; + } else { + t0 = $[1]; + } + const childProps = t0; + let t1; + if ($[2] !== childProps) { + let t2; + if ($[4] === Symbol.for("react.memo_cache_sentinel")) { + t2 = ["hello world"]; + $[4] = t2; + } else { + t2 = $[4]; + } + t1 = React.createElement("div", childProps, t2); + $[2] = childProps; + $[3] = t1; + } else { + t1 = $[3]; + } + const element = t1; + shallowCopy(childProps); + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +### Eval output +(kind: ok)
hello world
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.js new file mode 100644 index 0000000000000000000000000000000000000000..8b85f4f1c1bd58513870fdbaf40ff1fc2b58293d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/createElement-freeze.js @@ -0,0 +1,14 @@ +import React from 'react'; +import {shallowCopy} from 'shared-runtime'; + +function Component(props) { + const childProps = {style: {width: props.width}}; + const element = React.createElement('div', childProps, ['hello world']); + shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes + return element; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ca69b5a0923dbf2d262485d44f3964e971d12acc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +// @expectNothingCompiled @customOptOutDirectives:["use todo memo"] +function Component() { + 'use todo memo'; + return
hello world!
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +// @expectNothingCompiled @customOptOutDirectives:["use todo memo"] +function Component() { + "use todo memo"; + return
hello world!
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +### Eval output +(kind: ok)
hello world!
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.tsx b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.tsx new file mode 100644 index 0000000000000000000000000000000000000000..85e1583eb7afea6dcb41e3b34d522ef1085cd0c8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/custom-opt-out-directive.tsx @@ -0,0 +1,10 @@ +// @expectNothingCompiled @customOptOutDirectives:["use todo memo"] +function Component() { + 'use todo memo'; + return
hello world!
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b6c6b661d930e1ea4913b856dfc971160be714db --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo(props) { + let x = 0; + let y = 0; + while (y < props.max) { + x++; + y++; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{max: 10}], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo(props) { + let y = 0; + while (y < props.max) { + y++; + } + + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{ max: 10 }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 10 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.js new file mode 100644 index 0000000000000000000000000000000000000000..31a361b30eb82ba6b462a3bd4a44c3de364ffa87 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-loop.js @@ -0,0 +1,15 @@ +function foo(props) { + let x = 0; + let y = 0; + while (y < props.max) { + x++; + y++; + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{max: 10}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ca601add0b07aa0e98562225553305a11bdbe228 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +function Component(props) { + const _ = 42; + return props.value; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +function Component(props) { + return props.value; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) 42 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.js new file mode 100644 index 0000000000000000000000000000000000000000..cf2850bfb64ab89c9daaebb33b4d175c8f3aa710 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-const.js @@ -0,0 +1,9 @@ +function Component(props) { + const _ = 42; + return props.value; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..39aa901cbd3ff46363dcf8ccb0cc8960a733188c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-postfix-update.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function Component(props) { + let i = 0; + i++; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{i: 42}], +}; + +``` + +## Code + +```javascript +function Component(props) { + let i; + + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 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/dce-unused-postfix-update.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js new file mode 100644 index 0000000000000000000000000000000000000000..da67d5ed74b30dbeb6cdc7f9b07196672fb65e0a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-postfix-update.js @@ -0,0 +1,11 @@ +function Component(props) { + let i = 0; + i++; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{i: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ef30c992ef713e1f398cb994bf619aafda4ad75e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-prefix-update.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function Component(props) { + let i = 0; + --i; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{i: 42}], +}; + +``` + +## Code + +```javascript +function Component(props) { + let i; + + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ i: 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/dce-unused-prefix-update.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js new file mode 100644 index 0000000000000000000000000000000000000000..a14527255f1cbb226e43d40b8bc0ca385d1272f7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dce-unused-prefix-update.js @@ -0,0 +1,11 @@ +function Component(props) { + let i = 0; + --i; + i = props.i; + return i; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{i: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger-memoized.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger-memoized.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6d52fed074bad6d65a1a1d412bb656836bd30d37 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger-memoized.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +function Component(props) { + const x = []; + debugger; + x.push(props.value); + 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 x; + if ($[0] !== props.value) { + x = []; + debugger; + x.push(props.value); + $[0] = props.value; + $[1] = x; + } else { + x = $[1]; + } + 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/debugger-memoized.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger-memoized.js new file mode 100644 index 0000000000000000000000000000000000000000..fa899c11e3c6006b23aeca804bb2cc47dea373fe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger-memoized.js @@ -0,0 +1,12 @@ +function Component(props) { + const x = []; + debugger; + x.push(props.value); + 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/debugger.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..dcfdb8245479fb01be9cc1c2f78ac5af0a473570 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger.expect.md @@ -0,0 +1,48 @@ + +## Input + +```javascript +function Component(props) { + debugger; + if (props.cond) { + debugger; + } else { + while (props.cond) { + debugger; + } + } + debugger; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(props) { + debugger; + if (props.cond) { + debugger; + } else { + while (props.cond) { + debugger; + } + } + + debugger; +} + +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/debugger.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger.js new file mode 100644 index 0000000000000000000000000000000000000000..450f243d8b29c13cdacf9ee8102b674565374b9d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/debugger.js @@ -0,0 +1,17 @@ +function Component(props) { + debugger; + if (props.cond) { + debugger; + } else { + while (props.cond) { + debugger; + } + } + debugger; +} + +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/declare-reassign-variable-in-closure.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/declare-reassign-variable-in-closure.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..94d263ef3d699a51417eef18632f16a2836c78bf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/declare-reassign-variable-in-closure.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function Component(p) { + let x; + const foo = () => { + x = {}; + }; + 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(p) { + const $ = _c(1); + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const foo = () => { + x = {}; + }; + + foo(); + $[0] = x; + } else { + x = $[0]; + } + + 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/declare-reassign-variable-in-closure.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/declare-reassign-variable-in-closure.js new file mode 100644 index 0000000000000000000000000000000000000000..d69479ba749b8756c6412ffc8c25302118f176b7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/declare-reassign-variable-in-closure.js @@ -0,0 +1,15 @@ +function Component(p) { + let x; + const foo = () => { + x = {}; + }; + 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/deeply-nested-function-expressions-with-params.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-function-expressions-with-params.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7d0a1ffed1facc97166db46c1996fbc15d4c2df6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-function-expressions-with-params.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +function Foo() { + return (function t() { + let x = {}; + let y = {}; + return function a(x = () => {}) { + return (function b(y = []) { + return [x, y]; + })(); + }; + })(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = function a(t1) { + const x_0 = t1 === undefined ? _temp : t1; + return (function b(t2) { + const y_0 = t2 === undefined ? [] : t2; + return [x_0, y_0]; + })(); + }; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} +function _temp() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-function-expressions-with-params.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-function-expressions-with-params.js new file mode 100644 index 0000000000000000000000000000000000000000..310204ae62cdab2e5f03211e84ba3e7a710901df --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-function-expressions-with-params.js @@ -0,0 +1,16 @@ +function Foo() { + return (function t() { + let x = {}; + let y = {}; + return function a(x = () => {}) { + return (function b(y = []) { + return [x, y]; + })(); + }; + })(); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Foo, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b805d6eacd80f1ad376a572d7da6698735d67943 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.expect.md @@ -0,0 +1,245 @@ + +## Input + +```javascript +// Deeply nested JSX that exercises the JSON recursion limit. +// At the NAPI bridge, serde_json has a default limit of 128 which +// is exceeded by the nested structure of the serialized AST. + +function DeeplyNestedComponent() { + return ( +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + leaf + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ); +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // Deeply nested JSX that exercises the JSON recursion limit. +// At the NAPI bridge, serde_json has a default limit of 128 which +// is exceeded by the nested structure of the serialized AST. + +function DeeplyNestedComponent() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ( +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + leaf + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.js new file mode 100644 index 0000000000000000000000000000000000000000..e2436bb0bc69df9a10936294ecabfe231f209100 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/deeply-nested-jsx-recursion-limit.js @@ -0,0 +1,111 @@ +// Deeply nested JSX that exercises the JSON recursion limit. +// At the NAPI bridge, serde_json has a default limit of 128 which +// is exceeded by the nested structure of the serialized AST. + +function DeeplyNestedComponent() { + return ( +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + leaf + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..db406da6a18c29507aab37121a5d894b7b05bc29 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +function Component(x = [-1, 1]) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(t0) { + const $ = _c(2); + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? [-1, 1] : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +### Eval output +(kind: ok) [-1,1] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.js new file mode 100644 index 0000000000000000000000000000000000000000..bf0dd4b5ffe8c2a4bbd1a54e082565a1c0061103 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-array-with-unary.js @@ -0,0 +1,8 @@ +function Component(x = [-1, 1]) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0cd9742942a9c7f02190d17faa097d224abf0ec5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(x = identity([() => {}, true, 42, 'hello'])) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(t0) { + const $ = _c(2); + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? identity([_temp, true, 42, "hello"]) : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + return x; +} +function _temp() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +### Eval output +(kind: ok) ["[[ function params=0 ]]",true,42,"hello"] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.js new file mode 100644 index 0000000000000000000000000000000000000000..d8981d97b9ee319291a95b65d74419a44552d8ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-calls-global-function.js @@ -0,0 +1,10 @@ +import {identity} from 'shared-runtime'; + +function Component(x = identity([() => {}, true, 42, 'hello'])) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..43249cfc1e878a16e98e8228690ed6a0d6bc8212 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Component(x = () => {}) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +function Component(t0) { + const x = t0 === undefined ? _temp : t0; + return x; +} +function _temp() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +### Eval output +(kind: ok) "[[ function params=0 ]]" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..03403534087c9b2e9c937af53833c2861b13a4fd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-empty-callback.js @@ -0,0 +1,8 @@ +function Component(x = () => {}) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..e0b0b51308a30a5bddc8966490406148ea5fd01c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(x = () => [-1, true, 42.0, 'hello']) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +function Component(t0) { + const x = t0 === undefined ? _temp : t0; + return x; +} +function _temp() { + return [-1, true, 42, "hello"]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +### Eval output +(kind: ok) "[[ function params=0 ]]" \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.js new file mode 100644 index 0000000000000000000000000000000000000000..4034364ec9a0a9bd2d27ff31c858100b3fd48977 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/default-param-with-reorderable-callback.js @@ -0,0 +1,8 @@ +function Component(x = () => [-1, true, 42.0, 'hello']) { + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-computed-property.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-computed-property.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..02be1a0ca478284362dd3f526ed2e3fffba2bfd3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-computed-property.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +function Component(props) { + const x = {a: props.a, b: props.b}; + const key = 'b'; + delete x[key]; + 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(3); + let x; + if ($[0] !== props.a || $[1] !== props.b) { + x = { a: props.a, b: props.b }; + + delete x["b"]; + $[0] = props.a; + $[1] = props.b; + $[2] = x; + } else { + x = $[2]; + } + 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/delete-computed-property.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-computed-property.js new file mode 100644 index 0000000000000000000000000000000000000000..5cc8ceb8e1e001eac40c43b8a947ab58f970f53a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-computed-property.js @@ -0,0 +1,12 @@ +function Component(props) { + const x = {a: props.a, b: props.b}; + const key = 'b'; + delete x[key]; + 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/delete-property.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-property.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a68721b5a221cd04bc9aee49e5aa6d9d391351a2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-property.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +function Component(props) { + const x = {a: props.a, b: props.b}; + delete x.b; + 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(3); + let x; + if ($[0] !== props.a || $[1] !== props.b) { + x = { a: props.a, b: props.b }; + delete x.b; + $[0] = props.a; + $[1] = props.b; + $[2] = x; + } else { + x = $[2]; + } + 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/delete-property.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-property.js new file mode 100644 index 0000000000000000000000000000000000000000..4d0354f3c31a86888bb998d890109d4a63a281fe --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/delete-property.js @@ -0,0 +1,11 @@ +function Component(props) { + const x = {a: props.a, b: props.b}; + delete x.b; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies-outputs.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies-outputs.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..80f0508fd8330fa6167b7e525fc430d3becf89d2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies-outputs.expect.md @@ -0,0 +1,70 @@ + +## Input + +```javascript +function foo(a, b) { + const x = []; + x.push(a); +
{x}
; + + const y = []; + if (x.length) { + y.push(x); + } + if (b) { + y.push(b); + } + return y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(a, b) { + const $ = _c(5); + let x; + if ($[0] !== a) { + x = []; + x.push(a); + $[0] = a; + $[1] = x; + } else { + x = $[1]; + } + let y; + if ($[2] !== b || $[3] !== x) { + y = []; + if (x.length) { + y.push(x); + } + + if (b) { + y.push(b); + } + $[2] = b; + $[3] = x; + $[4] = y; + } else { + y = $[4]; + } + + return y; +} + +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/dependencies-outputs.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies-outputs.js new file mode 100644 index 0000000000000000000000000000000000000000..4aed98527db042ac51d9feaabef7216d72cd060a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies-outputs.js @@ -0,0 +1,20 @@ +function foo(a, b) { + const x = []; + x.push(a); +
{x}
; + + const y = []; + if (x.length) { + y.push(x); + } + if (b) { + y.push(b); + } + return y; +} + +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/dependencies.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..057c6ba25338d810e403d5740c6a537c49d9f5a1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +function foo(x, y, z) { + const items = [z]; + items.push(x); + + const items2 = []; + if (x) { + items2.push(y); + } + + if (y) { + items.push(x); + } + + return items2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo(x, y, z) { + const $ = _c(3); + const items = [z]; + items.push(x); + let items2; + if ($[0] !== x || $[1] !== y) { + items2 = []; + if (x) { + items2.push(y); + } + $[0] = x; + $[1] = y; + $[2] = items2; + } else { + items2 = $[2]; + } + + if (y) { + items.push(x); + } + + return items2; +} + +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/dependencies.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies.js new file mode 100644 index 0000000000000000000000000000000000000000..206ee7a106851e785fcedc2a83a2af7599852ae3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dependencies.js @@ -0,0 +1,21 @@ +function foo(x, y, z) { + const items = [z]; + items.push(x); + + const items2 = []; + if (x) { + items2.push(y); + } + + if (y) { + items.push(x); + } + + return items2; +} + +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/destructure-array-assignment-to-context-var.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-assignment-to-context-var.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1268cbcfdc3d3ad153e9e424b4122b326f7f7571 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-assignment-to-context-var.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + let x; + [x] = props.value; + const foo = () => { + x = identity(props.value[0]); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [42]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let x; + if ($[0] !== props.value) { + const [t0] = props.value; + x = t0; + const foo = () => { + x = identity(props.value[0]); + }; + + foo(); + $[0] = props.value; + $[1] = x; + } else { + x = $[1]; + } + let t0; + if ($[2] !== x) { + t0 = { x }; + $[2] = x; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [42] }], +}; + +``` + +### Eval output +(kind: ok) {"x":42} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-assignment-to-context-var.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-assignment-to-context-var.js new file mode 100644 index 0000000000000000000000000000000000000000..349bea080c1f3a5a8ac05985880a7976f737b240 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-assignment-to-context-var.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + let x; + [x] = props.value; + const foo = () => { + x = identity(props.value[0]); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [42]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..769e4871f4ad8155d34bc1826e66795e9b7040e8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + let [x] = props.value; + const foo = () => { + x = identity(props.value[0]); + }; + foo(); + return
{x}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [42]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let x; + if ($[0] !== props.value) { + const [t0] = props.value; + x = t0; + const foo = () => { + x = identity(props.value[0]); + }; + + foo(); + $[0] = props.value; + $[1] = x; + } else { + x = $[1]; + } + let t0; + if ($[2] !== x) { + t0 =
{x}
; + $[2] = x; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [42] }], +}; + +``` + +### Eval output +(kind: ok)
42
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.js new file mode 100644 index 0000000000000000000000000000000000000000..bc9324a35f06d48dfcf96c55733aa5fc1e0b7bf0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-array-declaration-to-context-var.js @@ -0,0 +1,15 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + let [x] = props.value; + const foo = () => { + x = identity(props.value[0]); + }; + foo(); + return
{x}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [42]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cbe56f29c07f91d34683975f902e7a3ea06f245c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +let someGlobal = {}; +function component(a) { + let x = {a, someGlobal}; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['value 1'], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +let someGlobal = {}; +function component(a) { + const $ = _c(2); + let t0; + if ($[0] !== a) { + t0 = { a, someGlobal }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ["value 1"], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {"a":"value 1","someGlobal":{}} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.js new file mode 100644 index 0000000000000000000000000000000000000000..926a18e1e4b84f151e4da778be1de83bfc28ec18 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-capture-global.js @@ -0,0 +1,11 @@ +let someGlobal = {}; +function component(a) { + let x = {a, someGlobal}; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: component, + params: ['value 1'], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..de9083fbec86709a75881ef1a8fc0260b245f5a4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function Component(props) { + const [x = [-1, 1]] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: []}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + const [t0] = props.value; + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? [-1, 1] : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [] }], +}; + +``` + +### Eval output +(kind: ok) [-1,1] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.js new file mode 100644 index 0000000000000000000000000000000000000000..56f1ab229a9394dcb49dcf1968613db91f446cd4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-default-array-with-unary.js @@ -0,0 +1,9 @@ +function Component(props) { + const [x = [-1, 1]] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: []}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3b2768d350a686136e779068ac53936eca5f9fe3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-direct-reassignment.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +function foo(props) { + let x, y; + ({x, y} = {x: props.a, y: props.b}); + console.log(x); // prevent DCE from eliminating `x` altogether + x = props.c; + return x + y; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +// @enablePreserveExistingMemoizationGuarantees:false +function foo(props) { + let x; + let y; + ({ x, y } = { x: props.a, y: props.b }); + console.log(x); + x = props.c; + return x + y; +} + +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/destructure-direct-reassignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-direct-reassignment.js new file mode 100644 index 0000000000000000000000000000000000000000..ec10d66f26d2716d4ce968992f66be3362a53049 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-direct-reassignment.js @@ -0,0 +1,14 @@ +// @enablePreserveExistingMemoizationGuarantees:false +function foo(props) { + let x, y; + ({x, y} = {x: props.a, y: props.b}); + console.log(x); // prevent DCE from eliminating `x` altogether + x = props.c; + return x + y; +} + +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/destructure-in-branch-ssa.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b15978910626795aa2aae31afb5c582bd39648f1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.expect.md @@ -0,0 +1,84 @@ + +## Input + +```javascript +function useFoo(props: { + x?: string; + y?: string; + z?: string; + doDestructure: boolean; +}) { + let x = null; + let y = null; + let z = null; + const myList = []; + if (props.doDestructure) { + ({x, y, z} = props); + + myList.push(z); + } + return { + x, + y, + myList, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{x: 'hello', y: 'world', doDestructure: true}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function useFoo(props) { + const $ = _c(9); + + let x = null; + let y = null; + let z; + let myList; + if ($[0] !== props) { + myList = []; + if (props.doDestructure) { + ({ x, y, z } = props); + + myList.push(z); + } + $[0] = props; + $[1] = myList; + $[2] = x; + $[3] = y; + $[4] = z; + } else { + myList = $[1]; + x = $[2]; + y = $[3]; + z = $[4]; + } + let t0; + if ($[5] !== myList || $[6] !== x || $[7] !== y) { + t0 = { x, y, myList }; + $[5] = myList; + $[6] = x; + $[7] = y; + $[8] = t0; + } else { + t0 = $[8]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{ x: "hello", y: "world", doDestructure: true }], +}; + +``` + +### Eval output +(kind: ok) {"x":"hello","y":"world","myList":[null]} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts new file mode 100644 index 0000000000000000000000000000000000000000..68ebba9322dc9e38d8d5d286881fbdcaffa019b5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-in-branch-ssa.ts @@ -0,0 +1,26 @@ +function useFoo(props: { + x?: string; + y?: string; + z?: string; + doDestructure: boolean; +}) { + let x = null; + let y = null; + let z = null; + const myList = []; + if (props.doDestructure) { + ({x, y, z} = props); + + myList.push(z); + } + return { + x, + y, + myList, + }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [{x: 'hello', y: 'world', doDestructure: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..559cd4445269cbbcbf83787000617ca305392730 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +function foo() { + const {'data-foo-bar': x, a: y, data: z} = {'data-foo-bar': 1, a: 2, data: 3}; + return [x, y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo() { + const $ = _c(2); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { "data-foo-bar": 1, a: 2, data: 3 }; + $[0] = t0; + } else { + t0 = $[0]; + } + const { "data-foo-bar": x, a: y, data: z } = t0; + let t1; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + t1 = [x, y, z]; + $[1] = t1; + } else { + t1 = $[1]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) [1,2,3] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.js new file mode 100644 index 0000000000000000000000000000000000000000..5cda90c4d5386c5a1b921689ef9ed2538659753a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-mixed-property-key-types.js @@ -0,0 +1,10 @@ +function foo() { + const {'data-foo-bar': x, a: y, data: z} = {'data-foo-bar': 1, a: 2, data: 3}; + return [x, y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..e66ef2df13d5ff680d96d86c6d0bc7399cb2ccec --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.expect.md @@ -0,0 +1,65 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + let x; + ({x} = props); + const foo = () => { + x = identity(props.x); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let x; + if ($[0] !== props) { + const { x: t0 } = props; + x = t0; + const foo = () => { + x = identity(props.x); + }; + + foo(); + $[0] = props; + $[1] = x; + } else { + x = $[1]; + } + let t0; + if ($[2] !== x) { + t0 = { x }; + $[2] = x; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"x":42} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.js new file mode 100644 index 0000000000000000000000000000000000000000..d2a5c7d52c7dd69896ec291025c758e92cdb5ad8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-assignment-to-context-var.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + let x; + ({x} = props); + const foo = () => { + x = identity(props.x); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..66799c5c4720b8c14993f96c7b54fd3bba16dd68 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + let {x} = props; + const foo = () => { + x = identity(props.x); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + let x; + if ($[0] !== props) { + const { x: t0 } = props; + x = t0; + const foo = () => { + x = identity(props.x); + }; + + foo(); + $[0] = props; + $[1] = x; + } else { + x = $[1]; + } + let t0; + if ($[2] !== x) { + t0 = { x }; + $[2] = x; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: 42 }], +}; + +``` + +### Eval output +(kind: ok) {"x":42} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.js new file mode 100644 index 0000000000000000000000000000000000000000..bf1c778bc2d588eddaaf7da5a943b7e29f42ed1e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-object-declaration-to-context-var.js @@ -0,0 +1,15 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + let {x} = props; + const foo = () => { + x = identity(props.x); + }; + foo(); + return {x}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..56213c82566bde26a0767f8c43129097bd3e216c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function foo({'data-foo-bar': dataTestID}) { + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{'data-foo-bar': {}}], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo(t0) { + const { "data-foo-bar": dataTestID } = t0; + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{ "data-foo-bar": {} }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.js new file mode 100644 index 0000000000000000000000000000000000000000..a402345d5c0ab7e6b5c6564de5455dfc2e9338fa --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key-invalid-identifier.js @@ -0,0 +1,9 @@ +function foo({'data-foo-bar': dataTestID}) { + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{'data-foo-bar': {}}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..5b30debae7768ad06e8257fe83af4e8c88a5e800 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function foo({data: dataTestID}) { + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{data: {}}], + isComponent: false, +}; + +``` + +## Code + +```javascript +function foo(t0) { + const { data: dataTestID } = t0; + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{ data: {} }], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) {} \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.js new file mode 100644 index 0000000000000000000000000000000000000000..9749e11d46dea54da335315ae539f224e0930086 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-param-string-literal-key.js @@ -0,0 +1,9 @@ +function foo({data: dataTestID}) { + return dataTestID; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [{data: {}}], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ba35e5af57b289bdce18cf33f30ff6660e8c49c0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo() { + const {'data-foo-bar': t} = {'data-foo-bar': 1}; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { "data-foo-bar": 1 }; + $[0] = t0; + } else { + t0 = $[0]; + } + const { "data-foo-bar": t } = t0; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 1 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.js new file mode 100644 index 0000000000000000000000000000000000000000..3d6eb9d95ee7cdfdf347e3e12f2b5904991c06a0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-invalid-identifier-property-key.js @@ -0,0 +1,10 @@ +function foo() { + const {'data-foo-bar': t} = {'data-foo-bar': 1}; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b233f52cb699a8be465dfed2292682eb18cb40ba --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function foo() { + const {data: t} = {data: 1}; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function foo() { + const $ = _c(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = { data: 1 }; + $[0] = t0; + } else { + t0 = $[0]; + } + const { data: t } = t0; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) 1 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.js new file mode 100644 index 0000000000000000000000000000000000000000..6bb26396ba2ea1dd93a1fba4e84944b506071bdc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructure-string-literal-property-key.js @@ -0,0 +1,10 @@ +function foo() { + const {data: t} = {data: 1}; + return t; +} + +export const FIXTURE_ENTRYPOINT = { + fn: foo, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..971f9a1f4e45d7d841fbc492678c1d98c42d3c9f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-default.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function Component(props) { + const [[x] = ['default']] = props.y; + 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); + const [t0] = props.y; + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? ["default"] : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const [x] = t1; + 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/destructuring-array-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-default.js new file mode 100644 index 0000000000000000000000000000000000000000..e744d01265f3be590aaa4486c47bf82bc29255bc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-default.js @@ -0,0 +1,10 @@ +function Component(props) { + const [[x] = ['default']] = props.y; + 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/destructuring-array-param-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-param-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..414d3b1801a49e2ea7a9d2736cec1c3b32d83c60 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-param-default.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Component([a = 2]) { + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(t0) { + const [t1] = t0; + const a = t1 === undefined ? 2 : t1; + return a; +} + +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/destructuring-array-param-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-param-default.js new file mode 100644 index 0000000000000000000000000000000000000000..37a48cda67d475e4576875d03921606521bab734 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-array-param-default.js @@ -0,0 +1,9 @@ +function Component([a = 2]) { + return a; +} + +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/destructuring-assignment-array-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..966826aab82f6b3731d47ac32ea209fa761881c3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +function Component(props) { + let x; + if (props.cond) { + [[x] = ['default']] = props.y; + } else { + x = props.fallback; + } + 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 x; + if (props.cond) { + const [t0] = props.y; + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? ["default"] : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + [x] = t1; + } else { + x = props.fallback; + } + + 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/destructuring-assignment-array-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.js new file mode 100644 index 0000000000000000000000000000000000000000..6561b0cd9886828c988f528b6eccac575567fe4d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment-array-default.js @@ -0,0 +1,15 @@ +function Component(props) { + let x; + if (props.cond) { + [[x] = ['default']] = props.y; + } else { + x = props.fallback; + } + 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/destructuring-assignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..98025e3fa999709d259a83f1bb10973ff1109d20 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment.expect.md @@ -0,0 +1,73 @@ + +## Input + +```javascript +function foo(a, b, c) { + let d, g, n, o; + [ + d, + [ + { + e: {f: g}, + }, + ], + ] = a; + ({ + l: { + m: [[n]], + }, + o, + } = b); + return {d, g, n, o}; +} + +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(5); + let d; + let g; + let n; + let o; + const [t0, t1] = a; + d = t0; + const [t2] = t1; + const { e: t3 } = t2; + ({ f: g } = t3); + const { l: t4, o: t5 } = b; + const { m: t6 } = t4; + const [t7] = t6; + [n] = t7; + o = t5; + let t8; + if ($[0] !== d || $[1] !== g || $[2] !== n || $[3] !== o) { + t8 = { d, g, n, o }; + $[0] = d; + $[1] = g; + $[2] = n; + $[3] = o; + $[4] = t8; + } else { + t8 = $[4]; + } + return t8; +} + +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/destructuring-assignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment.js new file mode 100644 index 0000000000000000000000000000000000000000..7b61954c196d225f6ee12cd215ba5fcf4e404575 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-assignment.js @@ -0,0 +1,24 @@ +function foo(a, b, c) { + let d, g, n, o; + [ + d, + [ + { + e: {f: g}, + }, + ], + ] = a; + ({ + l: { + m: [[n]], + }, + o, + } = b); + return {d, g, n, o}; +} + +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/destructuring-default-at-array-hole.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..17db0c2263a1bcb72f2ed7e88c8f307e538ba610 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + // destructure slot index has a hole in the input, should return default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [, /* hole! */ 3.14]}], +}; + +``` + +## Code + +```javascript +function Component(props) { + const [t0] = props.value; + const x = t0 === undefined ? 42 : t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [, /* hole! */ 3.14] }], +}; + +``` + +### 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/destructuring-default-at-array-hole.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.js new file mode 100644 index 0000000000000000000000000000000000000000..1495095de04be0dbceacae768d399003ba73a0f9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-array-hole.js @@ -0,0 +1,10 @@ +function Component(props) { + // destructure slot index has a hole in the input, should return default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [, /* hole! */ 3.14]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d51ef440a6f1be76b1ee8545d49b96b825bffaf9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + // destructure slot index has an explicit null in the input, should return null (not the default) + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [null]}], +}; + +``` + +## Code + +```javascript +function Component(props) { + const [t0] = props.value; + const x = t0 === undefined ? 42 : t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [null] }], +}; + +``` + +### Eval output +(kind: ok) null \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.js new file mode 100644 index 0000000000000000000000000000000000000000..7b41f5e01e21a1224f6585d804dc487f0959f991 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-null.js @@ -0,0 +1,10 @@ +function Component(props) { + // destructure slot index has an explicit null in the input, should return null (not the default) + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [null]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8c1058adf6e461006f69aef11c1f34554e8ddd6b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + // destructure slot index has an explicit undefined in the input, should return default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [undefined]}], +}; + +``` + +## Code + +```javascript +function Component(props) { + const [t0] = props.value; + const x = t0 === undefined ? 42 : t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [undefined] }], +}; + +``` + +### 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/destructuring-default-at-explicit-undefined.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.js new file mode 100644 index 0000000000000000000000000000000000000000..ea3a917fc60f7216b29b2010b37bc4bca7418c2b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-at-explicit-undefined.js @@ -0,0 +1,10 @@ +function Component(props) { + // destructure slot index has an explicit undefined in the input, should return default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: [undefined]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2bc5f650e1d0a66a75ce58e46129fc1d8cb2d4d9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +function Component(props) { + // destructure past end of empty array, should evaluate to default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: []}], +}; + +``` + +## Code + +```javascript +function Component(props) { + const [t0] = props.value; + const x = t0 === undefined ? 42 : t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: [] }], +}; + +``` + +### 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/destructuring-default-past-end-of-array.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.js new file mode 100644 index 0000000000000000000000000000000000000000..6050d72ed7c70a24bfa753d4119adda7030ba852 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-default-past-end-of-array.js @@ -0,0 +1,10 @@ +function Component(props) { + // destructure past end of empty array, should evaluate to default + const [x = 42] = props.value; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: []}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3a8f9e84cac1425b6641d15402f41e95cc073a64 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.expect.md @@ -0,0 +1,131 @@ + +## Input + +```javascript +import {Stringify, graphql} from 'shared-runtime'; + +function useFragment(_arg1, _arg2) { + 'use no forget'; + return { + urls: ['url1', 'url2', 'url3'], + comments: ['comment1'], + }; +} + +function Component(props) { + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post + ); + const allUrls = []; + // `media` and `urls` are exported from the scope that will wrap this code, + // but `comments` is not (it doesn't need to be memoized, bc the callback + // only checks `comments.length`) + // because of the scope, the let declaration for media and urls are lifted + // out of the scope, and the destructure statement ends up turning into + // a reassignment, instead of a const declaration. this means we try to + // reassign `comments` when there's no declaration for it. + const {media = null, comments = [], urls = []} = post; + const onClick = e => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + allUrls.push(...urls); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{post: {}}], + isComponent: true, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify, graphql } from "shared-runtime"; + +function useFragment(_arg1, _arg2) { + "use no forget"; + return { + urls: ["url1", "url2", "url3"], + comments: ["comment1"], + }; +} + +function Component(props) { + const $ = _c(8); + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post, + ); + let t0; + if ($[0] !== post) { + const allUrls = []; + const { media: t1, comments: t2, urls: t3 } = post; + const media = t1 === undefined ? null : t1; + let t4; + if ($[2] !== t2) { + t4 = t2 === undefined ? [] : t2; + $[2] = t2; + $[3] = t4; + } else { + t4 = $[3]; + } + const comments = t4; + let t5; + if ($[4] !== t3) { + t5 = t3 === undefined ? [] : t3; + $[4] = t3; + $[5] = t5; + } else { + t5 = $[5]; + } + const urls = t5; + let t6; + if ($[6] !== comments.length) { + t6 = (e) => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + $[6] = comments.length; + $[7] = t6; + } else { + t6 = $[7]; + } + const onClick = t6; + allUrls.push(...urls); + t0 = ; + $[0] = post; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ post: {} }], + isComponent: true, +}; + +``` + +### Eval output +(kind: ok)
{"media":null,"allUrls":["url1","url2","url3"],"onClick":"[[ 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/destructuring-mixed-scope-and-local-variables-with-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.js new file mode 100644 index 0000000000000000000000000000000000000000..3aa47dca46f4fdf3a5c10bd5932574c5079c08b0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-and-local-variables-with-default.js @@ -0,0 +1,43 @@ +import {Stringify, graphql} from 'shared-runtime'; + +function useFragment(_arg1, _arg2) { + 'use no forget'; + return { + urls: ['url1', 'url2', 'url3'], + comments: ['comment1'], + }; +} + +function Component(props) { + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post + ); + const allUrls = []; + // `media` and `urls` are exported from the scope that will wrap this code, + // but `comments` is not (it doesn't need to be memoized, bc the callback + // only checks `comments.length`) + // because of the scope, the let declaration for media and urls are lifted + // out of the scope, and the destructure statement ends up turning into + // a reassignment, instead of a const declaration. this means we try to + // reassign `comments` when there's no declaration for it. + const {media = null, comments = [], urls = []} = post; + const onClick = e => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + allUrls.push(...urls); + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{post: {}}], + isComponent: true, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fbb6e50871dc7570f944ff80df77013895ee848d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.expect.md @@ -0,0 +1,82 @@ + +## Input + +```javascript +import {useFragment} from 'shared-runtime'; + +function Component(props) { + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post + ); + const allUrls = []; + // `media` and `urls` are exported from the scope that will wrap this code, + // but `comments` is not (it doesn't need to be memoized, bc the callback + // only checks `comments.length`) + // because of the scope, the let declaration for media and urls are lifted + // out of the scope, and the destructure statement ends up turning into + // a reassignment, instead of a const declaration. this means we try to + // reassign `comments` when there's no declaration for it. + const {media, comments, urls} = post; + const onClick = e => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + allUrls.push(...urls); + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { useFragment } from "shared-runtime"; + +function Component(props) { + const $ = _c(4); + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post, + ); + let t0; + if ($[0] !== post) { + const allUrls = []; + const { media, comments, urls } = post; + let t1; + if ($[2] !== comments.length) { + t1 = (e) => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + $[2] = comments.length; + $[3] = t1; + } else { + t1 = $[3]; + } + const onClick = t1; + allUrls.push(...urls); + t0 = ; + $[0] = post; + $[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/destructuring-mixed-scope-declarations-and-locals.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.js new file mode 100644 index 0000000000000000000000000000000000000000..5d1377c458855032e4ca71918dc621e38a3c92f4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-mixed-scope-declarations-and-locals.js @@ -0,0 +1,29 @@ +import {useFragment} from 'shared-runtime'; + +function Component(props) { + const post = useFragment( + graphql` + fragment F on T { + id + } + `, + props.post + ); + const allUrls = []; + // `media` and `urls` are exported from the scope that will wrap this code, + // but `comments` is not (it doesn't need to be memoized, bc the callback + // only checks `comments.length`) + // because of the scope, the let declaration for media and urls are lifted + // out of the scope, and the destructure statement ends up turning into + // a reassignment, instead of a const declaration. this means we try to + // reassign `comments` when there's no declaration for it. + const {media, comments, urls} = post; + const onClick = e => { + if (!comments.length) { + return; + } + console.log(comments.length); + }; + allUrls.push(...urls); + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..48ae27c74e153ebeb4457c340a8f23c0fe7e3cb0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-default.expect.md @@ -0,0 +1,44 @@ + +## Input + +```javascript +function Component(props) { + const {x: {y} = {y: 'default'}} = props.y; + return y; +} + +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); + const { x: t0 } = props.y; + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? { y: "default" } : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const { y } = t1; + return y; +} + +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/destructuring-object-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-default.js new file mode 100644 index 0000000000000000000000000000000000000000..e9d7f3477f1419dea4d356cd5880db033009cd3f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-default.js @@ -0,0 +1,10 @@ +function Component(props) { + const {x: {y} = {y: 'default'}} = props.y; + return 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/destructuring-object-param-default.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-param-default.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3e2322fc9faf4748cb12b2fa2c330cf465c0b350 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-param-default.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Component({a = 2}) { + return a; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(t0) { + const { a: t1 } = t0; + const a = t1 === undefined ? 2 : t1; + return a; +} + +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/destructuring-object-param-default.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-param-default.js new file mode 100644 index 0000000000000000000000000000000000000000..180625d24927b3cd1cdb1da82f741f4877e95893 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-param-default.js @@ -0,0 +1,9 @@ +function Component({a = 2}) { + return a; +} + +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/destructuring-object-pattern-within-rest.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-pattern-within-rest.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..677107be83a2a03b94002c3cbc7c0d5bcd497801 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-pattern-within-rest.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +function Component(props) { + const [y, ...{z}] = props.value; + return [y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: ['y', {z: 'z!'}]}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(6); + let t0; + let y; + if ($[0] !== props.value) { + [y, ...t0] = props.value; + $[0] = props.value; + $[1] = t0; + $[2] = y; + } else { + t0 = $[1]; + y = $[2]; + } + const { z } = t0; + let t1; + if ($[3] !== y || $[4] !== z) { + t1 = [y, z]; + $[3] = y; + $[4] = z; + $[5] = t1; + } else { + t1 = $[5]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: ["y", { z: "z!" }] }], +}; + +``` + +### Eval output +(kind: ok) ["y",null] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-pattern-within-rest.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-pattern-within-rest.js new file mode 100644 index 0000000000000000000000000000000000000000..ebf4e4839ad1786efd1fb7a6ad19792f75c9e597 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-object-pattern-within-rest.js @@ -0,0 +1,9 @@ +function Component(props) { + const [y, ...{z}] = props.value; + return [y, z]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: ['y', {z: 'z!'}]}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-property-inference.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-property-inference.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a43d5756096bf36a61b55e4a4ffb693ae3523b44 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-property-inference.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +function Component(props) { + const x = []; + x.push(props.value); + const {length: y} = x; + foo(y); + return [x, y]; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(5); + let x; + if ($[0] !== props.value) { + x = []; + x.push(props.value); + $[0] = props.value; + $[1] = x; + } else { + x = $[1]; + } + const { length: y } = x; + foo(y); + let t0; + if ($[2] !== x || $[3] !== y) { + t0 = [x, y]; + $[2] = x; + $[3] = y; + $[4] = t0; + } else { + t0 = $[4]; + } + 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/destructuring-property-inference.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-property-inference.js new file mode 100644 index 0000000000000000000000000000000000000000..179b266efaa70e11ebb27ff8aeb62aefadfd4663 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-property-inference.js @@ -0,0 +1,7 @@ +function Component(props) { + const x = []; + x.push(props.value); + const {length: y} = x; + foo(y); + return [x, y]; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..3e1c4771eabfb33ec438c92e15af57d0841bef81 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +import {identity} from 'shared-runtime'; + +function Component(props) { + const { + x: {destructured}, + sameName: renamed, + } = props; + const sameName = identity(destructured); + + return [sameName, renamed]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: {destructured: 0}, sameName: 2}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { identity } from "shared-runtime"; + +function Component(props) { + const $ = _c(5); + const { x: t0, sameName: renamed } = props; + const { destructured } = t0; + let t1; + if ($[0] !== destructured) { + t1 = identity(destructured); + $[0] = destructured; + $[1] = t1; + } else { + t1 = $[1]; + } + const sameName = t1; + let t2; + if ($[2] !== renamed || $[3] !== sameName) { + t2 = [sameName, renamed]; + $[2] = renamed; + $[3] = sameName; + $[4] = t2; + } else { + t2 = $[4]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ x: { destructured: 0 }, sameName: 2 }], +}; + +``` + +### Eval output +(kind: ok) [0,2] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.js new file mode 100644 index 0000000000000000000000000000000000000000..73faee9ab1bcdd37c5b6b7501066cf1b464bf1ff --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.js @@ -0,0 +1,16 @@ +import {identity} from 'shared-runtime'; + +function Component(props) { + const { + x: {destructured}, + sameName: renamed, + } = props; + const sameName = identity(destructured); + + return [sameName, renamed]; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{x: {destructured: 0}, sameName: 2}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f981cf0c2d5f4aff06a33fa9fb695417a64faf1f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function Component(props) { + const [x = true ? 1 : 0] = props.y; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: []}], +}; + +``` + +## Code + +```javascript +function Component(props) { + const [t0] = props.y; + const x = t0 === undefined ? (true ? 1 : 0) : t0; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ y: [] }], +}; + +``` + +### 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/destructuring-with-conditional-as-default-value.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.js new file mode 100644 index 0000000000000000000000000000000000000000..4b8cf1b25dc8ebc71032694e11c325a9d6258f9a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-conditional-as-default-value.js @@ -0,0 +1,9 @@ +function Component(props) { + const [x = true ? 1 : 0] = props.y; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: []}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..6fce86805e241be12c682c53064a1e9cf0edf53a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.expect.md @@ -0,0 +1,45 @@ + +## Input + +```javascript +// @flow +function Component(props) { + const [x = ([]: Array)] = props.y; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: []}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + const [t0] = props.y; + let t1; + if ($[0] !== t0) { + t1 = t0 === undefined ? ([]: Array) : t0; + $[0] = t0; + $[1] = t1; + } else { + t1 = $[1]; + } + const x = t1; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ y: [] }], +}; + +``` + +### Eval output +(kind: ok) [] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.js new file mode 100644 index 0000000000000000000000000000000000000000..6de20262bd3c95c826aee173a758370b1e4d7e27 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring-with-typecast-as-default-value.flow.js @@ -0,0 +1,10 @@ +// @flow +function Component(props) { + const [x = ([]: Array)] = props.y; + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: []}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f292e83e16438bb1152d1789ea3fc7b5e9614dc0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring.expect.md @@ -0,0 +1,112 @@ + +## Input + +```javascript +function foo(a, b, c) { + const [ + d, + [ + { + e: {f}, + ...g + }, + ], + ...h + ] = a; + const { + l: { + m: [[n], ...o], + }, + p, + } = b; + return [d, f, g, h, n, o, p]; +} + +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(18); + let d; + let h; + let t0; + if ($[0] !== a) { + [d, t0, ...h] = a; + $[0] = a; + $[1] = d; + $[2] = h; + $[3] = t0; + } else { + d = $[1]; + h = $[2]; + t0 = $[3]; + } + const [t1] = t0; + let g; + let t2; + if ($[4] !== t1) { + ({ e: t2, ...g } = t1); + $[4] = t1; + $[5] = g; + $[6] = t2; + } else { + g = $[5]; + t2 = $[6]; + } + const { f } = t2; + const { l: t3, p } = b; + const { m: t4 } = t3; + let o; + let t5; + if ($[7] !== t4) { + [t5, ...o] = t4; + $[7] = t4; + $[8] = o; + $[9] = t5; + } else { + o = $[8]; + t5 = $[9]; + } + const [n] = t5; + let t6; + if ( + $[10] !== d || + $[11] !== f || + $[12] !== g || + $[13] !== h || + $[14] !== n || + $[15] !== o || + $[16] !== p + ) { + t6 = [d, f, g, h, n, o, p]; + $[10] = d; + $[11] = f; + $[12] = g; + $[13] = h; + $[14] = n; + $[15] = o; + $[16] = p; + $[17] = t6; + } else { + t6 = $[17]; + } + return t6; +} + +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/destructuring.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring.js new file mode 100644 index 0000000000000000000000000000000000000000..9b9db33cf70ffe50127a97c332202703cb74c719 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/destructuring.js @@ -0,0 +1,25 @@ +function foo(a, b, c) { + const [ + d, + [ + { + e: {f}, + ...g + }, + ], + ...h + ] = a; + const { + l: { + m: [[n], ...o], + }, + p, + } = b; + return [d, f, g, h, n, o, p]; +} + +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/do-while-break.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-break.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..35fcda1c124692d4ead85b4c06fbcbd6c326cfc2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-break.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function Component(props) { + do { + break; + } while (props.cond); + return props; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(props) { + return props; +} + +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/do-while-break.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-break.js new file mode 100644 index 0000000000000000000000000000000000000000..3975854f92aa50ccca2257162f1ca81324d9257e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-break.js @@ -0,0 +1,12 @@ +function Component(props) { + do { + break; + } while (props.cond); + return props; +} + +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/do-while-compound-test.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-compound-test.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..845955d5c83800aec15eb9c8001b2223aeaaf4dc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-compound-test.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +function Component(props) { + let x = [1, 2, 3]; + let ret = []; + do { + let item = x.pop(); + ret.push(item * 2); + } while (x.length && props.cond); + return ret; +} + +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 ret; + if ($[0] !== props) { + const x = [1, 2, 3]; + ret = []; + do { + const item = x.pop(); + ret.push(item * 2); + } while (x.length && props.cond); + $[0] = props; + $[1] = ret; + } else { + ret = $[1]; + } + return ret; +} + +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/do-while-compound-test.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-compound-test.js new file mode 100644 index 0000000000000000000000000000000000000000..50f8f86264ea3e360c3ef3b8ff2ac75cf509618a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-compound-test.js @@ -0,0 +1,15 @@ +function Component(props) { + let x = [1, 2, 3]; + let ret = []; + do { + let item = x.pop(); + ret.push(item * 2); + } while (x.length && props.cond); + return ret; +} + +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/do-while-conditional-break.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-conditional-break.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1dd74b2d934e39387c593aa55cb0cf40d6729a5d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-conditional-break.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +function Component(props) { + let x = [0, 1, 2, 3]; + do { + if (x === 0) { + break; + } + mutate(x); + } while (props.cond); + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(2); + let x; + if ($[0] !== props) { + x = [0, 1, 2, 3]; + do { + if (x === 0) { + break; + } + + mutate(x); + } while (props.cond); + $[0] = props; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-conditional-break.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-conditional-break.js new file mode 100644 index 0000000000000000000000000000000000000000..c64286efc349ea9ed4e9e57614546fd312622c58 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-conditional-break.js @@ -0,0 +1,10 @@ +function Component(props) { + let x = [0, 1, 2, 3]; + do { + if (x === 0) { + break; + } + mutate(x); + } while (props.cond); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..26feb29eed65a24863cd2119f730d79b86a871c3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +function Component() { + const x = [0, 1, 2, 3]; + const ret = []; + do { + const item = x.pop(); + if (item === 0) { + continue; + } + ret.push(item / 2); + } while (x.length); + + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component() { + const $ = _c(1); + let ret; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const x = [0, 1, 2, 3]; + ret = []; + do { + const item = x.pop(); + if (item === 0) { + continue; + } + + ret.push(item / 2); + } while (x.length); + $[0] = ret; + } else { + ret = $[0]; + } + + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) [1.5,1,0.5] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.js new file mode 100644 index 0000000000000000000000000000000000000000..bf355adb280196ac91ee1b2e89e48fea647faa9a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-continue.js @@ -0,0 +1,19 @@ +function Component() { + const x = [0, 1, 2, 3]; + const ret = []; + do { + const item = x.pop(); + if (item === 0) { + continue; + } + ret.push(item / 2); + } while (x.length); + + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-early-unconditional-break.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-early-unconditional-break.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9200a81b22a7c7bac4793def7584fae726036fe5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-early-unconditional-break.expect.md @@ -0,0 +1,36 @@ + +## Input + +```javascript +function Component(props) { + let x = [1, 2, 3]; + do { + mutate(x); + break; + } while (props.cond); + return x; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(1); + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + x = [1, 2, 3]; + + mutate(x); + $[0] = x; + } else { + x = $[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/do-while-early-unconditional-break.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-early-unconditional-break.js new file mode 100644 index 0000000000000000000000000000000000000000..764f2d4be9f1321f8071f11cb3da821ceb2817c1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-early-unconditional-break.js @@ -0,0 +1,8 @@ +function Component(props) { + let x = [1, 2, 3]; + do { + mutate(x); + break; + } while (props.cond); + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9bafc6d8efbb70f7dfd891b457fbe31761171a7f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +function Component() { + let x = [1, 2, 3]; + let ret = []; + do { + let item = x.pop(); + ret.push(item * 2); + } while (x.length); + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component() { + const $ = _c(1); + let ret; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const x = [1, 2, 3]; + ret = []; + do { + const item = x.pop(); + ret.push(item * 2); + } while (x.length); + $[0] = ret; + } else { + ret = $[0]; + } + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; + +``` + +### Eval output +(kind: ok) [6,4,2] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.js new file mode 100644 index 0000000000000000000000000000000000000000..ac836aee747261514f1dde86f291a62fb971b53d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/do-while-simple.js @@ -0,0 +1,15 @@ +function Component() { + let x = [1, 2, 3]; + let ret = []; + do { + let item = x.pop(); + ret.push(item * 2); + } while (x.length); + return ret; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: false, +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..4b2c7e31b1dd5f1437647d916f2ed29c31ee4db6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.expect.md @@ -0,0 +1,89 @@ + +## Input + +```javascript +function Component(props) { + let x = 0; + label: if (props.a) { + x = 1; + } else { + if (props.b) { + x = 2; + } else { + break label; + } + x = 3; + } + label2: switch (props.c) { + case 'a': { + x = 4; + break; + } + case 'b': { + break label2; + } + case 'c': { + x = 5; + // intentional fallthrough + } + default: { + x = 6; + } + } + if (props.d) { + return null; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function Component(props) { + let x = 0; + bb0: if (props.a) { + x = 1; + } else { + if (props.b) { + } else { + break bb0; + } + + x = 3; + } + bb1: bb2: switch (props.c) { + case "a": { + x = 4; + break bb2; + } + case "b": { + break bb1; + } + case "c": + default: { + x = 6; + } + } + if (props.d) { + return null; + } + + 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/dominator.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.js new file mode 100644 index 0000000000000000000000000000000000000000..e22dee4ca6c671512723a8913585c96675fc9a51 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dominator.js @@ -0,0 +1,39 @@ +function Component(props) { + let x = 0; + label: if (props.a) { + x = 1; + } else { + if (props.b) { + x = 2; + } else { + break label; + } + x = 3; + } + label2: switch (props.c) { + case 'a': { + x = 4; + break; + } + case 'b': { + break label2; + } + case 'c': { + x = 5; + // intentional fallthrough + } + default: { + x = 6; + } + } + if (props.d) { + return null; + } + 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/dont-memoize-primitive-function-call-non-escaping-useMemo.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping-useMemo.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..93b08128a0afed4177bb4b5353bea62d6f1158e4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping-useMemo.expect.md @@ -0,0 +1,77 @@ + +## Input + +```javascript +// @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import {useMemo} from 'react'; +import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; + +function Component(props) { + const result = useMemo( + () => makeObject(props.value).value + 1, + [props.value] + ); + console.log(result); + return 'ok'; +} + +function makeObject(value) { + console.log(value); + return {value}; +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [ + {value: 42}, + {value: 42}, + {value: 3.14}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + ], +}; + +``` + +## Code + +```javascript +// @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import { useMemo } from "react"; +import { makeObject_Primitives, ValidateMemoization } from "shared-runtime"; + +function Component(props) { + const result = makeObject(props.value).value + 1; + + console.log(result); + return "ok"; +} + +function makeObject(value) { + console.log(value); + return { value }; +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], + sequentialRenders: [ + { value: 42 }, + { value: 42 }, + { value: 3.14 }, + { value: 3.14 }, + { value: 42 }, + { value: 3.14 }, + { value: 42 }, + { value: 3.14 }, + ], +}; + +``` + +### 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/dont-memoize-primitive-function-call-non-escaping-useMemo.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping-useMemo.js new file mode 100644 index 0000000000000000000000000000000000000000..2ee24917c53e62493805b30fdb2011a30028acb1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping-useMemo.js @@ -0,0 +1,32 @@ +// @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import {useMemo} from 'react'; +import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; + +function Component(props) { + const result = useMemo( + () => makeObject(props.value).value + 1, + [props.value] + ); + console.log(result); + return 'ok'; +} + +function makeObject(value) { + console.log(value); + return {value}; +} + +export const TODO_FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [ + {value: 42}, + {value: 42}, + {value: 3.14}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..0abd02f41222e1cf0d35c87392758ac6c7869886 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.expect.md @@ -0,0 +1,81 @@ + +## Input + +```javascript +// @expectNothingCompiled @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import {useMemo} from 'react'; +import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; + +function Component(props) { + const result = makeObject(props.value).value + 1; + console.log(result); + return 'ok'; +} + +function makeObject(value) { + console.log(value); + return {value}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [ + {value: 42}, + {value: 42}, + {value: 3.14}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + ], +}; + +``` + +## Code + +```javascript +// @expectNothingCompiled @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import { useMemo } from "react"; +import { makeObject_Primitives, ValidateMemoization } from "shared-runtime"; + +function Component(props) { + const result = makeObject(props.value).value + 1; + console.log(result); + return "ok"; +} + +function makeObject(value) { + console.log(value); + return { value }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], + sequentialRenders: [ + { value: 42 }, + { value: 42 }, + { value: 3.14 }, + { value: 3.14 }, + { value: 42 }, + { value: 3.14 }, + { value: 42 }, + { value: 3.14 }, + ], +}; + +``` + +### Eval output +(kind: ok) "ok" +"ok" +"ok" +"ok" +"ok" +"ok" +"ok" +"ok" +logs: [42,43,42,43,3.14,4.140000000000001,3.14,4.140000000000001,42,43,3.14,4.140000000000001,42,43,3.14,4.140000000000001] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.js new file mode 100644 index 0000000000000000000000000000000000000000..7477ee0a9dc91bf9032faf75519f2fa3c912efd4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-memoize-primitive-function-call-non-escaping.js @@ -0,0 +1,29 @@ +// @expectNothingCompiled @compilationMode:"infer" @enablePreserveExistingMemoizationGuarantees @validatePreserveExistingMemoizationGuarantees +import {useMemo} from 'react'; +import {makeObject_Primitives, ValidateMemoization} from 'shared-runtime'; + +function Component(props) { + const result = makeObject(props.value).value + 1; + console.log(result); + return 'ok'; +} + +function makeObject(value) { + console.log(value); + return {value}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], + sequentialRenders: [ + {value: 42}, + {value: 42}, + {value: 3.14}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + {value: 42}, + {value: 3.14}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ce5bfda64406b241c084f6b961a296446cc47124 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.expect.md @@ -0,0 +1,162 @@ + +## Input + +```javascript +import {ValidateMemoization} from 'shared-runtime'; + +// Achieving Forget's level of memoization precision in this example isn't possible with useMemo +// without significantly altering the code, so disable the non-Forget evaluation of this fixture. +// @disableNonForgetInSprout +function Component({a, b, c}) { + const x = []; + let y; + if (a) { + y = [b]; + } + x.push(c); + + // this scope should not merge with the above scope because y does not invalidate + // on changes to `c` + const z = [y]; + + // return [x, z]; + return ( + <> + + + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: false, b: null, c: 0}], + sequentialRenders: [ + {a: false, b: null, c: 0}, + {a: false, b: null, c: 1}, + {a: true, b: 0, c: 1}, + {a: true, b: 1, c: 1}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { ValidateMemoization } from "shared-runtime"; + +// Achieving Forget's level of memoization precision in this example isn't possible with useMemo +// without significantly altering the code, so disable the non-Forget evaluation of this fixture. +// @disableNonForgetInSprout +function Component(t0) { + const $ = _c(25); + const { a, b, c } = t0; + let x; + let y; + if ($[0] !== a || $[1] !== b || $[2] !== c) { + x = []; + if (a) { + let t1; + if ($[5] !== b) { + t1 = [b]; + $[5] = b; + $[6] = t1; + } else { + t1 = $[6]; + } + y = t1; + } + + x.push(c); + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + $[4] = y; + } else { + x = $[3]; + y = $[4]; + } + let t1; + if ($[7] !== y) { + t1 = [y]; + $[7] = y; + $[8] = t1; + } else { + t1 = $[8]; + } + const z = t1; + let t2; + if ($[9] !== a || $[10] !== b || $[11] !== c) { + t2 = [a, b, c]; + $[9] = a; + $[10] = b; + $[11] = c; + $[12] = t2; + } else { + t2 = $[12]; + } + let t3; + if ($[13] !== t2 || $[14] !== x) { + t3 = ; + $[13] = t2; + $[14] = x; + $[15] = t3; + } else { + t3 = $[15]; + } + let t4; + if ($[16] !== a || $[17] !== b) { + t4 = [a, b]; + $[16] = a; + $[17] = b; + $[18] = t4; + } else { + t4 = $[18]; + } + let t5; + if ($[19] !== t4 || $[20] !== z) { + t5 = ; + $[19] = t4; + $[20] = z; + $[21] = t5; + } else { + t5 = $[21]; + } + let t6; + if ($[22] !== t3 || $[23] !== t5) { + t6 = ( + <> + {t3} + {t5} + + ); + $[22] = t3; + $[23] = t5; + $[24] = t6; + } else { + t6 = $[24]; + } + return t6; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ a: false, b: null, c: 0 }], + sequentialRenders: [ + { a: false, b: null, c: 0 }, + { a: false, b: null, c: 1 }, + { a: true, b: 0, c: 1 }, + { a: true, b: 1, c: 1 }, + ], +}; + +``` + +### Eval output +(kind: ok)
{"inputs":[false,null,0],"output":[0]}
{"inputs":[false,null],"output":[null]}
+
{"inputs":[false,null,1],"output":[1]}
{"inputs":[false,null],"output":[null]}
+
{"inputs":[true,0,1],"output":[1]}
{"inputs":[true,0],"output":[[0]]}
+
{"inputs":[true,1,1],"output":[1]}
{"inputs":[true,1],"output":[[1]]}
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..25728f0ada7b160e99de5e0e33046908e1fa1ee1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-if-dep-is-inner-declaration-of-previous-scope.js @@ -0,0 +1,36 @@ +import {ValidateMemoization} from 'shared-runtime'; + +// Achieving Forget's level of memoization precision in this example isn't possible with useMemo +// without significantly altering the code, so disable the non-Forget evaluation of this fixture. +// @disableNonForgetInSprout +function Component({a, b, c}) { + const x = []; + let y; + if (a) { + y = [b]; + } + x.push(c); + + // this scope should not merge with the above scope because y does not invalidate + // on changes to `c` + const z = [y]; + + // return [x, z]; + return ( + <> + + + + ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{a: false, b: null, c: 0}], + sequentialRenders: [ + {a: false, b: null, c: 0}, + {a: false, b: null, c: 1}, + {a: true, b: 0, c: 1}, + {a: true, b: 1, c: 1}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a59d1a566c1090602fbfbad7974ec9683eb5edb0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.expect.md @@ -0,0 +1,70 @@ + +## Input + +```javascript +import {Stringify, makeObject_Primitives} from 'shared-runtime'; + +function Component(props) { + const array = [props.count]; + const x = makeObject_Primitives(); + const element =
{array}
; + console.log(x); + return
{element}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{count: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify, makeObject_Primitives } from "shared-runtime"; + +function Component(props) { + const $ = _c(6); + let t0; + if ($[0] !== props.count) { + t0 = [props.count]; + $[0] = props.count; + $[1] = t0; + } else { + t0 = $[1]; + } + const array = t0; + const x = makeObject_Primitives(); + let t1; + if ($[2] !== array) { + t1 =
{array}
; + $[2] = array; + $[3] = t1; + } else { + t1 = $[3]; + } + const element = t1; + console.log(x); + let t2; + if ($[4] !== element) { + t2 =
{element}
; + $[4] = element; + $[5] = t2; + } else { + t2 = $[5]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ count: 42 }], +}; + +``` + +### Eval output +(kind: ok)
42
+logs: [{ a: 0, b: 'value1', c: true }] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.js new file mode 100644 index 0000000000000000000000000000000000000000..e4066d28c631b39cd2037115b3fe5d0f2dfde6ba --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-store-const-used-later.js @@ -0,0 +1,14 @@ +import {Stringify, makeObject_Primitives} from 'shared-runtime'; + +function Component(props) { + const array = [props.count]; + const x = makeObject_Primitives(); + const element =
{array}
; + console.log(x); + return
{element}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{count: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cae53674bc72ace1a610bbd8a564ade6e7fd414d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.expect.md @@ -0,0 +1,80 @@ + +## Input + +```javascript +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let x; + const array = [props.count]; + x = array; + const element =
{array}
; + return ( +
+ {element} + {x} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{count: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Component(props) { + const $ = _c(7); + let x; + let t0; + if ($[0] !== props.count) { + t0 = [props.count]; + $[0] = props.count; + $[1] = t0; + } else { + t0 = $[1]; + } + const array = t0; + x = array; + let t1; + if ($[2] !== array) { + t1 =
{array}
; + $[2] = array; + $[3] = t1; + } else { + t1 = $[3]; + } + const element = t1; + let t2; + if ($[4] !== element || $[5] !== x) { + t2 = ( +
+ {element} + {x} +
+ ); + $[4] = element; + $[5] = x; + $[6] = t2; + } else { + t2 = $[6]; + } + return t2; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ count: 42 }], +}; + +``` + +### Eval output +(kind: ok)
42
42
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.js new file mode 100644 index 0000000000000000000000000000000000000000..f0b9bfb9ea56bfaccc4a5c01edef33b7ea62be2f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/dont-merge-overlapping-scopes-with-intermediate-reassignment.js @@ -0,0 +1,19 @@ +import {Stringify} from 'shared-runtime'; + +function Component(props) { + let x; + const array = [props.count]; + x = array; + const element =
{array}
; + return ( +
+ {element} + {x} +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{count: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-manual-memoization-react-module.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-manual-memoization-react-module.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1836cdafb41e4c6233c09701139d171efd766932 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-manual-memoization-react-module.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +// useMemo imported from 'react' should be detected and dropped. +// The module name matching must handle 'react' correctly. + +import {useMemo} from 'react'; + +function Component({items}) { + const sorted = useMemo(() => [...items].sort(), [items]); + return
{sorted}
; +} + +export default Component; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // useMemo imported from 'react' should be detected and dropped. +// The module name matching must handle 'react' correctly. + +import { useMemo } from "react"; + +function Component(t0) { + const $ = _c(4); + const { items } = t0; + let t1; + if ($[0] !== items) { + t1 = [...items].sort(); + $[0] = items; + $[1] = t1; + } else { + t1 = $[1]; + } + const sorted = t1; + let t2; + if ($[2] !== sorted) { + t2 =
{sorted}
; + $[2] = sorted; + $[3] = t2; + } else { + t2 = $[3]; + } + return t2; +} + +export default Component; + +``` + +### 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/drop-manual-memoization-react-module.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-manual-memoization-react-module.js new file mode 100644 index 0000000000000000000000000000000000000000..efb8948a198789cdb99227a39fe6d601a02b5651 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-manual-memoization-react-module.js @@ -0,0 +1,11 @@ +// useMemo imported from 'react' should be detected and dropped. +// The module name matching must handle 'react' correctly. + +import {useMemo} from 'react'; + +function Component({items}) { + const sorted = useMemo(() => [...items].sort(), [items]); + return
{sorted}
; +} + +export default Component; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..be8e2268290ae8959d237dd970da05032c3b5ae0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +import * as React from 'react'; + +function Component(props) { + const onClick = React.useCallback(() => { + console.log(props.value); + }, [props.value]); + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import * as React from "react"; + +function Component(props) { + const $ = _c(4); + let t0; + if ($[0] !== props.value) { + t0 = () => { + console.log(props.value); + }; + $[0] = props.value; + $[1] = t0; + } else { + t0 = $[1]; + } + const onClick = t0; + let t1; + if ($[2] !== onClick) { + t1 =
; + $[2] = onClick; + $[3] = t1; + } else { + t1 = $[3]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.js new file mode 100644 index 0000000000000000000000000000000000000000..e43417185832cc9bbd7a75522ae0e9787669affd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usecallback.js @@ -0,0 +1,13 @@ +import * as React from 'react'; + +function Component(props) { + const onClick = React.useCallback(() => { + console.log(props.value); + }, [props.value]); + return
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..42a3484335d72312a5e7902b28ddf0260e3f2d12 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +import * as React from 'react'; + +function Component(props) { + const x = React.useMemo(() => { + const x = []; + x.push(props.value); + return x; + }, [props.value]); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import * as React from "react"; + +function Component(props) { + const $ = _c(2); + let x; + if ($[0] !== props.value) { + x = []; + x.push(props.value); + $[0] = props.value; + $[1] = x; + } else { + x = $[1]; + } + const x_0 = x; + + return x_0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) [42] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.js new file mode 100644 index 0000000000000000000000000000000000000000..09c5f136f3bd2a3b5b63b01ea7849896f4b9a0f8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/drop-methodcall-usememo.js @@ -0,0 +1,15 @@ +import * as React from 'react'; + +function Component(props) { + const x = React.useMemo(() => { + const x = []; + x.push(props.value); + return x; + }, [props.value]); + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..68b0122ea92dd1fddfcc9d1baad37fd2eeb0db5d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md @@ -0,0 +1,91 @@ + +## Input + +```javascript +function Component(props) { + let x = []; + if (props.cond) { + x.push(props.a); + if (props.b) { + const y = [props.b]; + x.push(y); + // oops no memo! + return x; + } + // oops no memo! + return x; + } else { + return foo(); + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, a: 42, b: 3.14}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +function Component(props) { + const $ = _c(7); + let t0; + 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); + if (props.b) { + let t1; + if ($[4] !== props.b) { + t1 = [props.b]; + $[4] = props.b; + $[5] = t1; + } else { + t1 = $[5]; + } + const y = t1; + x.push(y); + t0 = x; + break bb0; + } + + t0 = x; + break bb0; + } else { + let t1; + if ($[6] === Symbol.for("react.memo_cache_sentinel")) { + t1 = foo(); + $[6] = t1; + } else { + t1 = $[6]; + } + t0 = t1; + break bb0; + } + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.cond; + $[3] = t0; + } else { + t0 = $[3]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ cond: true, a: 42, b: 3.14 }], +}; + +``` + +### Eval output +(kind: ok) [42,[3.14]] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..53eb06bc591e48bbab1a6b90b791fb194c95f3d5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.js @@ -0,0 +1,21 @@ +function Component(props) { + let x = []; + if (props.cond) { + x.push(props.a); + if (props.b) { + const y = [props.b]; + x.push(y); + // oops no memo! + return x; + } + // oops no memo! + return x; + } else { + return foo(); + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{cond: true, a: 42, b: 3.14}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..56fdd4ccf82fbbb4dbf00e204f30a2ad15b248d3 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md @@ -0,0 +1,120 @@ + +## Input + +```javascript +import {makeArray} from 'shared-runtime'; + +/** + * This fixture tests what happens when a reactive has no declarations (other than an early return), + * no reassignments, and no dependencies. In this case the only thing we can use to decide if we + * should take the if or else branch is the early return declaration. But if that uses the same + * sentinel as the memo cache sentinel, then if the previous execution did not early return it will + * look like we didn't execute the memo block yet, and we'll needlessly re-execute instead of skipping + * to the else branch. + * + * We have to use a distinct sentinel for the early return value. + * + * Here the fixture will always take the "else" branch and never early return. Logging (not included) + * confirms that the scope for `x` only executes once, on the first render of the component. + */ +let ENABLE_FEATURE = false; + +function Component(props) { + let x = []; + if (ENABLE_FEATURE) { + x.push(42); + return x; + } else { + console.log('fallthrough'); + } + return makeArray(props.a); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + {a: 42}, + {a: 42}, + {a: 3.14}, + {a: 3.14}, + {a: 42}, + {a: 3.14}, + {a: 42}, + {a: 3.14}, + ], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { makeArray } from "shared-runtime"; + +/** + * This fixture tests what happens when a reactive has no declarations (other than an early return), + * no reassignments, and no dependencies. In this case the only thing we can use to decide if we + * should take the if or else branch is the early return declaration. But if that uses the same + * sentinel as the memo cache sentinel, then if the previous execution did not early return it will + * look like we didn't execute the memo block yet, and we'll needlessly re-execute instead of skipping + * to the else branch. + * + * We have to use a distinct sentinel for the early return value. + * + * Here the fixture will always take the "else" branch and never early return. Logging (not included) + * confirms that the scope for `x` only executes once, on the first render of the component. + */ +let ENABLE_FEATURE = false; + +function Component(props) { + const $ = _c(3); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + const x = []; + if (ENABLE_FEATURE) { + x.push(42); + t0 = x; + break bb0; + } else { + console.log("fallthrough"); + } + } + $[0] = t0; + } else { + t0 = $[0]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + let t1; + if ($[1] !== props.a) { + t1 = makeArray(props.a); + $[1] = props.a; + $[2] = t1; + } else { + t1 = $[2]; + } + return t1; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + { a: 42 }, + { a: 42 }, + { a: 3.14 }, + { a: 3.14 }, + { a: 42 }, + { a: 3.14 }, + { a: 42 }, + { a: 3.14 }, + ], +}; + +``` + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.js new file mode 100644 index 0000000000000000000000000000000000000000..e289445513c1b7a05530e2b0e430b142aaca8ad2 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.js @@ -0,0 +1,42 @@ +import {makeArray} from 'shared-runtime'; + +/** + * This fixture tests what happens when a reactive has no declarations (other than an early return), + * no reassignments, and no dependencies. In this case the only thing we can use to decide if we + * should take the if or else branch is the early return declaration. But if that uses the same + * sentinel as the memo cache sentinel, then if the previous execution did not early return it will + * look like we didn't execute the memo block yet, and we'll needlessly re-execute instead of skipping + * to the else branch. + * + * We have to use a distinct sentinel for the early return value. + * + * Here the fixture will always take the "else" branch and never early return. Logging (not included) + * confirms that the scope for `x` only executes once, on the first render of the component. + */ +let ENABLE_FEATURE = false; + +function Component(props) { + let x = []; + if (ENABLE_FEATURE) { + x.push(42); + return x; + } else { + console.log('fallthrough'); + } + return makeArray(props.a); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + {a: 42}, + {a: 42}, + {a: 3.14}, + {a: 3.14}, + {a: 42}, + {a: 3.14}, + {a: 42}, + {a: 3.14}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..31df829e0c203086872433f507bfc2692095e4a8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md @@ -0,0 +1,114 @@ + +## Input + +```javascript +import {makeArray} from 'shared-runtime'; + +function Component(props) { + let x = []; + if (props.cond) { + x.push(props.a); + // oops no memo! + return x; + } else { + return makeArray(props.b); + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + // pattern 1 + {cond: true, a: 42}, + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + {cond: false, b: 3.14}, + // pattern 1 + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + // pattern 1 + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + ], +}; + +``` + +## 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] !== 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 ($[4] !== props.b) { + t1 = makeArray(props.b); + $[4] = props.b; + $[5] = t1; + } else { + t1 = $[5]; + } + t0 = t1; + break bb0; + } + } + $[0] = props.a; + $[1] = props.b; + $[2] = props.cond; + $[3] = t0; + } else { + t0 = $[3]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + // pattern 1 + { cond: true, a: 42 }, + { cond: true, a: 42 }, + // pattern 2 + { cond: false, b: 3.14 }, + { cond: false, b: 3.14 }, + // pattern 1 + { cond: true, a: 42 }, + // pattern 2 + { cond: false, b: 3.14 }, + // pattern 1 + { cond: true, a: 42 }, + // pattern 2 + { cond: false, b: 3.14 }, + ], +}; + +``` + +### Eval output +(kind: ok) [42] +[42] +[3.14] +[3.14] +[42] +[3.14] +[42] +[3.14] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..7446d76fb320c25ba707dde4c34703936cb6b8f6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.js @@ -0,0 +1,33 @@ +import {makeArray} from 'shared-runtime'; + +function Component(props) { + let x = []; + if (props.cond) { + x.push(props.a); + // oops no memo! + return x; + } else { + return makeArray(props.b); + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + sequentialRenders: [ + // pattern 1 + {cond: true, a: 42}, + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + {cond: false, b: 3.14}, + // pattern 1 + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + // pattern 1 + {cond: true, a: 42}, + // pattern 2 + {cond: false, b: 3.14}, + ], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..622f7356495442dafa8d8cf560a2d250ddc1badf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return.expect.md @@ -0,0 +1,38 @@ + +## Input + +```javascript +function MyApp(props) { + let res; + if (props.cond) { + return; + } else { + res = 1; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: MyApp, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; + +``` + +## Code + +```javascript +function MyApp(props) { + if (props.cond) { + return; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: MyApp, + 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/early-return.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return.js new file mode 100644 index 0000000000000000000000000000000000000000..4b04d9242161ea72840209ad96897c1efde33b54 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/early-return.js @@ -0,0 +1,14 @@ +function MyApp(props) { + let res; + if (props.cond) { + return; + } else { + res = 1; + } +} + +export const FIXTURE_ENTRYPOINT = { + fn: MyApp, + params: ['TodoAdd'], + isComponent: 'TodoAdd', +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a6ee8a798b583be285710c37056721c61ab55384 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +import {useRef} from 'react'; + +function useThing(fn) { + const fnRef = useRef(fn); + const ref = useRef(null); + + if (ref.current === null) { + ref.current = function (this: unknown, ...args) { + return fnRef.current.call(this, ...args); + }; + } + return ref.current; +} + +``` + + +## Error + +``` +Found 1 error: + +Error: Expected a non-reserved identifier name + +`this` is a reserved word in JavaScript and cannot be used as an identifier name. +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.ts new file mode 100644 index 0000000000000000000000000000000000000000..2937ba8df5e281b14cf5412e8fb7fb8ff229060d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/ecma/error.reserved-words.ts @@ -0,0 +1,13 @@ +import {useRef} from 'react'; + +function useThing(fn) { + const fnRef = useRef(fn); + const ref = useRef(null); + + if (ref.current === null) { + ref.current = function (this: unknown, ...args) { + return fnRef.current.call(this, ...args); + }; + } + return ref.current; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..18be3b3889f408e981f02dc6f3d898ecc815717c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.expect.md @@ -0,0 +1,64 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({value, enabled}) { + const [localValue, setLocalValue] = useState(''); + + useEffect(() => { + if (enabled) { + setLocalValue(value); + } else { + setLocalValue('disabled'); + } + }, [value, enabled]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'test', enabled: true}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ value, enabled }) { + const [localValue, setLocalValue] = useState(""); + + useEffect(() => { + if (enabled) { + setLocalValue(value); + } else { + setLocalValue("disabled"); + } + }, [value, enabled]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "test", enabled: true }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [value]\n\nData Flow Tree:\n└── value (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":9,"column":6,"index":263},"end":{"line":9,"column":19,"index":276},"filename":"derived-state-conditionally-in-effect.ts","identifierName":"setLocalValue"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":16,"column":1,"index":397},"filename":"derived-state-conditionally-in-effect.ts"},"fnName":"Component","memoSlots":6,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
test
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.js new file mode 100644 index 0000000000000000000000000000000000000000..4cdcb53bb2364464b8219b04cbb5cde57dc34b4d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-conditionally-in-effect.js @@ -0,0 +1,21 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({value, enabled}) { + const [localValue, setLocalValue] = useState(''); + + useEffect(() => { + if (enabled) { + setLocalValue(value); + } else { + setLocalValue('disabled'); + } + }, [value, enabled]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'test', enabled: true}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b3ddc4346e201a7557c1ac92d98cc9deb1564b66 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component({input = 'empty'}) { + const [currInput, setCurrInput] = useState(input); + const localConst = 'local const'; + + useEffect(() => { + setCurrInput(input + localConst); + }, [input, localConst]); + + return
{currInput}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{input: 'test'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +export default function Component({ input = "empty" }) { + const [currInput, setCurrInput] = useState(input); + const localConst = "local const"; + + useEffect(() => { + setCurrInput(input + localConst); + }, [input, localConst]); + + return
{currInput}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ input: "test" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [input]\n\nData Flow Tree:\n└── input (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":9,"column":4,"index":295},"end":{"line":9,"column":16,"index":307},"filename":"derived-state-from-default-props.ts","identifierName":"setCurrInput"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":141},"end":{"line":13,"column":1,"index":391},"filename":"derived-state-from-default-props.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
testlocal const
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.js new file mode 100644 index 0000000000000000000000000000000000000000..9d559946beaded6e7a6bf3d7419e1e795bfa0653 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-default-props.js @@ -0,0 +1,18 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component({input = 'empty'}) { + const [currInput, setCurrInput] = useState(input); + const localConst = 'local const'; + + useEffect(() => { + setCurrInput(input + localConst); + }, [input, localConst]); + + return
{currInput}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{input: 'test'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-local-state-in-effect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-local-state-in-effect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..2121087e6f162a3dc7019fb2768fb307d2d2568d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-local-state-in-effect.expect.md @@ -0,0 +1,52 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import {useEffect, useState} from 'react'; + +function Component({shouldChange}) { + const [count, setCount] = useState(0); + + useEffect(() => { + if (shouldChange) { + setCount(count + 1); + } + }, [count]); + + return
{count}
; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import { useEffect, useState } from "react"; + +function Component({ shouldChange }) { + const [count, setCount] = useState(0); + + useEffect(() => { + if (shouldChange) { + setCount(count + 1); + } + }, [count]); + + return
{count}
; +} + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nState: [count]\n\nData Flow Tree:\n└── count (State)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":10,"column":6,"index":256},"end":{"line":10,"column":14,"index":264},"filename":"derived-state-from-local-state-in-effect.ts","identifierName":"setCount"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":127},"end":{"line":15,"column":1,"index":329},"filename":"derived-state-from-local-state-in-effect.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":3,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/derived-state-from-local-state-in-effect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-local-state-in-effect.js new file mode 100644 index 0000000000000000000000000000000000000000..db84ab8be45f1c1eb96c4af2dfff10c52fb73748 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-local-state-in-effect.js @@ -0,0 +1,15 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import {useEffect, useState} from 'react'; + +function Component({shouldChange}) { + const [count, setCount] = useState(0); + + useEffect(() => { + if (shouldChange) { + setCount(count + 1); + } + }, [count]); + + return
{count}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..41e64d8149db5010a041a8d900c096debbb91d82 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.expect.md @@ -0,0 +1,72 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({firstName}) { + const [lastName, setLastName] = useState('Doe'); + const [fullName, setFullName] = useState('John'); + + const middleName = 'D.'; + + useEffect(() => { + setFullName(firstName + ' ' + middleName + ' ' + lastName); + }, [firstName, middleName, lastName]); + + return ( +
+ setLastName(e.target.value)} /> +
{fullName}
+
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{firstName: 'John'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ firstName }) { + const [lastName, setLastName] = useState("Doe"); + const [fullName, setFullName] = useState("John"); + + const middleName = "D."; + + useEffect(() => { + setFullName(firstName + " " + middleName + " " + lastName); + }, [firstName, middleName, lastName]); + + return ( +
+ setLastName(e.target.value)} /> +
{fullName}
+
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ firstName: "John" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [firstName]\nState: [lastName]\n\nData Flow Tree:\n├── firstName (Prop)\n└── lastName (State)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":11,"column":4,"index":316},"end":{"line":11,"column":15,"index":327},"filename":"derived-state-from-prop-local-state-and-component-scope.ts","identifierName":"setFullName"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":20,"column":1,"index":561},"filename":"derived-state-from-prop-local-state-and-component-scope.ts"},"fnName":"Component","memoSlots":12,"memoBlocks":5,"memoValues":6,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
John D. Doe
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.js new file mode 100644 index 0000000000000000000000000000000000000000..31b77d14817198b49b84b85234ae661573d48344 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-local-state-and-component-scope.js @@ -0,0 +1,25 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({firstName}) { + const [lastName, setLastName] = useState('Doe'); + const [fullName, setFullName] = useState('John'); + + const middleName = 'D.'; + + useEffect(() => { + setFullName(firstName + ' ' + middleName + ' ' + lastName); + }, [firstName, middleName, lastName]); + + return ( +
+ setLastName(e.target.value)} /> +
{fullName}
+
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{firstName: 'John'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..960cf987d4eeacd53b65815223c891d8050bc433 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({initialName}) { + const [name, setName] = useState(''); + + useEffect(() => { + setName(initialName); + }, [initialName]); + + return ( +
+ setName(e.target.value)} /> +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{initialName: 'John'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ initialName }) { + const [name, setName] = useState(""); + + useEffect(() => { + setName(initialName); + }, [initialName]); + + return ( +
+ setName(e.target.value)} /> +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ initialName: "John" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":16,"column":1,"index":378},"filename":"derived-state-from-prop-setter-call-outside-effect-no-error.ts"},"fnName":"Component","memoSlots":6,"memoBlocks":3,"memoValues":4,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..e454caf2ec815590ede0ffc8196b7b8b13359bf8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-call-outside-effect-no-error.js @@ -0,0 +1,21 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({initialName}) { + const [name, setName] = useState(''); + + useEffect(() => { + setName(initialName); + }, [initialName]); + + return ( +
+ setName(e.target.value)} /> +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{initialName: 'John'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-ternary.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-ternary.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..226a3938bd7d3f7d235dd6497579263b61fdeee0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-ternary.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @outputMode:"lint" + +function Component({value}) { + const [checked, setChecked] = useState(''); + + useEffect(() => { + setChecked(value === '' ? [] : value.split(',')); + }, [value]); + + return
{checked}
; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @outputMode:"lint" + +function Component({ value }) { + const [checked, setChecked] = useState(""); + + useEffect(() => { + setChecked(value === "" ? [] : value.split(",")); + }, [value]); + + return
{checked}
; +} + +``` + +### 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/effect-derived-computations/derived-state-from-prop-setter-ternary.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-ternary.js new file mode 100644 index 0000000000000000000000000000000000000000..1c020d3015d10aa401043adc9d1c1a38c8138135 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-ternary.js @@ -0,0 +1,11 @@ +// @validateNoDerivedComputationsInEffects_exp @outputMode:"lint" + +function Component({value}) { + const [checked, setChecked] = useState(''); + + useEffect(() => { + setChecked(value === '' ? [] : value.split(',')); + }, [value]); + + return
{checked}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..de184ede639465e0bdfab333adbb84cb0c97791b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function MockComponent({onSet}) { + return
onSet('clicked')}>Mock Component
; +} + +function Component({propValue}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + }, [propValue]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function MockComponent({ onSet }) { + return
onSet("clicked")}>Mock Component
; +} + +function Component({ propValue }) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + }, [propValue]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ propValue: "test" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":6,"column":1,"index":230},"filename":"derived-state-from-prop-setter-used-outside-effect-no-error.ts"},"fnName":"MockComponent","memoSlots":2,"memoBlocks":1,"memoValues":1,"prunedMemoBlocks":0,"prunedMemoValues":0} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":8,"column":0,"index":232},"end":{"line":15,"column":1,"index":421},"filename":"derived-state-from-prop-setter-used-outside-effect-no-error.ts"},"fnName":"Component","memoSlots":4,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
Mock Component
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..879d582c924d6330bd9cfbf9def60c1343b69bbd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-setter-used-outside-effect-no-error.js @@ -0,0 +1,20 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function MockComponent({onSet}) { + return
onSet('clicked')}>Mock Component
; +} + +function Component({propValue}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + }, [propValue]); + + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..61c4e2b82de8107e9ce1c3c13e007712440655f7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({value}) { + const [localValue, setLocalValue] = useState(''); + + useEffect(() => { + setLocalValue(value); + document.title = `Value: ${value}`; + }, [value]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'test'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ value }) { + const [localValue, setLocalValue] = useState(""); + + useEffect(() => { + setLocalValue(value); + document.title = `Value: ${value}`; + }, [value]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: "test" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [value]\n\nData Flow Tree:\n└── value (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":8,"column":4,"index":233},"end":{"line":8,"column":17,"index":246},"filename":"derived-state-from-prop-with-side-effect.ts","identifierName":"setLocalValue"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":13,"column":1,"index":346},"filename":"derived-state-from-prop-with-side-effect.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
test
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.js new file mode 100644 index 0000000000000000000000000000000000000000..b6cebdb4080747a8d82905b5c355fc302ace32f0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-prop-with-side-effect.js @@ -0,0 +1,18 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({value}) { + const [localValue, setLocalValue] = useState(''); + + useEffect(() => { + setLocalValue(value); + document.title = `Value: ${value}`; + }, [value]); + + return
{localValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 'test'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..f11a4b26b533535538dd84fcb731683c377617e5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.expect.md @@ -0,0 +1,59 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState, useRef} from 'react'; + +export default function Component({test}) { + const [local, setLocal] = useState(''); + + const myRef = useRef(null); + + useEffect(() => { + setLocal(myRef.current + test); + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{test: 'testString'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState, useRef } from "react"; + +export default function Component({ test }) { + const [local, setLocal] = useState(""); + + const myRef = useRef(null); + + useEffect(() => { + setLocal(myRef.current + test); + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ test: "testString" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":149},"end":{"line":14,"column":1,"index":347},"filename":"derived-state-from-ref-and-state-no-error.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok) nulltestString \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..9425aee24419ff761102fcc7f649090367654b6e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/derived-state-from-ref-and-state-no-error.js @@ -0,0 +1,19 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState, useRef} from 'react'; + +export default function Component({test}) { + const [local, setLocal] = useState(''); + + const myRef = useRef(null); + + useEffect(() => { + setLocal(myRef.current + test); + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{test: 'testString'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cdac7ffc63ac41af81b75d607cc5e0f9f80c075b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue}) { + const [value, setValue] = useState(null); + + function localFunction() { + console.log('local function'); + } + + useEffect(() => { + setValue(propValue); + localFunction(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ propValue }) { + const [value, setValue] = useState(null); + + function localFunction() { + console.log("local function"); + } + + useEffect(() => { + setValue(propValue); + localFunction(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ propValue: "test" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [propValue]\n\nData Flow Tree:\n└── propValue (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":12,"column":4,"index":298},"end":{"line":12,"column":12,"index":306},"filename":"effect-contains-local-function-call.ts","identifierName":"setValue"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":17,"column":1,"index":390},"filename":"effect-contains-local-function-call.ts"},"fnName":"Component","memoSlots":6,"memoBlocks":3,"memoValues":4,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
test
+logs: ['local function'] \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.js new file mode 100644 index 0000000000000000000000000000000000000000..3eabb40feeb63dfe360f25b0dcedab0963debf98 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-local-function-call.js @@ -0,0 +1,22 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue}) { + const [value, setValue] = useState(null); + + function localFunction() { + console.log('local function'); + } + + useEffect(() => { + setValue(propValue); + localFunction(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..374877863dd3bd0967c4bcd0f3dfa7dd470c55a0 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.expect.md @@ -0,0 +1,56 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue, onChange}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + onChange(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test', onChange: () => {}}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ propValue, onChange }) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + onChange(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ propValue: "test", onChange: () => {} }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":12,"column":1,"index":325},"filename":"effect-contains-prop-function-call-no-error.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":3,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":16,"column":41,"index":421},"end":{"line":16,"column":49,"index":429},"filename":"effect-contains-prop-function-call-no-error.ts"},"fnName":null,"memoSlots":0,"memoBlocks":0,"memoValues":0,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
test
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..c9c9778ab844c27181d6c8f57e9f5c9dc1caef22 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-contains-prop-function-call-no-error.js @@ -0,0 +1,17 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue, onChange}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + onChange(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test', onChange: () => {}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-used-in-dep-array-still-errors.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-used-in-dep-array-still-errors.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..954feeaf6386bf8dd380aa9d26c57fbd17604666 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-used-in-dep-array-still-errors.expect.md @@ -0,0 +1,42 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({prop}) { + const [s, setS] = useState(0); + useEffect(() => { + setS(prop); + }, [prop, setS]); + + return
{prop}
; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({ prop }) { + const [s, setS] = useState(0); + useEffect(() => { + setS(prop); + }, [prop, setS]); + + return
{prop}
; +} + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [prop]\n\nData Flow Tree:\n└── prop (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":6,"column":4,"index":169},"end":{"line":6,"column":8,"index":173},"filename":"effect-used-in-dep-array-still-errors.ts","identifierName":"setS"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":3,"column":0,"index":83},"end":{"line":10,"column":1,"index":231},"filename":"effect-used-in-dep-array-still-errors.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/effect-used-in-dep-array-still-errors.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-used-in-dep-array-still-errors.js new file mode 100644 index 0000000000000000000000000000000000000000..bf48efbbc1007e15a4992ccca499f7d4715899ca --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-used-in-dep-array-still-errors.js @@ -0,0 +1,10 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({prop}) { + const [s, setS] = useState(0); + useEffect(() => { + setS(prop); + }, [prop, setS]); + + return
{prop}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..ac72a4414844633081e77c62910fe961c0ad8b0e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import {useEffect, useState} from 'react'; + +function Component(file: File) { + const [imageUrl, setImageUrl] = useState(null); + + /* + * Cleaning up the variable or a source of the variable used to setState + * inside the effect communicates that we always need to clean up something + * which is a valid use case for useEffect. In which case we want to + * avoid an throwing + */ + useEffect(() => { + const imageUrlPrepared = URL.createObjectURL(file); + setImageUrl(imageUrlPrepared); + return () => URL.revokeObjectURL(imageUrlPrepared); + }, [file]); + + return ; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import { useEffect, useState } from "react"; + +function Component(file: File) { + const [imageUrl, setImageUrl] = useState(null); + + /* + * Cleaning up the variable or a source of the variable used to setState + * inside the effect communicates that we always need to clean up something + * which is a valid use case for useEffect. In which case we want to + * avoid an throwing + */ + useEffect(() => { + const imageUrlPrepared = URL.createObjectURL(file); + setImageUrl(imageUrlPrepared); + return () => URL.revokeObjectURL(imageUrlPrepared); + }, [file]); + + return ; +} + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":5,"column":0,"index":127},"end":{"line":21,"column":1,"index":719},"filename":"effect-with-cleanup-function-depending-on-derived-computation-value.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.js new file mode 100644 index 0000000000000000000000000000000000000000..16e52562bd834f3473bf659e088480285d95e4a5 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-cleanup-function-depending-on-derived-computation-value.js @@ -0,0 +1,21 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +import {useEffect, useState} from 'react'; + +function Component(file: File) { + const [imageUrl, setImageUrl] = useState(null); + + /* + * Cleaning up the variable or a source of the variable used to setState + * inside the effect communicates that we always need to clean up something + * which is a valid use case for useEffect. In which case we want to + * avoid an throwing + */ + useEffect(() => { + const imageUrlPrepared = URL.createObjectURL(file); + setImageUrl(imageUrlPrepared); + return () => URL.revokeObjectURL(imageUrlPrepared); + }, [file]); + + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..58328b2e9597b26dbb8772c53f3508475831c81c --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + globalCall(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component({ propValue }) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + globalCall(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ propValue: "test" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":12,"column":1,"index":317},"filename":"effect-with-global-function-call-no-error.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: exception) globalCall is not defined \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..565e23bb0fe3dfee216a5c309f8092cc10bcd8f9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/effect-with-global-function-call-no-error.js @@ -0,0 +1,17 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component({propValue}) { + const [value, setValue] = useState(null); + useEffect(() => { + setValue(propValue); + globalCall(); + }, [propValue]); + + return
{value}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{propValue: 'test'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/from-props-setstate-in-effect-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/from-props-setstate-in-effect-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..7ba22f51864207814c533e199abc57408ef6f361 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/from-props-setstate-in-effect-no-error.expect.md @@ -0,0 +1,39 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @enableTreatSetIdentifiersAsStateSetters @loggerTestOnly @outputMode:"lint" + +function Component({setParentState, prop}) { + useEffect(() => { + setParentState(prop); + }, [prop]); + + return
{prop}
; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @enableTreatSetIdentifiersAsStateSetters @loggerTestOnly @outputMode:"lint" + +function Component({ setParentState, prop }) { + useEffect(() => { + setParentState(prop); + }, [prop]); + + return
{prop}
; +} + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":3,"column":0,"index":124},"end":{"line":9,"column":1,"index":259},"filename":"from-props-setstate-in-effect-no-error.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":3,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/from-props-setstate-in-effect-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/from-props-setstate-in-effect-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..1754209d83713bc06296417a36bd0d5fe05298b4 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/from-props-setstate-in-effect-no-error.js @@ -0,0 +1,9 @@ +// @validateNoDerivedComputationsInEffects_exp @enableTreatSetIdentifiersAsStateSetters @loggerTestOnly @outputMode:"lint" + +function Component({setParentState, prop}) { + useEffect(() => { + setParentState(prop); + }, [prop]); + + return
{prop}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/function-expression-mutation-edge-case.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/function-expression-mutation-edge-case.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..9e03cfb1bc43837a142d34d5ab7c6323dd3cb79d --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/function-expression-mutation-edge-case.expect.md @@ -0,0 +1,86 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component() { + const [foo, setFoo] = useState({}); + const [bar, setBar] = useState(new Set()); + + /* + * isChanged is considered context of the effect's function expression, + * if we don't bail out of effect mutation derivation tracking, isChanged + * will inherit the sources of the effect's function expression. + * + * This is innacurate and with the multiple passes ends up causing an infinite loop. + */ + useEffect(() => { + let isChanged = false; + + const newData = foo.map(val => { + bar.someMethod(val); + isChanged = true; + }); + + if (isChanged) { + setFoo(newData); + } + }, [foo, bar]); + + return ( +
+ {foo}, {bar} +
+ ); +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component() { + const [foo, setFoo] = useState({}); + const [bar, setBar] = useState(new Set()); + + /* + * isChanged is considered context of the effect's function expression, + * if we don't bail out of effect mutation derivation tracking, isChanged + * will inherit the sources of the effect's function expression. + * + * This is innacurate and with the multiple passes ends up causing an infinite loop. + */ + useEffect(() => { + let isChanged = false; + + const newData = foo.map((val) => { + bar.someMethod(val); + isChanged = true; + }); + + if (isChanged) { + setFoo(newData); + } + }, [foo, bar]); + + return ( +
+ {foo}, {bar} +
+ ); +} + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nState: [foo, bar]\n\nData Flow Tree:\n└── newData\n ├── foo (State)\n └── bar (State)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":23,"column":6,"index":682},"end":{"line":23,"column":12,"index":688},"filename":"function-expression-mutation-edge-case.ts","identifierName":"setFoo"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":3,"column":0,"index":83},"end":{"line":32,"column":1,"index":781},"filename":"function-expression-mutation-edge-case.ts"},"fnName":"Component","memoSlots":9,"memoBlocks":4,"memoValues":5,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/function-expression-mutation-edge-case.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/function-expression-mutation-edge-case.js new file mode 100644 index 0000000000000000000000000000000000000000..856209928d1c613adbac054ff0eff6f1416b8349 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/function-expression-mutation-edge-case.js @@ -0,0 +1,32 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component() { + const [foo, setFoo] = useState({}); + const [bar, setBar] = useState(new Set()); + + /* + * isChanged is considered context of the effect's function expression, + * if we don't bail out of effect mutation derivation tracking, isChanged + * will inherit the sources of the effect's function expression. + * + * This is innacurate and with the multiple passes ends up causing an infinite loop. + */ + useEffect(() => { + let isChanged = false; + + const newData = foo.map(val => { + bar.someMethod(val); + isChanged = true; + }); + + if (isChanged) { + setFoo(newData); + } + }, [foo, bar]); + + return ( +
+ {foo}, {bar} +
+ ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..585ddcc5732ebff8d418b2a1ee3b4b2a0ef2282b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.expect.md @@ -0,0 +1,62 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component() { + const [firstName, setFirstName] = useState('Taylor'); + const lastName = 'Swift'; + + // 🔴 Avoid: redundant state and unnecessary Effect + const [fullName, setFullName] = useState(''); + useEffect(() => { + setFullName(firstName + ' ' + lastName); + }, [firstName, lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +function Component() { + const [firstName, setFirstName] = useState("Taylor"); + const lastName = "Swift"; + + // 🔴 Avoid: redundant state and unnecessary Effect + const [fullName, setFullName] = useState(""); + useEffect(() => { + setFullName(firstName + " " + lastName); + }, [firstName, lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nState: [firstName]\n\nData Flow Tree:\n└── firstName (State)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":11,"column":4,"index":360},"end":{"line":11,"column":15,"index":371},"filename":"invalid-derived-computation-in-effect.ts","identifierName":"setFullName"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":126},"end":{"line":15,"column":1,"index":464},"filename":"invalid-derived-computation-in-effect.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
Taylor Swift
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.js new file mode 100644 index 0000000000000000000000000000000000000000..6cd45831215a131420ec3d06b744f6e8975006ef --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-computation-in-effect.js @@ -0,0 +1,20 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +function Component() { + const [firstName, setFirstName] = useState('Taylor'); + const lastName = 'Swift'; + + // 🔴 Avoid: redundant state and unnecessary Effect + const [fullName, setFullName] = useState(''); + useEffect(() => { + setFullName(firstName + ' ' + lastName); + }, [firstName, lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..a4bcc1f1090c7baf612c4a6cae96a5c18f6a57d6 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component(props) { + const [displayValue, setDisplayValue] = useState(''); + + useEffect(() => { + const computed = props.prefix + props.value + props.suffix; + setDisplayValue(computed); + }, [props.prefix, props.value, props.suffix]); + + return
{displayValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prefix: '[', value: 'test', suffix: ']'}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +export default function Component(props) { + const [displayValue, setDisplayValue] = useState(""); + + useEffect(() => { + const computed = props.prefix + props.value + props.suffix; + setDisplayValue(computed); + }, [props.prefix, props.value, props.suffix]); + + return
{displayValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ prefix: "[", value: "test", suffix: "]" }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [props]\n\nData Flow Tree:\n└── computed\n └── props (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":9,"column":4,"index":314},"end":{"line":9,"column":19,"index":329},"filename":"invalid-derived-state-from-computed-props.ts","identifierName":"setDisplayValue"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":141},"end":{"line":13,"column":1,"index":428},"filename":"invalid-derived-state-from-computed-props.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
[test]
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.js new file mode 100644 index 0000000000000000000000000000000000000000..4243834c2f6c9467cb4ce52706b7e0c1b52d1c95 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-computed-props.js @@ -0,0 +1,18 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component(props) { + const [displayValue, setDisplayValue] = useState(''); + + useEffect(() => { + const computed = props.prefix + props.value + props.suffix; + setDisplayValue(computed); + }, [props.prefix, props.value, props.suffix]); + + return
{displayValue}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{prefix: '[', value: 'test', suffix: ']'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..d89b3091b51225f5756e95777c3de18b65f45aed --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.expect.md @@ -0,0 +1,60 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component({props}) { + const [fullName, setFullName] = useState( + props.firstName + ' ' + props.lastName + ); + + useEffect(() => { + setFullName(props.firstName + ' ' + props.lastName); + }, [props.firstName, props.lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{props: {firstName: 'John', lastName: 'Doe'}}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState } from "react"; + +export default function Component({ props }) { + const [fullName, setFullName] = useState( + props.firstName + " " + props.lastName, + ); + + useEffect(() => { + setFullName(props.firstName + " " + props.lastName); + }, [props.firstName, props.lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ props: { firstName: "John", lastName: "Doe" } }], +}; + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nProps: [props]\n\nData Flow Tree:\n└── props (Prop)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":10,"column":4,"index":288},"end":{"line":10,"column":15,"index":299},"filename":"invalid-derived-state-from-destructured-props.ts","identifierName":"setFullName"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":141},"end":{"line":14,"column":1,"index":416},"filename":"invalid-derived-state-from-destructured-props.ts"},"fnName":"Component","memoSlots":6,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok)
John Doe
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.js new file mode 100644 index 0000000000000000000000000000000000000000..abb1643e692189eda40313bdb98056aeaed7a520 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/invalid-derived-state-from-destructured-props.js @@ -0,0 +1,19 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState} from 'react'; + +export default function Component({props}) { + const [fullName, setFullName] = useState( + props.firstName + ' ' + props.lastName + ); + + useEffect(() => { + setFullName(props.firstName + ' ' + props.lastName); + }, [props.firstName, props.lastName]); + + return
{fullName}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{props: {firstName: 'John', lastName: 'Doe'}}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..70174794dc78fa43a22e0f461ccec68f380301c8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.expect.md @@ -0,0 +1,67 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState, useRef} from 'react'; + +export default function Component({test}) { + const [local, setLocal] = useState(0); + + const myRef = useRef(null); + + useEffect(() => { + if (myRef.current) { + setLocal(test); + } else { + setLocal(test + test); + } + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{test: 4}], +}; + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import { useEffect, useState, useRef } from "react"; + +export default function Component({ test }) { + const [local, setLocal] = useState(0); + + const myRef = useRef(null); + + useEffect(() => { + if (myRef.current) { + setLocal(test); + } else { + setLocal(test + test); + } + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ test: 4 }], +}; + +``` + +## Logs + +``` +{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":15,"index":149},"end":{"line":18,"column":1,"index":405},"filename":"ref-conditional-in-effect-no-error.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0} +``` + +### Eval output +(kind: ok) 8 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.js new file mode 100644 index 0000000000000000000000000000000000000000..a5424ab03b2d61771ad1b2c829e87272ae04727e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/ref-conditional-in-effect-no-error.js @@ -0,0 +1,23 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" +import {useEffect, useState, useRef} from 'react'; + +export default function Component({test}) { + const [local, setLocal] = useState(0); + + const myRef = useRef(null); + + useEffect(() => { + if (myRef.current) { + setLocal(test); + } else { + setLocal(test + test); + } + }, [test]); + + return <>{local}; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{test: 4}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1a1e668b95328f573d9c5c0a83590cb945f76d03 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.expect.md @@ -0,0 +1,58 @@ + +## Input + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({prop}) { + const [s, setS] = useState(); + const [second, setSecond] = useState(prop); + + /* + * `second` is a source of state. It will inherit the value of `prop` in + * the first render, but after that it will no longer be updated when + * `prop` changes. So we shouldn't consider `second` as being derived from + * `prop` + */ + useEffect(() => { + setS(second); + }, [second]); + + return
{s}
; +} + +``` + +## Code + +```javascript +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({ prop }) { + const [s, setS] = useState(); + const [second, setSecond] = useState(prop); + + /* + * `second` is a source of state. It will inherit the value of `prop` in + * the first render, but after that it will no longer be updated when + * `prop` changes. So we shouldn't consider `second` as being derived from + * `prop` + */ + useEffect(() => { + setS(second); + }, [second]); + + return
{s}
; +} + +``` + +## Logs + +``` +{"kind":"CompileError","detail":{"category":"EffectDerivationsOfState","reason":"You might not need an effect. Derive values in render, not effects.","description":"Using an effect triggers an additional render which can hurt performance and user experience, potentially briefly showing stale values to the user\n\nThis setState call is setting a derived value that depends on the following reactive sources:\n\nState: [second]\n\nData Flow Tree:\n└── second (State)\n\nSee: https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state","severity":"Error","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":14,"column":4,"index":462},"end":{"line":14,"column":8,"index":466},"filename":"usestate-derived-from-prop-no-show-in-data-flow-tree.ts","identifierName":"setS"},"message":"This should be computed during render, not in an effect"}]},"fnLoc":null} +{"kind":"CompileSuccess","fnLoc":{"start":{"line":3,"column":0,"index":83},"end":{"line":18,"column":1,"index":519},"filename":"usestate-derived-from-prop-no-show-in-data-flow-tree.ts"},"fnName":"Component","memoSlots":5,"memoBlocks":2,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":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/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.js new file mode 100644 index 0000000000000000000000000000000000000000..3be4e88a07ad3877db3dae817e6a7c9389f7cbab --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/effect-derived-computations/usestate-derived-from-prop-no-show-in-data-flow-tree.js @@ -0,0 +1,18 @@ +// @validateNoDerivedComputationsInEffects_exp @loggerTestOnly @outputMode:"lint" + +function Component({prop}) { + const [s, setS] = useState(); + const [second, setSecond] = useState(prop); + + /* + * `second` is a source of state. It will inherit the value of `prop` in + * the first render, but after that it will no longer be updated when + * `prop` changes. So we shouldn't consider `second` as being derived from + * `prop` + */ + useEffect(() => { + setS(second); + }, [second]); + + return
{s}
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..313facae6882bebe9a2cb798634af95fa11fd8b1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +import {getNumber} from 'shared-runtime'; + +function useFoo() { + try { + return getNumber(); + } catch {} +} +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { getNumber } from "shared-runtime"; + +function useFoo() { + const $ = _c(1); + try { + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = getNumber(); + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; + } catch {} +} + +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [], +}; + +``` + +### Eval output +(kind: ok) 4 \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.ts b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a42d45afe3b2310b48ea3bee46f5bffb02400cf --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-catch-statement.ts @@ -0,0 +1,11 @@ +import {getNumber} from 'shared-runtime'; + +function useFoo() { + try { + return getNumber(); + } catch {} +} +export const FIXTURE_ENTRYPOINT = { + fn: useFoo, + params: [], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..eeb0ba6c96db90577c5e87ce3d5d31550ee5128b --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md @@ -0,0 +1,53 @@ + +## Input + +```javascript +// @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + 'use forget'; + // eslint-disable-next-line foo/not-react-related + return
{props.text}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{text: 'Hello'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + "use forget"; + const $ = _c(2); + let t0; + if ($[0] !== props.text) { + t0 =
{props.text}
; + $[0] = props.text; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ text: "Hello" }], +}; + +``` + +### Eval output +(kind: ok)
Hello
\ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js new file mode 100644 index 0000000000000000000000000000000000000000..b81132d3b82699653f0707a27ee2615e32974675 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js @@ -0,0 +1,15 @@ +// @eslintSuppressionRules:[] + +// The suppression here shouldn't cause compilation to get skipped +// Previously we had a bug where an empty list of suppressions would +// create a regexp that matched any suppression +function Component(props) { + 'use forget'; + // eslint-disable-next-line foo/not-react-related + return
{props.text}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{text: 'Hello'}], +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..026b9f2f1101f0cba5eb4a77d7877691f404ba1a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.expect.md @@ -0,0 +1,33 @@ + +## Input + +```javascript +function Component(props) { + const computedKey = props.key; + const {[computedKey]: x} = props.val; + + return x; +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized + + x$8. + +error._todo.computed-lval-in-destructure.ts:5:9 + 3 | const {[computedKey]: x} = props.val; + 4 | +> 5 | return x; + | ^ this is uninitialized + 6 | } + 7 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.js new file mode 100644 index 0000000000000000000000000000000000000000..71cb38c25a15ab1ba7167ecb6a552f6ea23696a8 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error._todo.computed-lval-in-destructure.js @@ -0,0 +1,6 @@ +function Component(props) { + const computedKey = props.key; + const {[computedKey]: x} = props.val; + + return x; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b7b707b3e748bc723a97f66d09e314b622c13a61 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function Component() { + const Foo = () => { + someGlobal = true; + }; + return ; +} + +``` + + +## Error + +``` +Found 1 error: + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `someGlobal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.assign-global-in-component-tag-function.ts:3:4 + 1 | function Component() { + 2 | const Foo = () => { +> 3 | someGlobal = true; + | ^^^^^^^^^^ `someGlobal` cannot be reassigned + 4 | }; + 5 | return ; + 6 | } +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.js new file mode 100644 index 0000000000000000000000000000000000000000..2982fdf70858fe2571bae0363731644085533eba --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-component-tag-function.js @@ -0,0 +1,6 @@ +function Component() { + const Foo = () => { + someGlobal = true; + }; + return ; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..1f5ac0c83dfaec13d58f410d2b59588377d8d3e1 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +function Component() { + const foo = () => { + someGlobal = true; + }; + // Children are generally access/called during render, so + // modifying a global in a children function is almost + // certainly a mistake. + return {foo}; +} + +``` + + +## Error + +``` +Found 1 error: + +Error: Cannot reassign variables declared outside of the component/hook + +Variable `someGlobal` is declared outside of the component/hook. Reassigning this value during render is a form of side effect, which can cause unpredictable behavior depending on when the component happens to re-render. If this variable is used in rendering, use useState instead. Otherwise, consider updating it in an effect. (https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render). + +error.assign-global-in-jsx-children.ts:3:4 + 1 | function Component() { + 2 | const foo = () => { +> 3 | someGlobal = true; + | ^^^^^^^^^^ `someGlobal` cannot be reassigned + 4 | }; + 5 | // Children are generally access/called during render, so + 6 | // modifying a global in a children function is almost +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.js new file mode 100644 index 0000000000000000000000000000000000000000..82554e8ac43ba84699a516b88e5c7ef9fc2f0bf9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-global-in-jsx-children.js @@ -0,0 +1,9 @@ +function Component() { + const foo = () => { + someGlobal = true; + }; + // Children are generally access/called during render, so + // modifying a global in a children function is almost + // certainly a mistake. + return {foo}; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..c89e773f3212c07221b784c29564b47c0ce77fdd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +// Fixture to test that we show a hint to name as `ref` or `-Ref` when attempting +// to assign .current inside an effect +function Component({foo}) { + useEffect(() => { + foo.current = true; + }, [foo]); +} + +``` + + +## Error + +``` +Found 1 error: + +Error: This value cannot be modified + +Modifying component props or hook arguments is not allowed. Consider using a local variable instead. + +error.assign-ref-in-effect-hint.ts:5:4 + 3 | function Component({foo}) { + 4 | useEffect(() => { +> 5 | foo.current = true; + | ^^^ `foo` cannot be modified + 6 | }, [foo]); + 7 | } + 8 | + +Hint: If this value is a Ref (value returned by `useRef()`), rename the variable to end in "Ref". +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.js new file mode 100644 index 0000000000000000000000000000000000000000..154673495964834afedba55719f5f8121dda4a88 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.assign-ref-in-effect-hint.js @@ -0,0 +1,7 @@ +// Fixture to test that we show a hint to name as `ref` or `-Ref` when attempting +// to assign .current inside an effect +function Component({foo}) { + useEffect(() => { + foo.current = true; + }, [foo]); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..fe0ade4b91e5e9151ae18e631be1765d488f881e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.expect.md @@ -0,0 +1,35 @@ + +## Input + +```javascript +// @enableFlowSuppressions + +function Foo(props) { + // $FlowFixMe[react-rule-hook] + useX(); + return null; +} + +``` + + +## Error + +``` +Found 1 error: + +Error: React Compiler has skipped optimizing this component because one or more React rule violations were reported by Flow + +React Compiler only works when your components follow all the rules of React, disabling them may result in unexpected or incorrect behavior. Found suppression `$FlowFixMe[react-rule-hook]`. + +error.bailout-on-flow-suppression.ts:4:2 + 2 | + 3 | function Foo(props) { +> 4 | // $FlowFixMe[react-rule-hook] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Found React rule suppression + 5 | useX(); + 6 | return null; + 7 | } +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.js new file mode 100644 index 0000000000000000000000000000000000000000..957004260e13c0ec6220bb05032177f1f49460bc --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-flow-suppression.js @@ -0,0 +1,7 @@ +// @enableFlowSuppressions + +function Foo(props) { + // $FlowFixMe[react-rule-hook] + useX(); + return null; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..df3524de027e49805b014e92eb3e2f68e94cd0ac --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.expect.md @@ -0,0 +1,51 @@ + +## Input + +```javascript +// @eslintSuppressionRules:["my-app","react-rule"] @validateExhaustiveMemoizationDependencies:false + +/* eslint-disable my-app/react-rule */ +function lowercasecomponent() { + 'use forget'; + const x = []; + // eslint-disable-next-line my-app/react-rule + return
{x}
; +} +/* eslint-enable my-app/react-rule */ + +``` + + +## Error + +``` +Found 2 errors: + +Error: React Compiler has skipped optimizing this component because one or more React ESLint rules were disabled + +React Compiler only works when your components follow all the rules of React, disabling them may result in unexpected or incorrect behavior. Found suppression `eslint-disable my-app/react-rule`. + +error.bailout-on-suppression-of-custom-rule.ts:3:0 + 1 | // @eslintSuppressionRules:["my-app","react-rule"] @validateExhaustiveMemoizationDependencies:false + 2 | +> 3 | /* eslint-disable my-app/react-rule */ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Found React rule suppression + 4 | function lowercasecomponent() { + 5 | 'use forget'; + 6 | const x = []; + +Error: React Compiler has skipped optimizing this component because one or more React ESLint rules were disabled + +React Compiler only works when your components follow all the rules of React, disabling them may result in unexpected or incorrect behavior. Found suppression `eslint-disable-next-line my-app/react-rule`. + +error.bailout-on-suppression-of-custom-rule.ts:7:2 + 5 | 'use forget'; + 6 | const x = []; +> 7 | // eslint-disable-next-line my-app/react-rule + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Found React rule suppression + 8 | return
{x}
; + 9 | } + 10 | /* eslint-enable my-app/react-rule */ +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.js new file mode 100644 index 0000000000000000000000000000000000000000..b9344d663b90a0a30ecadbf9296e98853d6f0897 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.js @@ -0,0 +1,10 @@ +// @eslintSuppressionRules:["my-app","react-rule"] @validateExhaustiveMemoizationDependencies:false + +/* eslint-disable my-app/react-rule */ +function lowercasecomponent() { + 'use forget'; + const x = []; + // eslint-disable-next-line my-app/react-rule + return
{x}
; +} +/* eslint-enable my-app/react-rule */ diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..437bcae9288d8ad4d8ed3fe360464ea425082eb7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.expect.md @@ -0,0 +1,36 @@ + +## Input + +```javascript +// @flow +function Foo() { + try { + doSomething(); + } catch (e) { + foo(() => e); + } + return
; +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: Expected all references to a variable to be consistently local or context references + +Identifier e$2 is referenced as a context variable, but was previously referenced as a local variable. + + 4 | doSomething(); + 5 | } catch (e) { +> 6 | foo(() => e); + | ^ this is local + 7 | } + 8 | return
; + 9 | } +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.js new file mode 100644 index 0000000000000000000000000000000000000000..6d672ee6d8264e520be652524f5bead1381d3451 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-context-variable-catch-in-lambda.js @@ -0,0 +1,9 @@ +// @flow +function Foo() { + try { + doSomething(); + } catch (e) { + foo(() => e); + } + return
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..194493ca28d1cc9ce0a9eac61248a7d99342687a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.expect.md @@ -0,0 +1,88 @@ + +## Input + +```javascript +/** + * @flow strict-local + * @format + */ + +'use strict'; + +import type {SelectorStrict} from 'SelectorStrict'; +import type LoadObject from 'LoadObject'; + +import DataAtom from 'DataAtom'; +// eslint-disable-next-line custom/no-restricted-imports +import BaseSingleDataStore from 'BaseSingleDataStore'; + +import storeToSelector from 'storeToSelector'; +import invariant from 'invariant'; +import createLoadObjectSelector from 'createLoadObjectSelector'; + +/** + * Create a ad-hoc store + selector that lives as long as it's used + * + * Allows the easy use of Suspense and caching without having to define a + * new store/selector file every time we need to load data from a + * controller + * + * Use: + * const dogSelector = adHocSelector(() => promiseAsyncRequest(DogsController.getURIBuilder().getURI()), + * + * const dogComponent = createSuspenseContainer(dogSelector, dog => ); +); + */ + +const stores = new Set(); + +function adHocSelector( + storeName: string, + genFunction: () => Promise +): SelectorStrict, unknown, unknown> { + invariant( + !stores.has(storeName), + 'adHocSelector was run multiple times for store name %s', + storeName + ); + stores.add(storeName); + class AdHocStore extends BaseSingleDataStore { + __loadPromise: () => Promise = genFunction; + } + + const adHocStore = new AdHocStore(storeName, DataAtom); + + return createLoadObjectSelector([storeToSelector(adHocStore)], () => + adHocStore.getData() + ); +} + +// $FlowFixMe[incompatible-type] +export default adHocSelector as ( + storeName: string, + genFunction: () => Promise +) => SelectorStrict>; + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized + + AdHocStore$16:TFunction. + +error.bug-hir_loc_diff_2.ts:49:25 + 47 | } + 48 | +> 49 | const adHocStore = new AdHocStore(storeName, DataAtom); + | ^^^^^^^^^^ this is uninitialized + 50 | + 51 | return createLoadObjectSelector([storeToSelector(adHocStore)], () => + 52 | adHocStore.getData() +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.js new file mode 100644 index 0000000000000000000000000000000000000000..850cd523a4ccf81a4cbe7292015ec997c8800410 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_loc_diff_2.js @@ -0,0 +1,60 @@ +/** + * @flow strict-local + * @format + */ + +'use strict'; + +import type {SelectorStrict} from 'SelectorStrict'; +import type LoadObject from 'LoadObject'; + +import DataAtom from 'DataAtom'; +// eslint-disable-next-line custom/no-restricted-imports +import BaseSingleDataStore from 'BaseSingleDataStore'; + +import storeToSelector from 'storeToSelector'; +import invariant from 'invariant'; +import createLoadObjectSelector from 'createLoadObjectSelector'; + +/** + * Create a ad-hoc store + selector that lives as long as it's used + * + * Allows the easy use of Suspense and caching without having to define a + * new store/selector file every time we need to load data from a + * controller + * + * Use: + * const dogSelector = adHocSelector(() => promiseAsyncRequest(DogsController.getURIBuilder().getURI()), + * + * const dogComponent = createSuspenseContainer(dogSelector, dog => ); +); + */ + +const stores = new Set(); + +function adHocSelector( + storeName: string, + genFunction: () => Promise +): SelectorStrict, unknown, unknown> { + invariant( + !stores.has(storeName), + 'adHocSelector was run multiple times for store name %s', + storeName + ); + stores.add(storeName); + class AdHocStore extends BaseSingleDataStore { + __loadPromise: () => Promise = genFunction; + } + + const adHocStore = new AdHocStore(storeName, DataAtom); + + return createLoadObjectSelector([storeToSelector(adHocStore)], () => + adHocStore.getData() + ); +} + +// $FlowFixMe[incompatible-type] +export default adHocSelector as ( + storeName: string, + genFunction: () => Promise +) => SelectorStrict>; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8451b37122427abb0e0dd96550fbd9e84b312712 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +/** + * @flow strict-local + * @format + * @providesInline resize_plugin + */ +/*globals message, origin, __nativeHelper*/ +(function () { + // $FlowFixMe[incompatible-use] + // $FlowFixMe[incompatible-use] + if (x) { + } else { + // In iOS, we have to wait a minimum of time before trying to close the + // window, this is most lightly related to the time it takes to animate the + // tab change. + var delay = x; + if (delay) { + x(function () {}, delay); + } else { + } + } +})(); + +``` + + +## Error + +``` +Found 1 error: + +Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + +error.bug-hir_structural_diff.ts:15:4 + 13 | // window, this is most lightly related to the time it takes to animate the + 14 | // tab change. +> 15 | var delay = x; + | ^^^^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration + 16 | if (delay) { + 17 | x(function () {}, delay); + 18 | } else { +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.js new file mode 100644 index 0000000000000000000000000000000000000000..714944ceee8dea1b378e15ca88fe0062c3ed6603 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-hir_structural_diff.js @@ -0,0 +1,21 @@ +/** + * @flow strict-local + * @format + * @providesInline resize_plugin + */ +/*globals message, origin, __nativeHelper*/ +(function () { + // $FlowFixMe[incompatible-use] + // $FlowFixMe[incompatible-use] + if (x) { + } else { + // In iOS, we have to wait a minimum of time before trying to close the + // window, this is most lightly related to the time it takes to animate the + // tab change. + var delay = x; + if (delay) { + x(function () {}, delay); + } else { + } + } +})(); diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..8968351e78e259358b85440cf5dcee500fe9396a --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.expect.md @@ -0,0 +1,46 @@ + +## Input + +```javascript +import {useCallback, useRef} from 'react'; + +export default function useThunkDispatch(state, dispatch, extraArg) { + const stateRef = useRef(state); + stateRef.current = state; + + return useCallback( + function thunk(action) { + if (typeof action === 'function') { + return action(thunk, () => stateRef.current, extraArg); + } else { + dispatch(action); + return undefined; + } + }, + [dispatch, extraArg] + ); +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized + + thunk$14. + +error.bug-infer-mutation-aliasing-effects.ts:10:22 + 8 | function thunk(action) { + 9 | if (typeof action === 'function') { +> 10 | return action(thunk, () => stateRef.current, extraArg); + | ^^^^^ this is uninitialized + 11 | } else { + 12 | dispatch(action); + 13 | return undefined; +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.js new file mode 100644 index 0000000000000000000000000000000000000000..3309406fc70ca38ac3bda2d4fce5219e2c30500f --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-effects.js @@ -0,0 +1,18 @@ +import {useCallback, useRef} from 'react'; + +export default function useThunkDispatch(state, dispatch, extraArg) { + const stateRef = useRef(state); + stateRef.current = state; + + return useCallback( + function thunk(action) { + if (typeof action === 'function') { + return action(thunk, () => stateRef.current, extraArg); + } else { + dispatch(action); + return undefined; + } + }, + [dispatch, extraArg] + ); +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..bf039d1601d8a0c47fc421ba0b0b5700b6af42cd --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +// @flow +function Foo() { + function hasError() { + let hasError = false; + return hasError; + } + return
; +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [InferMutationAliasingEffects] Expected value kind to be initialized + + hasError_0$8. + + 5 | return hasError; + 6 | } +> 7 | return
; + | ^^^^^^^^ this is uninitialized + 8 | } + 9 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.js new file mode 100644 index 0000000000000000000000000000000000000000..a6fc340d26a2c2744f528eced5055661498f478e --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-infer-mutation-aliasing-function-shadows-own-name.js @@ -0,0 +1,8 @@ +// @flow +function Foo() { + function hasError() { + let hasError = false; + return hasError; + } + return
; +} diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..cd311c6f219652acb6f49528924eaef2071b8bbb --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.expect.md @@ -0,0 +1,31 @@ + +## Input + +```javascript +const YearsAndMonthsSince = () => { + const diff = foo(); + const months = Math.floor(diff.bar()); + return <>{months}; +}; + +``` + + +## Error + +``` +Found 1 error: + +Invariant: [Codegen] Internal error: MethodCall::property must be an unpromoted + unmemoized MemberExpression + +error.bug-invariant-codegen-methodcall.ts:3:17 + 1 | const YearsAndMonthsSince = () => { + 2 | const diff = foo(); +> 3 | const months = Math.floor(diff.bar()); + | ^^^^^^^^^^ Got: 'Identifier' + 4 | return <>{months}; + 5 | }; + 6 | +``` + + \ No newline at end of file diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.js b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.js new file mode 100644 index 0000000000000000000000000000000000000000..948182653cbe017d9b85173d5f0cf7223d0624b7 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-codegen-methodcall.js @@ -0,0 +1,5 @@ +const YearsAndMonthsSince = () => { + const diff = foo(); + const months = Math.floor(diff.bar()); + return <>{months}; +}; diff --git a/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-couldnt-find-binding-for-decl.expect.md b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-couldnt-find-binding-for-decl.expect.md new file mode 100644 index 0000000000000000000000000000000000000000..b50ad7035939ec85e450cba1a0726f1f40925ef9 --- /dev/null +++ b/github_code/react__react/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.bug-invariant-couldnt-find-binding-for-decl.expect.md @@ -0,0 +1,37 @@ + +## Input + +```javascript +import {useEffect} from 'react'; + +export function Foo() { + useEffect(() => { + try { + // do something + } catch ({status}) { + // do something + } + }, []); +} + +``` + + +## Error + +``` +Found 1 error: + +Invariant: (BuildHIR::lowerAssignment) Could not find binding for declaration. + +error.bug-invariant-couldnt-find-binding-for-decl.ts:7:14 + 5 | try { + 6 | // do something +> 7 | } catch ({status}) { + | ^^^^^^ (BuildHIR::lowerAssignment) Could not find binding for declaration. + 8 | // do something + 9 | } + 10 | }, []); +``` + + \ No newline at end of file