forked from eslint/eslint
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
cbdc73b
commit 8424eca
Showing
5 changed files
with
157 additions
and
1 deletion.
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
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,54 @@ | ||
# Suggest using the rest parameters instead of `arguments` (prefer-rest-params) | ||
|
||
There is rest parameters in ES2015. | ||
We can use that feature for variadic functions instead of `arguments` variable. | ||
|
||
`arguments` does not have methods of `Array.prototype`, so it's inconvenience a bit. | ||
|
||
## Rule Details | ||
|
||
This rule is aimed to flag usage of `arguments` variables. | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
function foo() { | ||
console.log(arguments); /*error Use the rest parameters instead of "arguments". */ | ||
} | ||
|
||
function foo(action) { | ||
var args = [].slice.call(arguments, 1); /*error Use the rest parameters instead of "arguments". */ | ||
action.apply(null, args); | ||
} | ||
``` | ||
|
||
The following patterns are considered not problems: | ||
|
||
```js | ||
function foo(...args) { | ||
console.log(args); | ||
} | ||
|
||
function foo(action, ...args) { | ||
action.apply(null, args); // or `action(...args)`, related with the `prefer-spread` rule. | ||
} | ||
|
||
// Note: the implicit arguments can be overwritten. | ||
function foo(arguments) { | ||
console.log(arguments); // This is the first argument. | ||
} | ||
function foo() { | ||
var arguments = 0; | ||
console.log(arguments); // This is a local variable. | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
This rule should not be used in ES3/5 environments. | ||
|
||
In ES2015 (ES6) or later, if you don't want to be notified about `arguments` variables, then it's safe to disable this rule. | ||
|
||
## Related rules | ||
|
||
* [prefer-spread](prefer-spread.md) |
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,71 @@ | ||
/** | ||
* @fileoverview Rule to | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Gets the variable object of `arguments` which is defined implicitly. | ||
* @param {escope.Scope} scope - A scope to get. | ||
* @returns {escope.Variable} The found variable object. | ||
*/ | ||
function getVariableOfArguments(scope) { | ||
var variables = scope.variables; | ||
for (var i = 0; i < variables.length; ++i) { | ||
var variable = variables[i]; | ||
if (variable.name === "arguments") { | ||
// If there was a parameter which is named "arguments", the implicit "arguments" is not defined. | ||
// So does fast return with null. | ||
return (variable.identifiers.length === 0) ? variable : null; | ||
} | ||
} | ||
|
||
/* istanbul ignore next : unreachable */ | ||
return null; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
/** | ||
* Reports a given reference. | ||
* | ||
* @param {escope.Reference} reference - A reference to report. | ||
* @returns {void} | ||
*/ | ||
function report(reference) { | ||
context.report({ | ||
node: reference.identifier, | ||
message: "Use the rest parameters instead of \"arguments\"." | ||
}); | ||
} | ||
|
||
/** | ||
* Reports references of the implicit `arguments` variable if exist. | ||
* | ||
* @returns {void} | ||
*/ | ||
function checkForArguments() { | ||
var argumentsVar = getVariableOfArguments(context.getScope()); | ||
if (argumentsVar) { | ||
argumentsVar.references.forEach(report); | ||
} | ||
} | ||
|
||
return { | ||
FunctionDeclaration: checkForArguments, | ||
FunctionExpression: checkForArguments | ||
}; | ||
}; | ||
|
||
module.exports.schema = []; |
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,29 @@ | ||
/** | ||
* @fileoverview Tests for prefer-rest-params rule. | ||
* @author Toru Nagashima | ||
* @copyright 2015 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../../../lib/rules/prefer-rest-params"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run("prefer-rest-params", rule, { | ||
valid: [ | ||
"arguments;", | ||
"function foo(arguments) { arguments; }", | ||
"function foo() { var arguments; arguments; }", | ||
{code: "var foo = () => arguments;", ecmaFeatures: {arrowFunctions: true}}, // Arrows don't have "arguments"., | ||
{code: "function foo(...args) { args; }", ecmaFeatures: {restParams: true}} | ||
], | ||
invalid: [ | ||
{code: "function foo() { arguments; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of \"arguments\"."}]} | ||
] | ||
}); |