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
57 changes: 28 additions & 29 deletions lib/rules/no-restricted-properties.js
Expand Up @@ -142,40 +142,39 @@ 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;

if (node.parent.type === "VariableDeclarator") {
objectName = node.parent.init && node.parent.init.type === "Identifier" ? node.parent.init.name : "";
} else if ((node.parent.type === "AssignmentExpression" || node.parent.type === "AssignmentPattern") && node.parent.right.type === "Identifier") {
objectName = node.parent.right.name;
} else {
objectName = "";
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
},
AssignmentExpression: checkDestructuringAssignment,
AssignmentPattern: checkDestructuringAssignment

if (restrictedProperties.has(objectName)) {
node.properties.forEach(property => {
const propertyName = astUtils.getStaticPropertyName(property);

checkPropertyAccess(node, objectName, propertyName);
});
} else if (globallyRestrictedObjects.has(objectName)) {
const propertyName = astUtils.getStaticPropertyName(node.properties[0]);

checkPropertyAccess(node, objectName, propertyName);
} else {
node.properties.forEach(property => {
const propertyName = astUtils.getStaticPropertyName(property);

checkPropertyAccess(node, "", propertyName);
});
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
};
}
};
140 changes: 136 additions & 4 deletions tests/lib/rules/no-restricted-properties.js
Expand Up @@ -113,10 +113,6 @@ ruleTester.run("no-restricted-properties", rule, {
code: "let {unrelated} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
}, {
code: "let {baz: {bar: qux}} = foo;",
options: [{ object: "foo", property: "bar" }],
parserOptions: { ecmaVersion: 6 }
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}, {
code: "let {bar} = foo.baz;",
options: [{ object: "foo", property: "bar" }],
Expand Down Expand Up @@ -173,6 +169,10 @@ ruleTester.run("no-restricted-properties", rule, {
code: "class C { #foo; foo() { this.#foo; } }",
options: [{ property: "#foo" }],
parserOptions: { ecmaVersion: 2022 }
}, {
code: "var { foo: { prop } } = obj;",
options: [{ object: "obj", property: "prop" }],
parserOptions: { ecmaVersion: 6 }
}
],

Expand Down 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: ""
}
}]
}
]
});