refactor(linter): move settings and env to the config module (#2181)

This commit is contained in:
Boshen 2024-01-26 18:51:06 +08:00 committed by GitHub
parent 87bc19830e
commit a17e43eefe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 69 additions and 56 deletions

View file

@ -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<String>);
pub struct ESLintEnv(Vec<String>);
impl Env {
impl ESLintEnv {
pub fn new(env: Vec<String>) -> 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<String>;
fn deref(&self) -> &Self::Target {

View file

@ -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
/// <https://eslint.org/docs/latest/use/configure/configuration-files-new#configuration-objects>
pub struct ESLintConfig {
rules: Vec<ESLintRuleConfig>,
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<Vec<ESLintRuleConfig>, Error> {
.collect::<Result<Vec<_>, 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) {

View file

@ -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 }
}

View file

@ -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<Path>,
settings: Arc<LintSettings>,
settings: Arc<ESLintSettings>,
env: Arc<Env>,
env: Arc<ESLintEnv>,
}
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<LintSettings>) -> Self {
pub fn with_settings(mut self, settings: &Arc<ESLintSettings>) -> Self {
self.settings = Arc::clone(settings);
self
}
#[must_use]
pub fn with_env(mut self, env: &Arc<Env>) -> Self {
pub fn with_env(mut self, env: &Arc<ESLintEnv>) -> 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
}

View file

@ -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<LintSettings>,
env: Arc<Env>,
settings: Arc<ESLintSettings>,
env: Arc<ESLintEnv>,
}
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
}

View file

@ -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<String>) -> 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<RuleEnum>, LintSettings, Env), Error> {
) -> Result<(Vec<RuleEnum>, ESLintSettings, ESLintEnv), Error> {
let config = self.config_path.as_ref().map(|path| ESLintConfig::new(path)).transpose()?;
let mut rules: FxHashSet<RuleEnum> = FxHashSet::default();

View file

@ -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)

View file

@ -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 {