feat(cli, linter): propage --fix option and apply fixes

closes #68
This commit is contained in:
Boshen 2023-03-17 12:59:53 +08:00
parent b441c579f0
commit 678cfae967
No known key found for this signature in database
GPG key ID: 6AC90C77AAAA6ABC
5 changed files with 25 additions and 23 deletions

View file

@ -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))

View file

@ -19,6 +19,7 @@ impl<'a> Fix<'a> {
}
}
#[derive(Debug)]
pub struct FixResult<'a> {
pub fixed: bool,
pub fixed_code: Cow<'a, str>,

View file

@ -27,6 +27,8 @@ pub struct Linter {
rules: Vec<RuleEnum>,
early_error_javascript: EarlyErrorJavaScript,
fix: bool,
}
impl Linter {
@ -49,7 +51,13 @@ impl Linter {
#[must_use]
pub fn from_rules(rules: Vec<RuleEnum>) -> 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<Semantic<'a>>,
source_text: &'a str,
fix: bool,
) -> Vec<Message<'a>> {
let ctx = LintContext::new(source_text, semantic.clone(), fix);
pub fn run<'a>(&self, semantic: &Rc<Semantic<'a>>, source_text: &'a str) -> Vec<Message<'a>> {
let ctx = LintContext::new(source_text, semantic.clone(), self.fix);
for node in semantic.nodes().iter() {
self.early_error_javascript.run(node, &ctx);

View file

@ -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;
}

View file

@ -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)