Skip to content

Commit

Permalink
refactor(transformer): ImportKind use BoundIdentifier (#6559)
Browse files Browse the repository at this point in the history
Follow-on after #6434.

Store `BoundIdentifier` in `ImportKind`, rather than storing `name` and `symbol_id` separately. Pure refactor - just allows shortening some code.
  • Loading branch information
overlookmotel committed Oct 15, 2024
1 parent 602df9d commit 7e57a1d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
6 changes: 1 addition & 5 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ impl<'a, 'ctx> HelperLoader<'a, 'ctx> {
fn add_imports(&self) {
self.ctx.helper_loader.loaded_helpers.borrow_mut().drain().for_each(
|(_, (source, import))| {
self.ctx.module_imports.add_import(
source,
ImportKind::new_default(import.name, import.symbol_id),
false,
);
self.ctx.module_imports.add_import(source, ImportKind::new_default(import), false);
},
);
}
Expand Down
47 changes: 15 additions & 32 deletions crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use oxc_ast::{ast::*, NONE};
use oxc_semantic::ReferenceFlags;
use oxc_span::{Atom, SPAN};
use oxc_syntax::symbol::SymbolId;
use oxc_traverse::{Traverse, TraverseCtx};
use oxc_traverse::{BoundIdentifier, Traverse, TraverseCtx};

use crate::TransformCtx;

Expand All @@ -65,27 +65,21 @@ impl<'a, 'ctx> Traverse<'a> for ModuleImports<'a, 'ctx> {
#[derive(Clone)]
pub struct NamedImport<'a> {
imported: Atom<'a>,
local: Atom<'a>,
symbol_id: SymbolId,
}

pub struct DefaultImport<'a> {
local: Atom<'a>,
symbol_id: SymbolId,
local: BoundIdentifier<'a>,
}

pub enum ImportKind<'a> {
Named(NamedImport<'a>),
Default(DefaultImport<'a>),
Default(BoundIdentifier<'a>),
}

impl<'a> ImportKind<'a> {
pub fn new_named(imported: Atom<'a>, local: Atom<'a>, symbol_id: SymbolId) -> Self {
Self::Named(NamedImport { imported, local, symbol_id })
pub fn new_named(imported: Atom<'a>, local: BoundIdentifier<'a>) -> Self {
Self::Named(NamedImport { imported, local })
}

pub fn new_default(local: Atom<'a>, symbol_id: SymbolId) -> Self {
Self::Default(DefaultImport { local, symbol_id })
pub fn new_default(local: BoundIdentifier<'a>) -> Self {
Self::Default(local)
}
}

Expand Down Expand Up @@ -183,21 +177,17 @@ impl<'a> ModuleImportsStore<'a> {
names: Vec<ImportKind<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let specifiers = ctx.ast.vec_from_iter(names.into_iter().map(|kind| match kind {
ImportKind::Named(name) => {
let local = name.local;
let specifiers = ctx.ast.vec_from_iter(names.into_iter().map(|import| match import {
ImportKind::Named(import) => {
ImportDeclarationSpecifier::ImportSpecifier(ctx.ast.alloc_import_specifier(
SPAN,
ModuleExportName::IdentifierName(IdentifierName::new(SPAN, name.imported)),
BindingIdentifier::new_with_symbol_id(SPAN, local, name.symbol_id),
ModuleExportName::IdentifierName(IdentifierName::new(SPAN, import.imported)),
import.local.create_binding_identifier(),
ImportOrExportKind::Value,
))
}
ImportKind::Default(name) => ImportDeclarationSpecifier::ImportDefaultSpecifier(
ctx.ast.alloc_import_default_specifier(
SPAN,
BindingIdentifier::new_with_symbol_id(SPAN, name.local, name.symbol_id),
),
ImportKind::Default(local) => ImportDeclarationSpecifier::ImportDefaultSpecifier(
ctx.ast.alloc_import_default_specifier(SPAN, local.create_binding_identifier()),
),
}));

Expand Down Expand Up @@ -230,15 +220,8 @@ impl<'a> ModuleImportsStore<'a> {
let arg = Argument::from(ctx.ast.expression_string_literal(SPAN, source));
ctx.ast.vec1(arg)
};
let ImportKind::Default(name) = names.into_iter().next().unwrap() else { unreachable!() };
let id = {
let ident = BindingIdentifier::new_with_symbol_id(SPAN, name.local, name.symbol_id);
ctx.ast.binding_pattern(
ctx.ast.binding_pattern_kind_from_binding_identifier(ident),
NONE,
false,
)
};
let Some(ImportKind::Default(local)) = names.into_iter().next() else { unreachable!() };
let id = local.create_binding_pattern(ctx);
let decl = {
let init = ctx.ast.expression_call(SPAN, callee, NONE, args, false);
let decl = ctx.ast.variable_declarator(SPAN, var_kind, id, Some(init), false);
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a, 'ctx> AutomaticScriptBindings<'a, 'ctx> {
) -> BoundIdentifier<'a> {
let binding =
ctx.generate_uid_in_root_scope(variable_name, SymbolFlags::FunctionScopedVariable);
let import = ImportKind::new_default(binding.name.clone(), binding.symbol_id);
let import = ImportKind::new_default(binding.clone());
self.ctx.module_imports.add_import(source, import, front);
binding
}
Expand Down Expand Up @@ -297,8 +297,7 @@ impl<'a, 'ctx> AutomaticModuleBindings<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let binding = ctx.generate_uid_in_root_scope(name, SymbolFlags::Import);
let import =
ImportKind::new_named(Atom::from(name), binding.name.clone(), binding.symbol_id);
let import = ImportKind::new_named(Atom::from(name), binding.clone());
self.ctx.module_imports.add_import(source, import, false);
binding
}
Expand Down

0 comments on commit 7e57a1d

Please sign in to comment.