Skip to content

Commit

Permalink
feat(linter): add rule eslint/new-cap
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Dec 28, 2024
1 parent f2a1e89 commit 13dfe71
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions crates/oxc_linter/src/rules/eslint/new_cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ fn vec_str_serde_value(
.collect::<Vec<CompactStr>>()
}

fn regex_serde_value(map: &serde_json::Map<String, serde_json::Value>, key: &str) -> Option<Regex> {
let value = map.get(key)?;
let err = format!("eslint/new-cap: expect configuration option '{key}' to be a regex string.");

let regex_string = value.as_str().expect(&err);

if let Ok(regex) = Regex::new(regex_string) {
return Some(regex);
}

None
}

impl From<&serde_json::Value> for NewCap {
fn from(raw: &serde_json::Value) -> Self {
let Some(config_entry) = raw.get(0) else {
Expand Down Expand Up @@ -105,9 +118,9 @@ impl From<&serde_json::Value> for NewCap {
"newIsCapExceptions",
caps_allowed_vec(),
),
new_is_cap_exception_pattern: None,
new_is_cap_exception_pattern: regex_serde_value(config, "newIsCapExceptionPattern"),
cap_is_new_exceptions: vec_str_serde_value(config, "capIsNewExceptions", vec![]),
cap_is_new_exception_pattern: None,
cap_is_new_exception_pattern: regex_serde_value(config, "capIsNewExceptionPattern"),
properties: bool_serde_value(config, "properties"),
}))
}
Expand Down Expand Up @@ -460,11 +473,10 @@ impl Rule for NewCap {

let allowed = *capitalization != GetCapResult::Lower
|| is_cap_allowed_expression(
callee,
short_name,
name,
&self.new_is_cap_exceptions,
&self.new_is_cap_exception_pattern,
self.new_is_cap_exception_pattern.as_ref(),
)
|| (!self.properties && short_name != name);

Expand All @@ -490,11 +502,10 @@ impl Rule for NewCap {

let allowed = *capitalization != GetCapResult::Upper
|| is_cap_allowed_expression(
callee,
short_name,
name,
&caps_is_new_exceptions,
&self.cap_is_new_exception_pattern,
self.cap_is_new_exception_pattern.as_ref(),
)
|| (!self.properties && short_name != name);

Expand Down Expand Up @@ -613,11 +624,10 @@ fn extract_name_from_expression(expression: &Expression) -> Option<CompactStr> {
}

fn is_cap_allowed_expression(
expression: &Expression<'_>,
short_name: &CompactStr,
name: &CompactStr,
exceptions: &Vec<CompactStr>,
patterns: &Option<Regex>,
exceptions: &[CompactStr],
patterns: Option<&Regex>,
) -> bool {
if exceptions.contains(name) || exceptions.contains(short_name) {
return true;
Expand All @@ -627,6 +637,10 @@ fn is_cap_allowed_expression(
return true;
}

if let Some(pattern) = &patterns {
return pattern.find(name).is_some();
};

false
}

Expand Down Expand Up @@ -695,28 +709,28 @@ fn test() {
"var x = Foo(42);",
Some(serde_json::json!([{ "capIsNew": true, "capIsNewExceptions": ["Foo"] }])),
),
// ("var x = Foo(42);", Some(serde_json::json!([{ "capIsNewExceptionPattern": "^Foo" }]))),
("var x = Foo(42);", Some(serde_json::json!([{ "capIsNewExceptionPattern": "^Foo" }]))),
(
"var x = new foo(42);",
Some(serde_json::json!([{ "newIsCap": true, "newIsCapExceptions": ["foo"] }])),
),
// ("var x = new foo(42);", Some(serde_json::json!([{ "newIsCapExceptionPattern": "^foo" }]))),
("var x = new foo(42);", Some(serde_json::json!([{ "newIsCapExceptionPattern": "^foo" }]))),
("var x = Object(42);", Some(serde_json::json!([{ "capIsNewExceptions": ["Foo"] }]))),
("var x = Foo.Bar(42);", Some(serde_json::json!([{ "capIsNewExceptions": ["Bar"] }]))),
("var x = Foo.Bar(42);", Some(serde_json::json!([{ "capIsNewExceptions": ["Foo.Bar"] }]))),
// (
// "var x = Foo.Bar(42);",
// Some(serde_json::json!([{ "capIsNewExceptionPattern": "^Foo\\.." }])),
// ),
(
"var x = Foo.Bar(42);",
Some(serde_json::json!([{ "capIsNewExceptionPattern": "^Foo\\.." }])),
),
("var x = new foo.bar(42);", Some(serde_json::json!([{ "newIsCapExceptions": ["bar"] }]))),
(
"var x = new foo.bar(42);",
Some(serde_json::json!([{ "newIsCapExceptions": ["foo.bar"] }])),
),
// (
// "var x = new foo.bar(42);",
// Some(serde_json::json!([{ "newIsCapExceptionPattern": "^foo\\.." }])),
// ),
(
"var x = new foo.bar(42);",
Some(serde_json::json!([{ "newIsCapExceptionPattern": "^foo\\.." }])),
),
("var x = new foo.bar(42);", Some(serde_json::json!([{ "properties": false }]))),
("var x = Foo.bar(42);", Some(serde_json::json!([{ "properties": false }]))),
(
Expand Down

0 comments on commit 13dfe71

Please sign in to comment.