Skip to content

Commit 0448125

Browse files
fix(compiler-core): dedupe renderSlot's default props (#4557)
1 parent ed6470c commit 0448125

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ describe('compiler: transform <slot> outlets', () => {
346346
callee: RENDER_SLOT,
347347
arguments: [`$slots`, `"default"`, `{}`, `undefined`, `true`]
348348
})
349+
const fallback = parseWithSlots(`<slot>fallback</slot>`, {
350+
slotted: false,
351+
scopeId: 'foo'
352+
})
353+
354+
const child = {
355+
type: NodeTypes.JS_FUNCTION_EXPRESSION,
356+
params: [],
357+
returns: [
358+
{
359+
type: NodeTypes.TEXT,
360+
content: `fallback`
361+
}
362+
]
363+
}
364+
expect((fallback.children[0] as ElementNode).codegenNode).toMatchObject({
365+
type: NodeTypes.JS_CALL_EXPRESSION,
366+
callee: RENDER_SLOT,
367+
arguments: [`$slots`, `"default"`, `{}`, child, `true`]
368+
})
349369
})
350370

351371
test(`error on unexpected custom directive on <slot>`, () => {

packages/compiler-core/src/transforms/transformSlotOutlet.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,27 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
2020

2121
const slotArgs: CallExpression['arguments'] = [
2222
context.prefixIdentifiers ? `_ctx.$slots` : `$slots`,
23-
slotName
23+
slotName,
24+
'{}',
25+
'undefined',
26+
'true'
2427
]
28+
let expectedLen = 2
2529

2630
if (slotProps) {
27-
slotArgs.push(slotProps)
31+
slotArgs[2] = slotProps
32+
expectedLen = 3
2833
}
2934

3035
if (children.length) {
31-
if (!slotProps) {
32-
slotArgs.push(`{}`)
33-
}
34-
slotArgs.push(createFunctionExpression([], children, false, false, loc))
36+
slotArgs[3] = createFunctionExpression([], children, false, false, loc)
37+
expectedLen = 4
3538
}
3639

3740
if (context.scopeId && !context.slotted) {
38-
if (!slotProps) {
39-
slotArgs.push(`{}`)
40-
}
41-
if (!children.length) {
42-
slotArgs.push(`undefined`)
43-
}
44-
slotArgs.push(`true`)
41+
expectedLen = 5
4542
}
43+
slotArgs.splice(expectedLen) // remove unused arguments
4644

4745
node.codegenNode = createCallExpression(
4846
context.helper(RENDER_SLOT),

0 commit comments

Comments
 (0)