From 6c855affa3b2dc006a040753abdca3253a1e7eb2 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Fri, 27 Sep 2024 02:14:27 +0000 Subject: [PATCH] fix(linter): only write fix results if source code has changed (#6096) Closes #6061. Read [this reply](https://github.com/oxc-project/oxc/issues/6061#issuecomment-2378226722) for context. --- crates/oxc_linter/src/context/mod.rs | 2 +- crates/oxc_linter/src/service.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/context/mod.rs b/crates/oxc_linter/src/context/mod.rs index f46386012..fc8484f18 100644 --- a/crates/oxc_linter/src/context/mod.rs +++ b/crates/oxc_linter/src/context/mod.rs @@ -277,7 +277,7 @@ impl<'a> LintContext<'a> { (Some(message), None) => diagnostic.with_help(message.to_owned()), _ => diagnostic, }; - if self.parent.fix.can_apply(rule_fix.kind()) { + if self.parent.fix.can_apply(rule_fix.kind()) && !rule_fix.is_empty() { let fix = rule_fix.into_fix(self.source_text()); self.add_diagnostic(Message::new(diagnostic, Some(fix))); } else { diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 297911c05..a2305eef2 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -268,7 +268,9 @@ impl Runtime { // TODO: Span is wrong, ban this feature for file process by `PartialLoader`. if !is_processed_by_partial_loader && self.linter.options().fix.is_some() { let fix_result = Fixer::new(source_text, messages).fix(); - fs::write(path, fix_result.fixed_code.as_bytes()).unwrap(); + if fix_result.fixed { + fs::write(path, fix_result.fixed_code.as_bytes()).unwrap(); + } messages = fix_result.messages; }