From 4c87f428bc501e087ab4882dae9b4fd2bc22041d Mon Sep 17 00:00:00 2001 From: akouryy Date: Sat, 30 Dec 2017 17:49:01 +0900 Subject: [PATCH] Fix: incorrect error messages of no-unused-vars (fixes #9774) (#9791) Remove varsIgnorePattern incorrectly included in error messages of no-unused-vars. --- lib/rules/no-unused-vars.js | 21 ++++++++------------- tests/lib/rules/no-unused-vars.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/rules/no-unused-vars.js b/lib/rules/no-unused-vars.js index 05940d5932a..04c2bc87996 100644 --- a/lib/rules/no-unused-vars.js +++ b/lib/rules/no-unused-vars.js @@ -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}`; diff --git a/tests/lib/rules/no-unused-vars.js b/tests/lib/rules/no-unused-vars.js index 3e452ce3d66..9615c62dd8a 100644 --- a/tests/lib/rules/no-unused-vars.js +++ b/tests/lib/rules/no-unused-vars.js @@ -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." }] } ] });