|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * |
| 4 | + * issue https://github.com/vuejs/eslint-plugin-vue/issues/403 |
| 5 | + * Style guide: https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential |
| 6 | + * |
| 7 | + * I implemented it with reference to `no-confusing-v-for-v-if` |
| 8 | + */ |
| 9 | +'use strict' |
| 10 | + |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | +// Requirements |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +const utils = require('../utils') |
| 16 | + |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | +// Helpers |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +/** |
| 22 | + * Check whether the given `v-if` node is using the variable which is defined by the `v-for` directive. |
| 23 | + * @param {ASTNode} vIf The `v-if` attribute node to check. |
| 24 | + * @returns {boolean} `true` if the `v-if` is using the variable which is defined by the `v-for` directive. |
| 25 | + */ |
| 26 | +function isUsingIterationVar (vIf) { |
| 27 | + return !!getVForUsingIterationVar(vIf) |
| 28 | +} |
| 29 | + |
| 30 | +function getVForUsingIterationVar (vIf) { |
| 31 | + const element = vIf.parent.parent |
| 32 | + for (var i = 0; i < vIf.value.references.length; i++) { |
| 33 | + const reference = vIf.value.references[i] |
| 34 | + |
| 35 | + const targetVFor = element.variables.find(variable => |
| 36 | + variable.id.name === reference.id.name && |
| 37 | + variable.kind === 'v-for' |
| 38 | + ) |
| 39 | + if (targetVFor) { |
| 40 | + return targetVFor.id.parent |
| 41 | + } |
| 42 | + } |
| 43 | + return undefined |
| 44 | +} |
| 45 | + |
| 46 | +// ------------------------------------------------------------------------------ |
| 47 | +// Rule Definition |
| 48 | +// ------------------------------------------------------------------------------ |
| 49 | + |
| 50 | +module.exports = { |
| 51 | + meta: { |
| 52 | + docs: { |
| 53 | + description: 'disallow use v-if on the same element as v-for', |
| 54 | + category: undefined, |
| 55 | + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.3.0/docs/rules/no-use-v-if-with-v-for.md' |
| 56 | + }, |
| 57 | + fixable: null, |
| 58 | + schema: [{ |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + allowUsingIterationVar: { |
| 62 | + type: 'boolean' |
| 63 | + } |
| 64 | + } |
| 65 | + }] |
| 66 | + }, |
| 67 | + |
| 68 | + create (context) { |
| 69 | + const options = context.options[0] || {} |
| 70 | + const allowUsingIterationVar = options.allowUsingIterationVar === true // default false |
| 71 | + return utils.defineTemplateBodyVisitor(context, { |
| 72 | + "VAttribute[directive=true][key.name='if']" (node) { |
| 73 | + const element = node.parent.parent |
| 74 | + |
| 75 | + if (utils.hasDirective(element, 'for')) { |
| 76 | + if (isUsingIterationVar(node)) { |
| 77 | + if (!allowUsingIterationVar) { |
| 78 | + const vFor = getVForUsingIterationVar(node) |
| 79 | + context.report({ |
| 80 | + node, |
| 81 | + loc: node.loc, |
| 82 | + message: "The '{{iteratorName}}' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.", |
| 83 | + data: { |
| 84 | + iteratorName: vFor.right.name |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + } else { |
| 89 | + context.report({ |
| 90 | + node, |
| 91 | + loc: node.loc, |
| 92 | + message: "This 'v-if' should be moved to the wrapper element." |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments