From 42b92128b9e820f6b3b233f1cf9fb840dbc3902b Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 20 Jun 2024 18:49:29 -0700 Subject: [PATCH] [compiler] add fixture for optimization across scopes [ghstack-poisoned] --- .../reordering-across-blocks.expect.md | 85 +++++++++++++++++++ .../compiler/reordering-across-blocks.js | 20 +++++ 2 files changed, 105 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.js diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.expect.md new file mode 100644 index 00000000000..eaac6a0480f --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.expect.md @@ -0,0 +1,85 @@ + +## Input + +```javascript +import { Stringify } from "shared-runtime"; + +function Component({ config }) { + const object = useMemo(() => { + const a = (event) => { + config?.onA?.(event); + }; + + const b = (event) => { + config?.onB?.(event); + }; + + return { + b, + a, + }; + }, [config]); + + return ; +} + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +import { Stringify } from "shared-runtime"; + +function Component(t0) { + const $ = _c(9); + const { config } = t0; + let t1; + let t2; + if ($[0] !== config) { + t2 = (event) => { + config?.onA?.(event); + }; + $[0] = config; + $[1] = t2; + } else { + t2 = $[1]; + } + const a = t2; + let t3; + if ($[2] !== config) { + t3 = (event_0) => { + config?.onB?.(event_0); + }; + $[2] = config; + $[3] = t3; + } else { + t3 = $[3]; + } + const b = t3; + let t4; + if ($[4] !== b || $[5] !== a) { + t4 = { b, a }; + $[4] = b; + $[5] = a; + $[6] = t4; + } else { + t4 = $[6]; + } + t1 = t4; + const object = t1; + let t5; + if ($[7] !== object) { + t5 = ; + $[7] = object; + $[8] = t5; + } else { + t5 = $[8]; + } + return t5; +} + +``` + +### Eval output +(kind: exception) Fixture not implemented \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.js new file mode 100644 index 00000000000..c46c3dfabac --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reordering-across-blocks.js @@ -0,0 +1,20 @@ +import { Stringify } from "shared-runtime"; + +function Component({ config }) { + const object = useMemo(() => { + const a = (event) => { + config?.onA?.(event); + }; + + const b = (event) => { + config?.onB?.(event); + }; + + return { + b, + a, + }; + }, [config]); + + return ; +}