705d0f409c412852dca4cf6dc4415a2a9c6eb799
[dotfiles/.git] / no-self-assign.js
1 /**
2  * @fileoverview Rule to disallow assignments where both sides are exactly the same
3  * @author Toru Nagashima
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Helpers
16 //------------------------------------------------------------------------------
17
18 const SPACES = /\s+/gu;
19
20 /**
21  * Checks whether the property of 2 given member expression nodes are the same
22  * property or not.
23  * @param {ASTNode} left A member expression node to check.
24  * @param {ASTNode} right Another member expression node to check.
25  * @returns {boolean} `true` if the member expressions have the same property.
26  */
27 function isSameProperty(left, right) {
28     if (left.property.type === "Identifier" &&
29         left.property.type === right.property.type &&
30         left.property.name === right.property.name &&
31         left.computed === right.computed
32     ) {
33         return true;
34     }
35
36     const lname = astUtils.getStaticPropertyName(left);
37     const rname = astUtils.getStaticPropertyName(right);
38
39     return lname !== null && lname === rname;
40 }
41
42 /**
43  * Checks whether 2 given member expression nodes are the reference to the same
44  * property or not.
45  * @param {ASTNode} left A member expression node to check.
46  * @param {ASTNode} right Another member expression node to check.
47  * @returns {boolean} `true` if the member expressions are the reference to the
48  *  same property or not.
49  */
50 function isSameMember(left, right) {
51     if (!isSameProperty(left, right)) {
52         return false;
53     }
54
55     const lobj = left.object;
56     const robj = right.object;
57
58     if (lobj.type !== robj.type) {
59         return false;
60     }
61     if (lobj.type === "MemberExpression") {
62         return isSameMember(lobj, robj);
63     }
64     if (lobj.type === "ThisExpression") {
65         return true;
66     }
67     return lobj.type === "Identifier" && lobj.name === robj.name;
68 }
69
70 /**
71  * Traverses 2 Pattern nodes in parallel, then reports self-assignments.
72  * @param {ASTNode|null} left A left node to traverse. This is a Pattern or
73  *      a Property.
74  * @param {ASTNode|null} right A right node to traverse. This is a Pattern or
75  *      a Property.
76  * @param {boolean} props The flag to check member expressions as well.
77  * @param {Function} report A callback function to report.
78  * @returns {void}
79  */
80 function eachSelfAssignment(left, right, props, report) {
81     if (!left || !right) {
82
83         // do nothing
84     } else if (
85         left.type === "Identifier" &&
86         right.type === "Identifier" &&
87         left.name === right.name
88     ) {
89         report(right);
90     } else if (
91         left.type === "ArrayPattern" &&
92         right.type === "ArrayExpression"
93     ) {
94         const end = Math.min(left.elements.length, right.elements.length);
95
96         for (let i = 0; i < end; ++i) {
97             const leftElement = left.elements[i];
98             const rightElement = right.elements[i];
99
100             // Avoid cases such as [...a] = [...a, 1]
101             if (
102                 leftElement &&
103                 leftElement.type === "RestElement" &&
104                 i < right.elements.length - 1
105             ) {
106                 break;
107             }
108
109             eachSelfAssignment(leftElement, rightElement, props, report);
110
111             // After a spread element, those indices are unknown.
112             if (rightElement && rightElement.type === "SpreadElement") {
113                 break;
114             }
115         }
116     } else if (
117         left.type === "RestElement" &&
118         right.type === "SpreadElement"
119     ) {
120         eachSelfAssignment(left.argument, right.argument, props, report);
121     } else if (
122         left.type === "ObjectPattern" &&
123         right.type === "ObjectExpression" &&
124         right.properties.length >= 1
125     ) {
126
127         /*
128          * Gets the index of the last spread property.
129          * It's possible to overwrite properties followed by it.
130          */
131         let startJ = 0;
132
133         for (let i = right.properties.length - 1; i >= 0; --i) {
134             const propType = right.properties[i].type;
135
136             if (propType === "SpreadElement" || propType === "ExperimentalSpreadProperty") {
137                 startJ = i + 1;
138                 break;
139             }
140         }
141
142         for (let i = 0; i < left.properties.length; ++i) {
143             for (let j = startJ; j < right.properties.length; ++j) {
144                 eachSelfAssignment(
145                     left.properties[i],
146                     right.properties[j],
147                     props,
148                     report
149                 );
150             }
151         }
152     } else if (
153         left.type === "Property" &&
154         right.type === "Property" &&
155         right.kind === "init" &&
156         !right.method
157     ) {
158         const leftName = astUtils.getStaticPropertyName(left);
159
160         if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) {
161             eachSelfAssignment(left.value, right.value, props, report);
162         }
163     } else if (
164         props &&
165         left.type === "MemberExpression" &&
166         right.type === "MemberExpression" &&
167         isSameMember(left, right)
168     ) {
169         report(right);
170     }
171 }
172
173 //------------------------------------------------------------------------------
174 // Rule Definition
175 //------------------------------------------------------------------------------
176
177 module.exports = {
178     meta: {
179         type: "problem",
180
181         docs: {
182             description: "disallow assignments where both sides are exactly the same",
183             category: "Best Practices",
184             recommended: true,
185             url: "https://eslint.org/docs/rules/no-self-assign"
186         },
187
188         schema: [
189             {
190                 type: "object",
191                 properties: {
192                     props: {
193                         type: "boolean",
194                         default: true
195                     }
196                 },
197                 additionalProperties: false
198             }
199         ]
200     },
201
202     create(context) {
203         const sourceCode = context.getSourceCode();
204         const [{ props = true } = {}] = context.options;
205
206         /**
207          * Reports a given node as self assignments.
208          * @param {ASTNode} node A node to report. This is an Identifier node.
209          * @returns {void}
210          */
211         function report(node) {
212             context.report({
213                 node,
214                 message: "'{{name}}' is assigned to itself.",
215                 data: {
216                     name: sourceCode.getText(node).replace(SPACES, "")
217                 }
218             });
219         }
220
221         return {
222             AssignmentExpression(node) {
223                 if (node.operator === "=") {
224                     eachSelfAssignment(node.left, node.right, props, report);
225                 }
226             }
227         };
228     }
229 };