mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(linter): read rule configuration from json
This commit is contained in:
parent
b4a5f13fb6
commit
8b9ebcd6e5
4 changed files with 28 additions and 7 deletions
|
|
@ -7,7 +7,7 @@ mod context;
|
|||
mod rule;
|
||||
mod rules;
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::{fs, rc::Rc};
|
||||
|
||||
use oxc_diagnostics::Error;
|
||||
pub(crate) use oxc_semantic::AstNode;
|
||||
|
|
@ -27,7 +27,20 @@ impl Linter {
|
|||
#[must_use]
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
Self { rules: RULES.to_vec() }
|
||||
let rules_config = Self::read_rules_configuration();
|
||||
let rules = rules_config.map_or_else(
|
||||
|| RULES.to_vec(),
|
||||
|rules_config| {
|
||||
RULES
|
||||
.iter()
|
||||
.map(|rule| {
|
||||
let value = rules_config.get(rule.name());
|
||||
rule.read_json(value.cloned())
|
||||
})
|
||||
.collect()
|
||||
},
|
||||
);
|
||||
Self { rules }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -47,4 +60,12 @@ impl Linter {
|
|||
|
||||
ctx.into_diagnostics()
|
||||
}
|
||||
|
||||
fn read_rules_configuration() -> Option<serde_json::Map<String, serde_json::Value>> {
|
||||
fs::read_to_string(".eslintrc.json")
|
||||
.ok()
|
||||
.and_then(|s| serde_json::from_str(&s).ok())
|
||||
.and_then(|v: serde_json::Value| v.get("rules").cloned())
|
||||
.and_then(|v| v.as_object().cloned())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,13 +27,13 @@ impl RuleEnum {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_json(&self, maybe_value: Option<serde_json::Value>) -> Self {
|
||||
pub fn read_json(&self, maybe_value: Option<serde_json::Value>) -> Self {
|
||||
match self {
|
||||
Self::NoDebugger(_) => {
|
||||
RuleEnum::NoDebugger(maybe_value.map(NoDebugger::from_json).unwrap_or_default())
|
||||
Self::NoDebugger(maybe_value.map(NoDebugger::from_json).unwrap_or_default())
|
||||
}
|
||||
Self::NoEmpty(_) => {
|
||||
RuleEnum::NoEmpty(maybe_value.map(NoEmpty::from_json).unwrap_or_default())
|
||||
Self::NoEmpty(maybe_value.map(NoEmpty::from_json).unwrap_or_default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ impl Rule for NoEmpty {
|
|||
Self {
|
||||
allow_empty_catch: obj
|
||||
.and_then(|v| v.get("allowEmptyCatch"))
|
||||
.and_then(|v| v.as_bool())
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ impl Tester {
|
|||
let semantic = SemanticBuilder::new().build(program, ret.trivias);
|
||||
let semantic = std::rc::Rc::new(semantic);
|
||||
let rule = RULES.iter().find(|rule| rule.name() == self.rule_name).unwrap();
|
||||
let rule = rule.from_json(config);
|
||||
let rule = rule.read_json(config);
|
||||
let diagnostics = Linter::from_rules(vec![rule]).run(&semantic);
|
||||
if diagnostics.is_empty() {
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue