From 155d7fcba32aab28b5376f7203778aaf4d7d3a75 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 21 Sep 2024 09:41:34 +0000 Subject: [PATCH] refactor(transformer): arrow function transform: ignore type fields when finding enclosing arrow function (#5944) Follow-on after #5850. No need to check type fields of `Function` or `ArrowFunctionExpression` when tracing up ancestors from a `this` expression - these fields cannot contain a `ThisExpression`. https://github.com/oxc-project/oxc/pull/5850#pullrequestreview-2319531991 --- .../src/es2015/arrow_functions.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/oxc_transformer/src/es2015/arrow_functions.rs b/crates/oxc_transformer/src/es2015/arrow_functions.rs index 55ace9a59..de10242de 100644 --- a/crates/oxc_transformer/src/es2015/arrow_functions.rs +++ b/crates/oxc_transformer/src/es2015/arrow_functions.rs @@ -268,9 +268,10 @@ impl<'a> ArrowFunctions<'a> { // ``` // // `this` resolves to the class / class instance (i.e. `this` defined *within* the class) in: - // * Class method bodies - // * Class property bodies - // * Class static blocks + // * Method body + // * Method param + // * Property value + // * Static block // // ```js // // All these `this` refer to `this` defined within the class @@ -281,6 +282,7 @@ impl<'a> ArrowFunctions<'a> { // d() { this } // static e() { this } // #f() { this } + // g(x = this) {} // static { this } // } // ``` @@ -291,24 +293,15 @@ impl<'a> ArrowFunctions<'a> { // Top level Ancestor::ProgramBody(_) // Function (includes class method body) - | Ancestor::FunctionTypeParameters(_) - | Ancestor::FunctionThisParam(_) | Ancestor::FunctionParams(_) - | Ancestor::FunctionReturnType(_) | Ancestor::FunctionBody(_) // Class property body | Ancestor::PropertyDefinitionValue(_) // Class static block | Ancestor::StaticBlockBody(_) => return None, - Ancestor::ArrowFunctionExpressionTypeParameters(func) => { - return Some(func.scope_id().get().unwrap()) - } Ancestor::ArrowFunctionExpressionParams(func) => { return Some(func.scope_id().get().unwrap()) } - Ancestor::ArrowFunctionExpressionReturnType(func) => { - return Some(func.scope_id().get().unwrap()) - } Ancestor::ArrowFunctionExpressionBody(func) => { return Some(func.scope_id().get().unwrap()) }