massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / operator-assignment.js
index b19ba0d02e1a1ba8cf8a9044f1b14ddcee99a4e4..a48d272519705563e9a1e8104a57a376c71825e6 100644 (file)
@@ -26,10 +26,10 @@ function isCommutativeOperatorWithShorthand(operator) {
 }
 
 /**
- * Checks whether an operator is not commuatative and has an operator assignment
+ * Checks whether an operator is not commutative and has an operator assignment
  * shorthand form.
  * @param   {string}  operator Operator to check.
- * @returns {boolean}          True if the operator is not commuatative and has
+ * @returns {boolean}          True if the operator is not commutative and has
  *     a shorthand form.
  */
 function isNonCommutativeOperatorWithShorthand(operator) {
@@ -40,45 +40,6 @@ function isNonCommutativeOperatorWithShorthand(operator) {
 // Rule Definition
 //------------------------------------------------------------------------------
 
-/**
- * Checks whether two expressions reference the same value. For example:
- *     a = a
- *     a.b = a.b
- *     a[0] = a[0]
- *     a['b'] = a['b']
- * @param   {ASTNode} a Left side of the comparison.
- * @param   {ASTNode} b Right side of the comparison.
- * @returns {boolean}   True if both sides match and reference the same value.
- */
-function same(a, b) {
-    if (a.type !== b.type) {
-        return false;
-    }
-
-    switch (a.type) {
-        case "Identifier":
-            return a.name === b.name;
-
-        case "Literal":
-            return a.value === b.value;
-
-        case "MemberExpression":
-
-            /*
-             * x[0] = x[0]
-             * x[y] = x[y]
-             * x.y = x.y
-             */
-            return same(a.object, b.object) && same(a.property, b.property);
-
-        case "ThisExpression":
-            return true;
-
-        default:
-            return false;
-    }
-}
-
 /**
  * Determines if the left side of a node can be safely fixed (i.e. if it activates the same getters/setters and)
  * toString calls regardless of whether assignment shorthand is used)
@@ -115,8 +76,8 @@ module.exports = {
 
         fixable: "code",
         messages: {
-            replaced: "Assignment can be replaced with operator assignment.",
-            unexpected: "Unexpected operator assignment shorthand."
+            replaced: "Assignment (=) can be replaced with operator assignment ({{operator}}=).",
+            unexpected: "Unexpected operator assignment ({{operator}}=) shorthand."
         }
     },
 
@@ -148,12 +109,13 @@ module.exports = {
             const operator = expr.operator;
 
             if (isCommutativeOperatorWithShorthand(operator) || isNonCommutativeOperatorWithShorthand(operator)) {
-                if (same(left, expr.left)) {
+                if (astUtils.isSameReference(left, expr.left, true)) {
                     context.report({
                         node,
                         messageId: "replaced",
+                        data: { operator },
                         fix(fixer) {
-                            if (canBeFixed(left)) {
+                            if (canBeFixed(left) && canBeFixed(expr.left)) {
                                 const equalsToken = getOperatorToken(node);
                                 const operatorToken = getOperatorToken(expr);
                                 const leftText = sourceCode.getText().slice(node.range[0], equalsToken.range[0]);
@@ -169,7 +131,7 @@ module.exports = {
                             return null;
                         }
                     });
-                } else if (same(left, expr.right) && isCommutativeOperatorWithShorthand(operator)) {
+                } else if (astUtils.isSameReference(left, expr.right, true) && isCommutativeOperatorWithShorthand(operator)) {
 
                     /*
                      * This case can't be fixed safely.
@@ -178,7 +140,8 @@ module.exports = {
                      */
                     context.report({
                         node,
-                        messageId: "replaced"
+                        messageId: "replaced",
+                        data: { operator }
                     });
                 }
             }
@@ -190,10 +153,11 @@ module.exports = {
          * @returns {void}
          */
         function prohibit(node) {
-            if (node.operator !== "=") {
+            if (node.operator !== "=" && !astUtils.isLogicalAssignmentOperator(node.operator)) {
                 context.report({
                     node,
                     messageId: "unexpected",
+                    data: { operator: node.operator },
                     fix(fixer) {
                         if (canBeFixed(node.left)) {
                             const firstToken = sourceCode.getFirstToken(node);
@@ -214,12 +178,12 @@ module.exports = {
                             ) {
                                 rightText = `${sourceCode.text.slice(operatorToken.range[1], node.right.range[0])}(${sourceCode.getText(node.right)})`;
                             } else {
-                                const firstRightToken = sourceCode.getFirstToken(node.right);
+                                const tokenAfterOperator = sourceCode.getTokenAfter(operatorToken, { includeComments: true });
                                 let rightTextPrefix = "";
 
                                 if (
-                                    operatorToken.range[1] === firstRightToken.range[0] &&
-                                    !astUtils.canTokensBeAdjacent(newOperator, firstRightToken)
+                                    operatorToken.range[1] === tokenAfterOperator.range[0] &&
+                                    !astUtils.canTokensBeAdjacent({ type: "Punctuator", value: newOperator }, tokenAfterOperator)
                                 ) {
                                     rightTextPrefix = " "; // foo+=+bar -> foo= foo+ +bar
                                 }