Skip to content

Bug: [no-unused-vars] Error message doesn't inform about caught errors pattern when caught error is reassigned but not used #18607

@kirkwaiblinger

Description

@kirkwaiblinger

Environment

playground

What parser are you using?

Default (Espree)

What did you do?

Configuration
["warn", { "caughtErrorsIgnorePattern" : "foo" }]
try {
} catch (_) { 
  _ = 'foo'
}

What did you expect to happen?

'_' is assigned a value but never used. Allowed unused caught errors must match /foo/u

What actually happened?

'_' is assigned a value but never used.

Link to Minimal Reproducible Example

https://eslint.org/play/#eyJ0ZXh0IjoidHJ5IHtcbn0gY2F0Y2ggKF9mKSB7IFxuICBsZXQgXztcbiAgXyA9ICdmb28nXG59Iiwib3B0aW9ucyI6eyJydWxlcyI6eyJuby11bnVzZWQtdmFycyI6WyJ3YXJuIix7ImNhdWdodEVycm9yc0lnbm9yZVBhdHRlcm4iOiJmb28ifV19LCJsYW5ndWFnZU9wdGlvbnMiOnsic291cmNlVHlwZSI6Im1vZHVsZSIsInBhcnNlck9wdGlvbnMiOnsiZWNtYUZlYXR1cmVzIjp7fX19fX0=

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

There's just lack of parity between

function getDefinedMessageData(unusedVar) {
const def = unusedVar.defs && unusedVar.defs[0];
let additionalMessageData = "";
if (def) {
let pattern;
let variableDescription;
switch (def.type) {
case "CatchClause":
if (config.caughtErrorsIgnorePattern) {
[variableDescription, pattern] = getVariableDescription("catch-clause");
}
break;
case "Parameter":
if (config.argsIgnorePattern) {
[variableDescription, pattern] = getVariableDescription("parameter");
}
break;
default:
if (config.varsIgnorePattern) {
[variableDescription, pattern] = getVariableDescription("variable");
}
break;
}
if (pattern && variableDescription) {
additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`;
}
}
return {
varName: unusedVar.name,
action: "defined",
additional: additionalMessageData
};
}

, which considers catch clause, and
function getAssignedMessageData(unusedVar) {
const def = unusedVar.defs && unusedVar.defs[0];
let additionalMessageData = "";
if (def) {
let pattern;
let variableDescription;
if (def.name.parent.type === "ArrayPattern" && config.destructuredArrayIgnorePattern) {
[variableDescription, pattern] = getVariableDescription("array-destructure");
} else if (config.varsIgnorePattern) {
[variableDescription, pattern] = getVariableDescription("variable");
}
if (pattern && variableDescription) {
additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`;
}
}
return {
varName: unusedVar.name,
action: "assigned a value",
additional: additionalMessageData
};
}

, which doesn't

See also #18606

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionbugESLint is working incorrectlyrepro:yesIssues with a reproducible exampleruleRelates to ESLint's core rules

    Type

    No type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions