blob: 95a562c4a2f4de618cf7aeed647b5a049b2f6e4d [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Rule to
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Helpers
10//------------------------------------------------------------------------------
11
12/**
13 * Gets the variable object of `arguments` which is defined implicitly.
14 * @param {eslint-scope.Scope} scope - A scope to get.
15 * @returns {eslint-scope.Variable} The found variable object.
16 */
17function getVariableOfArguments(scope) {
18 const variables = scope.variables;
19
20 for (let i = 0; i < variables.length; ++i) {
21 const variable = variables[i];
22
23 if (variable.name === "arguments") {
24
25 /*
26 * If there was a parameter which is named "arguments", the implicit "arguments" is not defined.
27 * So does fast return with null.
28 */
29 return (variable.identifiers.length === 0) ? variable : null;
30 }
31 }
32
33 /* istanbul ignore next : unreachable */
34 return null;
35}
36
37/**
38 * Checks if the given reference is not normal member access.
39 *
40 * - arguments .... true // not member access
41 * - arguments[i] .... true // computed member access
42 * - arguments[0] .... true // computed member access
43 * - arguments.length .... false // normal member access
44 *
45 * @param {eslint-scope.Reference} reference - The reference to check.
46 * @returns {boolean} `true` if the reference is not normal member access.
47 */
48function isNotNormalMemberAccess(reference) {
49 const id = reference.identifier;
50 const parent = id.parent;
51
52 return !(
53 parent.type === "MemberExpression" &&
54 parent.object === id &&
55 !parent.computed
56 );
57}
58
59//------------------------------------------------------------------------------
60// Rule Definition
61//------------------------------------------------------------------------------
62
63module.exports = {
64 meta: {
65 type: "suggestion",
66
67 docs: {
68 description: "require rest parameters instead of `arguments`",
69 category: "ECMAScript 6",
70 recommended: false,
71 url: "https://eslint.org/docs/rules/prefer-rest-params"
72 },
73
74 schema: []
75 },
76
77 create(context) {
78
79 /**
80 * Reports a given reference.
81 *
82 * @param {eslint-scope.Reference} reference - A reference to report.
83 * @returns {void}
84 */
85 function report(reference) {
86 context.report({
87 node: reference.identifier,
88 loc: reference.identifier.loc,
89 message: "Use the rest parameters instead of 'arguments'."
90 });
91 }
92
93 /**
94 * Reports references of the implicit `arguments` variable if exist.
95 *
96 * @returns {void}
97 */
98 function checkForArguments() {
99 const argumentsVar = getVariableOfArguments(context.getScope());
100
101 if (argumentsVar) {
102 argumentsVar
103 .references
104 .filter(isNotNormalMemberAccess)
105 .forEach(report);
106 }
107 }
108
109 return {
110 "FunctionDeclaration:exit": checkForArguments,
111 "FunctionExpression:exit": checkForArguments
112 };
113 }
114};