minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-param-reassign.js
1 /**
2  * @fileoverview Disallow reassignment of function parameters.
3  * @author Nat Burns
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 const stopNodePattern = /(?:Statement|Declaration|Function(?:Expression)?|Program)$/u;
12
13 module.exports = {
14     meta: {
15         type: "suggestion",
16
17         docs: {
18             description: "disallow reassigning `function` parameters",
19             category: "Best Practices",
20             recommended: false,
21             url: "https://eslint.org/docs/rules/no-param-reassign"
22         },
23
24         schema: [
25             {
26                 oneOf: [
27                     {
28                         type: "object",
29                         properties: {
30                             props: {
31                                 enum: [false]
32                             }
33                         },
34                         additionalProperties: false
35                     },
36                     {
37                         type: "object",
38                         properties: {
39                             props: {
40                                 enum: [true]
41                             },
42                             ignorePropertyModificationsFor: {
43                                 type: "array",
44                                 items: {
45                                     type: "string"
46                                 },
47                                 uniqueItems: true
48                             },
49                             ignorePropertyModificationsForRegex: {
50                                 type: "array",
51                                 items: {
52                                     type: "string"
53                                 },
54                                 uniqueItems: true
55                             }
56                         },
57                         additionalProperties: false
58                     }
59                 ]
60             }
61         ]
62     },
63
64     create(context) {
65         const props = context.options[0] && context.options[0].props;
66         const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || [];
67         const ignoredPropertyAssignmentsForRegex = context.options[0] && context.options[0].ignorePropertyModificationsForRegex || [];
68
69         /**
70          * Checks whether or not the reference modifies properties of its variable.
71          * @param {Reference} reference A reference to check.
72          * @returns {boolean} Whether or not the reference modifies properties of its variable.
73          */
74         function isModifyingProp(reference) {
75             let node = reference.identifier;
76             let parent = node.parent;
77
78             while (parent && (!stopNodePattern.test(parent.type) ||
79                     parent.type === "ForInStatement" || parent.type === "ForOfStatement")) {
80                 switch (parent.type) {
81
82                     // e.g. foo.a = 0;
83                     case "AssignmentExpression":
84                         return parent.left === node;
85
86                     // e.g. ++foo.a;
87                     case "UpdateExpression":
88                         return true;
89
90                     // e.g. delete foo.a;
91                     case "UnaryExpression":
92                         if (parent.operator === "delete") {
93                             return true;
94                         }
95                         break;
96
97                     // e.g. for (foo.a in b) {}
98                     case "ForInStatement":
99                     case "ForOfStatement":
100                         if (parent.left === node) {
101                             return true;
102                         }
103
104                         // this is a stop node for parent.right and parent.body
105                         return false;
106
107                     // EXCLUDES: e.g. cache.get(foo.a).b = 0;
108                     case "CallExpression":
109                         if (parent.callee !== node) {
110                             return false;
111                         }
112                         break;
113
114                     // EXCLUDES: e.g. cache[foo.a] = 0;
115                     case "MemberExpression":
116                         if (parent.property === node) {
117                             return false;
118                         }
119                         break;
120
121                     // EXCLUDES: e.g. ({ [foo]: a }) = bar;
122                     case "Property":
123                         if (parent.key === node) {
124                             return false;
125                         }
126
127                         break;
128
129                     // EXCLUDES: e.g. (foo ? a : b).c = bar;
130                     case "ConditionalExpression":
131                         if (parent.test === node) {
132                             return false;
133                         }
134
135                         break;
136
137                     // no default
138                 }
139
140                 node = parent;
141                 parent = node.parent;
142             }
143
144             return false;
145         }
146
147         /**
148          * Tests that an identifier name matches any of the ignored property assignments.
149          * First we test strings in ignoredPropertyAssignmentsFor.
150          * Then we instantiate and test RegExp objects from ignoredPropertyAssignmentsForRegex strings.
151          * @param {string} identifierName A string that describes the name of an identifier to
152          * ignore property assignments for.
153          * @returns {boolean} Whether the string matches an ignored property assignment regular expression or not.
154          */
155         function isIgnoredPropertyAssignment(identifierName) {
156             return ignoredPropertyAssignmentsFor.includes(identifierName) ||
157                 ignoredPropertyAssignmentsForRegex.some(ignored => new RegExp(ignored, "u").test(identifierName));
158         }
159
160         /**
161          * Reports a reference if is non initializer and writable.
162          * @param {Reference} reference A reference to check.
163          * @param {int} index The index of the reference in the references.
164          * @param {Reference[]} references The array that the reference belongs to.
165          * @returns {void}
166          */
167         function checkReference(reference, index, references) {
168             const identifier = reference.identifier;
169
170             if (identifier &&
171                 !reference.init &&
172
173                 /*
174                  * Destructuring assignments can have multiple default value,
175                  * so possibly there are multiple writeable references for the same identifier.
176                  */
177                 (index === 0 || references[index - 1].identifier !== identifier)
178             ) {
179                 if (reference.isWrite()) {
180                     context.report({ node: identifier, message: "Assignment to function parameter '{{name}}'.", data: { name: identifier.name } });
181                 } else if (props && isModifyingProp(reference) && !isIgnoredPropertyAssignment(identifier.name)) {
182                     context.report({ node: identifier, message: "Assignment to property of function parameter '{{name}}'.", data: { name: identifier.name } });
183                 }
184             }
185         }
186
187         /**
188          * Finds and reports references that are non initializer and writable.
189          * @param {Variable} variable A variable to check.
190          * @returns {void}
191          */
192         function checkVariable(variable) {
193             if (variable.defs[0].type === "Parameter") {
194                 variable.references.forEach(checkReference);
195             }
196         }
197
198         /**
199          * Checks parameters of a given function node.
200          * @param {ASTNode} node A function node to check.
201          * @returns {void}
202          */
203         function checkForFunction(node) {
204             context.getDeclaredVariables(node).forEach(checkVariable);
205         }
206
207         return {
208
209             // `:exit` is needed for the `node.parent` property of identifier nodes.
210             "FunctionDeclaration:exit": checkForFunction,
211             "FunctionExpression:exit": checkForFunction,
212             "ArrowFunctionExpression:exit": checkForFunction
213         };
214
215     }
216 };