Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions crates/oxc_linter/src/rules/unicorn/catch_error_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,36 +202,9 @@ impl CatchErrorName {
return;
}

if binding_ident.name.starts_with('_') {
let mut iter =
ctx.semantic().symbol_references(binding_ident.symbol_id()).peekable();

if iter.peek().is_some() {
ctx.diagnostic_with_fix(
catch_error_name_diagnostic(
binding_ident.name.as_str(),
&self.name,
binding_ident.span,
),
|fixer| {
let fixer = fixer.for_multifix();
let mut declaration_fix = fixer.new_fix_with_capacity(2);

declaration_fix
.push(fixer.replace(binding_ident.span, self.name.clone()));

for reference in iter {
let node = ctx.nodes().get_node(reference.node_id()).kind();
let Some(id) = node.as_identifier_reference() else { continue };

declaration_fix.push(fixer.replace(id.span, self.name.clone()));
}

declaration_fix
},
);
}

let symbol_id = binding_ident.symbol_id();
let mut iter = ctx.semantic().symbol_references(symbol_id).peekable();
if binding_ident.name.starts_with('_') && iter.peek().is_none() {
return;
}

Expand All @@ -241,7 +214,27 @@ impl CatchErrorName {
&self.name,
binding_ident.span,
),
|fixer| fixer.replace(binding_ident.span, self.name.clone()),
|fixer| {
let basic_fix = fixer.replace(binding_ident.span, self.name.clone());
if iter.peek().is_none() {
return basic_fix;
}

let fixer = fixer.for_multifix();
let capacity = ctx.scoping().get_resolved_reference_ids(symbol_id).len() + 1;

let mut declaration_fix = fixer.new_fix_with_capacity(capacity);

declaration_fix.push(basic_fix);
for reference in iter {
let node = ctx.nodes().get_node(reference.node_id()).kind();
let Some(id) = node.as_identifier_reference() else { continue };

declaration_fix.push(fixer.replace(id.span, self.name.clone()));
}

declaration_fix
},
);
}
}
Expand Down Expand Up @@ -306,6 +299,7 @@ fn test() {
("try { } catch (notMatching) { }", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
("try { } catch (notMatching) { }", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
("try { } catch (_) { console.log(_) }", None),
("try { } catch (err) { console.error(err) }", None),
("promise.catch(notMatching => { })", Some(serde_json::json!([{"ignore": ["unicorn"]}]))),
("promise.catch((foo) => { })", None),
("promise.catch(function (foo) { })", None),
Expand Down Expand Up @@ -356,6 +350,11 @@ fn test() {
"try { } catch (error) { console.log(error) }",
None,
),
(
"try { } catch (err) { console.error(err) }",
"try { } catch (error) { console.error(error) }",
None,
),
(
"promise.catch(notMatching => { })",
"promise.catch(error => { })",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ source: crates/oxc_linter/src/tester.rs
· ─
╰────

⚠ eslint-plugin-unicorn(catch-error-name): The catch parameter "err" should be named "error"
╭─[catch_error_name.tsx:1:16]
1 │ try { } catch (err) { console.error(err) }
· ───
╰────

⚠ eslint-plugin-unicorn(catch-error-name): The catch parameter "notMatching" should be named "error"
╭─[catch_error_name.tsx:1:15]
1 │ promise.catch(notMatching => { })
Expand Down
Loading