Skip to content

Commit

Permalink
fix: rename in SimpleAssignmentTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Feb 26, 2024
1 parent bca7db3 commit 1d24efc
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 117 deletions.
76 changes: 18 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 41 additions & 13 deletions crates/rolldown/src/finalizer/impl_visit_mut_for_finalizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc::{
allocator::{self},
allocator,
ast::{
ast::{self},
ast::{self, SimpleAssignmentTarget},
VisitMut,
},
};
Expand Down Expand Up @@ -278,17 +278,23 @@ impl<'ast, 'me: 'ast> VisitMut<'ast> for Finalizer<'me, 'ast> {
if ident.name != canonical_name {
ident.name = canonical_name.clone();
}
*ident.symbol_id.get_mut() = None;
} else {
// Some `BindingIdentifier`s constructed by bundler don't have `SymbolId` and we just ignore them.
}
}

fn visit_identifier_reference(&mut self, ident: &mut ast::IdentifierReference) {
debug_assert!(
self.is_global_identifier_reference(ident) || ident.reference_id.get().is_none(),
"{} doesn't get processed in {}",
ident.name,
self.ctx.module.repr_name
);
}

fn visit_call_expression(&mut self, expr: &mut ast::CallExpression<'ast>) {
if let ast::Expression::Identifier(id_ref) = &mut expr.callee {
if let Some(new_name) = self.generate_finalized_expr_for_reference(id_ref, true) {
expr.callee = new_name;
}
}
self.try_rewrite_identifier_reference_expr(&mut expr.callee, true);

// visit children
for arg in expr.arguments.iter_mut() {
Expand Down Expand Up @@ -327,11 +333,7 @@ impl<'ast, 'me: 'ast> VisitMut<'ast> for Finalizer<'me, 'ast> {
}
}

if let Some(id_ref) = expr.as_identifier() {
if let Some(new_expr) = self.generate_finalized_expr_for_reference(id_ref, false) {
*expr = new_expr;
}
}
self.try_rewrite_identifier_reference_expr(expr, false);

// visit children
self.visit_expression_match(expr);
Expand Down Expand Up @@ -408,7 +410,7 @@ impl<'ast, 'me: 'ast> VisitMut<'ast> for Finalizer<'me, 'ast> {
}

fn visit_import_expression(&mut self, expr: &mut ast::ImportExpression<'ast>) {
// Make sure the import expression is in correct form. If it's not, we should ignore it.
// Make sure the import expression is in correct form. If it's not, we should leave it as it is.
match &mut expr.source {
ast::Expression::StringLiteral(str) if expr.arguments.len() == 0 => {
let rec_id = self.ctx.module.imports[&expr.span];
Expand All @@ -435,4 +437,30 @@ impl<'ast, 'me: 'ast> VisitMut<'ast> for Finalizer<'me, 'ast> {
self.visit_expression(arg);
}
}

fn visit_simple_assignment_target(&mut self, target: &mut SimpleAssignmentTarget<'ast>) {
self.rewrite_simple_assignment_target(target);

// visit children
match target {
SimpleAssignmentTarget::AssignmentTargetIdentifier(ident) => {
self.visit_identifier_reference(ident);
}
SimpleAssignmentTarget::MemberAssignmentTarget(expr) => {
self.visit_member_expression(expr);
}
SimpleAssignmentTarget::TSAsExpression(expr) => {
self.visit_expression(&mut expr.expression);
}
SimpleAssignmentTarget::TSSatisfiesExpression(expr) => {
self.visit_expression(&mut expr.expression);
}
SimpleAssignmentTarget::TSNonNullExpression(expr) => {
self.visit_expression(&mut expr.expression);
}
SimpleAssignmentTarget::TSTypeAssertion(expr) => {
self.visit_expression(&mut expr.expression);
}
}
}
}
46 changes: 1 addition & 45 deletions crates/rolldown/src/finalizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rolldown_oxc::{AstSnippet, BindingPatternExt, Dummy, IntoIn, TakeIn};
mod finalizer_context;
mod impl_visit_mut_for_finalizer;
pub use finalizer_context::FinalizerContext;
mod rename;

pub struct Finalizer<'me, 'ast> {
pub alloc: &'ast Allocator,
Expand Down Expand Up @@ -77,51 +78,6 @@ where
true
}

/// return `None` if
/// - the reference is for a global variable/the reference doesn't have a `SymbolId`
/// - the reference doesn't have a `ReferenceId`
/// - the canonical name is the same as the original name
fn generate_finalized_expr_for_reference(
&self,
id_ref: &IdentifierReference,
is_callee: bool,
) -> Option<ast::Expression<'ast>> {
// Some `IdentifierReference`s constructed by bundler don't have `ReferenceId` and we just ignore them.
let reference_id = id_ref.reference_id.get()?;

// we will hit this branch if the reference points to a global variable
let symbol_id = self.scope.symbol_id_for(reference_id)?;

let symbol_ref: SymbolRef = (self.ctx.id, symbol_id).into();
let canonical_ref = self.ctx.symbols.par_canonical_ref_for(symbol_ref);
let symbol = self.ctx.symbols.get(canonical_ref);

if let Some(ns_alias) = &symbol.namespace_alias {
let canonical_ns_name = self.canonical_name_for(ns_alias.namespace_ref);
let prop_name = &ns_alias.property_name;
let access_expr = self
.snippet
.literal_prop_access_member_expr_expr(canonical_ns_name.clone(), prop_name.clone());

return Some(if is_callee {
// `foo()` might be transformed to `xxx.foo()`. To keep the semantic of callee's `this` binding,
// we need to wrap the transformed callee. Make it like `(0, xxx.foo)()`.
let wrapped_callee =
self.snippet.seq2_in_paren_expr(self.snippet.number_expr(0.0), access_expr);
wrapped_callee
} else {
access_expr
});
}

let canonical_name = self.canonical_name_for(canonical_ref);
if id_ref.name != canonical_name {
return Some(self.snippet.id_ref_expr(canonical_name.clone()));
}

None
}

fn generate_finalized_expr_for_symbol_ref(&self, symbol_ref: SymbolRef) -> ast::Expression<'ast> {
let canonical_ref = self.ctx.symbols.par_canonical_ref_for(symbol_ref);
let symbol = self.ctx.symbols.get(canonical_ref);
Expand Down

0 comments on commit 1d24efc

Please sign in to comment.