}
/**
- * 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) {
// 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)
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."
}
},
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]);
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.
*/
context.report({
node,
- messageId: "replaced"
+ messageId: "replaced",
+ data: { operator }
});
}
}
* @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);
) {
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
}