blob: 3a28584f6bc1263d39fdf81d7ce8eafea30e3b3b [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.
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +010014 * @param {eslint-scope.Scope} scope A scope to get.
Yang Guo4fd355c2019-09-19 10:59:03 +020015 * @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
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +010044 * @param {eslint-scope.Reference} reference The reference to check.
Yang Guo4fd355c2019-09-19 10:59:03 +020045 * @returns {boolean} `true` if the reference is not normal member access.
46 */
47function isNotNormalMemberAccess(reference) {
48 const id = reference.identifier;
49 const parent = id.parent;
50
51 return !(
52 parent.type === "MemberExpression" &&
53 parent.object === id &&
54 !parent.computed
55 );
56}
57
58//------------------------------------------------------------------------------
59// Rule Definition
60//------------------------------------------------------------------------------
61
62module.exports = {
63 meta: {
64 type: "suggestion",
65
66 docs: {
67 description: "require rest parameters instead of `arguments`",
68 category: "ECMAScript 6",
69 recommended: false,
70 url: "https://eslint.org/docs/rules/prefer-rest-params"
71 },
72
73 schema: []
74 },
75
76 create(context) {
77
78 /**
79 * Reports a given reference.
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +010080 * @param {eslint-scope.Reference} reference A reference to report.
Yang Guo4fd355c2019-09-19 10:59:03 +020081 * @returns {void}
82 */
83 function report(reference) {
84 context.report({
85 node: reference.identifier,
86 loc: reference.identifier.loc,
87 message: "Use the rest parameters instead of 'arguments'."
88 });
89 }
90
91 /**
92 * Reports references of the implicit `arguments` variable if exist.
Yang Guo4fd355c2019-09-19 10:59:03 +020093 * @returns {void}
94 */
95 function checkForArguments() {
96 const argumentsVar = getVariableOfArguments(context.getScope());
97
98 if (argumentsVar) {
99 argumentsVar
100 .references
101 .filter(isNotNormalMemberAccess)
102 .forEach(report);
103 }
104 }
105
106 return {
107 "FunctionDeclaration:exit": checkForArguments,
108 "FunctionExpression:exit": checkForArguments
109 };
110 }
111};