Skip to content

Commit

Permalink
Fix: incorrect error messages of no-unused-vars (fixes #9774) (#9791)
Browse files Browse the repository at this point in the history
Remove varsIgnorePattern incorrectly included in error messages of no-unused-vars.
  • Loading branch information
akouryy authored and aladdin-add committed Dec 30, 2017
1 parent bbabf34 commit 4c87f42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/rules/no-unused-vars.js
Expand Up @@ -105,26 +105,21 @@ module.exports = {
* @returns {string} The warning message to be used with this unused variable.
*/
function getDefinedMessage(unusedVar) {
const defType = unusedVar.defs && unusedVar.defs[0] && unusedVar.defs[0].type;
let type;
let pattern;

if (config.varsIgnorePattern) {
if (defType === "CatchClause" && config.caughtErrorsIgnorePattern) {
type = "args";
pattern = config.caughtErrorsIgnorePattern.toString();
} else if (defType === "Parameter" && config.argsIgnorePattern) {
type = "args";
pattern = config.argsIgnorePattern.toString();
} else if (defType !== "Parameter" && config.varsIgnorePattern) {
type = "vars";
pattern = config.varsIgnorePattern.toString();
}

if (unusedVar.defs && unusedVar.defs[0] && unusedVar.defs[0].type) {
const defType = unusedVar.defs[0].type;

if (defType === "CatchClause" && config.caughtErrorsIgnorePattern) {
type = "args";
pattern = config.caughtErrorsIgnorePattern.toString();
} else if (defType === "Parameter" && config.argsIgnorePattern) {
type = "args";
pattern = config.argsIgnorePattern.toString();
}
}

const additional = type ? ` Allowed unused ${type} must match ${pattern}.` : "";

return `'{{name}}' is defined but never used.${additional}`;
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -675,6 +675,18 @@ ruleTester.run("no-unused-vars", rule, {
"'a' is defined but never used.",
"'c' is defined but never used."
]
},

// https://github.com/eslint/eslint/issues/9774
{
code: "(function(_a) {})();",
options: [{ args: "all", varsIgnorePattern: "^_" }],
errors: [{ message: "'_a' is defined but never used." }]
},
{
code: "(function(_a) {})();",
options: [{ args: "all", caughtErrorsIgnorePattern: "^_" }],
errors: [{ message: "'_a' is defined but never used." }]
}
]
});

0 comments on commit 4c87f42

Please sign in to comment.