123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 'use strict'
- const utils = require('../utils')
- 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) {
- const orOperands = splitByOr(node)
- return {
- node,
- operands: orOperands.map((orOperand) => {
- const andOperands = splitByAnd(orOperand)
- return {
- node: orOperand,
- operands: andOperands
- }
- })
- }
- }
- module.exports = {
- meta: {
- type: 'problem',
- docs: {
- description:
- 'disallow duplicate conditions in `v-if` / `v-else-if` chains',
- categories: ['vue3-essential', '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 tokenStore =
- context.parserServices.getTemplateBodyTokenStore &&
- context.parserServices.getTemplateBodyTokenStore()
-
- function equal(a, b) {
- if (a.type !== b.type) {
- return false
- }
- if (
- a.type === 'LogicalExpression' &&
- b.type === 'LogicalExpression' &&
- (a.operator === '||' || a.operator === '&&') &&
- a.operator === b.operator
- ) {
- return (
- (equal(a.left, b.left) && equal(a.right, b.right)) ||
- (equal(a.left, b.right) && equal(a.right, b.left))
- )
- }
- return utils.equalTokens(a, b, tokenStore)
- }
-
- function isSubset(operandsA, operandsB) {
- return operandsA.operands.every((operandA) =>
- operandsB.operands.some((operandB) => equal(operandA, operandB))
- )
- }
- return utils.defineTemplateBodyVisitor(context, {
- "VAttribute[directive=true][key.name.name='else-if']"(node) {
- if (!node.value || !node.value.expression) {
- return
- }
- const test = node.value.expression
- const conditionsToCheck =
- test.type === 'LogicalExpression' && test.operator === '&&'
- ? [...splitByAnd(test), test]
- : [test]
- const listToCheck = conditionsToCheck.map(buildOrOperands)
-
- let current = node.parent.parent
- while (current && (current = utils.prevSibling(current))) {
- const vIf = utils.getDirective(current, 'if')
- const currentTestDir = vIf || utils.getDirective(current, 'else-if')
- if (!currentTestDir) {
- return
- }
- if (currentTestDir.value && currentTestDir.value.expression) {
- const currentOrOperands = buildOrOperands(
- currentTestDir.value.expression
- )
- for (const condition of listToCheck) {
- const operands = (condition.operands = condition.operands.filter(
- (orOperand) => {
- return !currentOrOperands.operands.some((currentOrOperand) =>
- isSubset(currentOrOperand, orOperand)
- )
- }
- ))
- if (!operands.length) {
- context.report({
- node: condition.node,
- messageId: 'unexpected'
- })
- return
- }
- }
- }
- if (vIf) {
- return
- }
- }
- }
- })
- }
- }
|