@@ -719,6 +719,23 @@ describe('compiler: transform component slots', () => {
|
||||
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test('generate flag on forwarded slots', () => {
|
||||
const { slots } = parseWithSlots(`<Comp><slot/></Comp>`)
|
||||
expect(slots).toMatchObject({
|
||||
type: NodeTypes.JS_OBJECT_EXPRESSION,
|
||||
properties: [
|
||||
{
|
||||
key: { content: `default` },
|
||||
value: { type: NodeTypes.JS_FUNCTION_EXPRESSION }
|
||||
},
|
||||
{
|
||||
key: { content: `_` },
|
||||
value: { content: `3` } // forwarded
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
describe('errors', () => {
|
||||
test('error on extraneous children w/ named default slot', () => {
|
||||
const onError = jest.fn()
|
||||
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
} from '../utils'
|
||||
import { CREATE_SLOTS, RENDER_LIST, WITH_CTX } from '../runtimeHelpers'
|
||||
import { parseForExpression, createForLoopParams } from './vFor'
|
||||
import { SlotFlags } from '@vue/shared/src'
|
||||
|
||||
const defaultFallback = createSimpleExpression(`undefined`, false)
|
||||
|
||||
@@ -321,13 +322,19 @@ export function buildSlots(
|
||||
}
|
||||
}
|
||||
|
||||
const slotFlag = hasDynamicSlots
|
||||
? SlotFlags.DYNAMIC
|
||||
: hasForwardedSlots(node.children)
|
||||
? SlotFlags.FORWARDED
|
||||
: SlotFlags.STABLE
|
||||
|
||||
let slots = createObjectExpression(
|
||||
slotsProperties.concat(
|
||||
createObjectProperty(
|
||||
`_`,
|
||||
// 2 = compiled but dynamic = can skip normalization, but must run diff
|
||||
// 1 = compiled and static = can skip normalization AND diff as optimized
|
||||
createSimpleExpression(hasDynamicSlots ? `2` : `1`, false)
|
||||
createSimpleExpression('' + slotFlag, false)
|
||||
)
|
||||
),
|
||||
loc
|
||||
@@ -354,3 +361,19 @@ function buildDynamicSlot(
|
||||
createObjectProperty(`fn`, fn)
|
||||
])
|
||||
}
|
||||
|
||||
function hasForwardedSlots(children: TemplateChildNode[]): boolean {
|
||||
for (let i = 0; i < children.length; i++) {
|
||||
const child = children[i]
|
||||
if (child.type === NodeTypes.ELEMENT) {
|
||||
if (
|
||||
child.tagType === ElementTypes.SLOT ||
|
||||
(child.tagType === ElementTypes.ELEMENT &&
|
||||
hasForwardedSlots(child.children))
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user