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 all commits
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
92 changes: 90 additions & 2 deletions lib/rules/no-prototype-builtins.js
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,61 @@ 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
const sourceCode = context.sourceCode;

/*
* 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;
}

/*
* A call on a ChainExpression (e.g. (a?.hasOwnProperty)(c)) will trigger
* no-unsafe-optional-chaining which should be fixed before this suggestion
*/
if (node.callee.type === "ChainExpression") {
return null;
}

const objectVariable = astUtils.getVariableByName(sourceCode.getScope(node), "Object");

/*
* We can't use Object if the global Object was shadowed,
* or Object does not exist in the global scope for some reason
*/
if (!objectVariable || objectVariable.scope.type !== "global" || objectVariable.defs.length > 0) {
return null;
}

let objectText = sourceCode.getText(callee.object);

if (astUtils.getPrecedence(callee.object) <= astUtils.getPrecedence({ type: "SequenceExpression" })) {
objectText = `(${objectText})`;
}

const openParenToken = sourceCode.getTokenAfter(
node.callee,
astUtils.isOpeningParenToken
);
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
136 changes: 132 additions & 4 deletions tests/lib/rules/no-prototype-builtins.js
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,30 +187,116 @@ 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"
}]
},
{

// Can't suggest Object.prototype when Object is shadowed
code: "(function(Object) {return foo.hasOwnProperty('bar');})",
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{
code: "foo.hasOwnProperty('bar')",
globals: {
Object: "off"
},
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }],
name: "Can't suggest Object.prototype when there is no Object global variable"
},

// Optional chaining
{
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: "(a,b).hasOwnProperty('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "prototypeBuildIn",
data: { prop: "hasOwnProperty" },
suggestions: [

// Make sure the SequenceExpression has parentheses before other arguments
{
messageId: "callObjectPrototype",
output: "Object.prototype.hasOwnProperty.call((a,b), 'bar')"
}
]
}]
},
{

// No suggestion where no-unsafe-optional-chaining is reported on the call
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" }, suggestions: [] }]
},
{
code: "foo?.['hasOwnProperty']('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
},
{

// No suggestion where no-unsafe-optional-chaining is reported on the call
code: "(foo?.[`hasOwnProperty`])('bar')",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" } }]
errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
}
]
});