Skip to content

Commit

Permalink
refactor(transformer): remove unsued self.ctx (#6022)
Browse files Browse the repository at this point in the history
They are remnants from our past code.
  • Loading branch information
Boshen committed Sep 24, 2024
1 parent 0658576 commit 2b380c8
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 149 deletions.
58 changes: 32 additions & 26 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ use oxc_syntax::{
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
use serde::Deserialize;

use crate::{
context::Ctx,
helpers::{bindings::BoundIdentifier, stack::SparseStack},
};
use crate::helpers::{bindings::BoundIdentifier, stack::SparseStack};

#[derive(Debug, Default, Clone, Deserialize)]
pub struct ArrowFunctionsOptions {
Expand All @@ -147,15 +144,14 @@ pub struct ArrowFunctionsOptions {
}

pub struct ArrowFunctions<'a> {
ctx: Ctx<'a>,
_options: ArrowFunctionsOptions,
this_var_stack: SparseStack<BoundIdentifier<'a>>,
}

impl<'a> ArrowFunctions<'a> {
pub fn new(options: ArrowFunctionsOptions, ctx: Ctx<'a>) -> Self {
pub fn new(options: ArrowFunctionsOptions) -> Self {
// `SparseStack` is created with 1 empty entry, for `Program`
Self { ctx, _options: options, this_var_stack: SparseStack::new() }
Self { _options: options, this_var_stack: SparseStack::new() }
}
}

Expand All @@ -164,9 +160,13 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
// <https://www.typescriptlang.org/play/?#code/HYQwtgpgzgDiDGEAEAxA9mpBvAsAKCSXjWCgBckANJAXiQAoBKWgPiTIAsBLKAbnwC++fGDQATAK4AbZACEQAJ2z5CxUhWp0mrdtz6D8QA>

/// Insert `var _this = this;` for the global scope.
fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(this_var) = self.this_var_stack.take() {
self.insert_this_var_statement_at_the_top_of_statements(&mut program.body, &this_var);
self.insert_this_var_statement_at_the_top_of_statements(
&mut program.body,
&this_var,
ctx,
);
}
debug_assert!(self.this_var_stack.len() == 1);
}
Expand All @@ -186,13 +186,14 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
/// }
/// ```
/// Insert the var _this = this; statement outside the arrow function
fn exit_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
fn exit_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(this_var) = self.this_var_stack.pop() {
let Some(body) = &mut func.body else { unreachable!() };

self.insert_this_var_statement_at_the_top_of_statements(
&mut body.statements,
&this_var,
ctx,
);
}
}
Expand All @@ -201,9 +202,13 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
self.this_var_stack.push(None);
}

fn exit_static_block(&mut self, block: &mut StaticBlock<'a>, _ctx: &mut TraverseCtx<'a>) {
fn exit_static_block(&mut self, block: &mut StaticBlock<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(this_var) = self.this_var_stack.pop() {
self.insert_this_var_statement_at_the_top_of_statements(&mut block.body, &this_var);
self.insert_this_var_statement_at_the_top_of_statements(
&mut block.body,
&this_var,
ctx,
);
}
}

Expand All @@ -214,7 +219,7 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
) {
if let JSXElementName::ThisExpression(this) = element_name {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*element_name = self.ctx.ast.jsx_element_name_from_identifier_reference(ident);
*element_name = ctx.ast.jsx_element_name_from_identifier_reference(ident);
}
};
}
Expand All @@ -226,16 +231,15 @@ impl<'a> Traverse<'a> for ArrowFunctions<'a> {
) {
if let JSXMemberExpressionObject::ThisExpression(this) = object {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*object =
self.ctx.ast.jsx_member_expression_object_from_identifier_reference(ident);
*object = ctx.ast.jsx_member_expression_object_from_identifier_reference(ident);
}
}
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::ThisExpression(this) = expr {
if let Some(ident) = self.get_this_identifier(this.span, ctx) {
*expr = self.ctx.ast.expression_from_identifier_reference(ident);
*expr = ctx.ast.expression_from_identifier_reference(ident);
}
}
}
Expand Down Expand Up @@ -336,7 +340,7 @@ impl<'a> ArrowFunctions<'a> {
// Function (includes class method body)
| Ancestor::FunctionParams(_)
| Ancestor::FunctionBody(_)
// Class property body
// Class property body
| Ancestor::PropertyDefinitionValue(_)
// Class static block
| Ancestor::StaticBlockBody(_) => return None,
Expand All @@ -352,6 +356,7 @@ impl<'a> ArrowFunctions<'a> {
unreachable!();
}

#[expect(clippy::unused_self)]
fn transform_arrow_function_expression(
&mut self,
arrow_function_expr: ArrowFunctionExpression<'a>,
Expand All @@ -364,7 +369,7 @@ impl<'a> ArrowFunctions<'a> {
let stmt = body.statements.pop().unwrap();
let Statement::ExpressionStatement(stmt) = stmt else { unreachable!() };
let stmt = stmt.unbox();
let return_statement = self.ctx.ast.statement_return(stmt.span, Some(stmt.expression));
let return_statement = ctx.ast.statement_return(stmt.span, Some(stmt.expression));
body.statements.push(return_statement);
}

Expand All @@ -387,35 +392,36 @@ impl<'a> ArrowFunctions<'a> {
);
new_function.scope_id.set(Some(scope_id));

Expression::FunctionExpression(self.ctx.ast.alloc(new_function))
Expression::FunctionExpression(ctx.ast.alloc(new_function))
}

/// Insert `var _this = this;` at the top of the statements.
#[expect(clippy::unused_self)]
fn insert_this_var_statement_at_the_top_of_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
this_var: &BoundIdentifier<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let binding_pattern = self.ctx.ast.binding_pattern(
self.ctx
.ast
let binding_pattern = ctx.ast.binding_pattern(
ctx.ast
.binding_pattern_kind_from_binding_identifier(this_var.create_binding_identifier()),
NONE,
false,
);

let variable_declarator = self.ctx.ast.variable_declarator(
let variable_declarator = ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
binding_pattern,
Some(self.ctx.ast.expression_this(SPAN)),
Some(ctx.ast.expression_this(SPAN)),
false,
);

let stmt = self.ctx.ast.alloc_variable_declaration(
let stmt = ctx.ast.alloc_variable_declaration(
SPAN,
VariableDeclarationKind::Var,
self.ctx.ast.vec1(variable_declarator),
ctx.ast.vec1(variable_declarator),
false,
);

Expand Down
10 changes: 1 addition & 9 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
mod arrow_functions;
mod options;

use std::rc::Rc;

pub use arrow_functions::{ArrowFunctions, ArrowFunctionsOptions};
pub use options::ES2015Options;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::Ctx;

#[allow(dead_code)]
pub struct ES2015<'a> {
ctx: Ctx<'a>,
options: ES2015Options,

// Plugins
arrow_functions: ArrowFunctions<'a>,
}

impl<'a> ES2015<'a> {
pub fn new(options: ES2015Options, ctx: Ctx<'a>) -> Self {
pub fn new(options: ES2015Options) -> Self {
Self {
arrow_functions: ArrowFunctions::new(
options.arrow_function.clone().unwrap_or_default(),
Rc::clone(&ctx),
),
ctx,
options,
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use oxc_span::SPAN;
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::{context::Ctx, helpers::stack::SparseStack};
use crate::helpers::stack::SparseStack;

/// ES2016: Exponentiation Operator
///
Expand All @@ -46,7 +46,6 @@ use crate::{context::Ctx, helpers::stack::SparseStack};
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-exponentiation-operator>
/// * <https://github.com/babel/babel/blob/main/packages/babel-helper-builder-binary-assignment-operator-visitor>
pub struct ExponentiationOperator<'a> {
_ctx: Ctx<'a>,
var_declarations: SparseStack<Vec<'a, VariableDeclarator<'a>>>,
}

Expand All @@ -57,8 +56,8 @@ struct Exploded<'a> {
}

impl<'a> ExponentiationOperator<'a> {
pub fn new(ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, var_declarations: SparseStack::new() }
pub fn new() -> Self {
Self { var_declarations: SparseStack::new() }
}
}

Expand Down
10 changes: 2 additions & 8 deletions crates/oxc_transformer/src/es2016/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
mod exponentiation_operator;
mod options;

use std::rc::Rc;

pub use exponentiation_operator::ExponentiationOperator;
pub use options::ES2016Options;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::Ctx;

#[allow(dead_code)]
pub struct ES2016<'a> {
ctx: Ctx<'a>,
options: ES2016Options,

// Plugins
exponentiation_operator: ExponentiationOperator<'a>,
}

impl<'a> ES2016<'a> {
pub fn new(options: ES2016Options, ctx: Ctx<'a>) -> Self {
Self { exponentiation_operator: ExponentiationOperator::new(Rc::clone(&ctx)), ctx, options }
pub fn new(options: ES2016Options) -> Self {
Self { exponentiation_operator: ExponentiationOperator::new(), options }
}
}

Expand Down
18 changes: 5 additions & 13 deletions crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
mod object_rest_spread;
mod options;

use std::rc::Rc;

pub use object_rest_spread::{ObjectRestSpread, ObjectRestSpreadOptions};
pub use options::ES2018Options;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::Ctx;

#[allow(dead_code)]
pub struct ES2018<'a> {
ctx: Ctx<'a>,
pub struct ES2018 {
options: ES2018Options,

// Plugins
object_rest_spread: ObjectRestSpread<'a>,
object_rest_spread: ObjectRestSpread,
}

impl<'a> ES2018<'a> {
pub fn new(options: ES2018Options, ctx: Ctx<'a>) -> Self {
impl ES2018 {
pub fn new(options: ES2018Options) -> Self {
Self {
object_rest_spread: ObjectRestSpread::new(
options.object_rest_spread.unwrap_or_default(),
Rc::clone(&ctx),
),
ctx,
options,
}
}
}

impl<'a> Traverse<'a> for ES2018<'a> {
impl<'a> Traverse<'a> for ES2018 {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.object_rest_spread.is_some() {
self.object_rest_spread.enter_expression(expr, ctx);
Expand Down
24 changes: 10 additions & 14 deletions crates/oxc_transformer/src/es2018/object_rest_spread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
//! * Babel plugin implementation: <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-object-rest-spread>
//! * Object rest/spread TC39 proposal: <https://github.com/tc39/proposal-object-rest-spread>

use std::rc::Rc;

use object_rest::ObjectRest;
use object_spread::ObjectSpread;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use serde::Deserialize;

use crate::context::Ctx;
mod object_rest;
mod object_spread;

Expand All @@ -46,28 +43,27 @@ pub struct ObjectRestSpreadOptions {
pub(crate) use_built_ins: bool,
}

#[allow(dead_code)]
pub struct ObjectRestSpread<'a> {
ctx: Ctx<'a>,
pub struct ObjectRestSpread {
#[allow(dead_code)]
options: ObjectRestSpreadOptions,

// Plugins
object_spread: ObjectSpread<'a>,
object_rest: ObjectRest<'a>,
object_spread: ObjectSpread,
#[allow(dead_code)]
object_rest: ObjectRest,
}

impl<'a> ObjectRestSpread<'a> {
pub fn new(options: ObjectRestSpreadOptions, ctx: Ctx<'a>) -> Self {
impl ObjectRestSpread {
pub fn new(options: ObjectRestSpreadOptions) -> Self {
Self {
object_spread: ObjectSpread::new(options, Rc::clone(&ctx)),
object_rest: ObjectRest::new(options, Rc::clone(&ctx)),
ctx,
object_spread: ObjectSpread::new(options),
object_rest: ObjectRest::new(options),
options,
}
}
}

impl<'a> Traverse<'a> for ObjectRestSpread<'a> {
impl<'a> Traverse<'a> for ObjectRestSpread {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.object_spread.enter_expression(expr, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
//! * Object rest/spread TC39 proposal: <https://github.com/tc39/proposal-object-rest-spread>

use super::ObjectRestSpreadOptions;
use crate::context::Ctx;

pub struct ObjectRest<'a> {
_ctx: Ctx<'a>,
pub struct ObjectRest {
_options: ObjectRestSpreadOptions,
}

impl<'a> ObjectRest<'a> {
pub fn new(options: ObjectRestSpreadOptions, ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, _options: options }
impl ObjectRest {
pub fn new(options: ObjectRestSpreadOptions) -> Self {
Self { _options: options }
}
}
Loading

0 comments on commit 2b380c8

Please sign in to comment.