@sinonjs/eslint-plugin-no-prototype-methods
Advanced tools
Comparing version 0.1.0 to 0.1.1
# Changes | ||
## 0.1.1 | ||
- [`507c254`](https://github.com/sinonjs/eslint-plugin-no-prototype-methods/commit/507c254d5e535b4e7a0ab8925767b7d3a27de91b) | ||
Add tests for no-prototype-methods | ||
This also fixes a bug, where arrays were not always detected, because of the overlap in prototype methods with object | ||
_Released on 2020-11-26._ | ||
## 0.1.0 | ||
@@ -4,0 +12,0 @@ |
/** | ||
* @fileoverview Disallow direct use of prototype methods of builtins | ||
* @file Disallow direct use of prototype methods of builtins | ||
* @author Morgan Roderick | ||
@@ -17,7 +17,3 @@ */ | ||
// import all rules in lib/rules | ||
module.exports.rules = requireIndex(__dirname + "/rules"); | ||
"use strict"; | ||
function getPrototypeMethods(prototype) { | ||
return Object.getOwnPropertyNames(prototype).filter(function(name) { | ||
return typeof prototype[name] === "function" && prototype.hasOwnProperty(name); | ||
}); | ||
} | ||
var getPrototypeMethods = require("../get-prototype-methods"); | ||
@@ -13,57 +9,67 @@ var DISALLOWED_ARRAY_PROPS = getPrototypeMethods(Array.prototype); | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow calling prototype methods directly", | ||
category: "Possible Errors", | ||
recommended: false, | ||
url: "https://github.com/sinonjs/no-prototype-builtins" | ||
}, | ||
schema: [], | ||
type: 'problem' | ||
meta: { | ||
docs: { | ||
description: "disallow calling prototype methods directly", | ||
category: "Possible Errors", | ||
recommended: false, | ||
url: "https://github.com/sinonjs/no-prototype-builtins", | ||
}, | ||
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; | ||
schema: [], | ||
type: "problem", | ||
}, | ||
create: function (context) { | ||
/** | ||
* Reports if a disallowed property is used in a CallExpression | ||
* | ||
* @param {object} 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; | ||
var isArray = | ||
(node.callee.object.object && | ||
node.callee.object.object.name === "Array") || | ||
node.callee.object.type === "ArrayExpression"; | ||
var isObject = | ||
node.callee.object.object && | ||
node.callee.object.object.name === "Object"; | ||
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 | ||
}); | ||
} | ||
} | ||
if (isObject && DISALLOWED_OBJECT_PROPS.indexOf(propName) > -1) { | ||
context.report({ | ||
message: | ||
"Do not access Object prototype method '{{prop}}' from target object.", | ||
loc: node.callee.property.loc.start, | ||
data: { | ||
prop: propName, | ||
}, | ||
node: node, | ||
}); | ||
} | ||
return { | ||
CallExpression: disallowBuiltIns | ||
}; | ||
if (isArray && DISALLOWED_ARRAY_PROPS.indexOf(propName) > -1) { | ||
context.report({ | ||
message: | ||
"Do not access Array prototype method '{{prop}}' from target object.", | ||
loc: node.callee.property.loc.start, | ||
data: { | ||
prop: propName, | ||
}, | ||
node: node, | ||
}); | ||
} | ||
} | ||
return { | ||
CallExpression: disallowBuiltIns, | ||
}; | ||
}, | ||
}; |
{ | ||
"name": "@sinonjs/eslint-plugin-no-prototype-methods", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Disallow direct use of prototype methods of builtins", | ||
@@ -17,3 +17,4 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha tests --recursive", | ||
"lint": "eslint .", | ||
"test": "mocha tests --recursive --reporter dot", | ||
"version": "changes --commits --footer", | ||
@@ -26,5 +27,12 @@ "postversion": "git push --follow-tags && npm publish --access public" | ||
"devDependencies": { | ||
"@sinonjs/referee-sinon": "^8.0.0", | ||
"@studio/changes": "^2.0.1", | ||
"eslint": "^7.1.0", | ||
"mocha": "^7.2.0" | ||
"eslint-config-prettier": "^6.9.0", | ||
"eslint-config-sinon": "^4.0.0", | ||
"eslint-plugin-jsdoc": "^30.7.6", | ||
"eslint-plugin-mocha": "^6.1.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"mocha": "^7.2.0", | ||
"prettier": "^2.1.2" | ||
}, | ||
@@ -31,0 +39,0 @@ "engines": { |
11892
10
199
10