refactor(linter): move LintSettings to its own file (#2052)

This commit is contained in:
Boshen 2024-01-16 17:23:55 +08:00 committed by GitHub
parent ae4e714713
commit f514410427
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 33 deletions

View file

@ -168,8 +168,7 @@ fn parse_settings_from_root(root_json: &Value) -> LintSettings {
pub fn parse_settings(setting_value: &Value) -> LintSettings {
if let Value::Object(settings_object) = setting_value {
if let Some(Value::Object(jsx_a11y)) = settings_object.get("jsx-a11y") {
let mut jsx_a11y_setting =
JsxA11y { polymorphic_prop_name: None, components: FxHashMap::default() };
let mut jsx_a11y_setting = JsxA11y::new(None, FxHashMap::default());
if let Some(Value::Object(components)) = jsx_a11y.get("components") {
let components_map: FxHashMap<String, String> = components
@ -186,7 +185,7 @@ pub fn parse_settings(setting_value: &Value) -> LintSettings {
.set_polymorphic_prop_name(Some(String::from(polymorphic_prop_name)));
}
return LintSettings { jsx_a11y: jsx_a11y_setting };
return LintSettings::new(jsx_a11y_setting);
}
}

View file

@ -15,13 +15,13 @@ pub mod partial_loader;
pub mod rule;
mod rules;
mod service;
mod settings;
mod utils;
use rustc_hash::FxHashMap;
use std::{io::Write, rc::Rc, sync::Arc};
use oxc_diagnostics::Report;
pub(crate) use oxc_semantic::AstNode;
use rustc_hash::FxHashMap;
pub use crate::{
context::LintContext,
@ -30,8 +30,13 @@ pub use crate::{
options::{AllowWarnDeny, LintOptions},
rule::RuleCategory,
service::LintService,
settings::LintSettings,
};
pub(crate) use rules::{RuleEnum, RULES};
pub(crate) use crate::{
rules::{RuleEnum, RULES},
settings::JsxA11y,
};
pub(crate) use oxc_semantic::AstNode;
#[cfg(target_pointer_width = "64")]
#[test]
@ -44,33 +49,6 @@ fn size_asserts() {
assert_eq_size!(RuleEnum, [u8; 16]);
}
#[derive(Debug, Clone)]
pub struct LintSettings {
jsx_a11y: JsxA11y,
}
impl Default for LintSettings {
fn default() -> Self {
Self { jsx_a11y: JsxA11y { polymorphic_prop_name: None, components: FxHashMap::default() } }
}
}
#[derive(Debug, Clone)]
pub struct JsxA11y {
polymorphic_prop_name: Option<String>,
components: FxHashMap<String, String>,
}
impl JsxA11y {
pub fn set_components(&mut self, components: FxHashMap<String, String>) {
self.components = components;
}
pub fn set_polymorphic_prop_name(&mut self, name: Option<String>) {
self.polymorphic_prop_name = name;
}
}
#[derive(Debug)]
pub struct Linter {
rules: Vec<(/* rule name */ &'static str, RuleEnum)>,

View file

@ -0,0 +1,42 @@
use rustc_hash::FxHashMap;
/// The `settings` field from ESLint config
#[derive(Debug, Clone)]
pub struct LintSettings {
pub jsx_a11y: JsxA11y,
}
impl Default for LintSettings {
fn default() -> Self {
Self { jsx_a11y: JsxA11y { polymorphic_prop_name: None, components: FxHashMap::default() } }
}
}
impl LintSettings {
pub fn new(jsx_a11y: JsxA11y) -> Self {
Self { jsx_a11y }
}
}
#[derive(Debug, Clone)]
pub struct JsxA11y {
pub polymorphic_prop_name: Option<String>,
pub components: FxHashMap<String, String>,
}
impl JsxA11y {
pub fn new(
polymorphic_prop_name: Option<String>,
components: FxHashMap<String, String>,
) -> Self {
Self { polymorphic_prop_name, components }
}
pub fn set_components(&mut self, components: FxHashMap<String, String>) {
self.components = components;
}
pub fn set_polymorphic_prop_name(&mut self, name: Option<String>) {
self.polymorphic_prop_name = name;
}
}