Skip to content

Commit 5d0a3d1

Browse files
g-planealaddin-add
authored andcommitted
New: "no-caller" (#38)
1 parent d23f231 commit 5d0a3d1

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

lib/rules/no-caller.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @fileoverview Add fixer to rule no-caller.
3+
* @author Pig Fang <[email protected]>
4+
*/
5+
"use strict";
6+
7+
const ruleComposer = require("eslint-rule-composer");
8+
const astUtils = require("eslint/lib/util/ast-utils");
9+
const utils = require("../utils");
10+
11+
const rule = utils.getFixableRule("no-caller", false);
12+
13+
module.exports = ruleComposer.mapReports(
14+
rule,
15+
problem => {
16+
problem.fix = fixer => {
17+
const { node } = problem;
18+
19+
if (node.property.name !== "callee") {
20+
return null;
21+
}
22+
23+
const nearestFunction = astUtils.getUpperFunction(node);
24+
25+
if (!nearestFunction) {
26+
return null;
27+
}
28+
29+
const name = nearestFunction.id && nearestFunction.id.name;
30+
31+
if (!name) {
32+
return null;
33+
}
34+
35+
return fixer.replaceText(node, name);
36+
};
37+
return problem;
38+
}
39+
);

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add prefix "autofix" to the rulename in eslintrc:
3636
Name | ✔️ | 🛠 | Description
3737
----- | ----- | ----- | -----
3838
[no-alert](https://eslint.org/docs/rules/no-alert) | | 🛠 | disallow the use of `alert`, `confirm`, and `prompt`
39+
[no-caller](https://eslint.org/docs/rules/no-caller) | | 🛠 | disallow the use of `arguments.caller` or `arguments.callee`
3940
[no-console](https://eslint.org/docs/rules/no-console) | ✔️ | 🛠 | disallow the use of `console`
4041
[no-debugger](https://eslint.org/docs/rules/no-debugger) | ✔️ | 🛠 | disallow the use of `debugger`
4142
[no-eq-null](https://eslint.org/docs/rules/no-eq-null) | | 🛠 | disallow `null` comparisons without type-checking operators

tests/lib/rules/no-caller.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @fileoverview Tests for rule no-caller.
3+
* @author Pig Fang <[email protected]>
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
const rule = require("../../../lib/rules/no-caller");
12+
const RuleTester = require("eslint").RuleTester;
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester();
19+
const errors = [{ type: "MemberExpression" }];
20+
21+
ruleTester.run("no-caller", rule, {
22+
valid: [
23+
"function foo() { foo(); }"
24+
],
25+
invalid: [
26+
{
27+
code: "function foo() { arguments.caller }",
28+
output: null,
29+
errors
30+
},
31+
{
32+
code: "arguments.callee",
33+
output: null,
34+
errors
35+
},
36+
{
37+
code: "var foo = function () { arguments.callee }",
38+
output: null,
39+
errors
40+
},
41+
{
42+
code: "var foo = () => { arguments.callee }",
43+
parserOptions: { ecmaVersion: 6 },
44+
output: null,
45+
errors
46+
},
47+
{
48+
code: "function foo() { arguments.callee }",
49+
output: "function foo() { foo }",
50+
errors
51+
},
52+
{
53+
code: "var foo = function foo() { arguments.callee }",
54+
output: "var foo = function foo() { foo }",
55+
errors
56+
}
57+
]
58+
});

0 commit comments

Comments
 (0)