Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): fix the detection of forwarded slots #3353

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/compiler-core/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { transformElement } from '../../src/transforms/transformElement'
import { transformOn } from '../../src/transforms/vOn'
import { transformBind } from '../../src/transforms/vBind'
import { transformExpression } from '../../src/transforms/transformExpression'
import { transformSlotOutlet } from '../../src/transforms/transformSlotOutlet'
import {
trackSlotScopes,
trackVForSlotScopes
Expand All @@ -34,6 +35,7 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) {
...(options.prefixIdentifiers
? [trackVForSlotScopes, transformExpression]
: []),
transformSlotOutlet,
transformElement,
trackSlotScopes
],
Expand Down Expand Up @@ -737,9 +739,8 @@ 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({
describe('forwarded slots', () => {
const toMatch = {
type: NodeTypes.JS_OBJECT_EXPRESSION,
properties: [
{
Expand All @@ -751,6 +752,20 @@ describe('compiler: transform component slots', () => {
value: { content: `3 /* FORWARDED */` }
}
]
}
test('<slot> tag only', () => {
const { slots } = parseWithSlots(`<Comp><slot/></Comp>`)
expect(slots).toMatchObject(toMatch)
})

test('<slot> tag w/ v-if', () => {
const { slots } = parseWithSlots(`<Comp><slot v-if="ok"/></Comp>`)
expect(slots).toMatchObject(toMatch)
})

test('<slot> tag w/ v-for', () => {
const { slots } = parseWithSlots(`<Comp><slot v-for="a in b"/></Comp>`)
expect(slots).toMatchObject(toMatch)
})
})

Expand Down
27 changes: 19 additions & 8 deletions packages/compiler-core/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,25 @@ function buildDynamicSlot(
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
}
switch (child.type) {
case NodeTypes.ELEMENT:
if (
child.tagType === ElementTypes.SLOT ||
(child.tagType === ElementTypes.ELEMENT &&
hasForwardedSlots(child.children))
) {
return true
}
break
case NodeTypes.IF:
if (hasForwardedSlots(child.branches)) return true
break
case NodeTypes.IF_BRANCH:
case NodeTypes.FOR:
if (hasForwardedSlots(child.children)) return true
break
default:
break
}
}
return false
Expand Down