Skip to content

Commit

Permalink
update no-prototype-builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 17, 2020
1 parent e13059f commit 5286f83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/rules/no-prototype-builtins.js
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -39,15 +45,19 @@ module.exports = {
* @returns {void}
*/
function disallowBuiltIns(node) {
if (node.callee.type !== "MemberExpression" || node.callee.computed) {

// TODO: just use `astUtils.getStaticPropertyName(node.callee)`
const callee = astUtils.skipChainExpression(node.callee);

if (callee.type !== "MemberExpression" || callee.computed) {
return;
}
const propName = node.callee.property.name;
const propName = callee.property.name;

if (DISALLOWED_PROPS.indexOf(propName) > -1) {
context.report({
messageId: "prototypeBuildIn",
loc: node.callee.property.loc,
loc: callee.property.loc,
data: { prop: propName },
node
});
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-prototype-builtins.js
Expand Up @@ -94,6 +94,18 @@ const invalid = [
data: { prop: "isPrototypeOf" },
type: "CallExpression"
}]
},

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

Expand Down

0 comments on commit 5286f83

Please sign in to comment.