Skip to content

Commit

Permalink
Update: prefer-rest-params relax for member accesses (fixes #5990) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and ilyavolodin committed Aug 12, 2016
1 parent df01c4f commit ebf8441
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
32 changes: 29 additions & 3 deletions lib/rules/prefer-rest-params.js
Expand Up @@ -32,6 +32,28 @@ function getVariableOfArguments(scope) {
return null;
}

/**
* Checks if the given reference is not normal member access.
*
* - arguments .... true // not member access
* - arguments[i] .... true // computed member access
* - arguments[0] .... true // computed member access
* - arguments.length .... false // normal member access
*
* @param {escope.Reference} reference - The reference to check.
* @returns {boolean} `true` if the reference is not normal member access.
*/
function isNotNormalMemberAccess(reference) {
const id = reference.identifier;
const parent = id.parent;

return !(
parent.type === "MemberExpression" &&
parent.object === id &&
!parent.computed
);
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -58,6 +80,7 @@ module.exports = {
function report(reference) {
context.report({
node: reference.identifier,
loc: reference.identifier.loc,
message: "Use the rest parameters instead of 'arguments'."
});
}
Expand All @@ -71,13 +94,16 @@ module.exports = {
const argumentsVar = getVariableOfArguments(context.getScope());

if (argumentsVar) {
argumentsVar.references.forEach(report);
argumentsVar
.references
.filter(isNotNormalMemberAccess)
.forEach(report);
}
}

return {
FunctionDeclaration: checkForArguments,
FunctionExpression: checkForArguments
"FunctionDeclaration:exit": checkForArguments,
"FunctionExpression:exit": checkForArguments
};
}
};
9 changes: 7 additions & 2 deletions tests/lib/rules/prefer-rest-params.js
Expand Up @@ -20,9 +20,14 @@ ruleTester.run("prefer-rest-params", rule, {
"function foo(arguments) { arguments; }",
"function foo() { var arguments; arguments; }",
{code: "var foo = () => arguments;", parserOptions: { ecmaVersion: 6 }}, // Arrows don't have "arguments".,
{code: "function foo(...args) { args; }", parserOptions: { ecmaVersion: 6 }}
{code: "function foo(...args) { args; }", parserOptions: { ecmaVersion: 6 }},
"function foo() { arguments.length; }",
"function foo() { arguments.callee; }",
],
invalid: [
{code: "function foo() { arguments; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of 'arguments'."}]}
{code: "function foo() { arguments; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of 'arguments'."}]},
{code: "function foo() { arguments[0]; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of 'arguments'."}]},
{code: "function foo() { arguments[1]; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of 'arguments'."}]},
{code: "function foo() { arguments[Symbol.iterator]; }", errors: [{type: "Identifier", message: "Use the rest parameters instead of 'arguments'."}]},
]
});

0 comments on commit ebf8441

Please sign in to comment.