-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d23f231
commit 5d0a3d1
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
}); |