eslint-plugin-unicorn
Version:
More than 300 powerful ESLint rules
123 lines (106 loc) • 3.02 kB
JavaScript
import {
getParenthesizedRange,
getPrecedence,
isSameReference,
PRECEDENCE_ADDITION,
wouldRemoveComments,
} from './utils/index.js';
/**
@import * as ESLint from 'eslint';
*/
const MESSAGE_ID_ERROR = 'no-misrefactored-assignment/error';
const MESSAGE_ID_SUGGESTION = 'no-misrefactored-assignment/suggestion';
const messages = {
[MESSAGE_ID_ERROR]: '`{{target}}` is duplicated in the right-hand side, which is likely a refactoring mistake.',
[MESSAGE_ID_SUGGESTION]: 'Remove the duplicated `{{target}}`.',
};
// Compound assignment operators whose binary operator is commutative, so the
// target can be either operand: `a += a + b` and `a += b + a`.
const commutativeOperators = new Set(['+=', '*=', '&=', '|=', '^=']);
// All arithmetic and bitwise compound assignment operators. The logical ones
// (`&&=`, `||=`, `??=`) are intentionally excluded.
const compoundOperators = new Set([
'+=',
'-=',
'*=',
'/=',
'%=',
'**=',
'&=',
'|=',
'^=',
'<<=',
'>>=',
'>>>=',
]);
/*
The kept operand replaces the whole `a + b` right-hand side, so its copied range must include surrounding parentheses when they are needed to keep its grouping.
*/
const getKeptOperandRange = (keptOperand, context) =>
getPrecedence(keptOperand) < PRECEDENCE_ADDITION
? getParenthesizedRange(keptOperand, context)
: context.sourceCode.getRange(keptOperand);
/**
@param {ESLint.Rule.RuleContext} context
*/
const create = context => {
const {sourceCode} = context;
context.on('AssignmentExpression', node => {
const {operator, left, right} = node;
if (!compoundOperators.has(operator)) {
return;
}
// `+=` -> `+`, `>>>=` -> `>>>`, etc.
if (right.type !== 'BinaryExpression' || right.operator !== operator.slice(0, -1)) {
return;
}
let keptOperand;
if (isSameReference(left, right.left)) {
keptOperand = right.right;
} else if (commutativeOperators.has(operator) && isSameReference(left, right.right)) {
keptOperand = right.left;
} else {
return;
}
const data = {target: sourceCode.getText(left)};
const problem = {
node,
messageId: MESSAGE_ID_ERROR,
data,
};
// The fix replaces the whole right-hand side with the kept operand, so it would drop comments outside the copied range. Skip the suggestion in that case.
const keptRange = getKeptOperandRange(keptOperand, context);
if (!wouldRemoveComments(context, right, [keptRange])) {
problem.suggest = [
{
messageId: MESSAGE_ID_SUGGESTION,
data,
/**
@param {ESLint.Rule.RuleFixer} fixer
*/
fix: fixer => fixer.replaceText(right, sourceCode.text.slice(...keptRange)),
},
];
}
return problem;
});
};
/**
@type {ESLint.Rule.RuleModule}
*/
const config = {
create,
meta: {
type: 'problem',
docs: {
description: 'Disallow misrefactored compound assignments where the target is duplicated in the right-hand side.',
recommended: 'unopinionated',
},
hasSuggestions: true,
messages,
languages: [
'js/js',
],
},
};
export default config;