Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix no-restricted-properties false negatives with unknown objects #17818

Merged
merged 8 commits into from Dec 14, 2023
4 changes: 4 additions & 0 deletions docs/src/rules/no-restricted-properties.md
Expand Up @@ -96,6 +96,10 @@ disallowedObjectName.disallowedPropertyName(); /*error Disallowed object propert
}] */

foo.__defineGetter__(bar, baz);

const { __defineGetter__ } = qux();

({ __defineGetter__ }) => {};
```

:::
Expand Down
43 changes: 15 additions & 28 deletions lib/rules/no-restricted-properties.js
Expand Up @@ -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));
});
}
};
}
};
132 changes: 132 additions & 0 deletions tests/lib/rules/no-restricted-properties.js
Expand Up @@ -546,6 +546,138 @@ ruleTester.run("no-restricted-properties", rule, {
},
type: "MemberExpression"
}]
}, {
code: "const { bar: { bad } = {} } = foo;",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, ig the ci will fail since the base is an older commit, anything that I should do to fix it ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI checks out from a merge commit, so the tests should pass.

Problem is only linting, as languageOptions should be at the same place where parserOptions were. If you could update that, it would be great.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is only linting, as languageOptions should be at the same place where parserOptions were. If you could update that, it would be great.

2023-12-14T12:03:14.9805284Z /home/runner/work/eslint/eslint/tests/lib/rules/no-restricted-properties.js
2023-12-14T12:03:14.9909087Z ##[error]  559:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9922248Z ##[error]  570:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9927445Z ##[error]  581:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9932354Z ##[error]  592:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9937079Z ##[error]  603:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9941897Z ##[error]  614:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9946759Z ##[error]  625:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9952174Z ##[error]  636:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9957490Z ##[error]  647:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9962312Z ##[error]  658:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9967210Z ##[error]  669:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering
2023-12-14T12:03:14.9972095Z ##[error]  680:13  error  The properties of a test case should be placed in a consistent order: [code, options, languageOptions, errors]  eslint-plugin/test-case-property-ordering

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye, done.

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "const { bar: { bad } } = foo;",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "const { bad } = foo();",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bad } = foo());",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bar: { bad } } = foo);",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bar: { bad } = {} } = foo);",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bad }) => {};",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bad } = {}) => {};",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bad: bar }) => {};",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "({ bar: { bad } = {} }) => {};",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "[{ bad }] = foo;",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}, {
code: "const [{ bad }] = foo;",
options: [{ property: "bad" }],
parserOptions: { ecmaVersion: 6 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parserOptions: { ecmaVersion: 6 },
languageOptions: { ecmaVersion: 6 },

errors: [{
messageId: "restrictedProperty",
data: {
propertyName: "bad",
message: ""
}
}]
}
]
});