mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[compiler] Allow hoisting of destructured variable declarations
Summary:
It doesn't seem as though this invariant was necessary
ghstack-source-id: b27e765259
Pull Request resolved: https://github.com/facebook/react/pull/30699
This commit is contained in:
@@ -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' &&
|
||||
|
||||
+62
@@ -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>
|
||||
+17
@@ -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: [],
|
||||
};
|
||||
Reference in New Issue
Block a user