|
| 1 | +'use strict'; |
| 2 | +const {isParenthesized, getParenthesizedText} = require('./utils/parentheses.js'); |
| 3 | +const isSameReference = require('./utils/is-same-reference.js'); |
| 4 | +const shouldAddParenthesesToLogicalExpressionChild = require('./utils/should-add-parentheses-to-logical-expression-child.js'); |
| 5 | +const needsSemicolon = require('./utils/needs-semicolon.js'); |
| 6 | + |
| 7 | +const MESSAGE_ID_ERROR = 'prefer-logical-operator-over-ternary/error'; |
| 8 | +const MESSAGE_ID_SUGGESTION = 'prefer-logical-operator-over-ternary/suggestion'; |
| 9 | +const messages = { |
| 10 | + [MESSAGE_ID_ERROR]: 'Prefer using a logical operator over a ternary.', |
| 11 | + [MESSAGE_ID_SUGGESTION]: 'Switch to `{{operator}}` operator.', |
| 12 | +}; |
| 13 | + |
| 14 | +function isSameNode(left, right, sourceCode) { |
| 15 | + if (isSameReference(left, right)) { |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + if (left.type !== right.type) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + switch (left.type) { |
| 24 | + case 'AwaitExpression': |
| 25 | + return isSameNode(left.argument, right.argument, sourceCode); |
| 26 | + |
| 27 | + case 'LogicalExpression': |
| 28 | + return ( |
| 29 | + left.operator === right.operator |
| 30 | + && isSameNode(left.left, right.left, sourceCode) |
| 31 | + && isSameNode(left.right, right.right, sourceCode) |
| 32 | + ); |
| 33 | + |
| 34 | + case 'UnaryExpression': |
| 35 | + return ( |
| 36 | + left.operator === right.operator |
| 37 | + && left.prefix === right.prefix |
| 38 | + && isSameNode(left.argument, right.argument, sourceCode) |
| 39 | + ); |
| 40 | + |
| 41 | + case 'UpdateExpression': |
| 42 | + return false; |
| 43 | + |
| 44 | + // No default |
| 45 | + } |
| 46 | + |
| 47 | + return sourceCode.getText(left) === sourceCode.getText(right); |
| 48 | +} |
| 49 | + |
| 50 | +function fix({ |
| 51 | + fixer, |
| 52 | + sourceCode, |
| 53 | + conditionalExpression, |
| 54 | + left, |
| 55 | + right, |
| 56 | + operator, |
| 57 | +}) { |
| 58 | + let text = [left, right].map((node, index) => { |
| 59 | + const isNodeParenthesized = isParenthesized(node, sourceCode); |
| 60 | + let text = isNodeParenthesized ? getParenthesizedText(node, sourceCode) : sourceCode.getText(node); |
| 61 | + |
| 62 | + if ( |
| 63 | + !isNodeParenthesized |
| 64 | + && shouldAddParenthesesToLogicalExpressionChild(node, {operator, property: index === 0 ? 'left' : 'right'}) |
| 65 | + ) { |
| 66 | + text = `(${text})`; |
| 67 | + } |
| 68 | + |
| 69 | + return text; |
| 70 | + }).join(` ${operator} `); |
| 71 | + |
| 72 | + // According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table |
| 73 | + // There should be no cases need add parentheses when switching ternary to logical expression |
| 74 | + |
| 75 | + // ASI |
| 76 | + if (needsSemicolon(sourceCode.getTokenBefore(conditionalExpression), sourceCode, text)) { |
| 77 | + text = `;${text}`; |
| 78 | + } |
| 79 | + |
| 80 | + return fixer.replaceText(conditionalExpression, text); |
| 81 | +} |
| 82 | + |
| 83 | +function getProblem({ |
| 84 | + sourceCode, |
| 85 | + conditionalExpression, |
| 86 | + left, |
| 87 | + right, |
| 88 | +}) { |
| 89 | + return { |
| 90 | + node: conditionalExpression, |
| 91 | + messageId: MESSAGE_ID_ERROR, |
| 92 | + suggest: ['??', '||'].map(operator => ({ |
| 93 | + messageId: MESSAGE_ID_SUGGESTION, |
| 94 | + data: {operator}, |
| 95 | + fix: fixer => fix({ |
| 96 | + fixer, |
| 97 | + sourceCode, |
| 98 | + conditionalExpression, |
| 99 | + left, |
| 100 | + right, |
| 101 | + operator, |
| 102 | + }), |
| 103 | + })), |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 108 | +const create = context => { |
| 109 | + const sourceCode = context.getSourceCode(); |
| 110 | + |
| 111 | + return { |
| 112 | + ConditionalExpression(conditionalExpression) { |
| 113 | + const {test, consequent, alternate} = conditionalExpression; |
| 114 | + |
| 115 | + // `foo ? foo : bar` |
| 116 | + if (isSameNode(test, consequent, sourceCode)) { |
| 117 | + return getProblem({ |
| 118 | + sourceCode, |
| 119 | + conditionalExpression, |
| 120 | + left: test, |
| 121 | + right: alternate, |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + // `!bar ? foo : bar` |
| 126 | + if ( |
| 127 | + test.type === 'UnaryExpression' |
| 128 | + && test.operator === '!' |
| 129 | + && test.prefix |
| 130 | + && isSameNode(test.argument, alternate, sourceCode) |
| 131 | + ) { |
| 132 | + return getProblem({ |
| 133 | + sourceCode, |
| 134 | + conditionalExpression, |
| 135 | + left: test.argument, |
| 136 | + right: consequent, |
| 137 | + }); |
| 138 | + } |
| 139 | + }, |
| 140 | + }; |
| 141 | +}; |
| 142 | + |
| 143 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 144 | +module.exports = { |
| 145 | + create, |
| 146 | + meta: { |
| 147 | + type: 'suggestion', |
| 148 | + docs: { |
| 149 | + description: 'Prefer using a logical operator over a ternary.', |
| 150 | + }, |
| 151 | + |
| 152 | + hasSuggestions: true, |
| 153 | + messages, |
| 154 | + }, |
| 155 | +}; |
0 commit comments