Skip to content

Commit

Permalink
New: "no-proto" (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and aladdin-add committed Feb 25, 2019
1 parent c26a399 commit d23f231
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/rules/no-proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @fileoverview Add fixer to rule no-proto.
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

const rule = utils.getFixableRule("no-proto", false);

module.exports = ruleComposer.mapReports(
rule,
(problem, { sourceCode }) => {
problem.fix = fixer => {
const { node } = problem;

return fixer.replaceText(node, `Object.getPrototypeOf(${sourceCode.getText(node.object)})`);
};
return problem;
}
);
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Name | ✔️ | 🛠 | Description
[no-eq-null](https://eslint.org/docs/rules/no-eq-null) | | 🛠 | disallow `null` comparisons without type-checking operators
[no-new-symbol](https://eslint.org/docs/rules/no-new-symbol) | | 🛠 | disallow `new` operators with the `Symbol` object
[no-plusplus](https://eslint.org/docs/rules/no-plusplus) | ✔️ | 🛠 | disallow the unary operators `++` and `--`
[no-proto](https://eslint.org/docs/rules/no-proto) | | 🛠 | disallow the use of the `__proto__` property
[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()`
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/no-proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @fileoverview Tests for rule no-proto.
* @author Pig Fang <g-plane@hotmail.com>
*/
"use strict";

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

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

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

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

ruleTester.run("no-proto", rule, {
valid: [
"Object.getPrototypeOf(obj)"
],
invalid: [
{
code: "obj.__proto__",
output: "Object.getPrototypeOf(obj)",
errors
},
{
code: "obj['__proto__']",
output: "Object.getPrototypeOf(obj)",
errors
}
]
});

0 comments on commit d23f231

Please sign in to comment.