Skip to content

Commit 1b2d90a

Browse files
authored
Merge branch 'minor' into edison/feat/SuspenseInterop
2 parents f62addb + 0668ea3 commit 1b2d90a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,25 @@ export function render(_ctx) {
228228
}"
229229
`;
230230
231+
exports[`compiler: transform slot > nested component should not inherit parent slots 1`] = `
232+
"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback } from 'vue';
233+
234+
export function render(_ctx) {
235+
const _component_Bar = _resolveComponent("Bar")
236+
const _component_Foo = _resolveComponent("Foo")
237+
const n2 = _createComponentWithFallback(_component_Foo, null, {
238+
"header": _withVaporCtx(() => {
239+
return null
240+
}),
241+
"default": _withVaporCtx(() => {
242+
const n1 = _createComponentWithFallback(_component_Bar)
243+
return n1
244+
})
245+
}, true)
246+
return n2
247+
}"
248+
`;
249+
231250
exports[`compiler: transform slot > nested component slot 1`] = `
232251
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, withVaporCtx as _withVaporCtx } from 'vue';
233252

packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ describe('compiler: transform slot', () => {
155155
})
156156
})
157157

158+
test('nested component should not inherit parent slots', () => {
159+
const { code } = compileWithSlots(`
160+
<Foo>
161+
<template #header></template>
162+
<Bar />
163+
</Foo>
164+
`)
165+
expect(code).toMatchSnapshot()
166+
})
167+
158168
test('named slots w/ implicit default slot', () => {
159169
const { ir, code } = compileWithSlots(
160170
`<Comp>

packages/compiler-vapor/src/transforms/transformElement.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
type IRProps,
3838
type IRPropsDynamicAttribute,
3939
type IRPropsStatic,
40+
type IRSlots,
4041
type VaporDirectiveNode,
4142
} from '../ir'
4243
import { EMPTY_EXPRESSION } from './utils'
@@ -51,6 +52,20 @@ export const isReservedProp: (key: string) => boolean = /*#__PURE__*/ makeMap(
5152
export const transformElement: NodeTransform = (node, context) => {
5253
let effectIndex = context.block.effect.length
5354
const getEffectIndex = () => effectIndex++
55+
56+
// If the element is a component, we need to isolate its slots context.
57+
// This ensures that slots defined for this component are not accidentally
58+
// inherited by its children components.
59+
let parentSlots: IRSlots[] | undefined
60+
if (
61+
node.type === NodeTypes.ELEMENT &&
62+
(node.tagType === ElementTypes.COMPONENT ||
63+
context.options.isCustomElement(node.tag))
64+
) {
65+
parentSlots = context.slots
66+
context.slots = []
67+
}
68+
5469
return function postTransformElement() {
5570
;({ node } = context)
5671
if (
@@ -96,6 +111,10 @@ export const transformElement: NodeTransform = (node, context) => {
96111
getEffectIndex,
97112
)
98113
}
114+
115+
if (parentSlots) {
116+
context.slots = parentSlots
117+
}
99118
}
100119
}
101120

0 commit comments

Comments
 (0)