Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions crates/oxc_transformer_plugins/src/inject_global_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl From<&InjectImport> for DotDefineState<'_> {
#[must_use]
pub struct InjectGlobalVariablesReturn {
pub scoping: Scoping,
pub changed: bool,
}

/// Injects import statements for global variables.
Expand All @@ -129,6 +130,8 @@ pub struct InjectGlobalVariables<'a> {
/// Identifiers for which dot define replaced a member expression.
replaced_dot_defines:
Vec<(/* identifier of member expression */ CompactStr, /* local */ CompactStr)>,

changed: bool,
}

impl<'a> Traverse<'a, ()> for InjectGlobalVariables<'a> {
Expand All @@ -144,9 +147,14 @@ impl<'a> InjectGlobalVariables<'a> {
config,
dot_defines: vec![],
replaced_dot_defines: vec![],
changed: false,
}
}

fn mark_as_changed(&mut self) {
self.changed = true;
}

pub fn build(
&mut self,
scoping: Scoping,
Expand Down Expand Up @@ -193,15 +201,15 @@ impl<'a> InjectGlobalVariables<'a> {
.collect::<Vec<_>>();

if injects.is_empty() {
return InjectGlobalVariablesReturn { scoping };
return InjectGlobalVariablesReturn { scoping, changed: self.changed };
}

self.inject_imports(&injects, program);

InjectGlobalVariablesReturn { scoping }
InjectGlobalVariablesReturn { scoping, changed: self.changed }
}

fn inject_imports(&self, injects: &[InjectImport], program: &mut Program<'a>) {
fn inject_imports(&mut self, injects: &[InjectImport], program: &mut Program<'a>) {
let imports = injects.iter().map(|inject| {
let specifiers = Some(self.ast.vec1(self.inject_import_to_specifier(inject)));
let source = self.ast.string_literal(SPAN, self.ast.atom(&inject.source), None);
Expand All @@ -212,6 +220,7 @@ impl<'a> InjectGlobalVariables<'a> {
Statement::from(import_decl)
});
program.body.splice(0..0, imports);
self.mark_as_changed();
}

fn inject_import_to_specifier(&self, inject: &InjectImport) -> ImportDeclarationSpecifier<'a> {
Expand Down Expand Up @@ -269,6 +278,7 @@ impl<'a> InjectGlobalVariables<'a> {

let value = self.ast.expression_identifier(SPAN, value_atom);
*expr = value;
self.mark_as_changed();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn test(source_text: &str, expected: &str, config: InjectGlobalVariablesConfig)
let ret = Parser::new(&allocator, source_text, source_type).parse();
let mut program = ret.program;
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
let _ = InjectGlobalVariables::new(&allocator, config).build(scoping, &mut program);
let ret = InjectGlobalVariables::new(&allocator, config).build(scoping, &mut program);
assert_eq!(ret.changed, source_text != expected);
let result = Codegen::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(&program)
Expand Down
Loading