2019-09-28 08:29:20 +08:00
|
|
|
import {
|
|
|
|
ElementNode,
|
|
|
|
ObjectExpression,
|
|
|
|
createObjectExpression,
|
|
|
|
NodeTypes,
|
2019-09-28 10:25:32 +08:00
|
|
|
createObjectProperty,
|
|
|
|
createSimpleExpression,
|
|
|
|
createFunctionExpression,
|
|
|
|
DirectiveNode,
|
|
|
|
ElementTypes,
|
|
|
|
ExpressionNode,
|
|
|
|
Property,
|
|
|
|
ChildNode,
|
|
|
|
SourceLocation
|
2019-09-28 08:29:20 +08:00
|
|
|
} from '../ast'
|
2019-09-28 12:19:24 +08:00
|
|
|
import { TransformContext, NodeTransform } from '../transform'
|
2019-09-28 08:29:20 +08:00
|
|
|
import { createCompilerError, ErrorCodes } from '../errors'
|
2019-09-28 10:25:32 +08:00
|
|
|
import { isString } from '@vue/shared'
|
|
|
|
|
|
|
|
const isVSlot = (p: ElementNode['props'][0]): p is DirectiveNode =>
|
|
|
|
p.type === NodeTypes.DIRECTIVE && p.name === 'slot'
|
2019-09-28 08:29:20 +08:00
|
|
|
|
2019-09-28 12:19:24 +08:00
|
|
|
// A NodeTransform that tracks scope identifiers for scoped slots so that they
|
|
|
|
// don't get prefixed by transformExpression. This transform is only applied
|
|
|
|
// in non-browser builds with { prefixIdentifiers: true }
|
|
|
|
export const trackSlotScopes: NodeTransform = (node, context) => {
|
|
|
|
if (
|
|
|
|
node.type === NodeTypes.ELEMENT &&
|
|
|
|
(node.tagType === ElementTypes.COMPONENT ||
|
|
|
|
node.tagType === ElementTypes.TEMPLATE)
|
|
|
|
) {
|
|
|
|
const vSlot = node.props.find(isVSlot)
|
|
|
|
if (vSlot && vSlot.exp) {
|
2019-09-29 04:02:08 +08:00
|
|
|
context.addIdentifiers(vSlot.exp)
|
|
|
|
return () => {
|
|
|
|
context.removeIdentifiers(vSlot.exp!)
|
2019-09-28 12:19:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instead of being a DirectiveTransform, v-slot processing is called during
|
|
|
|
// transformElement to build the slots object for a component.
|
2019-09-28 08:29:20 +08:00
|
|
|
export function buildSlots(
|
2019-09-28 10:25:32 +08:00
|
|
|
{ props, children, loc }: ElementNode,
|
2019-09-28 08:29:20 +08:00
|
|
|
context: TransformContext
|
2019-10-01 09:17:12 +08:00
|
|
|
): {
|
|
|
|
slots: ObjectExpression
|
|
|
|
hasDynamicSlotName: boolean
|
|
|
|
} {
|
2019-09-28 10:25:32 +08:00
|
|
|
const slots: Property[] = []
|
2019-10-01 09:17:12 +08:00
|
|
|
let hasDynamicSlotName = false
|
2019-09-28 10:25:32 +08:00
|
|
|
|
|
|
|
// 1. Check for default slot with slotProps on component itself.
|
|
|
|
// <Comp v-slot="{ prop }"/>
|
|
|
|
const explicitDefaultSlot = props.find(isVSlot)
|
|
|
|
if (explicitDefaultSlot) {
|
|
|
|
const { arg, exp, loc } = explicitDefaultSlot
|
|
|
|
if (arg) {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_NAMED_SLOT_ON_COMPONENT, loc)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
slots.push(buildSlot(`default`, exp, children, loc))
|
|
|
|
}
|
2019-09-28 08:29:20 +08:00
|
|
|
|
2019-09-28 10:25:32 +08:00
|
|
|
// 2. Iterate through children and check for template slots
|
|
|
|
// <template v-slot:foo="{ prop }">
|
|
|
|
let hasTemplateSlots = false
|
2019-09-28 13:35:49 +08:00
|
|
|
let extraneousChild: ChildNode | undefined = undefined
|
2019-09-28 10:25:32 +08:00
|
|
|
const seenSlotNames = new Set<string>()
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const child = children[i]
|
2019-09-28 13:35:49 +08:00
|
|
|
let slotDir
|
2019-09-28 10:25:32 +08:00
|
|
|
if (
|
|
|
|
child.type === NodeTypes.ELEMENT &&
|
2019-09-28 13:35:49 +08:00
|
|
|
child.tagType === ElementTypes.TEMPLATE &&
|
|
|
|
(slotDir = child.props.find(isVSlot))
|
2019-09-28 10:25:32 +08:00
|
|
|
) {
|
2019-09-28 13:35:49 +08:00
|
|
|
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.
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, dirLoc)
|
|
|
|
)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
!slotName ||
|
|
|
|
(slotName.type === NodeTypes.SIMPLE_EXPRESSION && slotName.isStatic)
|
|
|
|
) {
|
2019-10-01 09:17:12 +08:00
|
|
|
// check duplicate slot names
|
2019-09-28 13:35:49 +08:00
|
|
|
const name = slotName ? slotName.content : `default`
|
|
|
|
if (seenSlotNames.has(name)) {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc)
|
|
|
|
)
|
|
|
|
continue
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
2019-09-28 13:35:49 +08:00
|
|
|
seenSlotNames.add(name)
|
2019-10-01 09:17:12 +08:00
|
|
|
} else {
|
|
|
|
hasDynamicSlotName = true
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
2019-09-28 13:35:49 +08:00
|
|
|
slots.push(
|
|
|
|
buildSlot(slotName || `default`, slotProps, children, nodeLoc)
|
|
|
|
)
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
2019-09-28 13:35:49 +08:00
|
|
|
} else if (!extraneousChild) {
|
|
|
|
extraneousChild = child
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 13:35:49 +08:00
|
|
|
if (hasTemplateSlots && extraneousChild) {
|
2019-09-28 10:25:32 +08:00
|
|
|
context.onError(
|
|
|
|
createCompilerError(
|
|
|
|
ErrorCodes.X_EXTRANEOUS_NON_SLOT_CHILDREN,
|
2019-09-28 13:35:49 +08:00
|
|
|
extraneousChild.loc
|
2019-09-28 10:25:32 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!explicitDefaultSlot && !hasTemplateSlots) {
|
|
|
|
// implicit default slot.
|
|
|
|
slots.push(buildSlot(`default`, undefined, children, loc))
|
|
|
|
}
|
|
|
|
|
2019-10-01 09:17:12 +08:00
|
|
|
return {
|
|
|
|
slots: createObjectExpression(slots, loc),
|
|
|
|
hasDynamicSlotName
|
|
|
|
}
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildSlot(
|
|
|
|
name: string | ExpressionNode,
|
|
|
|
slotProps: ExpressionNode | undefined,
|
|
|
|
children: ChildNode[],
|
|
|
|
loc: SourceLocation
|
|
|
|
): Property {
|
|
|
|
return createObjectProperty(
|
|
|
|
isString(name) ? createSimpleExpression(name, true, loc) : name,
|
|
|
|
createFunctionExpression(
|
|
|
|
slotProps,
|
|
|
|
children,
|
|
|
|
children.length ? children[0].loc : loc
|
|
|
|
),
|
|
|
|
loc
|
|
|
|
)
|
2019-09-28 08:29:20 +08:00
|
|
|
}
|