refactor: simplify slot extraneous child check

This commit is contained in:
Evan You 2019-09-28 01:35:49 -04:00
parent 6461b3853e
commit a792d697a3

View File

@ -66,18 +66,18 @@ export function buildSlots(
// 2. Iterate through children and check for template slots
// <template v-slot:foo="{ prop }">
let hasTemplateSlots = false
let extraneousChild: ChildNode | undefined = undefined
const seenSlotNames = new Set<string>()
const nonSlotChildren: ChildNode[] = []
for (let i = 0; i < children.length; i++) {
const child = children[i]
let slotDir
if (
child.type === NodeTypes.ELEMENT &&
child.tagType === ElementTypes.TEMPLATE
child.tagType === ElementTypes.TEMPLATE &&
(slotDir = child.props.find(isVSlot))
) {
const { props, children, loc: nodeLoc } = child
const slotDir = props.find(isVSlot)
if (slotDir) {
hasTemplateSlots = true
const { children, loc: nodeLoc } = child
const { arg: slotName, exp: slotProps, loc: dirLoc } = slotDir
if (explicitDefaultSlot) {
// already has on-component default slot - this is incorrect usage.
@ -104,19 +104,16 @@ export function buildSlots(
buildSlot(slotName || `default`, slotProps, children, nodeLoc)
)
}
} else {
nonSlotChildren.push(child)
}
} else {
nonSlotChildren.push(child)
} else if (!extraneousChild) {
extraneousChild = child
}
}
if (hasTemplateSlots && nonSlotChildren.length) {
if (hasTemplateSlots && extraneousChild) {
context.onError(
createCompilerError(
ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN,
nonSlotChildren[0].loc
extraneousChild.loc
)
)
}