1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const SPACES = /\s+/g;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function isSameProperty(left, right) {
|
29 | if (left.property.type === "Identifier" &&
|
30 | left.property.type === right.property.type &&
|
31 | left.property.name === right.property.name &&
|
32 | left.computed === right.computed
|
33 | ) {
|
34 | return true;
|
35 | }
|
36 |
|
37 | const lname = astUtils.getStaticPropertyName(left);
|
38 | const rname = astUtils.getStaticPropertyName(right);
|
39 |
|
40 | return lname !== null && lname === rname;
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function isSameMember(left, right) {
|
53 | if (!isSameProperty(left, right)) {
|
54 | return false;
|
55 | }
|
56 |
|
57 | const lobj = left.object;
|
58 | const robj = right.object;
|
59 |
|
60 | if (lobj.type !== robj.type) {
|
61 | return false;
|
62 | }
|
63 | if (lobj.type === "MemberExpression") {
|
64 | return isSameMember(lobj, robj);
|
65 | }
|
66 | return lobj.type === "Identifier" && lobj.name === robj.name;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function eachSelfAssignment(left, right, props, report) {
|
81 | if (!left || !right) {
|
82 |
|
83 |
|
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 rightElement = right.elements[i];
|
98 |
|
99 | eachSelfAssignment(left.elements[i], rightElement, props, report);
|
100 |
|
101 |
|
102 | if (rightElement && rightElement.type === "SpreadElement") {
|
103 | break;
|
104 | }
|
105 | }
|
106 | } else if (
|
107 | left.type === "RestElement" &&
|
108 | right.type === "SpreadElement"
|
109 | ) {
|
110 | eachSelfAssignment(left.argument, right.argument, props, report);
|
111 | } else if (
|
112 | left.type === "ObjectPattern" &&
|
113 | right.type === "ObjectExpression" &&
|
114 | right.properties.length >= 1
|
115 | ) {
|
116 |
|
117 |
|
118 |
|
119 | let startJ = 0;
|
120 |
|
121 | for (let i = right.properties.length - 1; i >= 0; --i) {
|
122 | if (right.properties[i].type === "ExperimentalSpreadProperty") {
|
123 | startJ = i + 1;
|
124 | break;
|
125 | }
|
126 | }
|
127 |
|
128 | for (let i = 0; i < left.properties.length; ++i) {
|
129 | for (let j = startJ; j < right.properties.length; ++j) {
|
130 | eachSelfAssignment(
|
131 | left.properties[i],
|
132 | right.properties[j],
|
133 | props,
|
134 | report
|
135 | );
|
136 | }
|
137 | }
|
138 | } else if (
|
139 | left.type === "Property" &&
|
140 | right.type === "Property" &&
|
141 | !left.computed &&
|
142 | !right.computed &&
|
143 | right.kind === "init" &&
|
144 | !right.method &&
|
145 | left.key.name === right.key.name
|
146 | ) {
|
147 | eachSelfAssignment(left.value, right.value, props, report);
|
148 | } else if (
|
149 | props &&
|
150 | left.type === "MemberExpression" &&
|
151 | right.type === "MemberExpression" &&
|
152 | isSameMember(left, right)
|
153 | ) {
|
154 | report(right);
|
155 | }
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | module.exports = {
|
163 | meta: {
|
164 | docs: {
|
165 | description: "disallow assignments where both sides are exactly the same",
|
166 | category: "Best Practices",
|
167 | recommended: true
|
168 | },
|
169 |
|
170 | schema: [
|
171 | {
|
172 | type: "object",
|
173 | properties: {
|
174 | props: {
|
175 | type: "boolean"
|
176 | }
|
177 | },
|
178 | additionalProperties: false
|
179 | }
|
180 | ]
|
181 | },
|
182 |
|
183 | create(context) {
|
184 | const sourceCode = context.getSourceCode();
|
185 | const options = context.options[0];
|
186 | const props = Boolean(options && options.props);
|
187 |
|
188 | |
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | function report(node) {
|
195 | context.report({
|
196 | node,
|
197 | message: "'{{name}}' is assigned to itself.",
|
198 | data: {
|
199 | name: sourceCode.getText(node).replace(SPACES, "")
|
200 | }
|
201 | });
|
202 | }
|
203 |
|
204 | return {
|
205 | AssignmentExpression(node) {
|
206 | if (node.operator === "=") {
|
207 | eachSelfAssignment(node.left, node.right, props, report);
|
208 | }
|
209 | }
|
210 | };
|
211 | }
|
212 | };
|