Skip to content

Commit bd10f2b

Browse files
Boshenclaude
andcommitted
feat(transformer_plugins): add changed field to InjectGlobalVariablesReturn
Add a `changed: bool` field to `InjectGlobalVariablesReturn` to track whether the AST was modified during transformation. This allows callers to determine if injections or replacements were made. Changes: - Add `changed: bool` field to `InjectGlobalVariablesReturn` struct - Add change tracking field to `InjectGlobalVariables` struct - Create `mark_as_changed()` helper method to consolidate tracking - Update `replace_dot_defines` to call `mark_as_changed()` when replacements occur - Update `inject_imports` to call `mark_as_changed()` when imports are injected - Update `build` method to return `changed` field in all return paths 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a81267e commit bd10f2b

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
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
}

0 commit comments

Comments
 (0)