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: Add suggestions for no-prototype-builtins #17677

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 67 additions & 2 deletions lib/rules/no-prototype-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Returns true if the node or any of the objects
* to the left of it in the member/call chain is optional.
*
* e.g. `a?.b`, `a?.b.c`, `a?.()`, `a()?.()`
* @param {ASTNode} node The expression to check
* @returns {boolean} `true` if there is a short-circuiting optional `?.`
* in the same option chain to the left of this call or member expression,
* or the node itself is an optional call or member `?.`.
*/
function isAfterOptional(node) {
let leftNode;

if (node.type === "MemberExpression") {
leftNode = node.object;
} else if (node.type === "CallExpression") {
leftNode = node.callee;
} else {
return false;
}
if (node.optional) {
return true;
}
return isAfterOptional(leftNode);
}


//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -25,10 +56,13 @@ module.exports = {
url: "https://eslint.org/docs/latest/rules/no-prototype-builtins"
},

hasSuggestions: true,

schema: [],

messages: {
prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object."
prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object.",
callObjectPrototype: "Call Object.prototype.{{prop}} explicitly."
}
},

Expand Down Expand Up @@ -59,7 +93,38 @@ module.exports = {
messageId: "prototypeBuildIn",
loc: callee.property.loc,
data: { prop: propName },
node
node,
suggest: [
{
messageId: "callObjectPrototype",
data: { prop: propName },
fix(fixer) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

/*
* a call after an optional chain (e.g. a?.b.hasOwnProperty(c))
* must be fixed manually because the call can be short-circuited
*/
if (isAfterOptional(node)) {
return null;
}

const sourceCode = context.sourceCode;
const openParenToken = sourceCode.getTokenAfter(
node.callee,
astUtils.isOpeningParenToken
);
const objectText = sourceCode.getText(callee.object);
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
const isEmptyParameters = node.arguments.length === 0;
const delim = isEmptyParameters ? "" : ", ";
const fixes = [
fixer.replaceText(callee, `Object.prototype.${propName}.call`),
fixer.insertTextAfter(openParenToken, objectText + delim)
];

return fixes;
}
}
]
});
}
}
Expand Down
129 changes: 125 additions & 4 deletions tests/lib/rules/no-prototype-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 19,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.hasOwnProperty.call(foo, 'bar')"
}
],
type: "CallExpression"
}]
},
Expand All @@ -73,6 +79,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 18,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.isPrototypeOf.call(foo, 'bar')"
}
],
type: "CallExpression"
}]
},
Expand All @@ -84,6 +96,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endLine: 1,
endColumn: 25,
messageId: "prototypeBuildIn",
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.propertyIsEnumerable.call(foo, 'bar')"
}
],
data: { prop: "propertyIsEnumerable" }
}]
},
Expand All @@ -96,6 +114,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 23,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.hasOwnProperty.call(foo.bar, 'bar')"
}
],
type: "CallExpression"
}]
},
Expand All @@ -108,6 +132,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 26,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.isPrototypeOf.call(foo.bar.baz, 'bar')"
}
],
type: "CallExpression"
}]
},
Expand All @@ -120,6 +150,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 21,
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.hasOwnProperty.call(foo, 'bar')"
}
],
type: "CallExpression"
}]
},
Expand All @@ -133,6 +169,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 20,
messageId: "prototypeBuildIn",
data: { prop: "isPrototypeOf" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.isPrototypeOf.call(foo, 'bar').baz"
}
],
type: "CallExpression"
}]
},
Expand All @@ -145,6 +187,12 @@ ruleTester.run("no-prototype-builtins", rule, {
endColumn: 31,
messageId: "prototypeBuildIn",
data: { prop: "propertyIsEnumerable" },
suggestions: [
{
messageId: "callObjectPrototype",
output: String.raw`Object.prototype.propertyIsEnumerable.call(foo.bar, 'baz')`
}
],
type: "CallExpression"
}]
},
Expand All @@ -153,22 +201,95 @@ ruleTester.run("no-prototype-builtins", rule, {
{
code: "foo?.hasOwnProperty('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{
code: "foo?.bar.hasOwnProperty('baz')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{
code: "foo.hasOwnProperty?.('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{

/*
* if hasOwnProperty is part of a ChainExpresion
* and the optional part is before it, then don't suggest the fix
*/
code: "foo?.hasOwnProperty('bar').baz",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{

/*
* if hasOwnProperty is part of a ChainExpresion
* but the optional part is after it, then the fix is safe
*/
code: "foo.hasOwnProperty('bar')?.baz",
parserOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{
messageId: "callObjectPrototype",
output: "Object.prototype.hasOwnProperty.call(foo, 'bar')?.baz"
}
]
}]
},
{
code: "(foo?.hasOwnProperty)('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{

/*
* Note that the original may throw TypeError: (intermediate value) is not a function
* whereas the replacement may throw TypeError: Cannot convert undefined or null to object
*/
messageId: "callObjectPrototype",
output: "(Object.prototype.hasOwnProperty.call)(foo, 'bar')"
}
]
}]
},
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better not to provide the suggestion in cases like this (node.callee is a ChainExpression).

This is a possible error reported by no-unsafe-optional-chaining and should be fixed manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks I stopped giving the suggestion when node.callee.type === "ChainExpression". I do wonder whether we should remove the const callee = astUtils.skipChainExpression(node.callee); above and just not report any no-prototype-builtins for (foo?.hasOwnProperty)('bar') in favor of no-unsafe-optional-chaining.

Copy link
Member

Choose a reason for hiding this comment

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

I do wonder whether we should remove the const callee = astUtils.skipChainExpression(node.callee); above and just not report any no-prototype-builtins for (foo?.hasOwnProperty)('bar') in favor of no-unsafe-optional-chaining.

I think it's fine for both rules to report this code as those are two different problems.

{
code: "(foo?.hasOwnProperty)?.('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{
code: "foo?.['hasOwnProperty']('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{
code: "(foo?.[`hasOwnProperty`])('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [
{

/*
* Note that the original may throw TypeError: (intermediate value) is not a function
* whereas the replacement may throw TypeError: Cannot convert undefined or null to object
*/
messageId: "callObjectPrototype",
output: "(Object.prototype.hasOwnProperty.call)(foo, 'bar')"
}
]

}]
}
]
});