eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
196 lines (195 loc) • 9.01 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/valid-v-slot.ts
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
/**
* Get all `v-slot` directives on a given element.
*/
function getSlotDirectivesOnElement(node) {
return require_index.default.getDirectives(node, "slot");
}
/**
* Get all `v-slot` directives on the children of a given element, grouped by
* `v-if`/`v-else-if`/`v-else` sequences.
*/
function getSlotDirectivesOnChildren(node) {
const groups = [];
for (const group of require_index.default.iterateChildElementsChains(node)) {
const slotDirs = group.map((childElement) => childElement.name === "template" ? require_index.default.getDirective(childElement, "slot") : null).filter(require_index.default.isDef);
if (slotDirs.length > 0) groups.push(slotDirs);
}
return groups;
}
/**
* Get the normalized name of a given `v-slot` directive node with modifiers after `v-slot:` directive.
*/
function getNormalizedName(node, sourceCode) {
if (node.key.argument == null) return "default";
return node.key.modifiers.length === 0 ? sourceCode.getText(node.key.argument) : sourceCode.text.slice(node.key.argument.range[0], node.key.range[1]);
}
/**
* Get all `v-slot` directives which are distributed to the same slot as a given `v-slot` directive node.
* @param vSlotGroups The result of `getAllNamedSlotElements()`.
*/
function filterSameSlot(vSlotGroups, currentVSlot, currentVSlotVForVars, sourceCode, tokenStore) {
const currentName = getNormalizedName(currentVSlot, sourceCode);
return vSlotGroups.map((vSlots) => vSlots.filter((vSlot) => {
if (getNormalizedName(vSlot, sourceCode) !== currentName) return false;
const vForExpr = getVSlotVForVariableIfUsingIterationVars(vSlot, require_index.default.getDirective(vSlot.parent.parent, "for"));
return currentVSlotVForVars && vForExpr ? areVSlotVForVariablesEqual(currentVSlotVForVars, vForExpr, tokenStore) : !currentVSlotVForVars && !vForExpr;
})).filter((slots) => slots.length > 0);
}
/**
* Determines whether the two given `v-slot` variables are considered to be equal.
*/
function areVSlotVForVariablesEqual(a, b, tokenStore) {
if (a.variables.length !== b.variables.length || !isEqual(a.expr.right, b.expr.right)) return false;
const checkedVarNames = /* @__PURE__ */ new Set();
const len = Math.min(a.expr.left.length, b.expr.left.length);
for (let index = 0; index < len; index++) {
const aPtn = a.expr.left[index];
const bPtn = b.expr.left[index];
const aVar = a.variables.find((v) => require_index.default.isInRange(aPtn, v.id));
const bVar = b.variables.find((v) => require_index.default.isInRange(bPtn, v.id));
if (aVar && bVar) {
if (aVar.id.name !== bVar.id.name || !isEqual(aPtn, bPtn)) return false;
checkedVarNames.add(aVar.id.name);
} else if (aVar || bVar) return false;
}
return a.variables.every((v) => checkedVarNames.has(v.id.name) || b.variables.some((bv) => v.id.name === bv.id.name));
/**
* Determines whether the two given nodes are considered to be equal.
*/
function isEqual(a, b) {
return a.type === b.type && require_index.default.equalTokens(a, b, tokenStore);
}
}
/**
* Gets the `v-for` directive and variable that provide the variables used by the given` v-slot` directive.
*/
function getVSlotVForVariableIfUsingIterationVars(vSlot, vFor) {
const expr = vFor && vFor.value && vFor.value.expression;
const variables = expr && getUsingIterationVars(vSlot.key.argument, vSlot.parent.parent);
return expr && variables && variables.length > 0 ? {
expr,
variables
} : null;
}
/**
* Gets iterative variables if a given argument node is using iterative variables that the element defined.
*/
function getUsingIterationVars(argument, element) {
const vars = [];
if (argument && argument.type === "VExpressionContainer") {
for (const { variable } of argument.references) if (variable != null && variable.kind === "v-for" && variable.id.range[0] > element.startTag.range[0] && variable.id.range[1] < element.startTag.range[1]) vars.push(variable);
}
return vars;
}
/**
* Check whether a given argument node is using an scope variable that the directive defined.
*/
function isUsingScopeVar(vSlot) {
const argument = vSlot.key.argument;
const value = vSlot.value;
if (argument && value && argument.type === "VExpressionContainer") {
for (const { variable } of argument.references) if (variable != null && variable.kind === "scope" && variable.id.range[0] > value.range[0] && variable.id.range[1] < value.range[1]) return true;
}
return false;
}
/**
* If `allowModifiers` option is set to `true`, check whether a given argument node has invalid modifiers like `v-slot.foo`.
* Otherwise, check whether a given argument node has at least one modifier.
*/
function hasInvalidModifiers(vSlot, shouldAllowModifiers) {
return shouldAllowModifiers ? vSlot.key.argument == null && vSlot.key.modifiers.length > 0 : vSlot.key.modifiers.length > 0;
}
var valid_v_slot_default = {
meta: {
type: "problem",
docs: {
description: "enforce valid `v-slot` directives",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/valid-v-slot.html"
},
fixable: null,
schema: [{
type: "object",
properties: { allowModifiers: { type: "boolean" } },
additionalProperties: false
}],
messages: {
ownerMustBeCustomElement: "'v-slot' directive must be owned by a custom element, but '{{name}}' is not.",
namedSlotMustBeOnTemplate: "Named slots must use '<template>' on a custom element.",
defaultSlotMustBeOnTemplate: "Default slot must use '<template>' on a custom element when there are other named slots.",
disallowDuplicateSlotsOnElement: "An element cannot have multiple 'v-slot' directives.",
disallowDuplicateSlotsOnChildren: "An element cannot have multiple '<template>' elements which are distributed to the same slot.",
disallowArgumentUseSlotParams: "Dynamic argument of 'v-slot' directive cannot use that slot parameter.",
disallowAnyModifier: "'v-slot' directive doesn't support any modifier.",
requireAttributeValue: "'v-slot' directive on a custom element requires that attribute value."
}
},
create(context) {
const sourceCode = context.sourceCode;
const tokenStore = sourceCode.parserServices.getTemplateBodyTokenStore && sourceCode.parserServices.getTemplateBodyTokenStore();
const shouldAllowModifiers = (context.options[0] || {}).allowModifiers === true;
return require_index.default.defineTemplateBodyVisitor(context, { "VAttribute[directive=true][key.name.name='slot']"(node) {
const isDefaultSlot = node.key.argument == null || node.key.argument.type === "VIdentifier" && node.key.argument.name === "default";
const element = node.parent.parent;
const parentElement = element.parent;
const ownerElement = element.name === "template" ? parentElement : element;
if (ownerElement.type === "VDocumentFragment") return;
const vSlotsOnElement = getSlotDirectivesOnElement(element);
const vSlotGroupsOnChildren = getSlotDirectivesOnChildren(ownerElement);
if (!require_index.default.isCustomComponent(ownerElement)) context.report({
node,
messageId: "ownerMustBeCustomElement",
data: { name: ownerElement.rawName }
});
if (!isDefaultSlot && element.name !== "template") context.report({
node,
messageId: "namedSlotMustBeOnTemplate"
});
if (ownerElement === element && vSlotGroupsOnChildren.length > 0) context.report({
node,
messageId: "defaultSlotMustBeOnTemplate"
});
if (vSlotsOnElement.length >= 2 && vSlotsOnElement[0] !== node) context.report({
node,
messageId: "disallowDuplicateSlotsOnElement"
});
if (ownerElement === parentElement) {
const vFor = require_index.default.getDirective(element, "for");
const vSlotVForVar = getVSlotVForVariableIfUsingIterationVars(node, vFor);
const vSlotGroupsOfSameSlot = filterSameSlot(vSlotGroupsOnChildren, node, vSlotVForVar, sourceCode, tokenStore);
if (vSlotGroupsOfSameSlot.length >= 2 && !vSlotGroupsOfSameSlot[0].includes(node)) context.report({
node,
messageId: "disallowDuplicateSlotsOnChildren"
});
if (vFor && !vSlotVForVar) context.report({
node,
messageId: "disallowDuplicateSlotsOnChildren"
});
}
if (isUsingScopeVar(node)) context.report({
node,
messageId: "disallowArgumentUseSlotParams"
});
const lastModifier = node.key.modifiers.at(-1);
if (lastModifier && hasInvalidModifiers(node, shouldAllowModifiers)) context.report({
node,
loc: {
start: node.key.modifiers[0].loc.start,
end: lastModifier.loc.end
},
messageId: "disallowAnyModifier"
});
if (ownerElement === element && isDefaultSlot && (!node.value || require_index.default.isEmptyValueDirective(node, context) || require_index.default.isEmptyExpressionValueDirective(node, context))) context.report({
node,
messageId: "requireAttributeValue"
});
} });
}
};
exports.default = valid_v_slot_default;