|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
-
- "use strict";
-
-
-
-
-
- const astUtils = require("../util/ast-utils");
-
-
-
-
-
-
- function isCommutativeOperatorWithShorthand(operator) {
- return ["*", "&", "^", "|"].indexOf(operator) >= 0;
- }
-
-
- function isNonCommutativeOperatorWithShorthand(operator) {
- return ["+", "-", "/", "%", "<<", ">>", ">>>", "**"].indexOf(operator) >= 0;
- }
-
-
-
-
-
-
- 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":
-
-
-
- return same(a.object, b.object) && same(a.property, b.property);
-
- default:
- return false;
- }
- }
-
-
- function canBeFixed(node) {
- return node.type === "Identifier" ||
- node.type === "MemberExpression" && node.object.type === "Identifier" && (!node.computed || node.property.type === "Literal");
- }
-
- module.exports = {
- meta: {
- type: "suggestion",
-
- docs: {
- description: "require or disallow assignment operator shorthand where possible",
- category: "Stylistic Issues",
- recommended: false,
- url: "https://eslint.org/docs/rules/operator-assignment"
- },
-
- schema: [
- {
- enum: ["always", "never"]
- }
- ],
-
- fixable: "code"
- },
-
- create(context) {
-
- const sourceCode = context.getSourceCode();
-
-
-
- function getOperatorToken(node) {
- return sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
- }
-
-
-
- function verify(node) {
- if (node.operator !== "=" || node.right.type !== "BinaryExpression") {
- return;
- }
-
- const left = node.left;
- const expr = node.right;
- const operator = expr.operator;
-
- if (isCommutativeOperatorWithShorthand(operator) || isNonCommutativeOperatorWithShorthand(operator)) {
- if (same(left, expr.left)) {
- context.report({
- node,
- message: "Assignment can be replaced with operator assignment.",
- fix(fixer) {
- if (canBeFixed(left)) {
- const equalsToken = getOperatorToken(node);
- const operatorToken = getOperatorToken(expr);
- const leftText = sourceCode.getText().slice(node.range[0], equalsToken.range[0]);
- const rightText = sourceCode.getText().slice(operatorToken.range[1], node.right.range[1]);
-
- return fixer.replaceText(node, `${leftText}${expr.operator}=${rightText}`);
- }
- return null;
- }
- });
- } else if (same(left, expr.right) && isCommutativeOperatorWithShorthand(operator)) {
-
-
-
- context.report({
- node,
- message: "Assignment can be replaced with operator assignment."
- });
- }
- }
- }
-
-
-
- function prohibit(node) {
- if (node.operator !== "=") {
- context.report({
- node,
- message: "Unexpected operator assignment shorthand.",
- fix(fixer) {
- if (canBeFixed(node.left)) {
- const operatorToken = getOperatorToken(node);
- const leftText = sourceCode.getText().slice(node.range[0], operatorToken.range[0]);
- const newOperator = node.operator.slice(0, -1);
- let rightText;
-
-
- if (
- astUtils.getPrecedence(node.right) <= astUtils.getPrecedence({ type: "BinaryExpression", operator: newOperator }) &&
- !astUtils.isParenthesised(sourceCode, node.right)
- ) {
- rightText = `${sourceCode.text.slice(operatorToken.range[1], node.right.range[0])}(${sourceCode.getText(node.right)})`;
- } else {
- rightText = sourceCode.text.slice(operatorToken.range[1], node.range[1]);
- }
-
- return fixer.replaceText(node, `${leftText}= ${leftText}${newOperator}${rightText}`);
- }
- return null;
- }
- });
- }
- }
-
- return {
- AssignmentExpression: context.options[0] !== "never" ? verify : prohibit
- };
-
- }
- };
|