From cdaff8b7f1479fcb2ba8dd3bda0e7d02d1c56dc3 Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 7 Jul 2023 22:10:34 +0800 Subject: [PATCH] refactor(linter): expose LintContext as the API for Linter::run --- crates/oxc_cli/src/lint/runner.rs | 5 +++-- crates/oxc_cli/src/lint/runner_with_module_tree.rs | 5 +++-- crates/oxc_linter/src/context.rs | 12 +++++++++--- crates/oxc_linter/src/lib.rs | 9 ++++----- crates/oxc_linter/src/tester.rs | 6 +++--- crates/oxc_wasm/src/lib.rs | 5 +++-- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/crates/oxc_cli/src/lint/runner.rs b/crates/oxc_cli/src/lint/runner.rs index 20e433628..9efc45032 100644 --- a/crates/oxc_cli/src/lint/runner.rs +++ b/crates/oxc_cli/src/lint/runner.rs @@ -16,7 +16,7 @@ use oxc_diagnostics::{ thiserror::Error, Error, GraphicalReportHandler, Severity, }; -use oxc_linter::{Fixer, Linter, RuleCategory, RuleEnum, RULES}; +use oxc_linter::{Fixer, LintContext, Linter, RuleCategory, RuleEnum, RULES}; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; @@ -221,7 +221,8 @@ impl LintRunner { return Some(Self::wrap_diagnostics(path, &source_text, semantic_ret.errors)); }; - let result = linter.run(&Rc::new(semantic_ret.semantic)); + let lint_ctx = LintContext::new(&Rc::new(semantic_ret.semantic)); + let result = linter.run(lint_ctx); if result.is_empty() { return None; diff --git a/crates/oxc_cli/src/lint/runner_with_module_tree.rs b/crates/oxc_cli/src/lint/runner_with_module_tree.rs index c29eb9c0a..73138d455 100644 --- a/crates/oxc_cli/src/lint/runner_with_module_tree.rs +++ b/crates/oxc_cli/src/lint/runner_with_module_tree.rs @@ -15,7 +15,7 @@ use oxc_diagnostics::{ thiserror::Error, Error, GraphicalReportHandler, Severity, }; -use oxc_linter::{FixResult, Fixer, Linter, RuleCategory, RuleEnum, RULES}; +use oxc_linter::{FixResult, Fixer, LintContext, Linter, RuleCategory, RuleEnum, RULES}; use oxc_parser::{Parser, ParserReturn}; use oxc_resolver::{ResolveResult, Resolver}; use oxc_semantic::{SemanticBuilder, SemanticBuilderReturn}; @@ -325,7 +325,8 @@ fn run_for_file(path: &Path, runtime_data: &LinterRuntimeData) -> Result<()> { .filter(|path| !visited.contains(path)) .try_for_each(|path| run_for_file(&path, runtime_data))?; - let result = linter.run(&Rc::new(semantic)); + let lint_ctx = LintContext::new(&Rc::new(semantic)); + let result = linter.run(lint_ctx); if result.is_empty() { return Ok(()); diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 0a0229a06..ba445b471 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -25,19 +25,25 @@ pub struct LintContext<'a> { } impl<'a> LintContext<'a> { - pub fn new(semantic: &Rc>, fix: bool) -> Self { + pub fn new(semantic: &Rc>) -> Self { let disable_directives = DisableDirectivesBuilder::new(semantic.source_text(), semantic.trivias()).build(); Self { semantic: Rc::clone(semantic), diagnostics: RefCell::new(vec![]), disable_directives, - fix, + fix: false, current_rule_name: "", } } - pub fn semantic(&self) -> &Semantic<'a> { + #[must_use] + pub fn with_fix(mut self, fix: bool) -> Self { + self.fix = fix; + self + } + + pub fn semantic(&self) -> &Rc> { &self.semantic } diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 2af124091..307086722 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -16,11 +16,10 @@ use std::{fs, io::Write, rc::Rc}; pub use fixer::{FixResult, Fixer, Message}; pub(crate) use oxc_semantic::AstNode; -use oxc_semantic::Semantic; use rustc_hash::FxHashMap; -use crate::context::LintContext; pub use crate::{ + context::LintContext, rule::RuleCategory, rules::{RuleEnum, RULES}, }; @@ -28,7 +27,6 @@ pub use crate::{ #[derive(Debug)] pub struct Linter { rules: Vec, - fix: bool, } @@ -81,8 +79,9 @@ impl Linter { Self::from_rules(rules) } - pub fn run<'a>(&self, semantic: &Rc>) -> Vec> { - let mut ctx = LintContext::new(semantic, self.fix); + pub fn run<'a>(&self, ctx: LintContext<'a>) -> Vec> { + let semantic = Rc::clone(ctx.semantic()); + let mut ctx = ctx.with_fix(self.fix); for node in semantic.nodes().iter() { for rule in &self.rules { ctx.with_rule_name(rule.name()); diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 09409cb7c..16b9c0a20 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -7,7 +7,7 @@ use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; use serde_json::Value; -use crate::{rules::RULES, Linter}; +use crate::{rules::RULES, LintContext, Linter}; pub struct Tester { rule_name: &'static str, @@ -87,8 +87,8 @@ 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]).with_fix(false).run(&Rc::new(semantic_ret.semantic)); + let lint_context = LintContext::new(&Rc::new(semantic_ret.semantic)); + let result = Linter::from_rules(vec![rule]).with_fix(false).run(lint_context); if result.is_empty() { return true; } diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index c428d1520..2b3c3f0c9 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -6,7 +6,7 @@ use oxc_allocator::Allocator; use oxc_ast_lower::AstLower; use oxc_diagnostics::Error; use oxc_formatter::{Formatter, FormatterOptions}; -use oxc_linter::Linter; +use oxc_linter::{LintContext, Linter}; use oxc_minifier::{CompressOptions, Compressor, ManglerBuilder, Printer, PrinterOptions}; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; @@ -187,7 +187,8 @@ impl Oxc { self.save_diagnostics(semantic_ret.errors); let semantic = Rc::new(semantic_ret.semantic); - let linter_ret = Linter::new().run(&semantic); + let lint_ctx = LintContext::new(&semantic); + let linter_ret = Linter::new().run(lint_ctx); let diagnostics = linter_ret.into_iter().map(|e| e.error).collect(); self.save_diagnostics(diagnostics); }