dillon1000/react
Commit
Browse files [hoisting][patch] use Babel identifier apis in BuildHIR hoisting logic
---
Three functional changes:
- Instead of visiting all identifier references, explicitly traverse only
function decls/exprs. This avoids bugs like accidentally hoisting inline
references
```js
// input
const x = identity(y);
const y = 2;
// lowered HIR before this PR (simplified)
[0] DeclareContext HoistedConst y$0
[1] LoadContext y$0
[2] StoreLocal Const x$5 = identity([1])
```
- Rely on `isReferencedIdentifier()` instead of manually checking member
properties / assignments, which is error prone
```js
// added fixture hoisting-repro-variable-used-in-assignment
const callbk = () => {
// before this PR, we skip hoisting x because it's part of a declaration
const copy = x;
return copy;
};
const x = 2;
return callbk();
```
- Visit lvalues after rvalues. This allows for recursive self-references (e.g.
factorial)
From the Babel side, this change relies heavily on babel's scope binding
resolution logic. My understanding is:
- Babel guarantees node objects are uniqued (`node1 === node2` <--> node1 and
node2 are the same node in the ast)
- Each binding has exactly one `bindingIdentifier` (`binding.identifier`,
`getBindingIdentifier`, etc) which is identifier node @ its declaration site
```js
// x is a binding identifier
const x = 2;
// foo is a binding identifier
function foo() {
}
// param is a binding identifier
(param) => {...}
// this bar is a binding identifier
let bar;
// but not this bar
bar = 2;
```Changed paths14 files
First-parent comparisoncompiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts ModifiedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-hoisting2.expect.md DeletedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.bug-hoisting2.js DeletedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.dont-hoist-inline-reference.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.dont-hoist-inline-reference.js AddedD compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-recursive-references-inner-functions.expect.md DeletedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-object-method.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-object-method.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-recursive-call-within-lambda.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-recursive-call-within-lambda.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-recursive-call.expect.md AddedR compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.todo-recursive-references-inner-functions.ts →compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-recursive-call.ts RenamedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hoisting-repro-variable-used-in-assignment.js AddedPatch
Files changed
Rendering syntax-highlighted changes…