Skip to content

Commit 6c11740

Browse files
committedMar 13, 2025·
fix(linter): false positive in unicorn/catch-error-name (#9763)
closes #9723
1 parent 1d60e85 commit 6c11740

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed
 

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

+30-31
Original file line numberDiff line numberDiff line change
@@ -202,36 +202,9 @@ impl CatchErrorName {
202202
return;
203203
}
204204

205-
if binding_ident.name.starts_with('_') {
206-
let mut iter =
207-
ctx.semantic().symbol_references(binding_ident.symbol_id()).peekable();
208-
209-
if iter.peek().is_some() {
210-
ctx.diagnostic_with_fix(
211-
catch_error_name_diagnostic(
212-
binding_ident.name.as_str(),
213-
&self.name,
214-
binding_ident.span,
215-
),
216-
|fixer| {
217-
let fixer = fixer.for_multifix();
218-
let mut declaration_fix = fixer.new_fix_with_capacity(2);
219-
220-
declaration_fix
221-
.push(fixer.replace(binding_ident.span, self.name.clone()));
222-
223-
for reference in iter {
224-
let node = ctx.nodes().get_node(reference.node_id()).kind();
225-
let Some(id) = node.as_identifier_reference() else { continue };
226-
227-
declaration_fix.push(fixer.replace(id.span, self.name.clone()));
228-
}
229-
230-
declaration_fix
231-
},
232-
);
233-
}
234-
205+
let symbol_id = binding_ident.symbol_id();
206+
let mut iter = ctx.semantic().symbol_references(symbol_id).peekable();
207+
if binding_ident.name.starts_with('_') && iter.peek().is_none() {
235208
return;
236209
}
237210

@@ -241,7 +214,27 @@ impl CatchErrorName {
241214
&self.name,
242215
binding_ident.span,
243216
),
244-
|fixer| fixer.replace(binding_ident.span, self.name.clone()),
217+
|fixer| {
218+
let basic_fix = fixer.replace(binding_ident.span, self.name.clone());
219+
if iter.peek().is_none() {
220+
return basic_fix;
221+
}
222+
223+
let fixer = fixer.for_multifix();
224+
let capacity = ctx.scoping().get_resolved_reference_ids(symbol_id).len() + 1;
225+
226+
let mut declaration_fix = fixer.new_fix_with_capacity(capacity);
227+
228+
declaration_fix.push(basic_fix);
229+
for reference in iter {
230+
let node = ctx.nodes().get_node(reference.node_id()).kind();
231+
let Some(id) = node.as_identifier_reference() else { continue };
232+
233+
declaration_fix.push(fixer.replace(id.span, self.name.clone()));
234+
}
235+
236+
declaration_fix
237+
},
245238
);
246239
}
247240
}
@@ -306,6 +299,7 @@ fn test() {
306299
("try { } catch (notMatching) { }", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
307300
("try { } catch (notMatching) { }", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
308301
("try { } catch (_) { console.log(_) }", None),
302+
("try { } catch (err) { console.error(err) }", None),
309303
("promise.catch(notMatching => { })", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
310304
("promise.catch((foo) => { })", None),
311305
("promise.catch(function (foo) { })", None),
@@ -356,6 +350,11 @@ fn test() {
356350
"try { } catch (error) { console.log(error) }",
357351
None,
358352
),
353+
(
354+
"try { } catch (err) { console.error(err) }",
355+
"try { } catch (error) { console.error(error) }",
356+
None,
357+
),
359358
(
360359
"promise.catch(notMatching => { })",
361360
"promise.catch(error => { })",

‎crates/oxc_linter/src/snapshots/unicorn_catch_error_name.snap

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ source: crates/oxc_linter/src/tester.rs
5656
· ─
5757
╰────
5858

59+
eslint-plugin-unicorn(catch-error-name): The catch parameter "err" should be named "error"
60+
╭─[catch_error_name.tsx:1:16]
61+
1try { } catch (err) { console.error(err) }
62+
· ───
63+
╰────
64+
5965
eslint-plugin-unicorn(catch-error-name): The catch parameter "notMatching" should be named "error"
6066
╭─[catch_error_name.tsx:1:15]
6167
1promise.catch(notMatching => { })

0 commit comments

Comments
 (0)
Please sign in to comment.