2 * @fileoverview Rule to disallow assignments where both sides are exactly the same
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const SPACES = /\s+/gu;
21 * Checks whether the property of 2 given member expression nodes are the same
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.
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
36 const lname = astUtils.getStaticPropertyName(left);
37 const rname = astUtils.getStaticPropertyName(right);
39 return lname !== null && lname === rname;
43 * Checks whether 2 given member expression nodes are the reference to the same
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.
50 function isSameMember(left, right) {
51 if (!isSameProperty(left, right)) {
55 const lobj = left.object;
56 const robj = right.object;
58 if (lobj.type !== robj.type) {
61 if (lobj.type === "MemberExpression") {
62 return isSameMember(lobj, robj);
64 if (lobj.type === "ThisExpression") {
67 return lobj.type === "Identifier" && lobj.name === robj.name;
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
74 * @param {ASTNode|null} right A right node to traverse. This is a Pattern or
76 * @param {boolean} props The flag to check member expressions as well.
77 * @param {Function} report A callback function to report.
80 function eachSelfAssignment(left, right, props, report) {
81 if (!left || !right) {
85 left.type === "Identifier" &&
86 right.type === "Identifier" &&
87 left.name === right.name
91 left.type === "ArrayPattern" &&
92 right.type === "ArrayExpression"
94 const end = Math.min(left.elements.length, right.elements.length);
96 for (let i = 0; i < end; ++i) {
97 const leftElement = left.elements[i];
98 const rightElement = right.elements[i];
100 // Avoid cases such as [...a] = [...a, 1]
103 leftElement.type === "RestElement" &&
104 i < right.elements.length - 1
109 eachSelfAssignment(leftElement, rightElement, props, report);
111 // After a spread element, those indices are unknown.
112 if (rightElement && rightElement.type === "SpreadElement") {
117 left.type === "RestElement" &&
118 right.type === "SpreadElement"
120 eachSelfAssignment(left.argument, right.argument, props, report);
122 left.type === "ObjectPattern" &&
123 right.type === "ObjectExpression" &&
124 right.properties.length >= 1
128 * Gets the index of the last spread property.
129 * It's possible to overwrite properties followed by it.
133 for (let i = right.properties.length - 1; i >= 0; --i) {
134 const propType = right.properties[i].type;
136 if (propType === "SpreadElement" || propType === "ExperimentalSpreadProperty") {
142 for (let i = 0; i < left.properties.length; ++i) {
143 for (let j = startJ; j < right.properties.length; ++j) {
153 left.type === "Property" &&
154 right.type === "Property" &&
155 right.kind === "init" &&
158 const leftName = astUtils.getStaticPropertyName(left);
160 if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) {
161 eachSelfAssignment(left.value, right.value, props, report);
165 left.type === "MemberExpression" &&
166 right.type === "MemberExpression" &&
167 isSameMember(left, right)
173 //------------------------------------------------------------------------------
175 //------------------------------------------------------------------------------
182 description: "disallow assignments where both sides are exactly the same",
183 category: "Best Practices",
185 url: "https://eslint.org/docs/rules/no-self-assign"
197 additionalProperties: false
203 const sourceCode = context.getSourceCode();
204 const [{ props = true } = {}] = context.options;
207 * Reports a given node as self assignments.
208 * @param {ASTNode} node A node to report. This is an Identifier node.
211 function report(node) {
214 message: "'{{name}}' is assigned to itself.",
216 name: sourceCode.getText(node).replace(SPACES, "")
222 AssignmentExpression(node) {
223 if (node.operator === "=") {
224 eachSelfAssignment(node.left, node.right, props, report);