diff --git a/docs/src/rules/no-restricted-properties.md b/docs/src/rules/no-restricted-properties.md index 77eecc61133..2e681733fc2 100644 --- a/docs/src/rules/no-restricted-properties.md +++ b/docs/src/rules/no-restricted-properties.md @@ -11,7 +11,7 @@ Certain properties on objects may be disallowed in a codebase. This is useful fo ## Rule Details -This rule looks for accessing a given property key on a given object name, either when reading the property's value or invoking it as a function. You may specify an optional message to indicate an alternative API or a reason for the restriction. +This rule looks for accessing a given property key on a given object name, either when reading the property's value or invoking it as a function. You may specify an optional message to indicate an alternative API or a reason for the restriction. This rule applies to both properties accessed by dot notation and destructuring. ### Options @@ -96,6 +96,10 @@ disallowedObjectName.disallowedPropertyName(); /*error Disallowed object propert }] */ foo.__defineGetter__(bar, baz); + +const { __defineGetter__ } = qux(); + +({ __defineGetter__ }) => {}; ``` ::: diff --git a/lib/rules/no-restricted-properties.js b/lib/rules/no-restricted-properties.js index b0766326099..acd178ae53e 100644 --- a/lib/rules/no-restricted-properties.js +++ b/lib/rules/no-restricted-properties.js @@ -142,40 +142,27 @@ module.exports = { } } - /** - * Checks property accesses in a destructuring assignment expression, e.g. `var foo; ({foo} = bar);` - * @param {ASTNode} node An AssignmentExpression or AssignmentPattern node - * @returns {undefined} - */ - function checkDestructuringAssignment(node) { - if (node.right.type === "Identifier") { - const objectName = node.right.name; - - if (node.left.type === "ObjectPattern") { - node.left.properties.forEach(property => { - checkPropertyAccess(node.left, objectName, astUtils.getStaticPropertyName(property)); - }); - } - } - } - return { MemberExpression(node) { checkPropertyAccess(node, node.object && node.object.name, astUtils.getStaticPropertyName(node)); }, - VariableDeclarator(node) { - if (node.init && node.init.type === "Identifier") { - const objectName = node.init.name; - - if (node.id.type === "ObjectPattern") { - node.id.properties.forEach(property => { - checkPropertyAccess(node.id, objectName, astUtils.getStaticPropertyName(property)); - }); + ObjectPattern(node) { + let objectName = null; + + if (node.parent.type === "VariableDeclarator") { + if (node.parent.init && node.parent.init.type === "Identifier") { + objectName = node.parent.init.name; + } + } else if (node.parent.type === "AssignmentExpression" || node.parent.type === "AssignmentPattern") { + if (node.parent.right.type === "Identifier") { + objectName = node.parent.right.name; } } - }, - AssignmentExpression: checkDestructuringAssignment, - AssignmentPattern: checkDestructuringAssignment + + node.properties.forEach(property => { + checkPropertyAccess(node, objectName, astUtils.getStaticPropertyName(property)); + }); + } }; } }; diff --git a/tests/lib/rules/no-restricted-properties.js b/tests/lib/rules/no-restricted-properties.js index d470a18da97..01b8adc10d0 100644 --- a/tests/lib/rules/no-restricted-properties.js +++ b/tests/lib/rules/no-restricted-properties.js @@ -546,6 +546,138 @@ ruleTester.run("no-restricted-properties", rule, { }, type: "MemberExpression" }] + }, { + code: "const { bar: { bad } = {} } = foo;", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "const { bar: { bad } } = foo;", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "const { bad } = foo();", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bad } = foo());", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bar: { bad } } = foo);", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bar: { bad } = {} } = foo);", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bad }) => {};", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bad } = {}) => {};", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bad: bar }) => {};", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "({ bar: { bad } = {} }) => {};", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "[{ bad }] = foo;", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] + }, { + code: "const [{ bad }] = foo;", + options: [{ property: "bad" }], + languageOptions: { ecmaVersion: 6 }, + errors: [{ + messageId: "restrictedProperty", + data: { + propertyName: "bad", + message: "" + } + }] } ] });