Skip to content

Commit

Permalink
New: "no-caller" (#38)
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 d23f231 commit 5d0a3d1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/rules/no-caller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @fileoverview Add fixer to rule no-caller.
* @author Pig Fang <[email protected]>
*/
"use strict";

const ruleComposer = require("eslint-rule-composer");
const astUtils = require("eslint/lib/util/ast-utils");
const utils = require("../utils");

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

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

if (node.property.name !== "callee") {
return null;
}

const nearestFunction = astUtils.getUpperFunction(node);

if (!nearestFunction) {
return null;
}

const name = nearestFunction.id && nearestFunction.id.name;

if (!name) {
return null;
}

return fixer.replaceText(node, name);
};
return problem;
}
);
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add prefix "autofix" to the rulename in eslintrc:
Name | ✔️ | 🛠 | Description
----- | ----- | ----- | -----
[no-alert](https://eslint.org/docs/rules/no-alert) | | 🛠 | disallow the use of `alert`, `confirm`, and `prompt`
[no-caller](https://eslint.org/docs/rules/no-caller) | | 🛠 | disallow the use of `arguments.caller` or `arguments.callee`
[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-eq-null](https://eslint.org/docs/rules/no-eq-null) | | 🛠 | disallow `null` comparisons without type-checking operators
Expand Down
58 changes: 58 additions & 0 deletions tests/lib/rules/no-caller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @fileoverview Tests for rule no-caller.
* @author Pig Fang <[email protected]>
*/
"use strict";

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

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

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

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

ruleTester.run("no-caller", rule, {
valid: [
"function foo() { foo(); }"
],
invalid: [
{
code: "function foo() { arguments.caller }",
output: null,
errors
},
{
code: "arguments.callee",
output: null,
errors
},
{
code: "var foo = function () { arguments.callee }",
output: null,
errors
},
{
code: "var foo = () => { arguments.callee }",
parserOptions: { ecmaVersion: 6 },
output: null,
errors
},
{
code: "function foo() { arguments.callee }",
output: "function foo() { foo }",
errors
},
{
code: "var foo = function foo() { arguments.callee }",
output: "var foo = function foo() { foo }",
errors
}
]
});

0 comments on commit 5d0a3d1

Please sign in to comment.