Skip to content

Commit 2f85b31

Browse files
committed
feat(transformer_plugins): add changed field to InjectGlobalVariablesReturn (#14618)
## Summary Add a `changed: bool` field to `InjectGlobalVariablesReturn` to track whether the AST was modified during the global variable injection transformation. - Adds `changed` field to `InjectGlobalVariablesReturn` struct - Tracks changes via consolidated `mark_as_changed()` helper method - Updates `replace_dot_defines` when dot define replacements occur - Updates `inject_imports` when import statements are injected - Updates `build` method to return `changed` in all paths This allows callers to efficiently determine if injections or replacements were made without needing to compare the AST before and after transformation. ## Test plan - [x] All existing tests pass - [x] No new tests needed - change tracking is straightforward 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent baab7eb commit 2f85b31

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

crates/oxc_transformer_plugins/src/inject_global_variables.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl From<&InjectImport> for DotDefineState<'_> {
111111
#[must_use]
112112
pub struct InjectGlobalVariablesReturn {
113113
pub scoping: Scoping,
114+
pub changed: bool,
114115
}
115116

116117
/// Injects import statements for global variables.
@@ -129,6 +130,8 @@ pub struct InjectGlobalVariables<'a> {
129130
/// Identifiers for which dot define replaced a member expression.
130131
replaced_dot_defines:
131132
Vec<(/* identifier of member expression */ CompactStr, /* local */ CompactStr)>,
133+
134+
changed: bool,
132135
}
133136

134137
impl<'a> Traverse<'a, ()> for InjectGlobalVariables<'a> {
@@ -144,9 +147,14 @@ impl<'a> InjectGlobalVariables<'a> {
144147
config,
145148
dot_defines: vec![],
146149
replaced_dot_defines: vec![],
150+
changed: false,
147151
}
148152
}
149153

154+
fn mark_as_changed(&mut self) {
155+
self.changed = true;
156+
}
157+
150158
pub fn build(
151159
&mut self,
152160
scoping: Scoping,
@@ -193,15 +201,15 @@ impl<'a> InjectGlobalVariables<'a> {
193201
.collect::<Vec<_>>();
194202

195203
if injects.is_empty() {
196-
return InjectGlobalVariablesReturn { scoping };
204+
return InjectGlobalVariablesReturn { scoping, changed: self.changed };
197205
}
198206

199207
self.inject_imports(&injects, program);
200208

201-
InjectGlobalVariablesReturn { scoping }
209+
InjectGlobalVariablesReturn { scoping, changed: self.changed }
202210
}
203211

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

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

270279
let value = self.ast.expression_identifier(SPAN, value_atom);
271280
*expr = value;
281+
self.mark_as_changed();
272282
break;
273283
}
274284
}

crates/oxc_transformer_plugins/tests/integrations/inject_global_variables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ fn test(source_text: &str, expected: &str, config: InjectGlobalVariablesConfig)
1818
let ret = Parser::new(&allocator, source_text, source_type).parse();
1919
let mut program = ret.program;
2020
let scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
21-
let _ = InjectGlobalVariables::new(&allocator, config).build(scoping, &mut program);
21+
let ret = InjectGlobalVariables::new(&allocator, config).build(scoping, &mut program);
22+
assert_eq!(ret.changed, source_text != expected);
2223
let result = Codegen::new()
2324
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
2425
.build(&program)

0 commit comments

Comments
 (0)