|
| 1 | +use lazy_regex::Regex; |
1 | 2 | use oxc_ast::{ |
2 | 3 | AstKind, |
3 | 4 | ast::{Argument, BindingPatternKind, Expression}, |
@@ -25,7 +26,7 @@ pub struct CatchErrorName(Box<CatchErrorNameConfig>); |
25 | 26 |
|
26 | 27 | #[derive(Debug, Clone)] |
27 | 28 | pub struct CatchErrorNameConfig { |
28 | | - ignore: Vec<CompactStr>, |
| 29 | + ignore: Vec<Regex>, |
29 | 30 | name: CompactStr, |
30 | 31 | } |
31 | 32 |
|
@@ -127,14 +128,13 @@ impl Rule for CatchErrorName { |
127 | 128 | fn from_configuration(value: serde_json::Value) -> Self { |
128 | 129 | let ignored_names = value |
129 | 130 | .get(0) |
130 | | - .and_then(|v| v.get("ignored")) |
| 131 | + .and_then(|v| v.get("ignore")) |
131 | 132 | .and_then(serde_json::Value::as_array) |
132 | 133 | .unwrap_or(&vec![]) |
133 | 134 | .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>>(); |
138 | 138 |
|
139 | 139 | let allowed_name = CompactStr::from( |
140 | 140 | value |
@@ -176,7 +176,7 @@ impl Rule for CatchErrorName { |
176 | 176 |
|
177 | 177 | impl CatchErrorName { |
178 | 178 | 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)) |
180 | 180 | } |
181 | 181 |
|
182 | 182 | fn check_function_arguments(&self, arg: &Argument, ctx: &LintContext) { |
@@ -284,10 +284,23 @@ fn test() { |
284 | 284 | console.log(_); |
285 | 285 | } |
286 | 286 | ", |
287 | | - Some(serde_json::json!([{"ignored": ["_"]}])), |
| 287 | + Some(serde_json::json!([{"ignore": ["_"]}])), |
288 | 288 | ), |
289 | 289 | ("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 | + ), |
291 | 304 | ("try { } catch (exception) { }", Some(serde_json::json!([{"name": "exception"}]))), |
292 | 305 | ]; |
293 | 306 |
|
|
0 commit comments