Skip to content

Commit

Permalink
[compiler] Allow hoisting of destructured variable declarations
Browse files Browse the repository at this point in the history
Summary:
It doesn't seem as though this invariant was necessary

ghstack-source-id: b27e76525911d5cfc1991b5cfdb7b2074c039e21
Pull Request resolved: #30699
  • Loading branch information
mvitousek committed Aug 15, 2024
1 parent 19bd26b commit bb6acc2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
11 changes: 0 additions & 11 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,6 @@ function lowerStatement(
loc: id.parentPath.node.loc ?? GeneratedSource,
});
continue;
} else if (!binding.path.get('id').isIdentifier()) {
builder.errors.push({
severity: ErrorSeverity.Todo,
reason: 'Unsupported variable declaration type for hoisting',
description: `variable "${
binding.identifier.name
}" declared with ${binding.path.get('id').type}`,
suggestions: null,
loc: id.parentPath.node.loc ?? GeneratedSource,
});
continue;
} else if (
binding.kind !== 'const' &&
binding.kind !== 'var' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

## Input

```javascript
//@flow
component Foo() {
function foo() {
return (
<div>
{a} {z} {y}
</div>
);
}
const [a, {x: z, y = 10}] = [1, {x: 2}];
return foo();
}

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")) {
const foo = function foo() {
return (
<div>
{a} {z} {y}
</div>
);
};

const [t1, t2] = [1, { x: 2 }];
const a = t1;
const { x: t3, y: t4 } = t2;
const z = t3;
const y = t4 === undefined ? 10 : t4;
t0 = foo();
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};

```
### Eval output
(kind: ok) <div>1 2 10</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@flow
component Foo() {
function foo() {
return (
<div>
{a} {z} {y}
</div>
);
}
const [a, {x: z, y = 10}] = [1, {x: 2}];
return foo();
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};

0 comments on commit bb6acc2

Please sign in to comment.