Skip to content

Commit

Permalink
fix(oxc_transformer): inject_global_variables should considering stri…
Browse files Browse the repository at this point in the history
…ng imported name (#7768)

Co-authored-by: Boshen <[email protected]>
  • Loading branch information
IWANABETHATGUY and Boshen authored Dec 14, 2024
1 parent 9d77ab7 commit 9a30910
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/oxc_transformer/src/plugins/inject_global_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oxc_allocator::Allocator;
use oxc_ast::{ast::*, AstBuilder, NONE};
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::{CompactStr, SPAN};
use oxc_syntax::identifier;
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};

use super::{
Expand Down Expand Up @@ -210,11 +211,16 @@ impl<'a> InjectGlobalVariables<'a> {
fn inject_import_to_specifier(&self, inject: &InjectImport) -> ImportDeclarationSpecifier<'a> {
match &inject.specifier {
InjectImportSpecifier::Specifier { imported, local } => {
let imported = imported.as_deref().unwrap_or("default");
let imported_name = imported.as_deref().unwrap_or("default");
let imported = if identifier::is_identifier_name(imported_name) {
self.ast.module_export_name_identifier_name(SPAN, imported_name)
} else {
self.ast.module_export_name_string_literal(SPAN, imported_name, None)
};
let local = inject.replace_value.as_ref().unwrap_or(local).as_str();
self.ast.import_declaration_specifier_import_specifier(
SPAN,
self.ast.module_export_name_identifier_name(SPAN, imported),
imported,
self.ast.binding_identifier(SPAN, local),
ImportOrExportKind::Value,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ fn named() {
);
}

#[test]
fn string() {
let config = InjectGlobalVariablesConfig::new(vec![InjectImport::named_specifier(
"jquery",
Some("😊"),
"$",
)]);
test(
"$",
"
import { '😊' as $ } from 'jquery';
$
;
",
config,
);
}

#[test]
fn keypaths() {
// overwrites keypaths
Expand Down

0 comments on commit 9a30910

Please sign in to comment.