UNPKG

eslint-plugin-vue

Version:

Official ESLint plugin for Vue.js

74 lines (73 loc) • 2.67 kB
const require_index = require("../utils/index.js"); // lib/rules/no-use-v-if-with-v-for.ts /** * @author Yosuke Ota * * Style guide: https://vuejs.org/style-guide/rules-essential.html#avoid-v-if-with-v-for */ /** * Check whether the given `v-if` node is using the variable which is defined by the `v-for` directive. */ function isUsingIterationVar(vIf) { return getVForUsingIterationVar(vIf) !== null; } function getVForUsingIterationVar(vIf) { if (!vIf.value) return null; const element = vIf.parent.parent; for (const reference of vIf.value.references) { const targetVFor = element.variables.find((variable) => variable.id.name === reference.id.name && variable.kind === "v-for"); if (targetVFor) return targetVFor; } return null; } var no_use_v_if_with_v_for_default = { meta: { type: "suggestion", docs: { description: "disallow using `v-if` on the same element as `v-for`", categories: ["vue3-essential", "vue2-essential"], url: "https://eslint.vuejs.org/rules/no-use-v-if-with-v-for.html" }, fixable: null, schema: [{ type: "object", properties: { allowUsingIterationVar: { type: "boolean" } }, additionalProperties: false }], messages: { movedToWrapper: "This 'v-if' should be moved to the wrapper element.", shouldUseComputed: "The '{{iteratorName}}' {{kind}} 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'." } }, create(context) { const shouldAllowUsingIterationVar = (context.options[0] || {}).allowUsingIterationVar === true; return require_index.default.defineTemplateBodyVisitor(context, { "VAttribute[directive=true][key.name.name='if']"(node) { const element = node.parent.parent; if (require_index.default.hasDirective(element, "for")) { if (isUsingIterationVar(node)) { if (!shouldAllowUsingIterationVar) { const vForVar = getVForUsingIterationVar(node); if (!vForVar) return; let targetVForExpr = vForVar.id.parent; while (targetVForExpr.type !== "VForExpression") targetVForExpr = targetVForExpr.parent; const iteratorNode = targetVForExpr.right; context.report({ node, loc: node.loc, messageId: "shouldUseComputed", data: { iteratorName: iteratorNode.type === "Identifier" ? iteratorNode.name : context.sourceCode.getText(iteratorNode), kind: iteratorNode.type === "Identifier" ? "variable" : "expression" } }); } } else context.report({ node, loc: node.loc, messageId: "movedToWrapper" }); } } }); } }; exports.default = no_use_v_if_with_v_for_default;