Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ export function render(_ctx) {
}"
`;

exports[`compiler: transform slot > nested component should not inherit parent slots 1`] = `
"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _component_Bar = _resolveComponent("Bar")
const _component_Foo = _resolveComponent("Foo")
const n2 = _createComponentWithFallback(_component_Foo, null, {
"header": _withVaporCtx(() => {
return null
}),
"default": _withVaporCtx(() => {
const n1 = _createComponentWithFallback(_component_Bar)
return n1
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > nested component slot 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, withVaporCtx as _withVaporCtx } from 'vue';

Expand Down
10 changes: 10 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ describe('compiler: transform slot', () => {
})
})

test('nested component should not inherit parent slots', () => {
const { code } = compileWithSlots(`
<Foo>
<template #header></template>
<Bar />
</Foo>
`)
expect(code).toMatchSnapshot()
})

test('named slots w/ implicit default slot', () => {
const { ir, code } = compileWithSlots(
`<Comp>
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
type IRProps,
type IRPropsDynamicAttribute,
type IRPropsStatic,
type IRSlots,
type VaporDirectiveNode,
} from '../ir'
import { EMPTY_EXPRESSION } from './utils'
Expand All @@ -51,6 +52,20 @@ export const isReservedProp: (key: string) => boolean = /*#__PURE__*/ makeMap(
export const transformElement: NodeTransform = (node, context) => {
let effectIndex = context.block.effect.length
const getEffectIndex = () => effectIndex++

// If the element is a component, we need to isolate its slots context.
// This ensures that slots defined for this component are not accidentally
// inherited by its children components.
let parentSlots: IRSlots[] | undefined
if (
node.type === NodeTypes.ELEMENT &&
(node.tagType === ElementTypes.COMPONENT ||
context.options.isCustomElement(node.tag))
) {
parentSlots = context.slots
context.slots = []
}

return function postTransformElement() {
;({ node } = context)
if (
Expand Down Expand Up @@ -96,6 +111,10 @@ export const transformElement: NodeTransform = (node, context) => {
getEffectIndex,
)
}

if (parentSlots) {
context.slots = parentSlots
}
}
}

Expand Down
Loading