From 7e57a1d2fc5d21c8c8043c1ad2041bc30054f19f Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 15 Oct 2024 01:20:26 +0000 Subject: [PATCH] refactor(transformer): `ImportKind` use `BoundIdentifier` (#6559) Follow-on after #6434. Store `BoundIdentifier` in `ImportKind`, rather than storing `name` and `symbol_id` separately. Pure refactor - just allows shortening some code. --- .../src/common/helper_loader.rs | 6 +-- .../src/common/module_imports.rs | 47 ++++++------------- crates/oxc_transformer/src/react/jsx.rs | 5 +- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/crates/oxc_transformer/src/common/helper_loader.rs b/crates/oxc_transformer/src/common/helper_loader.rs index 1d9049182234d..8be0df283976f 100644 --- a/crates/oxc_transformer/src/common/helper_loader.rs +++ b/crates/oxc_transformer/src/common/helper_loader.rs @@ -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); }, ); } diff --git a/crates/oxc_transformer/src/common/module_imports.rs b/crates/oxc_transformer/src/common/module_imports.rs index 9d9e46af05fdf..a96672f3aedb7 100644 --- a/crates/oxc_transformer/src/common/module_imports.rs +++ b/crates/oxc_transformer/src/common/module_imports.rs @@ -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; @@ -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) } } @@ -183,21 +177,17 @@ impl<'a> ModuleImportsStore<'a> { names: Vec>, 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()), ), })); @@ -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); diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index 1e8fdae1c4818..d0f3daa8b310e 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -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 } @@ -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 }