Skip to content

Commit

Permalink
[compiler] Reordering of logical expressions
Browse files Browse the repository at this point in the history
ghstack-source-id: 0f92be69d258ddfdb10e39dcbc80819557be2441
Pull Request resolved: #30678
  • Loading branch information
mvitousek committed Aug 13, 2024
1 parent d48603a commit b2b0b0f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,21 @@ function isReorderableExpression(
allowLocalIdentifiers,
);
}
case 'LogicalExpression': {
const logical = expr as NodePath<t.LogicalExpression>;
return (
isReorderableExpression(
builder,
logical.get('left'),
allowLocalIdentifiers,
) &&
isReorderableExpression(
builder,
logical.get('right'),
allowLocalIdentifiers,
)
);
}
case 'ConditionalExpression': {
const conditional = expr as NodePath<t.ConditionalExpression>;
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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}],
};

0 comments on commit b2b0b0f

Please sign in to comment.