Skip to content

Commit

Permalink
New: add fixer for rule "no-prototype-builtins" (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Feb 12, 2019
1 parent dfb1379 commit 2276eb6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/rules/no-prototype-builtins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @fileoverview add fixer to rule no-prototype-builtins.
* @author Pig Fang<g-plane@hotmail.com>
*/
"use strict";

const ruleComposer = require("eslint-rule-composer");
const utils = require("../utils");

const rule = utils.getFixableRule("no-prototype-builtins");

module.exports = ruleComposer.mapReports(
rule,
(problem, { sourceCode }) => {
problem.fix = fixer => {
const { node } = problem;
const prop = node.callee.property.name;
const args = node.arguments.map(arg => sourceCode.getText(arg)).join(", ");

return fixer.replaceText(
node,
`Object.prototype.${prop}.call(${sourceCode.getText(node.callee.object)}, ${args})`
);
};
return problem;
}
);
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Name | ✔️ | 🛠 | Description
[no-console](https://eslint.org/docs/rules/no-console) | ✔️ | 🛠 | disallow the use of `console`
[no-debugger](https://eslint.org/docs/rules/no-debugger) | ✔️ | 🛠 | disallow the use of `debugger`
[no-plusplus](https://eslint.org/docs/rules/no-plusplus) | ✔️ | 🛠 | disallow the unary operators `++` and `--`
[no-prototype-builtins](https://eslint.org/docs/rules/no-prototype-builtins) | | 🛠 | disallow calling some `Object.prototype` methods directly on objects
[no-useless-concat](https://eslint.org/docs/rules/no-useless-concat) | | 🛠 | disallow unnecessary concatenation of literals or template literals
[prefer-spread](https://eslint.org/docs/rules/prefer-spread) | | 🛠 | require spread operators instead of `.apply()`
[radix](https://eslint.org/docs/rules/radix) | | 🛠 | enforce the consistent use of the radix argument when using `parseInt()`
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/no-prototype-builtins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @fileoverview Tests for rule no-prototype-builtins
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

const rule = require("../../../lib/rules/no-prototype-builtins");
const RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const errors = [{ type: "CallExpression" }];

ruleTester.run("no-prototype-builtins", rule, {
valid: [
"Object.prototype.hasOwnProperty.call(foo, \"bar\")"
],
invalid: [
{
code: "foo.hasOwnProperty('bar')",
output: "Object.prototype.hasOwnProperty.call(foo, 'bar')",
errors
},
{
code: "foo.bar.hasOwnProperty('baz')",
output: "Object.prototype.hasOwnProperty.call(foo.bar, 'baz')",
errors
},
{
code: "foo.isPrototypeOf('bar')",
output: "Object.prototype.isPrototypeOf.call(foo, 'bar')",
errors
},
{
code: "foo.propertyIsEnumerable('bar')",
output: "Object.prototype.propertyIsEnumerable.call(foo, 'bar')",
errors
}
]
});

0 comments on commit 2276eb6

Please sign in to comment.