Skip to content

Commit

Permalink
refactor(transformer): use ScopeFlags.is_arrow instead of inside_arro…
Browse files Browse the repository at this point in the history
…w_function_stack
  • Loading branch information
Dunqing authored and overlookmotel committed Sep 18, 2024
1 parent 0c8733d commit 727a66d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 81 deletions.
42 changes: 3 additions & 39 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ pub struct ArrowFunctions<'a> {
ctx: Ctx<'a>,
_options: ArrowFunctionsOptions,
this_var_stack: std::vec::Vec<Option<BoundIdentifier<'a>>>,
/// Stack to keep track of whether we are inside an arrow function or not.
inside_arrow_function_stack: std::vec::Vec<bool>,
}

impl<'a> ArrowFunctions<'a> {
Expand All @@ -104,7 +102,6 @@ impl<'a> ArrowFunctions<'a> {
_options: options,
// Initial entries for `Program` scope
this_var_stack: vec![None],
inside_arrow_function_stack: vec![false],
}
}
}
Expand All @@ -115,8 +112,6 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {

/// Insert `var _this = this;` for the global scope.
fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
debug_assert!(self.inside_arrow_function_stack.len() == 1);

assert!(self.this_var_stack.len() == 1);
let this_var = self.this_var_stack.pop().unwrap();
if let Some(this_var) = this_var {
Expand All @@ -127,7 +122,6 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
fn enter_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
if func.body.is_some() {
self.this_var_stack.push(None);
self.inside_arrow_function_stack.push(false);
}
}

Expand All @@ -154,32 +148,6 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
&this_var,
);
}

self.inside_arrow_function_stack.pop().unwrap();
}

fn enter_arrow_function_expression(
&mut self,
_arrow: &mut ArrowFunctionExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.inside_arrow_function_stack.push(true);
}

fn exit_arrow_function_expression(
&mut self,
_arrow: &mut ArrowFunctionExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.inside_arrow_function_stack.pop().unwrap();
}

fn enter_class(&mut self, _class: &mut Class<'a>, _ctx: &mut TraverseCtx<'a>) {
self.inside_arrow_function_stack.push(false);
}

fn exit_class(&mut self, _class: &mut Class<'a>, _ctx: &mut TraverseCtx<'a>) {
self.inside_arrow_function_stack.pop().unwrap();
}

fn enter_static_block(&mut self, _block: &mut StaticBlock<'a>, _ctx: &mut TraverseCtx<'a>) {
Expand All @@ -200,7 +168,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
ctx: &mut TraverseCtx<'a>,
) {
if let JSXElementName::ThisExpression(this) = element_name {
if !self.is_inside_arrow_function() {
if !ctx.current_scope_flags().is_arrow() {
return;
}

Expand All @@ -215,7 +183,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
ctx: &mut TraverseCtx<'a>,
) {
if let JSXMemberExpressionObject::ThisExpression(this) = object {
if !self.is_inside_arrow_function() {
if !ctx.current_scope_flags().is_arrow() {
return;
}

Expand All @@ -226,7 +194,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::ThisExpression(this_expr) = expr {
if !self.is_inside_arrow_function() {
if !ctx.current_scope_flags().is_arrow() {
return;
}

Expand All @@ -249,10 +217,6 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
}

impl<'a> ArrowFunctions<'a> {
fn is_inside_arrow_function(&self) -> bool {
*self.inside_arrow_function_stack.last().unwrap()
}

fn get_this_identifier(
&mut self,
span: Span,
Expand Down
32 changes: 0 additions & 32 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,6 @@ impl<'a> Traverse<'a> for ES2015<'a> {
}
}

fn enter_arrow_function_expression(
&mut self,
arrow: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_arrow_function_expression(arrow, ctx);
}
}

fn exit_arrow_function_expression(
&mut self,
arrow: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.arrow_function.is_some() {
self.arrow_functions.exit_arrow_function_expression(arrow, ctx);
}
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_expression(expr, ctx);
Expand All @@ -83,18 +63,6 @@ impl<'a> Traverse<'a> for ES2015<'a> {
}
}

fn enter_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_class(class, ctx);
}
}

fn exit_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.exit_class(class, ctx);
}
}

fn enter_static_block(&mut self, block: &mut StaticBlock<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.enter_static_block(block, ctx);
Expand Down
8 changes: 0 additions & 8 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {
ctx: &mut TraverseCtx<'a>,
) {
self.x0_typescript.enter_arrow_function_expression(arrow, ctx);
self.x3_es2015.enter_arrow_function_expression(arrow, ctx);
}

fn enter_binding_pattern(&mut self, pat: &mut BindingPattern<'a>, ctx: &mut TraverseCtx<'a>) {
Expand All @@ -156,11 +155,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {

fn enter_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
self.x0_typescript.enter_class(class, ctx);
self.x3_es2015.enter_class(class, ctx);
}

fn exit_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
self.x3_es2015.exit_class(class, ctx);
}

fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -311,8 +305,6 @@ impl<'a> Traverse<'a> for Transformer<'a> {
arrow: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x3_es2015.exit_arrow_function_expression(arrow, ctx);

// Some plugins may add new statements to the ArrowFunctionExpression's body,
// which can cause issues with the `() => x;` case, as it only allows a single statement.
// To address this, we wrap the last statement in a return statement and set the expression to false.
Expand Down
8 changes: 6 additions & 2 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
commit: 3bcfee23

Passed: 46/56
Passed: 46/57

# All Passed:
* babel-plugin-transform-nullish-coalescing-operator
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-arrow-functions
* babel-preset-typescript
* regexp


# babel-plugin-transform-arrow-functions (5/6)
* use-this-inside-blocks-in-arrow/input.js
x Output mismatch


# babel-plugin-transform-typescript (1/8)
* class-property-definition/input.ts
Unresolved references mismatch:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function outer() {
let f = () => {
{
let t = this;
}
};

let f2 = () => {
if (x) {
if (y) {
return this;
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function outer() {
var _this = this;

let f = function() {
{
let t = _this;
}
};

let f2 = function() {
if (x) {
if (y) {
return _this;
}
}
};
}

0 comments on commit 727a66d

Please sign in to comment.