UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

88 lines (87 loc) • 3.77 kB
const require_index = require("../utils/index.js"); // lib/rules/no-dupe-v-else-if.ts /** * @author Yosuke Ota * See LICENSE file in root directory for full license. */ /** * Splits the given node by the given logical operator. * @param operator Logical operator `||` or `&&`. * @returns Array of conditions that makes the node when joined by the operator. */ function splitByLogicalOperator(operator, node) { if (node.type === "LogicalExpression" && node.operator === operator) return [...splitByLogicalOperator(operator, node.left), ...splitByLogicalOperator(operator, node.right)]; return [node]; } function splitByOr(node) { return splitByLogicalOperator("||", node); } function splitByAnd(node) { return splitByLogicalOperator("&&", node); } function buildOrOperands(node) { return { node, operands: splitByOr(node).map((orOperand) => { return { node: orOperand, operands: splitByAnd(orOperand) }; }) }; } var no_dupe_v_else_if_default = { meta: { type: "problem", docs: { description: "disallow duplicate conditions in `v-if` / `v-else-if` chains", categories: ["vue3-essential", "vue2-essential"], url: "https://eslint.vuejs.org/rules/no-dupe-v-else-if.html" }, fixable: null, schema: [], messages: { unexpected: "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the `v-if` / `v-else-if` chain." } }, create(context) { const sourceCode = context.sourceCode; const tokenStore = sourceCode.parserServices.getTemplateBodyTokenStore && sourceCode.parserServices.getTemplateBodyTokenStore(); /** * Determines whether the two given nodes are considered to be equal. In particular, given that the nodes * represent expressions in a boolean context, `||` and `&&` can be considered as commutative operators. */ function isEqual(a, b) { return a.type === b.type && (require_index.default.equalTokens(a, b, tokenStore) || a.type === "LogicalExpression" && b.type === "LogicalExpression" && (a.operator === "||" || a.operator === "&&") && a.operator === b.operator && (isEqual(a.left, b.left) && isEqual(a.right, b.right) || isEqual(a.left, b.right) && isEqual(a.right, b.left))); } /** * Determines whether the first given AndOperands is a subset of the second given AndOperands. * * e.g. A: (a && b), B: (a && b && c): B is a subset of A. */ function isSubset(operandsA, operandsB) { return operandsA.operands.every((operandA) => operandsB.operands.some((operandB) => isEqual(operandA, operandB))); } return require_index.default.defineTemplateBodyVisitor(context, { "VAttribute[directive=true][key.name.name='else-if']"(node) { if (!node.value || !node.value.expression) return; const test = node.value.expression; const listToCheck = (test.type === "LogicalExpression" && test.operator === "&&" ? [...splitByAnd(test), test] : [test]).map(buildOrOperands); let current = node.parent.parent; while (current &&= require_index.default.prevSibling(current)) { const vIf = require_index.default.getDirective(current, "if"); const currentTestDir = vIf || require_index.default.getDirective(current, "else-if"); if (!currentTestDir) return; if (currentTestDir.value && currentTestDir.value.expression) { const currentOrOperands = buildOrOperands(currentTestDir.value.expression); for (const condition of listToCheck) if ((condition.operands = condition.operands.filter((orOperand) => currentOrOperands.operands.every((currentOrOperand) => !isSubset(currentOrOperand, orOperand)))).length === 0) { context.report({ node: condition.node, messageId: "unexpected" }); return; } } if (vIf) return; } } }); } }; exports.default = no_dupe_v_else_if_default;