diff --git a/lib/rules/no-shadow-restricted-names.js b/lib/rules/no-shadow-restricted-names.js index 865cee46ad7..70052f56f2d 100644 --- a/lib/rules/no-shadow-restricted-names.js +++ b/lib/rules/no-shadow-restricted-names.js @@ -24,36 +24,19 @@ module.exports = { const RESTRICTED = ["undefined", "NaN", "Infinity", "arguments", "eval"]; - /** - * Check if the node name is present inside the restricted list - * @param {ASTNode} id id to evaluate - * @returns {void} - * @private - */ - function checkForViolation(id) { - if (RESTRICTED.indexOf(id.name) > -1) { - context.report({ - node: id, - message: "Shadowing of global property '{{idName}}'.", - data: { - idName: id.name - } - }); - } - } - return { - VariableDeclarator(node) { - checkForViolation(node.id); - }, - ":function"(node) { - if (node.id) { - checkForViolation(node.id); + "VariableDeclaration, :function, CatchClause"(node) { + for (const variable of context.getDeclaredVariables(node)) { + if (variable.defs.length > 0 && RESTRICTED.includes(variable.name)) { + context.report({ + node: variable.defs[0].name, + message: "Shadowing of global property '{{idName}}'.", + data: { + idName: variable.name + } + }); + } } - node.params.forEach(checkForViolation); - }, - "CatchClause[param!=null]"(node) { - checkForViolation(node.param); } }; diff --git a/tests/lib/rules/no-shadow-restricted-names.js b/tests/lib/rules/no-shadow-restricted-names.js index 4655bf715df..3c014a70f5a 100644 --- a/tests/lib/rules/no-shadow-restricted-names.js +++ b/tests/lib/rules/no-shadow-restricted-names.js @@ -93,6 +93,23 @@ ruleTester.run("no-shadow-restricted-names", rule, { { message: "Shadowing of global property 'eval'.", type: "Identifier" }, { message: "Shadowing of global property 'eval'.", type: "Identifier" } ] + }, + { + code: "var [undefined] = [1]", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { message: "Shadowing of global property 'undefined'.", type: "Identifier" } + ] + }, + { + code: "var {undefined} = obj; var {a: undefined} = obj; var {a: {b: {undefined}}} = obj; var {a, ...undefined} = obj;", + parserOptions: { ecmaVersion: 9 }, + errors: [ + { message: "Shadowing of global property 'undefined'.", type: "Identifier" }, + { message: "Shadowing of global property 'undefined'.", type: "Identifier" }, + { message: "Shadowing of global property 'undefined'.", type: "Identifier" }, + { message: "Shadowing of global property 'undefined'.", type: "Identifier" } + ] } ] });