From b2b0b0f6c634603da6f486c12629218882c45f4a Mon Sep 17 00:00:00 2001 From: Mike Vitousek Date: Tue, 13 Aug 2024 15:09:59 -0400 Subject: [PATCH] [compiler] Reordering of logical expressions ghstack-source-id: 0f92be69d258ddfdb10e39dcbc80819557be2441 Pull Request resolved: https://github.com/facebook/react/pull/30678 --- .../src/HIR/BuildHIR.ts | 15 +++++++ .../compiler/logical-reorder.flow.expect.md | 43 +++++++++++++++++++ .../fixtures/compiler/logical-reorder.flow.js | 16 +++++++ 3 files changed, 74 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.js diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts index b9bddff6a58b5..859f1fdc45bed 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts @@ -2837,6 +2837,21 @@ function isReorderableExpression( allowLocalIdentifiers, ); } + case 'LogicalExpression': { + const logical = expr as NodePath; + return ( + isReorderableExpression( + builder, + logical.get('left'), + allowLocalIdentifiers, + ) && + isReorderableExpression( + builder, + logical.get('right'), + allowLocalIdentifiers, + ) + ); + } case 'ConditionalExpression': { const conditional = expr as NodePath; return ( diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.expect.md new file mode 100644 index 0000000000000..e24d2c5525813 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +//@flow + +const foo = undefined; + +component C( + ...{ + scope = foo ?? null, + }: any +) { + return scope; +} + +export const FIXTURE_ENTRYPOINT = { + fn: C, + params: [{scope: undefined}], +}; + +``` + +## Code + +```javascript +const foo = undefined; + +function C(t0) { + const { scope: t1 } = t0; + const scope = t1 === undefined ? (foo ?? null) : t1; + return scope; +} + +export const FIXTURE_ENTRYPOINT = { + fn: C, + params: [{ scope: undefined }], +}; + +``` + +### Eval output +(kind: ok) null \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.js new file mode 100644 index 0000000000000..497a1ceca1cd2 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/logical-reorder.flow.js @@ -0,0 +1,16 @@ +//@flow + +const foo = undefined; + +component C( + ...{ + scope = foo ?? null, + }: any +) { + return scope; +} + +export const FIXTURE_ENTRYPOINT = { + fn: C, + params: [{scope: undefined}], +};