fix(transformer/arrow-functions): correct scope for _this (#5189)

The `_this` will eventually be inserted into a `Program` or `Function`. So we have to go up to the corresponding `scope_id`
This commit is contained in:
Dunqing 2024-08-25 10:26:59 +00:00
parent 056c6679ec
commit d9ba5ad022
3 changed files with 11 additions and 114 deletions

View file

@ -217,8 +217,16 @@ impl<'a> ArrowFunctions<'a> {
fn get_this_name(&mut self, ctx: &mut TraverseCtx<'a>) -> BoundIdentifier<'a> {
let this_var = self.this_vars.last_mut().unwrap();
if this_var.is_none() {
this_var.replace(BoundIdentifier::new_uid_in_current_scope(
let target_scope_id =
ctx.scopes().ancestors(ctx.current_scope_id()).skip(1).find(|scope_id| {
let scope_flags = ctx.scopes().get_flags(*scope_id);
scope_flags.intersects(ScopeFlags::Function | ScopeFlags::Top)
&& !scope_flags.contains(ScopeFlags::Arrow)
});
this_var.replace(BoundIdentifier::new_uid(
"this",
target_scope_id.unwrap(),
SymbolFlags::FunctionScopedVariable,
ctx,
));

View file

@ -1,10 +1,11 @@
commit: 12619ffe
Passed: 280/953
Passed: 281/953
# All Passed:
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-react-display-name
* babel-plugin-transform-react-jsx-self
* babel-plugin-transform-react-jsx-source
@ -1918,35 +1919,11 @@ failed to resolve query: failed to parse the rest of input: ...''
# babel-plugin-transform-arrow-functions (0/6)
* assumption-newableArrowFunctions-false/basic/input.js
x Output mismatch
x Bindings mismatch:
| after transform: ScopeId(1): ["f"]
| rebuilt : ScopeId(1): ["_this2", "f"]
x Bindings mismatch:
| after transform: ScopeId(4): []
| rebuilt : ScopeId(4): ["_this"]
x Bindings mismatch:
| after transform: ScopeId(5): ["_this"]
| rebuilt : ScopeId(5): []
x Bindings mismatch:
| after transform: ScopeId(6): ["_this2"]
| rebuilt : ScopeId(6): []
x Symbol scope ID mismatch:
| after transform: SymbolId(6): ScopeId(6)
| rebuilt : SymbolId(1): ScopeId(1)
x Symbol flags mismatch:
| after transform: SymbolId(2): SymbolFlags(FunctionScopedVariable |
| ArrowFunction)
| rebuilt : SymbolId(3): SymbolFlags(FunctionScopedVariable)
x Symbol scope ID mismatch:
| after transform: SymbolId(5): ScopeId(5)
| rebuilt : SymbolId(6): ScopeId(4)
* assumption-newableArrowFunctions-false/naming/input.js
x Output mismatch
@ -5948,30 +5925,6 @@ failed to resolve query: failed to parse the rest of input: ...''
* react/arrow-functions/input.js
x Bindings mismatch:
| after transform: ScopeId(1): []
| rebuilt : ScopeId(1): ["_this"]
x Bindings mismatch:
| after transform: ScopeId(2): ["_this"]
| rebuilt : ScopeId(2): []
x Bindings mismatch:
| after transform: ScopeId(3): []
| rebuilt : ScopeId(3): ["_this2"]
x Bindings mismatch:
| after transform: ScopeId(4): ["_this2"]
| rebuilt : ScopeId(4): []
x Symbol scope ID mismatch:
| after transform: SymbolId(2): ScopeId(2)
| rebuilt : SymbolId(1): ScopeId(1)
x Symbol scope ID mismatch:
| after transform: SymbolId(3): ScopeId(4)
| rebuilt : SymbolId(3): ScopeId(3)
x Unresolved references mismatch:
| after transform: ["React", "this"]
| rebuilt : ["React"]
@ -6125,30 +6078,6 @@ failed to resolve query: failed to parse the rest of input: ...''
* react-automatic/arrow-functions/input.js
x Bindings mismatch:
| after transform: ScopeId(1): []
| rebuilt : ScopeId(1): ["_this"]
x Bindings mismatch:
| after transform: ScopeId(2): ["_this"]
| rebuilt : ScopeId(2): []
x Bindings mismatch:
| after transform: ScopeId(3): []
| rebuilt : ScopeId(3): ["_this2"]
x Bindings mismatch:
| after transform: ScopeId(4): ["_this2"]
| rebuilt : ScopeId(4): []
x Symbol scope ID mismatch:
| after transform: SymbolId(3): ScopeId(2)
| rebuilt : SymbolId(2): ScopeId(1)
x Symbol scope ID mismatch:
| after transform: SymbolId(4): ScopeId(4)
| rebuilt : SymbolId(4): ScopeId(3)
x Unresolved references mismatch:
| after transform: ["this"]
| rebuilt : []
@ -6282,34 +6211,6 @@ transform-react-jsx: unknown field `autoImport`, expected one of `runtime`, `dev
# babel-plugin-transform-react-jsx-self (2/3)
* react-source/arrow-function/input.js
x Bindings mismatch:
| after transform: ScopeId(0): ["fn"]
| rebuilt : ScopeId(0): ["_this", "fn"]
x Bindings mismatch:
| after transform: ScopeId(1): ["_this"]
| rebuilt : ScopeId(1): []
x Bindings mismatch:
| after transform: ScopeId(2): []
| rebuilt : ScopeId(2): ["_this2"]
x Bindings mismatch:
| after transform: ScopeId(3): ["_this2"]
| rebuilt : ScopeId(3): []
x Symbol scope ID mismatch:
| after transform: SymbolId(1): ScopeId(1)
| rebuilt : SymbolId(0): ScopeId(0)
x Symbol scope ID mismatch:
| after transform: SymbolId(2): ScopeId(3)
| rebuilt : SymbolId(2): ScopeId(2)
# babel-plugin-transform-react-jsx-development (6/10)
* cross-platform/disallow-__self-as-jsx-attribute/input.js
! Duplicate __self prop found.

View file

@ -8,18 +8,6 @@ Passed: 9/36
# babel-plugin-transform-arrow-functions (0/1)
* use-this-inside-blocks/input.js
x Bindings mismatch:
| after transform: ScopeId(1): []
| rebuilt : ScopeId(1): ["_this"]
x Bindings mismatch:
| after transform: ScopeId(3): ["_this"]
| rebuilt : ScopeId(3): []
x Symbol scope ID mismatch:
| after transform: SymbolId(3): ScopeId(3)
| rebuilt : SymbolId(1): ScopeId(1)
x Symbol flags mismatch:
| after transform: SymbolId(1): SymbolFlags(BlockScopedVariable |
| ArrowFunction)