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,
|
2019-10-01 21:27:34 +08:00
|
|
|
TemplateChildNode,
|
2019-10-03 05:18:11 +08:00
|
|
|
SourceLocation,
|
|
|
|
createConditionalExpression,
|
|
|
|
ConditionalExpression,
|
|
|
|
JSChildNode,
|
2019-10-03 11:10:41 +08:00
|
|
|
SimpleExpressionNode,
|
|
|
|
FunctionExpression,
|
|
|
|
CallExpression,
|
|
|
|
createCallExpression,
|
|
|
|
createArrayExpression
|
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-10-03 11:10:41 +08:00
|
|
|
import { findDir, isTemplateNode, assert, isVSlot } from '../utils'
|
|
|
|
import { CREATE_SLOTS, RENDER_LIST } from '../runtimeConstants'
|
|
|
|
import { parseForExpression, createForLoopParams } from './vFor'
|
2019-09-28 08:29:20 +08:00
|
|
|
|
2019-10-03 05:18:11 +08:00
|
|
|
const isStaticExp = (p: JSChildNode): p is SimpleExpressionNode =>
|
|
|
|
p.type === NodeTypes.SIMPLE_EXPRESSION && p.isStatic
|
|
|
|
|
|
|
|
const defaultFallback = createSimpleExpression(`undefined`, false)
|
|
|
|
|
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)
|
|
|
|
) {
|
2019-10-03 11:10:41 +08:00
|
|
|
const vSlot = findDir(node, 'slot')
|
|
|
|
if (vSlot) {
|
|
|
|
const { addIdentifiers, removeIdentifiers } = context
|
|
|
|
const slotProps = vSlot.exp
|
|
|
|
slotProps && addIdentifiers(slotProps)
|
2019-09-29 04:02:08 +08:00
|
|
|
return () => {
|
2019-10-03 11:10:41 +08:00
|
|
|
slotProps && removeIdentifiers(slotProps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A NodeTransform that tracks scope identifiers for scoped slots with v-for.
|
|
|
|
// This transform is only applied in non-browser builds with { prefixIdentifiers: true }
|
|
|
|
export const trackVForSlotScopes: NodeTransform = (node, context) => {
|
|
|
|
let vFor
|
|
|
|
if (
|
|
|
|
isTemplateNode(node) &&
|
|
|
|
node.props.some(isVSlot) &&
|
|
|
|
(vFor = findDir(node, 'for'))
|
|
|
|
) {
|
|
|
|
const result = (vFor.parseResult = parseForExpression(
|
|
|
|
vFor.exp as SimpleExpressionNode,
|
|
|
|
context
|
|
|
|
))
|
|
|
|
if (result) {
|
|
|
|
const { value, key, index } = result
|
|
|
|
const { addIdentifiers, removeIdentifiers } = context
|
|
|
|
value && addIdentifiers(value)
|
|
|
|
key && addIdentifiers(key)
|
|
|
|
index && addIdentifiers(index)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
value && removeIdentifiers(value)
|
|
|
|
key && removeIdentifiers(key)
|
|
|
|
index && removeIdentifiers(index)
|
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-10-03 11:10:41 +08:00
|
|
|
node: ElementNode,
|
2019-09-28 08:29:20 +08:00
|
|
|
context: TransformContext
|
2019-10-01 09:17:12 +08:00
|
|
|
): {
|
2019-10-03 11:10:41 +08:00
|
|
|
slots: ObjectExpression | CallExpression
|
2019-10-03 05:18:11 +08:00
|
|
|
hasDynamicSlots: boolean
|
2019-10-01 09:17:12 +08:00
|
|
|
} {
|
2019-10-03 11:10:41 +08:00
|
|
|
const { children, loc } = node
|
|
|
|
const slotsProperties: Property[] = []
|
|
|
|
const dynamicSlots: (ConditionalExpression | CallExpression)[] = []
|
2019-10-03 05:18:11 +08:00
|
|
|
let hasDynamicSlots = false
|
2019-09-28 10:25:32 +08:00
|
|
|
|
|
|
|
// 1. Check for default slot with slotProps on component itself.
|
|
|
|
// <Comp v-slot="{ prop }"/>
|
2019-10-03 11:10:41 +08:00
|
|
|
const explicitDefaultSlot = findDir(node, 'slot', true)
|
2019-09-28 10:25:32 +08:00
|
|
|
if (explicitDefaultSlot) {
|
|
|
|
const { arg, exp, loc } = explicitDefaultSlot
|
|
|
|
if (arg) {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_NAMED_SLOT_ON_COMPONENT, loc)
|
|
|
|
)
|
|
|
|
}
|
2019-10-03 11:10:41 +08:00
|
|
|
slotsProperties.push(buildDefaultSlot(exp, children, loc))
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
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-10-01 21:27:34 +08:00
|
|
|
let extraneousChild: TemplateChildNode | undefined = undefined
|
2019-09-28 10:25:32 +08:00
|
|
|
const seenSlotNames = new Set<string>()
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
2019-10-03 05:18:11 +08:00
|
|
|
const slotElement = children[i]
|
2019-09-28 13:35:49 +08:00
|
|
|
let slotDir
|
2019-10-03 05:18:11 +08:00
|
|
|
|
2019-09-28 10:25:32 +08:00
|
|
|
if (
|
2019-10-03 11:10:41 +08:00
|
|
|
!isTemplateNode(slotElement) ||
|
|
|
|
!(slotDir = findDir(slotElement, 'slot', true))
|
2019-09-28 10:25:32 +08:00
|
|
|
) {
|
2019-10-03 05:18:11 +08:00
|
|
|
// not a <template v-slot>, skip.
|
2019-10-03 11:10:41 +08:00
|
|
|
if (slotElement.type !== NodeTypes.COMMENT && !extraneousChild) {
|
|
|
|
extraneousChild = slotElement
|
|
|
|
}
|
2019-10-03 05:18:11 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (explicitDefaultSlot) {
|
|
|
|
// already has on-component default slot - this is incorrect usage.
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_MIXED_SLOT_USAGE, slotDir.loc)
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
hasTemplateSlots = true
|
|
|
|
const { children: slotChildren, loc: slotLoc } = slotElement
|
|
|
|
const {
|
|
|
|
arg: slotName = createSimpleExpression(`default`, true),
|
|
|
|
exp: slotProps,
|
|
|
|
loc: dirLoc
|
|
|
|
} = slotDir
|
|
|
|
|
|
|
|
// check if name is dynamic.
|
2019-10-03 06:03:42 +08:00
|
|
|
let staticSlotName: string | undefined
|
2019-10-03 05:18:11 +08:00
|
|
|
if (isStaticExp(slotName)) {
|
|
|
|
staticSlotName = slotName ? slotName.content : `default`
|
|
|
|
} else {
|
|
|
|
hasDynamicSlots = true
|
|
|
|
}
|
|
|
|
|
|
|
|
const slotFunction = createFunctionExpression(
|
|
|
|
slotProps,
|
|
|
|
slotChildren,
|
|
|
|
false,
|
|
|
|
slotChildren.length ? slotChildren[0].loc : slotLoc
|
|
|
|
)
|
|
|
|
|
2019-10-03 11:10:41 +08:00
|
|
|
// check if this slot is conditional (v-if/v-for)
|
2019-10-03 06:03:42 +08:00
|
|
|
let vIf: DirectiveNode | undefined
|
|
|
|
let vElse: DirectiveNode | undefined
|
2019-10-03 11:10:41 +08:00
|
|
|
let vFor: DirectiveNode | undefined
|
2019-10-03 06:03:42 +08:00
|
|
|
if ((vIf = findDir(slotElement, 'if'))) {
|
2019-10-03 05:18:11 +08:00
|
|
|
hasDynamicSlots = true
|
2019-10-03 11:10:41 +08:00
|
|
|
dynamicSlots.push(
|
|
|
|
createConditionalExpression(
|
|
|
|
vIf.exp!,
|
|
|
|
buildDynamicSlot(slotName, slotFunction),
|
|
|
|
defaultFallback
|
2019-09-28 13:35:49 +08:00
|
|
|
)
|
2019-10-03 05:18:11 +08:00
|
|
|
)
|
2019-10-03 06:03:42 +08:00
|
|
|
} else if (
|
2019-10-03 11:10:41 +08:00
|
|
|
(vElse = findDir(slotElement, /^else(-if)?$/, true /* allowEmpty */))
|
2019-10-03 06:03:42 +08:00
|
|
|
) {
|
2019-10-03 11:10:41 +08:00
|
|
|
// find adjacent v-if
|
|
|
|
let j = i
|
|
|
|
let prev
|
|
|
|
while (j--) {
|
|
|
|
prev = children[j]
|
|
|
|
if (prev.type !== NodeTypes.COMMENT) {
|
|
|
|
break
|
2019-10-03 05:18:11 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-03 11:10:41 +08:00
|
|
|
if (prev && isTemplateNode(prev) && findDir(prev, 'if')) {
|
|
|
|
// remove node
|
|
|
|
children.splice(i, 1)
|
|
|
|
i--
|
|
|
|
__DEV__ && assert(dynamicSlots.length > 0)
|
|
|
|
// attach this slot to previous conditional
|
|
|
|
let conditional = dynamicSlots[
|
|
|
|
dynamicSlots.length - 1
|
|
|
|
] as ConditionalExpression
|
2019-10-03 06:03:42 +08:00
|
|
|
while (
|
|
|
|
conditional.alternate.type === NodeTypes.JS_CONDITIONAL_EXPRESSION
|
2019-09-28 13:35:49 +08:00
|
|
|
) {
|
2019-10-03 06:03:42 +08:00
|
|
|
conditional = conditional.alternate
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
2019-10-03 06:03:42 +08:00
|
|
|
conditional.alternate = vElse.exp
|
|
|
|
? createConditionalExpression(
|
|
|
|
vElse.exp,
|
2019-10-03 11:10:41 +08:00
|
|
|
buildDynamicSlot(slotName, slotFunction),
|
2019-10-03 06:03:42 +08:00
|
|
|
defaultFallback
|
|
|
|
)
|
2019-10-03 11:10:41 +08:00
|
|
|
: buildDynamicSlot(slotName, slotFunction)
|
2019-10-03 05:18:11 +08:00
|
|
|
} else {
|
2019-10-03 11:10:41 +08:00
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_ELSE_NO_ADJACENT_IF, vElse.loc)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if ((vFor = findDir(slotElement, 'for'))) {
|
|
|
|
hasDynamicSlots = true
|
|
|
|
const parseResult =
|
|
|
|
vFor.parseResult ||
|
|
|
|
parseForExpression(vFor.exp as SimpleExpressionNode, context)
|
|
|
|
if (parseResult) {
|
|
|
|
// Render the dynamic slots as an array and add it to the createSlot()
|
|
|
|
// args. The runtime knows how to handle it appropriately.
|
|
|
|
dynamicSlots.push(
|
|
|
|
createCallExpression(context.helper(RENDER_LIST), [
|
|
|
|
parseResult.source,
|
|
|
|
createFunctionExpression(
|
|
|
|
createForLoopParams(parseResult),
|
|
|
|
buildDynamicSlot(slotName, slotFunction),
|
|
|
|
true
|
2019-10-03 06:03:42 +08:00
|
|
|
)
|
2019-10-03 11:10:41 +08:00
|
|
|
])
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_FOR_MALFORMED_EXPRESSION, vFor.loc)
|
2019-09-28 13:35:49 +08:00
|
|
|
)
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
2019-10-03 05:18:11 +08:00
|
|
|
} else {
|
|
|
|
// check duplicate static names
|
|
|
|
if (staticSlotName) {
|
|
|
|
if (seenSlotNames.has(staticSlotName)) {
|
|
|
|
context.onError(
|
|
|
|
createCompilerError(ErrorCodes.X_DUPLICATE_SLOT_NAMES, dirLoc)
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seenSlotNames.add(staticSlotName)
|
|
|
|
}
|
2019-10-03 11:10:41 +08:00
|
|
|
slotsProperties.push(createObjectProperty(slotName, slotFunction))
|
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.
|
2019-10-03 11:10:41 +08:00
|
|
|
slotsProperties.push(buildDefaultSlot(undefined, children, loc))
|
|
|
|
}
|
|
|
|
|
|
|
|
let slots: ObjectExpression | CallExpression = createObjectExpression(
|
|
|
|
slotsProperties,
|
|
|
|
loc
|
|
|
|
)
|
|
|
|
if (dynamicSlots.length) {
|
|
|
|
slots = createCallExpression(context.helper(CREATE_SLOTS), [
|
|
|
|
slots,
|
|
|
|
createArrayExpression(dynamicSlots)
|
|
|
|
])
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 09:17:12 +08:00
|
|
|
return {
|
2019-10-03 11:10:41 +08:00
|
|
|
slots,
|
2019-10-03 05:18:11 +08:00
|
|
|
hasDynamicSlots
|
2019-10-01 09:17:12 +08:00
|
|
|
}
|
2019-09-28 10:25:32 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 05:18:11 +08:00
|
|
|
function buildDefaultSlot(
|
2019-09-28 10:25:32 +08:00
|
|
|
slotProps: ExpressionNode | undefined,
|
2019-10-01 21:27:34 +08:00
|
|
|
children: TemplateChildNode[],
|
2019-09-28 10:25:32 +08:00
|
|
|
loc: SourceLocation
|
|
|
|
): Property {
|
|
|
|
return createObjectProperty(
|
2019-10-03 11:10:41 +08:00
|
|
|
`default`,
|
2019-09-28 10:25:32 +08:00
|
|
|
createFunctionExpression(
|
|
|
|
slotProps,
|
|
|
|
children,
|
2019-10-02 04:48:20 +08:00
|
|
|
false,
|
2019-09-28 10:25:32 +08:00
|
|
|
children.length ? children[0].loc : loc
|
2019-10-02 00:25:13 +08:00
|
|
|
)
|
2019-09-28 10:25:32 +08:00
|
|
|
)
|
2019-09-28 08:29:20 +08:00
|
|
|
}
|
2019-10-03 11:10:41 +08:00
|
|
|
|
|
|
|
function buildDynamicSlot(
|
|
|
|
name: ExpressionNode,
|
|
|
|
fn: FunctionExpression
|
|
|
|
): ObjectExpression {
|
|
|
|
return createObjectExpression([
|
|
|
|
createObjectProperty(`name`, name),
|
|
|
|
createObjectProperty(`fn`, fn)
|
|
|
|
])
|
|
|
|
}
|