refactor(transformer): remove unsued self.ctx (#6022)

They are remnants from our past code.
This commit is contained in:
Boshen 2024-09-24 10:14:04 +00:00
parent 0658576718
commit 2b380c8f15
15 changed files with 92 additions and 149 deletions

View file

@ -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 {
@ -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() }
}
}
@ -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);
}
@ -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,
);
}
}
@ -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,
);
}
}
@ -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);
}
};
}
@ -226,8 +231,7 @@ 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);
}
}
}
@ -235,7 +239,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 {
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);
}
}
}
@ -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,
@ -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>,
@ -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);
}
@ -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,
);

View file

@ -1,18 +1,12 @@
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
@ -20,13 +14,11 @@ pub struct ES2015<'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,
}
}

View file

@ -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
///
@ -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>>>,
}
@ -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() }
}
}

View file

@ -1,19 +1,13 @@
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
@ -21,8 +15,8 @@ pub struct ES2016<'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 }
}
}

View file

@ -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);

View file

@ -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;
@ -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);
}

View file

@ -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 }
}
}

View file

@ -32,19 +32,17 @@ use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};
use super::ObjectRestSpreadOptions;
use crate::context::Ctx;
pub struct ObjectSpread<'a> {
_ctx: Ctx<'a>,
pub struct ObjectSpread {
options: ObjectRestSpreadOptions,
}
impl<'a> ObjectSpread<'a> {
pub fn new(options: ObjectRestSpreadOptions, ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, options }
impl ObjectSpread {
pub fn new(options: ObjectRestSpreadOptions) -> Self {
Self { options }
}
}
impl<'a> Traverse<'a> for ObjectSpread<'a> {
impl<'a> Traverse<'a> for ObjectSpread {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let Expression::ObjectExpression(obj_expr) = expr else {
return;
@ -100,7 +98,7 @@ impl<'a> Traverse<'a> for ObjectSpread<'a> {
}
}
impl<'a> ObjectSpread<'a> {
impl<'a> ObjectSpread {
fn object_assign(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
let ident =
ctx.create_reference_id(SPAN, Atom::from("Object"), symbol_id, ReferenceFlags::Read);

View file

@ -1,31 +1,25 @@
mod optional_catch_binding;
mod options;
use std::rc::Rc;
pub use optional_catch_binding::OptionalCatchBinding;
pub use options::ES2019Options;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use crate::context::Ctx;
#[allow(dead_code)]
pub struct ES2019<'a> {
ctx: Ctx<'a>,
pub struct ES2019 {
options: ES2019Options,
// Plugins
optional_catch_binding: OptionalCatchBinding<'a>,
optional_catch_binding: OptionalCatchBinding,
}
impl<'a> ES2019<'a> {
pub fn new(options: ES2019Options, ctx: Ctx<'a>) -> Self {
Self { optional_catch_binding: OptionalCatchBinding::new(Rc::clone(&ctx)), ctx, options }
impl ES2019 {
pub fn new(options: ES2019Options) -> Self {
Self { optional_catch_binding: OptionalCatchBinding::new(), options }
}
}
impl<'a> Traverse<'a> for ES2019<'a> {
impl<'a> Traverse<'a> for ES2019 {
fn enter_catch_clause(&mut self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.optional_catch_binding {
self.optional_catch_binding.enter_catch_clause(clause, ctx);

View file

@ -37,19 +37,15 @@ use oxc_semantic::SymbolFlags;
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};
use crate::context::Ctx;
pub struct OptionalCatchBinding;
pub struct OptionalCatchBinding<'a> {
_ctx: Ctx<'a>,
}
impl<'a> OptionalCatchBinding<'a> {
pub fn new(ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx }
impl OptionalCatchBinding {
pub fn new() -> Self {
Self
}
}
impl<'a> Traverse<'a> for OptionalCatchBinding<'a> {
impl<'a> Traverse<'a> for OptionalCatchBinding {
/// If CatchClause has no param, add a parameter called `unused`.
#[allow(clippy::unused_self)]
fn enter_catch_clause(&mut self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {

View file

@ -1,19 +1,13 @@
mod nullish_coalescing_operator;
mod options;
use std::rc::Rc;
pub use nullish_coalescing_operator::NullishCoalescingOperator;
pub use options::ES2020Options;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use crate::context::Ctx;
#[allow(dead_code)]
pub struct ES2020<'a> {
ctx: Ctx<'a>,
options: ES2020Options,
// Plugins
@ -21,12 +15,8 @@ pub struct ES2020<'a> {
}
impl<'a> ES2020<'a> {
pub fn new(options: ES2020Options, ctx: Ctx<'a>) -> Self {
Self {
nullish_coalescing_operator: NullishCoalescingOperator::new(Rc::clone(&ctx)),
ctx,
options,
}
pub fn new(options: ES2020Options) -> Self {
Self { nullish_coalescing_operator: NullishCoalescingOperator::new(), options }
}
}

View file

@ -35,16 +35,15 @@ use oxc_span::SPAN;
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
use crate::{context::Ctx, helpers::stack::SparseStack};
use crate::helpers::stack::SparseStack;
pub struct NullishCoalescingOperator<'a> {
_ctx: Ctx<'a>,
var_declarations: SparseStack<Vec<'a, VariableDeclarator<'a>>>,
}
impl<'a> NullishCoalescingOperator<'a> {
pub fn new(ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, var_declarations: SparseStack::new() }
pub fn new() -> Self {
Self { var_declarations: SparseStack::new() }
}
}

View file

@ -60,16 +60,15 @@ use oxc_span::SPAN;
use oxc_syntax::operator::{AssignmentOperator, LogicalOperator};
use oxc_traverse::{Traverse, TraverseCtx};
use crate::{context::Ctx, helpers::stack::SparseStack};
use crate::helpers::stack::SparseStack;
pub struct LogicalAssignmentOperators<'a> {
_ctx: Ctx<'a>,
var_declarations: SparseStack<Vec<'a, VariableDeclarator<'a>>>,
}
impl<'a> LogicalAssignmentOperators<'a> {
pub fn new(ctx: Ctx<'a>) -> Self {
Self { _ctx: ctx, var_declarations: SparseStack::new() }
pub fn new() -> Self {
Self { var_declarations: SparseStack::new() }
}
}

View file

@ -1,19 +1,13 @@
mod logical_assignment_operators;
mod options;
use std::rc::Rc;
pub use logical_assignment_operators::LogicalAssignmentOperators;
pub use options::ES2021Options;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use crate::context::Ctx;
#[allow(dead_code)]
pub struct ES2021<'a> {
ctx: Ctx<'a>,
options: ES2021Options,
// Plugins
@ -21,12 +15,8 @@ pub struct ES2021<'a> {
}
impl<'a> ES2021<'a> {
pub fn new(options: ES2021Options, ctx: Ctx<'a>) -> Self {
Self {
logical_assignment_operators: LogicalAssignmentOperators::new(Rc::clone(&ctx)),
ctx,
options,
}
pub fn new(options: ES2021Options) -> Self {
Self { logical_assignment_operators: LogicalAssignmentOperators::new(), options }
}
}

View file

@ -73,8 +73,8 @@ pub struct Transformer<'a> {
x1_react: React<'a>,
x2_es2021: ES2021<'a>,
x2_es2020: ES2020<'a>,
x2_es2019: ES2019<'a>,
x2_es2018: ES2018<'a>,
x2_es2019: ES2019,
x2_es2018: ES2018,
x2_es2016: ES2016<'a>,
x3_es2015: ES2015<'a>,
x4_regexp: RegExp<'a>,
@ -101,12 +101,12 @@ impl<'a> Transformer<'a> {
ctx: Rc::clone(&ctx),
x0_typescript: TypeScript::new(options.typescript, Rc::clone(&ctx)),
x1_react: React::new(options.react, Rc::clone(&ctx)),
x2_es2021: ES2021::new(options.es2021, Rc::clone(&ctx)),
x2_es2020: ES2020::new(options.es2020, Rc::clone(&ctx)),
x2_es2019: ES2019::new(options.es2019, Rc::clone(&ctx)),
x2_es2018: ES2018::new(options.es2018, Rc::clone(&ctx)),
x2_es2016: ES2016::new(options.es2016, Rc::clone(&ctx)),
x3_es2015: ES2015::new(options.es2015, Rc::clone(&ctx)),
x2_es2021: ES2021::new(options.es2021),
x2_es2020: ES2020::new(options.es2020),
x2_es2019: ES2019::new(options.es2019),
x2_es2018: ES2018::new(options.es2018),
x2_es2016: ES2016::new(options.es2016),
x3_es2015: ES2015::new(options.es2015),
x4_regexp: RegExp::new(options.regexp, ctx),
}
}