Skip to content

Commit 0b539e3

Browse files
fix(linter): unicorn/catch-error-name wasn't using the ignore property (#12446)
changed ignored to ignore to match docs and unicorn config and also allowed regex in ignore vec fixes: #12430 the regex build will silently fail if its invalid, should I log it? if yes how to do it as using any print related function causes clippy to fail
1 parent d936024 commit 0b539e3

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

crates/oxc_linter/src/rules/unicorn/catch_error_name.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use lazy_regex::Regex;
12
use oxc_ast::{
23
AstKind,
34
ast::{Argument, BindingPatternKind, Expression},
@@ -25,7 +26,7 @@ pub struct CatchErrorName(Box<CatchErrorNameConfig>);
2526

2627
#[derive(Debug, Clone)]
2728
pub struct CatchErrorNameConfig {
28-
ignore: Vec<CompactStr>,
29+
ignore: Vec<Regex>,
2930
name: CompactStr,
3031
}
3132

@@ -127,14 +128,13 @@ impl Rule for CatchErrorName {
127128
fn from_configuration(value: serde_json::Value) -> Self {
128129
let ignored_names = value
129130
.get(0)
130-
.and_then(|v| v.get("ignored"))
131+
.and_then(|v| v.get("ignore"))
131132
.and_then(serde_json::Value::as_array)
132133
.unwrap_or(&vec![])
133134
.iter()
134-
.map(serde_json::Value::as_str)
135-
.filter(Option::is_some)
136-
.map(|x| CompactStr::from(x.unwrap()))
137-
.collect::<Vec<CompactStr>>();
135+
.filter_map(serde_json::Value::as_str)
136+
.filter_map(|x| Regex::new(x).ok())
137+
.collect::<Vec<Regex>>();
138138

139139
let allowed_name = CompactStr::from(
140140
value
@@ -176,7 +176,7 @@ impl Rule for CatchErrorName {
176176

177177
impl CatchErrorName {
178178
fn is_name_allowed(&self, name: &str) -> bool {
179-
self.name == name || self.ignore.iter().any(|s| s.as_str() == name)
179+
self.name == name || self.ignore.iter().any(|s| s.is_match(name))
180180
}
181181

182182
fn check_function_arguments(&self, arg: &Argument, ctx: &LintContext) {
@@ -284,10 +284,23 @@ fn test() {
284284
console.log(_);
285285
}
286286
",
287-
Some(serde_json::json!([{"ignored": ["_"]}])),
287+
Some(serde_json::json!([{"ignore": ["_"]}])),
288288
),
289289
("try { } catch (error) { }", None),
290-
("promise.catch(unicorn => { })", Some(serde_json::json!([{"ignored": ["unicorn"]}]))),
290+
("promise.catch(unicorn => { })", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
291+
// https://github.com/oxc-project/oxc/issues/12430
292+
(
293+
"try {
294+
// some codes
295+
} catch (error: unknown) {
296+
try {
297+
// some codes
298+
} catch (error2: unknown) {
299+
// some codes
300+
}
301+
}",
302+
Some(serde_json::json!([{"ignore": [ "^error\\d*$"]}])),
303+
),
291304
("try { } catch (exception) { }", Some(serde_json::json!([{"name": "exception"}]))),
292305
];
293306

0 commit comments

Comments
 (0)