refactor(linter): remove the LintSettings parameter from LintContext::new. (#2051)

This commit is contained in:
Boshen 2024-01-16 17:17:46 +08:00 committed by GitHub
parent 9e06bd7797
commit ae4e714713
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 45 deletions

View file

@ -14,7 +14,7 @@ use oxc_linter::{
AstroPartialLoader, JavaScriptSource, SveltePartialLoader, VuePartialLoader, AstroPartialLoader, JavaScriptSource, SveltePartialLoader, VuePartialLoader,
LINT_PARTIAL_LOADER_EXT, LINT_PARTIAL_LOADER_EXT,
}, },
LintContext, LintSettings, Linter, LintContext, Linter,
}; };
use oxc_parser::Parser; use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder; use oxc_semantic::SemanticBuilder;
@ -302,7 +302,6 @@ impl IsolatedLintHandler {
let lint_ctx = LintContext::new( let lint_ctx = LintContext::new(
path.to_path_buf().into_boxed_path(), path.to_path_buf().into_boxed_path(),
&Rc::new(semantic_ret.semantic), &Rc::new(semantic_ret.semantic),
LintSettings::default(),
); );
let result = linter.run(lint_ctx); let result = linter.run(lint_ctx);
@ -383,7 +382,7 @@ pub struct ServerLinter {
impl ServerLinter { impl ServerLinter {
pub fn new() -> Self { pub fn new() -> Self {
let linter = Linter::new().with_fix(true); let linter = Linter::default().with_fix(true);
Self { linter: Arc::new(linter) } Self { linter: Arc::new(linter) }
} }

View file

@ -1,4 +1,4 @@
use std::{cell::RefCell, path::Path, rc::Rc}; use std::{cell::RefCell, path::Path, rc::Rc, sync::Arc};
use oxc_codegen::{Codegen, CodegenOptions}; use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::Error; use oxc_diagnostics::Error;
@ -25,11 +25,11 @@ pub struct LintContext<'a> {
file_path: Box<Path>, file_path: Box<Path>,
settings: LintSettings, settings: Arc<LintSettings>,
} }
impl<'a> LintContext<'a> { impl<'a> LintContext<'a> {
pub fn new(file_path: Box<Path>, semantic: &Rc<Semantic<'a>>, settings: LintSettings) -> Self { pub fn new(file_path: Box<Path>, semantic: &Rc<Semantic<'a>>) -> Self {
let disable_directives = let disable_directives =
DisableDirectivesBuilder::new(semantic.source_text(), semantic.trivias()).build(); DisableDirectivesBuilder::new(semantic.source_text(), semantic.trivias()).build();
Self { Self {
@ -39,7 +39,7 @@ impl<'a> LintContext<'a> {
fix: false, fix: false,
current_rule_name: "", current_rule_name: "",
file_path, file_path,
settings, settings: Arc::new(LintSettings::default()),
} }
} }
@ -49,6 +49,12 @@ impl<'a> LintContext<'a> {
self self
} }
#[must_use]
pub fn with_settings(mut self, settings: &Arc<LintSettings>) -> Self {
self.settings = Arc::clone(settings);
self
}
pub fn semantic(&self) -> &Rc<Semantic<'a>> { pub fn semantic(&self) -> &Rc<Semantic<'a>> {
&self.semantic &self.semantic
} }
@ -57,8 +63,8 @@ impl<'a> LintContext<'a> {
&self.disable_directives &self.disable_directives
} }
pub fn settings(&self) -> LintSettings { pub fn settings(&self) -> &LintSettings {
self.settings.clone() &self.settings
} }
pub fn source_text(&self) -> &'a str { pub fn source_text(&self) -> &'a str {

View file

@ -17,7 +17,7 @@ mod rules;
mod service; mod service;
mod utils; mod utils;
use std::{io::Write, rc::Rc}; use std::{io::Write, rc::Rc, sync::Arc};
use oxc_diagnostics::Report; use oxc_diagnostics::Report;
pub(crate) use oxc_semantic::AstNode; pub(crate) use oxc_semantic::AstNode;
@ -75,33 +75,23 @@ impl JsxA11y {
pub struct Linter { pub struct Linter {
rules: Vec<(/* rule name */ &'static str, RuleEnum)>, rules: Vec<(/* rule name */ &'static str, RuleEnum)>,
options: LintOptions, options: LintOptions,
settings: LintSettings, settings: Arc<LintSettings>,
} }
impl Default for Linter { impl Default for Linter {
fn default() -> Self { fn default() -> Self {
Self::new() Self::from_options(LintOptions::default()).unwrap()
} }
} }
impl Linter { impl Linter {
pub fn new() -> Self {
let rules = RULES
.iter()
.filter(|&rule| rule.category() == RuleCategory::Correctness)
.cloned()
.map(|rule| (rule.name(), rule))
.collect::<Vec<_>>();
Self { rules, options: LintOptions::default(), settings: LintSettings::default() }
}
/// # Errors /// # Errors
/// ///
/// Returns `Err` if there are any errors parsing the configuration file. /// Returns `Err` if there are any errors parsing the configuration file.
pub fn from_options(options: LintOptions) -> Result<Self, Report> { pub fn from_options(options: LintOptions) -> Result<Self, Report> {
let (rules, settings) = options.derive_rules_and_settings()?; let (rules, settings) = options.derive_rules_and_settings()?;
let rules = rules.into_iter().map(|rule| (rule.name(), rule)).collect(); let rules = rules.into_iter().map(|rule| (rule.name(), rule)).collect();
Ok(Self { rules, options, settings }) Ok(Self { rules, options, settings: Arc::new(settings) })
} }
#[must_use] #[must_use]
@ -112,7 +102,7 @@ impl Linter {
#[must_use] #[must_use]
pub fn with_settings(mut self, settings: LintSettings) -> Self { pub fn with_settings(mut self, settings: LintSettings) -> Self {
self.settings = settings; self.settings = Arc::new(settings);
self self
} }
@ -132,7 +122,7 @@ impl Linter {
pub fn run<'a>(&self, ctx: LintContext<'a>) -> Vec<Message<'a>> { pub fn run<'a>(&self, ctx: LintContext<'a>) -> Vec<Message<'a>> {
let semantic = Rc::clone(ctx.semantic()); let semantic = Rc::clone(ctx.semantic());
let mut ctx = ctx.with_fix(self.options.fix); let mut ctx = ctx.with_fix(self.options.fix).with_settings(&self.settings);
for (rule_name, rule) in &self.rules { for (rule_name, rule) in &self.rules {
ctx.with_rule_name(rule_name); ctx.with_rule_name(rule_name);
@ -156,10 +146,6 @@ impl Linter {
ctx.into_message() ctx.into_message()
} }
pub fn get_settings(&self) -> LintSettings {
self.settings.clone()
}
pub fn print_rules<W: Write>(writer: &mut W) { pub fn print_rules<W: Write>(writer: &mut W) {
let rules_by_category = RULES.iter().fold( let rules_by_category = RULES.iter().fold(
FxHashMap::default(), FxHashMap::default(),

View file

@ -267,11 +267,8 @@ impl Runtime {
return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect(); return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect();
}; };
let lint_ctx = LintContext::new( let lint_ctx =
path.to_path_buf().into_boxed_path(), LintContext::new(path.to_path_buf().into_boxed_path(), &Rc::new(semantic_ret.semantic));
&Rc::new(semantic_ret.semantic),
self.linter.get_settings(),
);
self.linter.run(lint_ctx) self.linter.run(lint_ctx)
} }

View file

@ -228,7 +228,7 @@ pub fn get_element_type(context: &LintContext, element: &JSXOpeningElement) -> O
let JsxA11y { polymorphic_prop_name, components } = jsx_a11y; let JsxA11y { polymorphic_prop_name, components } = jsx_a11y;
if let Some(polymorphic_prop_name_value) = polymorphic_prop_name { if let Some(polymorphic_prop_name_value) = polymorphic_prop_name {
if let Some(as_tag) = has_jsx_prop_lowercase(element, &polymorphic_prop_name_value) { if let Some(as_tag) = has_jsx_prop_lowercase(element, polymorphic_prop_name_value) {
if let Some(JSXAttributeValue::StringLiteral(str)) = get_prop_value(as_tag) { if let Some(JSXAttributeValue::StringLiteral(str)) = get_prop_value(as_tag) {
return Some(String::from(str.value.as_str())); return Some(String::from(str.value.as_str()));
} }

View file

@ -12,7 +12,7 @@ use oxc::{
span::SourceType, span::SourceType,
transformer::{TransformOptions, TransformTarget, Transformer}, transformer::{TransformOptions, TransformTarget, Transformer},
}; };
use oxc_linter::{LintContext, LintSettings, Linter}; use oxc_linter::{LintContext, Linter};
use oxc_prettier::{Prettier, PrettierOptions}; use oxc_prettier::{Prettier, PrettierOptions};
use serde::Serialize; use serde::Serialize;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -185,9 +185,8 @@ impl Oxc {
self.save_diagnostics(semantic_ret.errors); self.save_diagnostics(semantic_ret.errors);
let semantic = Rc::new(semantic_ret.semantic); let semantic = Rc::new(semantic_ret.semantic);
let lint_ctx = let lint_ctx = LintContext::new(path.into_boxed_path(), &semantic);
LintContext::new(path.into_boxed_path(), &semantic, LintSettings::default()); let linter_ret = Linter::default().run(lint_ctx);
let linter_ret = Linter::new().run(lint_ctx);
let diagnostics = linter_ret.into_iter().map(|e| e.error).collect(); let diagnostics = linter_ret.into_iter().map(|e| e.error).collect();
self.save_diagnostics(diagnostics); self.save_diagnostics(diagnostics);
} }

View file

@ -2,7 +2,7 @@ use std::{path::PathBuf, rc::Rc};
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion}; use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_linter::{AllowWarnDeny, LintContext, LintOptions, LintSettings, Linter}; use oxc_linter::{AllowWarnDeny, LintContext, LintOptions, Linter};
use oxc_parser::Parser; use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder; use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType; use oxc_span::SourceType;
@ -30,11 +30,7 @@ fn bench_linter(criterion: &mut Criterion) {
let linter = Linter::from_options(lint_options).unwrap(); let linter = Linter::from_options(lint_options).unwrap();
let semantic = Rc::new(semantic_ret.semantic); let semantic = Rc::new(semantic_ret.semantic);
b.iter(|| { b.iter(|| {
linter.run(LintContext::new( linter.run(LintContext::new(PathBuf::from("").into_boxed_path(), &semantic))
PathBuf::from("").into_boxed_path(),
&semantic,
LintSettings::default(),
))
}); });
}, },
); );