Skip to content

Commit

Permalink
fix(transformer/arrow-functions): correct scope for _this (#5189)
Browse files Browse the repository at this point in the history
The `_this` will eventually be inserted into a `Program` or `Function`. So we have to go up to the corresponding `scope_id`
  • Loading branch information
Dunqing committed Aug 25, 2024
1 parent 056c667 commit d9ba5ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 114 deletions.
10 changes: 9 additions & 1 deletion crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
Expand Down
103 changes: 2 additions & 101 deletions tasks/transform_conformance/babel.snap.md
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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 : []
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d9ba5ad

Please sign in to comment.