|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +/** |
| 18 | + * @typedef {NonNullable<VExpressionContainer['expression']>} VExpression |
| 19 | + */ |
| 20 | +/** |
| 21 | + * @typedef {object} OrOperands |
| 22 | + * @property {VExpression} OrOperands.node |
| 23 | + * @property {AndOperands[]} OrOperands.operands |
| 24 | + * |
| 25 | + * @typedef {object} AndOperands |
| 26 | + * @property {VExpression} AndOperands.node |
| 27 | + * @property {VExpression[]} AndOperands.operands |
| 28 | + */ |
| 29 | +/** |
| 30 | + * Splits the given node by the given logical operator. |
| 31 | + * @param {string} operator Logical operator `||` or `&&`. |
| 32 | + * @param {VExpression} node The node to split. |
| 33 | + * @returns {VExpression[]} Array of conditions that makes the node when joined by the operator. |
| 34 | + */ |
| 35 | +function splitByLogicalOperator(operator, node) { |
| 36 | + if (node.type === 'LogicalExpression' && node.operator === operator) { |
| 37 | + return [ |
| 38 | + ...splitByLogicalOperator(operator, node.left), |
| 39 | + ...splitByLogicalOperator(operator, node.right) |
| 40 | + ] |
| 41 | + } |
| 42 | + return [node] |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @param {VExpression} node |
| 47 | + */ |
| 48 | +function splitByOr(node) { |
| 49 | + return splitByLogicalOperator('||', node) |
| 50 | +} |
| 51 | +/** |
| 52 | + * @param {VExpression} node |
| 53 | + */ |
| 54 | +function splitByAnd(node) { |
| 55 | + return splitByLogicalOperator('&&', node) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param {VExpression} node |
| 60 | + * @returns {OrOperands} |
| 61 | + */ |
| 62 | +function buildOrOperands(node) { |
| 63 | + const orOperands = splitByOr(node) |
| 64 | + return { |
| 65 | + node, |
| 66 | + operands: orOperands.map((orOperand) => { |
| 67 | + const andOperands = splitByAnd(orOperand) |
| 68 | + return { |
| 69 | + node: orOperand, |
| 70 | + operands: andOperands |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// ------------------------------------------------------------------------------ |
| 77 | +// Rule Definition |
| 78 | +// ------------------------------------------------------------------------------ |
| 79 | + |
| 80 | +module.exports = { |
| 81 | + meta: { |
| 82 | + type: 'problem', |
| 83 | + docs: { |
| 84 | + description: |
| 85 | + 'disallow duplicate conditions in `v-if` / `v-else-if` chains', |
| 86 | + categories: ['vue3-essential', 'essential'], |
| 87 | + url: 'https://eslint.vuejs.org/rules/no-dupe-v-else-if.html' |
| 88 | + }, |
| 89 | + fixable: null, |
| 90 | + schema: [], |
| 91 | + messages: { |
| 92 | + unexpected: |
| 93 | + 'This branch can never execute. Its condition is a duplicate or covered by previous conditions in the `v-if` / `v-else-if` chain.' |
| 94 | + } |
| 95 | + }, |
| 96 | + /** @param {RuleContext} context */ |
| 97 | + create(context) { |
| 98 | + const tokenStore = |
| 99 | + context.parserServices.getTemplateBodyTokenStore && |
| 100 | + context.parserServices.getTemplateBodyTokenStore() |
| 101 | + /** |
| 102 | + * Determines whether the two given nodes are considered to be equal. In particular, given that the nodes |
| 103 | + * represent expressions in a boolean context, `||` and `&&` can be considered as commutative operators. |
| 104 | + * @param {VExpression} a First node. |
| 105 | + * @param {VExpression} b Second node. |
| 106 | + * @returns {boolean} `true` if the nodes are considered to be equal. |
| 107 | + */ |
| 108 | + function equal(a, b) { |
| 109 | + if (a.type !== b.type) { |
| 110 | + return false |
| 111 | + } |
| 112 | + |
| 113 | + if ( |
| 114 | + a.type === 'LogicalExpression' && |
| 115 | + b.type === 'LogicalExpression' && |
| 116 | + (a.operator === '||' || a.operator === '&&') && |
| 117 | + a.operator === b.operator |
| 118 | + ) { |
| 119 | + return ( |
| 120 | + (equal(a.left, b.left) && equal(a.right, b.right)) || |
| 121 | + (equal(a.left, b.right) && equal(a.right, b.left)) |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + return utils.equalTokens(a, b, tokenStore) |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Determines whether the first given AndOperands is a subset of the second given AndOperands. |
| 130 | + * |
| 131 | + * e.g. A: (a && b), B: (a && b && c): B is a subset of A. |
| 132 | + * |
| 133 | + * @param {AndOperands} operandsA The AndOperands to compare from. |
| 134 | + * @param {AndOperands} operandsB The AndOperands to compare against. |
| 135 | + * @returns {boolean} `true` if the `andOperandsA` is a subset of the `andOperandsB`. |
| 136 | + */ |
| 137 | + function isSubset(operandsA, operandsB) { |
| 138 | + return operandsA.operands.every((operandA) => |
| 139 | + operandsB.operands.some((operandB) => equal(operandA, operandB)) |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + return utils.defineTemplateBodyVisitor(context, { |
| 144 | + "VAttribute[directive=true][key.name.name='else-if']"(node) { |
| 145 | + if (!node.value || !node.value.expression) { |
| 146 | + return |
| 147 | + } |
| 148 | + const test = node.value.expression |
| 149 | + const conditionsToCheck = |
| 150 | + test.type === 'LogicalExpression' && test.operator === '&&' |
| 151 | + ? [...splitByAnd(test), test] |
| 152 | + : [test] |
| 153 | + const listToCheck = conditionsToCheck.map(buildOrOperands) |
| 154 | + |
| 155 | + /** @type {VElement | null} */ |
| 156 | + let current = node.parent.parent |
| 157 | + while (current && (current = utils.prevSibling(current))) { |
| 158 | + const vIf = utils.getDirective(current, 'if') |
| 159 | + const currentTestDir = vIf || utils.getDirective(current, 'else-if') |
| 160 | + if (!currentTestDir) { |
| 161 | + return |
| 162 | + } |
| 163 | + if (currentTestDir.value && currentTestDir.value.expression) { |
| 164 | + const currentOrOperands = buildOrOperands( |
| 165 | + currentTestDir.value.expression |
| 166 | + ) |
| 167 | + |
| 168 | + for (const condition of listToCheck) { |
| 169 | + const operands = (condition.operands = condition.operands.filter( |
| 170 | + (orOperand) => { |
| 171 | + return !currentOrOperands.operands.some((currentOrOperand) => |
| 172 | + isSubset(currentOrOperand, orOperand) |
| 173 | + ) |
| 174 | + } |
| 175 | + )) |
| 176 | + if (!operands.length) { |
| 177 | + context.report({ |
| 178 | + node: condition.node, |
| 179 | + messageId: 'unexpected' |
| 180 | + }) |
| 181 | + return |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if (vIf) { |
| 187 | + return |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
0 commit comments