diff --git a/crates/oxc_linter/src/env.rs b/crates/oxc_linter/src/config/env.rs similarity index 60% rename from crates/oxc_linter/src/env.rs rename to crates/oxc_linter/src/config/env.rs index 9dae87713032f..159a228fef187 100644 --- a/crates/oxc_linter/src/env.rs +++ b/crates/oxc_linter/src/config/env.rs @@ -1,22 +1,24 @@ use std::{self, ops::Deref}; +/// Environment +/// https://eslint.org/docs/latest/use/configure/language-options#using-configuration-files #[derive(Debug, Clone)] -pub struct Env(Vec); +pub struct ESLintEnv(Vec); -impl Env { +impl ESLintEnv { pub fn new(env: Vec) -> Self { Self(env) } } /// The `env` field from ESLint config -impl Default for Env { +impl Default for ESLintEnv { fn default() -> Self { Self(vec!["builtin".to_string()]) } } -impl Deref for Env { +impl Deref for ESLintEnv { type Target = Vec; fn deref(&self) -> &Self::Target { diff --git a/crates/oxc_linter/src/config/mod.rs b/crates/oxc_linter/src/config/mod.rs index 418edc9a53b77..10d36653a6aaf 100644 --- a/crates/oxc_linter/src/config/mod.rs +++ b/crates/oxc_linter/src/config/mod.rs @@ -1,21 +1,30 @@ +mod env; +pub mod errors; +mod settings; + use std::path::Path; -pub mod errors; use oxc_diagnostics::{Error, FailedToOpenFileError, Report}; use rustc_hash::{FxHashMap, FxHashSet}; use serde_json::Value; -use crate::{rules::RuleEnum, settings::Nextjs, AllowWarnDeny, Env, JsxA11y, LintSettings}; +use crate::{rules::RuleEnum, AllowWarnDeny}; use self::errors::{ FailedToParseConfigError, FailedToParseConfigJsonError, FailedToParseJsonc, FailedToParseRuleValueError, }; +pub use self::{ + env::ESLintEnv, + settings::{ESLintSettings, JsxA11y, Nextjs}, +}; +/// ESLint Config +/// pub struct ESLintConfig { rules: Vec, - settings: LintSettings, - env: Env, + settings: ESLintSettings, + env: ESLintEnv, } #[derive(Debug)] @@ -35,7 +44,7 @@ impl ESLintConfig { Ok(Self { rules, settings, env }) } - pub fn properties(self) -> (LintSettings, Env) { + pub fn properties(self) -> (ESLintSettings, ESLintEnv) { (self.settings, self.env) } @@ -157,15 +166,17 @@ fn parse_rules(root_json: &Value) -> Result, Error> { .collect::, Error>>() } -fn parse_settings_from_root(root_json: &Value) -> LintSettings { - let Value::Object(root_object) = root_json else { return LintSettings::default() }; +fn parse_settings_from_root(root_json: &Value) -> ESLintSettings { + let Value::Object(root_object) = root_json else { return ESLintSettings::default() }; - let Some(settings_value) = root_object.get("settings") else { return LintSettings::default() }; + let Some(settings_value) = root_object.get("settings") else { + return ESLintSettings::default(); + }; parse_settings(settings_value) } -pub fn parse_settings(setting_value: &Value) -> LintSettings { +pub fn parse_settings(setting_value: &Value) -> ESLintSettings { if let Value::Object(settings_object) = setting_value { let mut jsx_a11y_setting = JsxA11y::new(None, FxHashMap::default()); let mut nextjs_setting = Nextjs::new(vec![]); @@ -197,20 +208,20 @@ pub fn parse_settings(setting_value: &Value) -> LintSettings { } } - return LintSettings::new(jsx_a11y_setting, nextjs_setting); + return ESLintSettings::new(jsx_a11y_setting, nextjs_setting); } - LintSettings::default() + ESLintSettings::default() } -fn parse_env_from_root(root_json: &Value) -> Env { - let Value::Object(root_object) = root_json else { return Env::default() }; +fn parse_env_from_root(root_json: &Value) -> ESLintEnv { + let Value::Object(root_object) = root_json else { return ESLintEnv::default() }; - let Some(env_value) = root_object.get("env") else { return Env::default() }; + let Some(env_value) = root_object.get("env") else { return ESLintEnv::default() }; let env_object = match env_value { Value::Object(env_object) => env_object, - _ => return Env::default(), + _ => return ESLintEnv::default(), }; let mut result = vec![]; @@ -222,7 +233,7 @@ fn parse_env_from_root(root_json: &Value) -> Env { } } - Env::new(result) + ESLintEnv::new(result) } fn parse_rule_name(name: &str) -> (&str, &str) { diff --git a/crates/oxc_linter/src/settings.rs b/crates/oxc_linter/src/config/settings.rs similarity index 87% rename from crates/oxc_linter/src/settings.rs rename to crates/oxc_linter/src/config/settings.rs index a32e2b8c2b7c1..f289e7f8e700e 100644 --- a/crates/oxc_linter/src/settings.rs +++ b/crates/oxc_linter/src/config/settings.rs @@ -1,13 +1,15 @@ use rustc_hash::FxHashMap; /// The `settings` field from ESLint config +/// +/// An object containing name-value pairs of information that should be available to all rules #[derive(Debug, Clone)] -pub struct LintSettings { +pub struct ESLintSettings { pub jsx_a11y: JsxA11y, pub nextjs: Nextjs, } -impl Default for LintSettings { +impl Default for ESLintSettings { fn default() -> Self { Self { jsx_a11y: JsxA11y { polymorphic_prop_name: None, components: FxHashMap::default() }, @@ -16,7 +18,7 @@ impl Default for LintSettings { } } -impl LintSettings { +impl ESLintSettings { pub fn new(jsx_a11y: JsxA11y, nextjs: Nextjs) -> Self { Self { jsx_a11y, nextjs } } diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 6f27f520e48d3..1d10bd4335013 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -9,7 +9,7 @@ use crate::{ disable_directives::{DisableDirectives, DisableDirectivesBuilder}, fixer::{Fix, Message}, javascript_globals::GLOBALS, - AstNode, Env, LintSettings, + AstNode, ESLintEnv, ESLintSettings, }; pub struct LintContext<'a> { @@ -26,9 +26,9 @@ pub struct LintContext<'a> { file_path: Box, - settings: Arc, + settings: Arc, - env: Arc, + env: Arc, } impl<'a> LintContext<'a> { @@ -42,8 +42,8 @@ impl<'a> LintContext<'a> { fix: false, current_rule_name: "", file_path, - settings: Arc::new(LintSettings::default()), - env: Arc::new(Env::default()), + settings: Arc::new(ESLintSettings::default()), + env: Arc::new(ESLintEnv::default()), } } @@ -54,13 +54,13 @@ impl<'a> LintContext<'a> { } #[must_use] - pub fn with_settings(mut self, settings: &Arc) -> Self { + pub fn with_settings(mut self, settings: &Arc) -> Self { self.settings = Arc::clone(settings); self } #[must_use] - pub fn with_env(mut self, env: &Arc) -> Self { + pub fn with_env(mut self, env: &Arc) -> Self { self.env = Arc::clone(env); self } @@ -73,7 +73,7 @@ impl<'a> LintContext<'a> { &self.disable_directives } - pub fn settings(&self) -> &LintSettings { + pub fn settings(&self) -> &ESLintSettings { &self.settings } @@ -89,7 +89,7 @@ impl<'a> LintContext<'a> { &self.file_path } - pub fn envs(&self) -> &Env { + pub fn envs(&self) -> &ESLintEnv { &self.env } diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index d0c568d982892..e6cb6cbf7b588 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -8,7 +8,6 @@ mod ast_util; mod config; mod context; mod disable_directives; -mod env; mod fixer; mod globals; mod javascript_globals; @@ -17,7 +16,6 @@ pub mod partial_loader; pub mod rule; mod rules; mod service; -mod settings; mod utils; use rustc_hash::FxHashMap; @@ -25,19 +23,19 @@ use std::{io::Write, rc::Rc, sync::Arc}; use oxc_diagnostics::Report; +use crate::{ + config::{ESLintEnv, ESLintSettings, JsxA11y}, + fixer::Fix, + fixer::{Fixer, Message}, + rule::RuleCategory, + rules::{RuleEnum, RULES}, +}; pub use crate::{ context::LintContext, - env::Env, - fixer::Fix, - fixer::{FixResult, Fixer, Message}, options::{AllowWarnDeny, LintOptions}, - rule::RuleCategory, - rules::RULES, service::LintService, - settings::LintSettings, }; -pub(crate) use crate::{rules::RuleEnum, settings::JsxA11y}; -pub(crate) use oxc_semantic::AstNode; +use oxc_semantic::AstNode; #[cfg(target_pointer_width = "64")] #[test] @@ -54,8 +52,8 @@ fn size_asserts() { pub struct Linter { rules: Vec<(/* rule name */ &'static str, RuleEnum)>, options: LintOptions, - settings: Arc, - env: Arc, + settings: Arc, + env: Arc, } impl Default for Linter { @@ -81,13 +79,13 @@ impl Linter { } #[must_use] - pub fn with_settings(mut self, settings: LintSettings) -> Self { + pub fn with_settings(mut self, settings: ESLintSettings) -> Self { self.settings = Arc::new(settings); self } #[must_use] - pub fn with_envs(mut self, env: Env) -> Self { + pub fn with_envs(mut self, env: ESLintEnv) -> Self { self.env = Arc::new(env); self } diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options.rs index 32ac324e0e053..57748d27e846a 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options.rs @@ -9,7 +9,7 @@ use crate::{ ESLintConfig, }, rules::RULES, - Env, LintSettings, RuleCategory, RuleEnum, + ESLintEnv, ESLintSettings, RuleCategory, RuleEnum, }; use oxc_diagnostics::Error; use rustc_hash::FxHashSet; @@ -28,7 +28,7 @@ pub struct LintOptions { pub jsx_a11y_plugin: bool, pub nextjs_plugin: bool, pub react_perf_plugin: bool, - pub env: Env, + pub env: ESLintEnv, } impl Default for LintOptions { @@ -43,7 +43,7 @@ impl Default for LintOptions { jsx_a11y_plugin: false, nextjs_plugin: false, react_perf_plugin: false, - env: Env::default(), + env: ESLintEnv::default(), } } } @@ -107,7 +107,7 @@ impl LintOptions { #[must_use] pub fn with_env(mut self, env: Vec) -> Self { - self.env = Env::new(env); + self.env = ESLintEnv::new(env); self } } @@ -178,7 +178,7 @@ impl LintOptions { /// * Returns `Err` if there are any errors parsing the configuration file. pub fn derive_rules_and_settings_and_env( &self, - ) -> Result<(Vec, LintSettings, Env), Error> { + ) -> Result<(Vec, ESLintSettings, ESLintEnv), Error> { let config = self.config_path.as_ref().map(|path| ESLintConfig::new(path)).transpose()?; let mut rules: FxHashSet = FxHashSet::default(); diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 0fc7dc5dee97c..cb0cf6740e18f 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -9,7 +9,7 @@ use oxc_diagnostics::DiagnosticService; use serde_json::Value; use crate::{ - config::parse_settings, rules::RULES, Fixer, LintOptions, LintService, LintSettings, Linter, + config::parse_settings, rules::RULES, ESLintSettings, Fixer, LintOptions, LintService, Linter, RuleEnum, }; @@ -188,8 +188,8 @@ impl Tester { ) -> TestResult { let allocator = Allocator::default(); let rule = self.find_rule().read_json(config); - let lint_settings: LintSettings = - settings.as_ref().map_or_else(LintSettings::default, parse_settings); + let lint_settings: ESLintSettings = + settings.as_ref().map_or_else(ESLintSettings::default, parse_settings); let options = LintOptions::default() .with_fix(is_fix) .with_import_plugin(self.import_plugin) diff --git a/crates/oxc_linter/src/utils/react.rs b/crates/oxc_linter/src/utils/react.rs index 40a3728d31a85..e9576ffc5d81c 100644 --- a/crates/oxc_linter/src/utils/react.rs +++ b/crates/oxc_linter/src/utils/react.rs @@ -8,7 +8,7 @@ use oxc_ast::{ }; use oxc_semantic::{AstNode, SymbolFlags}; -use crate::{JsxA11y, LintContext, LintSettings}; +use crate::{ESLintSettings, JsxA11y, LintContext}; pub fn is_create_element_call(call_expr: &CallExpression) -> bool { if let Some(member_expr) = call_expr.callee.get_member_expr() { @@ -224,7 +224,7 @@ pub fn get_element_type(context: &LintContext, element: &JSXOpeningElement) -> O return None; }; - let LintSettings { jsx_a11y, .. } = context.settings(); + let ESLintSettings { jsx_a11y, .. } = context.settings(); let JsxA11y { polymorphic_prop_name, components } = jsx_a11y; if let Some(polymorphic_prop_name_value) = polymorphic_prop_name {