UNPKG

eslint-plugin-unicorn

Version:
132 lines (115 loc) • 3.54 kB
import {isLiteral} from './ast/index.js'; import {fixSpaceAroundKeyword} from './fix/index.js'; import { getParenthesizedRange, getParenthesizedText, getStaticValueForControlFlow, isNumber, isParenthesized, needsSemicolon, shouldAddParenthesesToExpressionStart, wouldRemoveComments, } from './utils/index.js'; const MESSAGE_ID_ERROR = 'no-subtraction-comparison/error'; const MESSAGE_ID_SUGGESTION = 'no-subtraction-comparison/suggestion'; const messages = { [MESSAGE_ID_ERROR]: 'Prefer comparing the values directly over comparing the difference with `0`.', [MESSAGE_ID_SUGGESTION]: 'Replace with `{{replacement}}`.', }; // When the `0` is on the left, the comparison is mirrored, so the operator flips. // The equality operators are symmetric, so they map to themselves. const invertedOperator = { '>': '<', '>=': '<=', '<': '>', '<=': '>=', '===': '===', '!==': '!==', '==': '==', '!=': '!=', }; const operators = new Set(Object.keys(invertedOperator)); const strictOrderingOperators = new Set(['>', '<']); const isZero = node => isLiteral(node, 0); const isSubtraction = node => node.type === 'BinaryExpression' && node.operator === '-'; const isFiniteStaticNumber = (node, context) => { const value = getStaticValueForControlFlow(node, context)?.value; return typeof value === 'number' && Number.isFinite(value); }; /** @param {import('eslint').Rule.RuleContext} context */ const create = context => { const {sourceCode} = context; context.on('BinaryExpression', node => { if (!operators.has(node.operator)) { return; } let subtraction; let operator; if (isZero(node.right) && isSubtraction(node.left)) { subtraction = node.left; operator = node.operator; } else if (isZero(node.left) && isSubtraction(node.right)) { subtraction = node.right; operator = invertedOperator[node.operator]; } else { return; } const problem = { node, messageId: MESSAGE_ID_ERROR, }; const {left, right} = subtraction; // Keep comments intact by only offering edits when comments are preserved in the copied operands. if (wouldRemoveComments(context, node, [left, right].map(operand => getParenthesizedRange(operand, context)))) { return problem; } let replacement = `${getParenthesizedText(left, context)} ${operator} ${getParenthesizedText(right, context)}`; if (!isParenthesized(left, context) && shouldAddParenthesesToExpressionStart(left, context, node)) { replacement = `(${replacement})`; } /** @param {import('eslint').Rule.RuleFixer} fixer */ const fix = function * (fixer) { yield fixSpaceAroundKeyword(fixer, node, context); yield fixer.replaceText(node, needsSemicolon(sourceCode.getTokenBefore(node), context, replacement) ? `;${replacement}` : replacement); }; const canAutofix = strictOrderingOperators.has(operator) ? isNumber(left, context) && isNumber(right, context) : isFiniteStaticNumber(left, context) && isFiniteStaticNumber(right, context); if (canAutofix) { problem.fix = fix; } else { problem.suggest = [ { messageId: MESSAGE_ID_SUGGESTION, data: {replacement}, fix, }, ]; } return problem; }); }; /** @type {import('eslint').Rule.RuleModule} */ const config = { create, meta: { type: 'suggestion', docs: { description: 'Prefer comparing values directly over subtracting and comparing to `0`.', recommended: 'unopinionated', }, fixable: 'code', hasSuggestions: true, messages, languages: [ 'js/js', ], }, }; export default config;