From ae4e71471320ace123974af11de5c827dedbf4fe Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 16 Jan 2024 17:17:46 +0800 Subject: [PATCH] refactor(linter): remove the `LintSettings` parameter from `LintContext::new`. (#2051) --- crates/oxc_language_server/src/linter.rs | 5 ++--- crates/oxc_linter/src/context.rs | 18 ++++++++++------ crates/oxc_linter/src/lib.rs | 26 ++++++------------------ crates/oxc_linter/src/service.rs | 7 ++----- crates/oxc_linter/src/utils/react.rs | 2 +- crates/oxc_wasm/src/lib.rs | 7 +++---- tasks/benchmark/benches/linter.rs | 8 ++------ 7 files changed, 28 insertions(+), 45 deletions(-) diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index 20d4c3c0f..0cd3cd8a4 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -14,7 +14,7 @@ use oxc_linter::{ AstroPartialLoader, JavaScriptSource, SveltePartialLoader, VuePartialLoader, LINT_PARTIAL_LOADER_EXT, }, - LintContext, LintSettings, Linter, + LintContext, Linter, }; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; @@ -302,7 +302,6 @@ impl IsolatedLintHandler { let lint_ctx = LintContext::new( path.to_path_buf().into_boxed_path(), &Rc::new(semantic_ret.semantic), - LintSettings::default(), ); let result = linter.run(lint_ctx); @@ -383,7 +382,7 @@ pub struct ServerLinter { impl ServerLinter { pub fn new() -> Self { - let linter = Linter::new().with_fix(true); + let linter = Linter::default().with_fix(true); Self { linter: Arc::new(linter) } } diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 79a3153e8..269d93426 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -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_diagnostics::Error; @@ -25,11 +25,11 @@ pub struct LintContext<'a> { file_path: Box, - settings: LintSettings, + settings: Arc, } impl<'a> LintContext<'a> { - pub fn new(file_path: Box, semantic: &Rc>, settings: LintSettings) -> Self { + pub fn new(file_path: Box, semantic: &Rc>) -> Self { let disable_directives = DisableDirectivesBuilder::new(semantic.source_text(), semantic.trivias()).build(); Self { @@ -39,7 +39,7 @@ impl<'a> LintContext<'a> { fix: false, current_rule_name: "", file_path, - settings, + settings: Arc::new(LintSettings::default()), } } @@ -49,6 +49,12 @@ impl<'a> LintContext<'a> { self } + #[must_use] + pub fn with_settings(mut self, settings: &Arc) -> Self { + self.settings = Arc::clone(settings); + self + } + pub fn semantic(&self) -> &Rc> { &self.semantic } @@ -57,8 +63,8 @@ impl<'a> LintContext<'a> { &self.disable_directives } - pub fn settings(&self) -> LintSettings { - self.settings.clone() + pub fn settings(&self) -> &LintSettings { + &self.settings } pub fn source_text(&self) -> &'a str { diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index f7a7fc926..ec45179bf 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -17,7 +17,7 @@ mod rules; mod service; mod utils; -use std::{io::Write, rc::Rc}; +use std::{io::Write, rc::Rc, sync::Arc}; use oxc_diagnostics::Report; pub(crate) use oxc_semantic::AstNode; @@ -75,33 +75,23 @@ impl JsxA11y { pub struct Linter { rules: Vec<(/* rule name */ &'static str, RuleEnum)>, options: LintOptions, - settings: LintSettings, + settings: Arc, } impl Default for Linter { fn default() -> Self { - Self::new() + Self::from_options(LintOptions::default()).unwrap() } } impl Linter { - pub fn new() -> Self { - let rules = RULES - .iter() - .filter(|&rule| rule.category() == RuleCategory::Correctness) - .cloned() - .map(|rule| (rule.name(), rule)) - .collect::>(); - Self { rules, options: LintOptions::default(), settings: LintSettings::default() } - } - /// # Errors /// /// Returns `Err` if there are any errors parsing the configuration file. pub fn from_options(options: LintOptions) -> Result { let (rules, settings) = options.derive_rules_and_settings()?; 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] @@ -112,7 +102,7 @@ impl Linter { #[must_use] pub fn with_settings(mut self, settings: LintSettings) -> Self { - self.settings = settings; + self.settings = Arc::new(settings); self } @@ -132,7 +122,7 @@ impl Linter { pub fn run<'a>(&self, ctx: LintContext<'a>) -> Vec> { 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 { ctx.with_rule_name(rule_name); @@ -156,10 +146,6 @@ impl Linter { ctx.into_message() } - pub fn get_settings(&self) -> LintSettings { - self.settings.clone() - } - pub fn print_rules(writer: &mut W) { let rules_by_category = RULES.iter().fold( FxHashMap::default(), diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index adf8f7b5a..c94a36786 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -267,11 +267,8 @@ impl Runtime { return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect(); }; - let lint_ctx = LintContext::new( - path.to_path_buf().into_boxed_path(), - &Rc::new(semantic_ret.semantic), - self.linter.get_settings(), - ); + let lint_ctx = + LintContext::new(path.to_path_buf().into_boxed_path(), &Rc::new(semantic_ret.semantic)); self.linter.run(lint_ctx) } diff --git a/crates/oxc_linter/src/utils/react.rs b/crates/oxc_linter/src/utils/react.rs index e51c8ff23..bbbbf78cc 100644 --- a/crates/oxc_linter/src/utils/react.rs +++ b/crates/oxc_linter/src/utils/react.rs @@ -228,7 +228,7 @@ pub fn get_element_type(context: &LintContext, element: &JSXOpeningElement) -> O let JsxA11y { polymorphic_prop_name, components } = jsx_a11y; 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) { return Some(String::from(str.value.as_str())); } diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index a37a2f5c5..3873444d7 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -12,7 +12,7 @@ use oxc::{ span::SourceType, transformer::{TransformOptions, TransformTarget, Transformer}, }; -use oxc_linter::{LintContext, LintSettings, Linter}; +use oxc_linter::{LintContext, Linter}; use oxc_prettier::{Prettier, PrettierOptions}; use serde::Serialize; use wasm_bindgen::prelude::*; @@ -185,9 +185,8 @@ impl Oxc { self.save_diagnostics(semantic_ret.errors); let semantic = Rc::new(semantic_ret.semantic); - let lint_ctx = - LintContext::new(path.into_boxed_path(), &semantic, LintSettings::default()); - let linter_ret = Linter::new().run(lint_ctx); + let lint_ctx = LintContext::new(path.into_boxed_path(), &semantic); + let linter_ret = Linter::default().run(lint_ctx); let diagnostics = linter_ret.into_iter().map(|e| e.error).collect(); self.save_diagnostics(diagnostics); } diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index 5243c947e..7a552f169 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -2,7 +2,7 @@ use std::{path::PathBuf, rc::Rc}; use oxc_allocator::Allocator; 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_semantic::SemanticBuilder; use oxc_span::SourceType; @@ -30,11 +30,7 @@ fn bench_linter(criterion: &mut Criterion) { let linter = Linter::from_options(lint_options).unwrap(); let semantic = Rc::new(semantic_ret.semantic); b.iter(|| { - linter.run(LintContext::new( - PathBuf::from("").into_boxed_path(), - &semantic, - LintSettings::default(), - )) + linter.run(LintContext::new(PathBuf::from("").into_boxed_path(), &semantic)) }); }, );