Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: check destructuring for "no-shadow-restricted-names" (fixes #10467
…) (#10470)

* Fix: check destructing for "no-shadow-restricted-names" (fixes #10467)

* Fix: object destructing for "no-shadow-restricted-names"

* Chore: simplify rule "no-shadow-restricted-names"
  • Loading branch information
g-plane authored and platinumazure committed Jun 15, 2018
1 parent 7a7580b commit d477c5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
39 changes: 11 additions & 28 deletions lib/rules/no-shadow-restricted-names.js
Expand Up @@ -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);
}
};

Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-shadow-restricted-names.js
Expand Up @@ -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" }
]
}
]
});

0 comments on commit d477c5e

Please sign in to comment.