Skip to content

Commit

Permalink
refactor(traverse): generate_uid return a BoundIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 5, 2024
1 parent 365af45 commit 01ea97a
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 214 deletions.
10 changes: 4 additions & 6 deletions crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use oxc_allocator::Vec;
use oxc_ast::{ast::*, NONE};
use oxc_data_structures::stack::SparseStack;
use oxc_span::SPAN;
use oxc_syntax::symbol::SymbolId;
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};

use crate::TransformCtx;

Expand Down Expand Up @@ -68,15 +67,14 @@ impl<'a> VarDeclarationsStore<'a> {
}

/// Add a `VariableDeclarator` to be inserted at top of current enclosing statement block,
/// given `name` and `symbol_id`.
/// given a `BoundIdentifier`.
pub fn insert(
&self,
name: Atom<'a>,
symbol_id: SymbolId,
binding: &BoundIdentifier<'a>,
init: Option<Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
let ident = BindingIdentifier::new_with_symbol_id(SPAN, name, symbol_id);
let ident = binding.create_binding_identifier();
let ident = ctx.ast.binding_pattern_kind_from_binding_identifier(ident);
let ident = ctx.ast.binding_pattern(ident, NONE, false);
self.insert_binding_pattern(ident, init, ctx);
Expand Down
8 changes: 1 addition & 7 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,7 @@ impl<'a> ArrowFunctions<'a> {
) && !scope_flags.contains(ScopeFlags::Arrow)
})
.unwrap();

BoundIdentifier::new_uid(
"this",
target_scope_id,
SymbolFlags::FunctionScopedVariable,
ctx,
)
ctx.generate_uid("this", target_scope_id, SymbolFlags::FunctionScopedVariable)
});
Some(this_var.create_spanned_read_reference(span, ctx))
}
Expand Down
15 changes: 5 additions & 10 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,18 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
_ => "ref",
};

let symbol_id =
ctx.generate_uid_in_current_scope(name, SymbolFlags::FunctionScopedVariable);
let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));
let binding = ctx.generate_uid_in_current_scope(name, SymbolFlags::FunctionScopedVariable);

// var _name;
self.ctx.var_declarations.insert(symbol_name.clone(), symbol_id, None, ctx);
self.ctx.var_declarations.insert(&binding, None, ctx);

let ident =
ctx.create_bound_reference_id(SPAN, symbol_name, symbol_id, ReferenceFlags::Read);

// let ident = self.create_new_var_with_expression(&expr);
// Add new reference `_name = name` to nodes
let left = ctx.ast.simple_assignment_target_from_identifier_reference(
ctx.clone_identifier_reference(&ident, ReferenceFlags::Write),
binding.create_write_reference(ctx),
);
let op = AssignmentOperator::Assign;
nodes.push(ctx.ast.expression_assignment(SPAN, op, AssignmentTarget::from(left), expr));
ctx.ast.expression_from_identifier_reference(ident)

ctx.ast.expression_from_identifier_reference(binding.create_read_reference(ctx))
}
}
13 changes: 4 additions & 9 deletions crates/oxc_transformer/src/es2019/optional_catch_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! * Babel plugin implementation: <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-optional-catch-binding>
//! * Optional catch binding TC39 proposal: <https://github.com/tc39/proposal-optional-catch-binding>

use oxc_ast::{ast::*, NONE};
use oxc_ast::ast::*;
use oxc_semantic::SymbolFlags;
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};
Expand All @@ -53,17 +53,12 @@ impl<'a> Traverse<'a> for OptionalCatchBinding {
return;
}

let block_scope_id = clause.body.scope_id.get().unwrap();
let symbol_id = ctx.generate_uid(
let binding = ctx.generate_uid(
"unused",
block_scope_id,
clause.body.scope_id.get().unwrap(),
SymbolFlags::CatchVariable | SymbolFlags::FunctionScopedVariable,
);
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));
let binding_identifier = BindingIdentifier::new_with_symbol_id(SPAN, name, symbol_id);
let binding_pattern_kind =
ctx.ast.binding_pattern_kind_from_binding_identifier(binding_identifier);
let binding_pattern = ctx.ast.binding_pattern(binding_pattern_kind, NONE, false);
let binding_pattern = binding.create_binding_pattern(ctx);
let param = ctx.ast.catch_parameter(SPAN, binding_pattern);
clause.param = Some(param);
}
Expand Down
11 changes: 3 additions & 8 deletions crates/oxc_transformer/src/es2020/nullish_coalescing_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,16 @@ impl<'a, 'ctx> NullishCoalescingOperator<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> (BindingPattern<'a>, IdentifierReference<'a>) {
// Add `var name` to scope
let symbol_id = ctx.generate_uid_based_on_node(
let binding = ctx.generate_uid_based_on_node(
expr,
current_scope_id,
SymbolFlags::FunctionScopedVariable,
);
let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));

// var _name;
let binding_identifier =
BindingIdentifier::new_with_symbol_id(SPAN, symbol_name.clone(), symbol_id);
let id = ctx.ast.binding_pattern_kind_from_binding_identifier(binding_identifier);
let id = ctx.ast.binding_pattern(id, NONE, false);
let reference =
ctx.create_bound_reference_id(SPAN, symbol_name, symbol_id, ReferenceFlags::Read);
let id = binding.create_binding_pattern(ctx);

let reference = binding.create_read_reference(ctx);
(id, reference)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,11 @@ impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
return None;
}

let symbol_id = ctx
.generate_uid_in_current_scope_based_on_node(expr, SymbolFlags::FunctionScopedVariable);
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));

// var _name;
self.ctx.var_declarations.insert(name.clone(), symbol_id, None, ctx);
let binding = ctx
.generate_uid_in_current_scope_based_on_node(expr, SymbolFlags::FunctionScopedVariable);
self.ctx.var_declarations.insert(&binding, None, ctx);

Some(BoundIdentifier::new(name, symbol_id))
Some(binding)
}
}
17 changes: 7 additions & 10 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,11 @@ impl<'a, 'ctx> AutomaticScriptBindings<'a, 'ctx> {
front: bool,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let symbol_id =
let binding =
ctx.generate_uid_in_root_scope(variable_name, SymbolFlags::FunctionScopedVariable);
let variable_name = ctx.ast.atom(&ctx.symbols().names[symbol_id]);

let import = NamedImport::new(variable_name.clone(), None, symbol_id);
let import = NamedImport::new(binding.name.clone(), None, binding.symbol_id);
self.ctx.module_imports.add_import(source, import, front);
BoundIdentifier { name: variable_name, symbol_id }
binding
}
}

Expand Down Expand Up @@ -297,12 +295,11 @@ impl<'a, 'ctx> AutomaticModuleBindings<'a, 'ctx> {
source: Atom<'a>,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let symbol_id = ctx.generate_uid_in_root_scope(name, SymbolFlags::Import);
let local = ctx.ast.atom(&ctx.symbols().names[symbol_id]);

let import = NamedImport::new(Atom::from(name), Some(local.clone()), symbol_id);
let binding = ctx.generate_uid_in_root_scope(name, SymbolFlags::Import);
let import =
NamedImport::new(Atom::from(name), Some(binding.name.clone()), binding.symbol_id);
self.ctx.module_imports.add_import(source, import, false);
BoundIdentifier { name: local, symbol_id }
binding
}
}

Expand Down
12 changes: 5 additions & 7 deletions crates/oxc_transformer/src/react/jsx_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,12 @@ impl<'a, 'ctx> ReactJsxSource<'a, 'ctx> {
Some(decl)
}

fn get_filename_var(&mut self, ctx: &mut TraverseCtx<'a>) -> BoundIdentifier<'a> {
fn get_filename_var(&mut self, ctx: &mut TraverseCtx<'a>) -> &BoundIdentifier<'a> {
if self.filename_var.is_none() {
self.filename_var = Some(BoundIdentifier::new_uid_in_root_scope(
FILE_NAME_VAR,
SymbolFlags::FunctionScopedVariable,
ctx,
));
self.filename_var = Some(
ctx.generate_uid_in_root_scope(FILE_NAME_VAR, SymbolFlags::FunctionScopedVariable),
);
}
self.filename_var.as_ref().unwrap().clone()
self.filename_var.as_ref().unwrap()
}
}
15 changes: 5 additions & 10 deletions crates/oxc_transformer/src/react/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,9 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
reference_flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> AssignmentTarget<'a> {
let symbol_id = ctx.generate_uid_in_root_scope("c", SymbolFlags::FunctionScopedVariable);
self.registrations.push((symbol_id, persistent_id));
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));
let ident = ctx.create_bound_reference_id(SPAN, name, symbol_id, reference_flags);
let binding = ctx.generate_uid_in_root_scope("c", SymbolFlags::FunctionScopedVariable);
self.registrations.push((binding.symbol_id, persistent_id));
let ident = binding.create_reference(reference_flags, ctx);
let ident = ctx.ast.simple_assignment_target_from_identifier_reference(ident);
ctx.ast.assignment_target_simple(ident)
}
Expand Down Expand Up @@ -683,12 +682,8 @@ impl<'a, 'ctx> ReactRefresh<'a, 'ctx> {
.find(|scope_id| ctx.scopes().get_flags(*scope_id).is_var())
.unwrap_or_else(|| ctx.current_scope_id());

let symbol_id = ctx.generate_uid("s", target_scope_id, SymbolFlags::FunctionScopedVariable);

let symbol_name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));

let binding_identifier =
BindingIdentifier::new_with_symbol_id(SPAN, symbol_name.clone(), symbol_id);
let binding = ctx.generate_uid("s", target_scope_id, SymbolFlags::FunctionScopedVariable);
let binding_identifier = binding.create_binding_identifier();

// _s();
let call_expression = ctx.ast.statement_expression(
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/typescript/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {

// Reuse `TSModuleDeclaration`'s scope in transformed function
let scope_id = decl.scope_id.get().unwrap();
let symbol_id = ctx.generate_uid(&real_name, scope_id, SymbolFlags::FunctionScopedVariable);
let name = ctx.ast.atom(ctx.symbols().get_name(symbol_id));
let binding = ctx.generate_uid(&real_name, scope_id, SymbolFlags::FunctionScopedVariable);
let name = binding.name;

let directives;
let namespace_top_level;
Expand Down
60 changes: 21 additions & 39 deletions crates/oxc_traverse/src/context/bound_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use oxc_ast::ast::{BindingIdentifier, IdentifierReference};
use oxc_span::{Atom, Span, SPAN};
use oxc_syntax::{
reference::ReferenceFlags,
scope::ScopeId,
symbol::{SymbolFlags, SymbolId},
use oxc_ast::{
ast::{BindingIdentifier, BindingPattern, IdentifierReference},
NONE,
};
use oxc_span::{Atom, Span, SPAN};
use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId};

use crate::TraverseCtx;

Expand Down Expand Up @@ -47,44 +46,18 @@ impl<'a> BoundIdentifier<'a> {
Self { name, symbol_id }
}

/// Create `BoundIdentifier` for new binding in specified scope
pub fn new_uid(
name: &str,
scope_id: ScopeId,
flags: SymbolFlags,
ctx: &mut TraverseCtx<'a>,
) -> Self {
let symbol_id = ctx.generate_uid(name, scope_id, flags);
let name = ctx.ast.atom(&ctx.symbols().names[symbol_id]);
Self { name, symbol_id }
}

/// Create `BoundIdentifier` for new binding in root scope
pub fn new_uid_in_root_scope(
name: &str,
flags: SymbolFlags,
ctx: &mut TraverseCtx<'a>,
) -> Self {
let scope_id = ctx.scopes().root_scope_id();
Self::new_uid(name, scope_id, flags, ctx)
}

/// Create `BoundIdentifier` for new binding in current scope
#[allow(unused)]
pub fn new_uid_in_current_scope(
name: &str,
flags: SymbolFlags,
ctx: &mut TraverseCtx<'a>,
) -> Self {
let scope_id = ctx.current_scope_id();
Self::new_uid(name, scope_id, flags, ctx)
}

/// Create `BindingIdentifier` for this binding
pub fn create_binding_identifier(&self) -> BindingIdentifier<'a> {
BindingIdentifier::new_with_symbol_id(SPAN, self.name.clone(), self.symbol_id)
}

/// Create `BindingPattern` for this binding
pub fn create_binding_pattern(&self, ctx: &mut TraverseCtx<'a>) -> BindingPattern<'a> {
let ident = self.create_binding_identifier();
let binding_pattern_kind = ctx.ast.binding_pattern_kind_from_binding_identifier(ident);
ctx.ast.binding_pattern(binding_pattern_kind, NONE, false)
}

/// Create `IdentifierReference` referencing this binding, which is read from, with dummy `Span`
pub fn create_read_reference(&self, ctx: &mut TraverseCtx<'a>) -> IdentifierReference<'a> {
self.create_spanned_read_reference(SPAN, ctx)
Expand Down Expand Up @@ -134,6 +107,15 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_reference(span, ReferenceFlags::Read | ReferenceFlags::Write, ctx)
}

/// Create `IdentifierReference` referencing this binding, with specified `ReferenceFlags`
pub fn create_reference(
&self,
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> IdentifierReference<'a> {
self.create_spanned_reference(SPAN, flags, ctx)
}

/// Create `IdentifierReference` referencing this binding, with specified `Span` and `ReferenceFlags`
pub fn create_spanned_reference(
&self,
Expand Down
Loading

0 comments on commit 01ea97a

Please sign in to comment.