diff --git a/crates/oxc_cli/src/lib.rs b/crates/oxc_cli/src/lib.rs index f77279653..c37a8aa6d 100644 --- a/crates/oxc_cli/src/lib.rs +++ b/crates/oxc_cli/src/lib.rs @@ -16,7 +16,7 @@ use miette::NamedSource; use oxc_allocator::Allocator; use oxc_ast::SourceType; use oxc_diagnostics::{Error, MinifiedFileError, Severity}; -use oxc_linter::Linter; +use oxc_linter::{Fixer, Linter}; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; @@ -148,22 +148,18 @@ impl Cli { return Some(Self::wrap_diagnostics(path, &source_text, semantic_ret.errors)); }; - let result = Linter::new().run(&Rc::new(semantic_ret.semantic), &source_text, fix); + let result = Linter::new().with_fix(fix).run(&Rc::new(semantic_ret.semantic), &source_text); if result.is_empty() { return None; } - // if fix { - // let fix_result = Fixer::new(&source_text, result).fix(); - // fs::write(path, fix_result.fixed_code.as_bytes()) - // .unwrap_or_else(|_| panic!("{path:?} not found")); - // return fix_result - // .messages - // .into_iter() - // .map(|m| m.error.with_source_code(source.clone())) - // .collect(); - // } + if fix { + let fix_result = Fixer::new(&source_text, result).fix(); + fs::write(path, fix_result.fixed_code.as_bytes()).unwrap(); + let errors = fix_result.messages.into_iter().map(|m| m.error).collect(); + return Some(Self::wrap_diagnostics(path, &source_text, errors)); + } let errors = result.into_iter().map(|diagnostic| diagnostic.error).collect(); Some(Self::wrap_diagnostics(path, &source_text, errors)) diff --git a/crates/oxc_linter/src/fixer.rs b/crates/oxc_linter/src/fixer.rs index bc203a4ca..d2988f335 100644 --- a/crates/oxc_linter/src/fixer.rs +++ b/crates/oxc_linter/src/fixer.rs @@ -19,6 +19,7 @@ impl<'a> Fix<'a> { } } +#[derive(Debug)] pub struct FixResult<'a> { pub fixed: bool, pub fixed_code: Cow<'a, str>, diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 93cc7ab34..98a01a8b0 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -27,6 +27,8 @@ pub struct Linter { rules: Vec, early_error_javascript: EarlyErrorJavaScript, + + fix: bool, } impl Linter { @@ -49,7 +51,13 @@ impl Linter { #[must_use] pub fn from_rules(rules: Vec) -> Self { - Self { rules, early_error_javascript: EarlyErrorJavaScript } + Self { rules, early_error_javascript: EarlyErrorJavaScript, fix: false } + } + + #[must_use] + pub fn with_fix(mut self, yes: bool) -> Self { + self.fix = yes; + self } #[must_use] @@ -75,13 +83,8 @@ impl Linter { } #[must_use] - pub fn run<'a>( - &self, - semantic: &Rc>, - source_text: &'a str, - fix: bool, - ) -> Vec> { - let ctx = LintContext::new(source_text, semantic.clone(), fix); + pub fn run<'a>(&self, semantic: &Rc>, source_text: &'a str) -> Vec> { + let ctx = LintContext::new(source_text, semantic.clone(), self.fix); for node in semantic.nodes().iter() { self.early_error_javascript.run(node, &ctx); diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index e8715011d..88b2e27d3 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -70,8 +70,9 @@ impl Tester { .find(|rule| rule.name() == self.rule_name) .unwrap_or_else(|| panic!("Rule not found: {}", &self.rule_name)); let rule = rule.read_json(config); - let result = - Linter::from_rules(vec![rule]).run(&Rc::new(semantic_ret.semantic), source_text, false); + let result = Linter::from_rules(vec![rule]) + .with_fix(false) + .run(&Rc::new(semantic_ret.semantic), source_text); if result.is_empty() { return true; } diff --git a/crates/oxc_wasm/src/driver.rs b/crates/oxc_wasm/src/driver.rs index 0f63f8ea8..ab9681edf 100644 --- a/crates/oxc_wasm/src/driver.rs +++ b/crates/oxc_wasm/src/driver.rs @@ -40,7 +40,8 @@ impl Driver { diagnostics.extend( Linter::from_json_str(eslintrc) - .run(&Rc::new(semantic_ret.semantic), source_text, false) + .with_fix(false) + .run(&Rc::new(semantic_ret.semantic), source_text) .into_iter() .map(|m| m.error.with_source_code(source.clone())) .chain(ret.errors)