mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(linter): expose LintContext as the API for Linter::run
This commit is contained in:
parent
1aaeb794a4
commit
cdaff8b7f1
6 changed files with 25 additions and 17 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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(());
|
||||
|
|
|
|||
|
|
@ -25,19 +25,25 @@ pub struct LintContext<'a> {
|
|||
}
|
||||
|
||||
impl<'a> LintContext<'a> {
|
||||
pub fn new(semantic: &Rc<Semantic<'a>>, fix: bool) -> Self {
|
||||
pub fn new(semantic: &Rc<Semantic<'a>>) -> 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<Semantic<'a>> {
|
||||
&self.semantic
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<RuleEnum>,
|
||||
|
||||
fix: bool,
|
||||
}
|
||||
|
||||
|
|
@ -81,8 +79,9 @@ impl Linter {
|
|||
Self::from_rules(rules)
|
||||
}
|
||||
|
||||
pub fn run<'a>(&self, semantic: &Rc<Semantic<'a>>) -> Vec<Message<'a>> {
|
||||
let mut ctx = LintContext::new(semantic, self.fix);
|
||||
pub fn run<'a>(&self, ctx: LintContext<'a>) -> Vec<Message<'a>> {
|
||||
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());
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue