source: imaps-frontend/node_modules/eslint/lib/rules/prefer-rest-params.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/**
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 /* c8 ignore next */
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 * @param {eslint-scope.Reference} reference The reference to check.
45 * @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
62/** @type {import('../shared/types').Rule} */
63module.exports = {
64 meta: {
65 type: "suggestion",
66
67 docs: {
68 description: "Require rest parameters instead of `arguments`",
69 recommended: false,
70 url: "https://eslint.org/docs/latest/rules/prefer-rest-params"
71 },
72
73 schema: [],
74
75 messages: {
76 preferRestParams: "Use the rest parameters instead of 'arguments'."
77 }
78 },
79
80 create(context) {
81
82 const sourceCode = context.sourceCode;
83
84 /**
85 * Reports a given reference.
86 * @param {eslint-scope.Reference} reference A reference to report.
87 * @returns {void}
88 */
89 function report(reference) {
90 context.report({
91 node: reference.identifier,
92 loc: reference.identifier.loc,
93 messageId: "preferRestParams"
94 });
95 }
96
97 /**
98 * Reports references of the implicit `arguments` variable if exist.
99 * @param {ASTNode} node The node representing the function.
100 * @returns {void}
101 */
102 function checkForArguments(node) {
103 const argumentsVar = getVariableOfArguments(sourceCode.getScope(node));
104
105 if (argumentsVar) {
106 argumentsVar
107 .references
108 .filter(isNotNormalMemberAccess)
109 .forEach(report);
110 }
111 }
112
113 return {
114 "FunctionDeclaration:exit": checkForArguments,
115 "FunctionExpression:exit": checkForArguments
116 };
117 }
118};
Note: See TracBrowser for help on using the repository browser.