Skip to content

Commit

Permalink
added eslint rule to catch prototype method usage sinonjs#1826
Browse files Browse the repository at this point in the history
this rule started life as a direct copy of the built-in
ESLint rule:

https://eslint.org/docs/rules/no-prototype-builtins
  • Loading branch information
fearphage committed Jul 11, 2018
1 parent 421bb3d commit d5a79ca
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends: 'eslint-config-sinon'
extends: sinon

globals:
ArrayBuffer: false
Expand All @@ -9,9 +9,11 @@ globals:

plugins:
- ie11
- local-rules

rules:
ie11/no-collection-args: error
ie11/no-for-in-const: error
ie11/no-loop-func: warn
ie11/no-weak-collections: error
local-rules/no-prototype-methods: error
75 changes: 75 additions & 0 deletions eslint-local-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";

function getPrototypeMethods(prototype) {
/* eslint-disable local-rules/no-prototype-methods */
return Object.getOwnPropertyNames(prototype).filter(function (name) {
return typeof prototype[name] === "function" && prototype.hasOwnProperty(name);
});
}

var DISALLOWED_ARRAY_PROPS = getPrototypeMethods(Array.prototype);

var DISALLOWED_OBJECT_PROPS = getPrototypeMethods(Object.prototype);

module.exports = {
// rule to disallow direct use of prototype methods of builtins
"no-prototype-methods": {
meta: {
docs: {
description: "disallow calling prototype methods directly",
category: "Possible Errors",
recommended: false,
url: "https://eslint.org/docs/rules/no-prototype-builtins"
},

schema: []
},

create: function (context) {
/**
* Reports if a disallowed property is used in a CallExpression
* @param {ASTNode} node The CallExpression node.
* @returns {void}
*/
function disallowBuiltIns(node) {
if (
node.callee.type !== "MemberExpression"
|| node.callee.computed
// allow static method calls
|| node.callee.object.name === "Array"
|| node.callee.object.name === "Object"
) {
return;
}
var propName = node.callee.property.name;

if (DISALLOWED_OBJECT_PROPS.indexOf(propName) > -1) {
context.report({
message: "Do not access {{obj}} prototype method '{{prop}}' from target object.",
loc: node.callee.property.loc.start,
data: {
obj: "Object",
prop: propName
},
node: node
});
}
else if (DISALLOWED_ARRAY_PROPS.indexOf(propName) > -1) {
context.report({
message: "Do not access {{obj}} prototype method '{{prop}}' from target object.",
loc: node.callee.property.loc.start,
data: {
obj: "Array",
prop: propName
},
node: node
});
}
}

return {
CallExpression: disallowBuiltIns
};
}
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"eslint": "^4.19.1",
"eslint-config-sinon": "^1.0.0",
"eslint-plugin-ie11": "^1.0.0",
"eslint-plugin-local-rules": "^0.1.0",
"eslint-plugin-mocha": "^4.2.0",
"esm": "3.0.37",
"husky": "^0.14.2",
Expand Down

0 comments on commit d5a79ca

Please sign in to comment.