|
| 1 | +'use strict'; |
| 2 | +const {fixSpaceAroundKeyword} = require('./fix/index.js'); |
| 3 | + |
| 4 | +const MESSAGE_ID = 'prefer-math-min-max'; |
| 5 | +const messages = { |
| 6 | + [MESSAGE_ID]: 'Prefer `Math.{{method}}()` to simplify ternary expressions.', |
| 7 | +}; |
| 8 | + |
| 9 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 10 | +const create = context => ({ |
| 11 | + /** @param {import('estree').ConditionalExpression} conditionalExpression */ |
| 12 | + ConditionalExpression(conditionalExpression) { |
| 13 | + const {test, consequent, alternate} = conditionalExpression; |
| 14 | + |
| 15 | + if (test.type !== 'BinaryExpression') { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + const {operator, left, right} = test; |
| 20 | + const [leftText, rightText, alternateText, consequentText] = [left, right, alternate, consequent].map(node => context.sourceCode.getText(node)); |
| 21 | + |
| 22 | + const isGreaterOrEqual = operator === '>' || operator === '>='; |
| 23 | + const isLessOrEqual = operator === '<' || operator === '<='; |
| 24 | + |
| 25 | + let method; |
| 26 | + |
| 27 | + // Prefer `Math.min()` |
| 28 | + if ( |
| 29 | + // `height > 50 ? 50 : height` |
| 30 | + (isGreaterOrEqual && leftText === alternateText && rightText === consequentText) |
| 31 | + // `height < 50 ? height : 50` |
| 32 | + || (isLessOrEqual && leftText === consequentText && rightText === alternateText) |
| 33 | + ) { |
| 34 | + method = 'min'; |
| 35 | + } else if ( |
| 36 | + // `height > 50 ? height : 50` |
| 37 | + (isGreaterOrEqual && leftText === consequentText && rightText === alternateText) |
| 38 | + // `height < 50 ? 50 : height` |
| 39 | + || (isLessOrEqual && leftText === alternateText && rightText === consequentText) |
| 40 | + ) { |
| 41 | + method = 'max'; |
| 42 | + } |
| 43 | + |
| 44 | + if (!method) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + node: conditionalExpression, |
| 50 | + messageId: MESSAGE_ID, |
| 51 | + data: {method}, |
| 52 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 53 | + * fix(fixer) { |
| 54 | + const {sourceCode} = context; |
| 55 | + |
| 56 | + yield * fixSpaceAroundKeyword(fixer, conditionalExpression, sourceCode); |
| 57 | + |
| 58 | + const argumentsText = [left, right] |
| 59 | + .map(node => node.type === 'SequenceExpression' ? `(${sourceCode.getText(node)})` : sourceCode.getText(node)) |
| 60 | + .join(', '); |
| 61 | + |
| 62 | + yield fixer.replaceText(conditionalExpression, `Math.${method}(${argumentsText})`); |
| 63 | + }, |
| 64 | + }; |
| 65 | + }, |
| 66 | +}); |
| 67 | + |
| 68 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 69 | +module.exports = { |
| 70 | + create, |
| 71 | + meta: { |
| 72 | + type: 'problem', |
| 73 | + docs: { |
| 74 | + description: 'Prefer `Math.min()` and `Math.max()` over ternaries for simple comparisons.', |
| 75 | + recommended: true, |
| 76 | + }, |
| 77 | + fixable: 'code', |
| 78 | + messages, |
| 79 | + }, |
| 80 | +}; |
0 commit comments