Skip to content

Commit 6f9ac2f

Browse files
committed
chore: minor tweaks
1 parent 4070a94 commit 6f9ac2f

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ export const trackVForSlotScopes: NodeTransform = (node, context) => {
100100

101101
export type SlotFnBuilder = (
102102
slotProps: ExpressionNode | undefined,
103-
vForProps: ExpressionNode | undefined,
103+
vForExp: ExpressionNode | undefined,
104104
slotChildren: TemplateChildNode[],
105105
loc: SourceLocation
106106
) => FunctionExpression
107107

108-
const buildClientSlotFn: SlotFnBuilder = (props, _vFor, children, loc) =>
108+
const buildClientSlotFn: SlotFnBuilder = (props, _vForExp, children, loc) =>
109109
createFunctionExpression(
110110
props,
111111
children,
@@ -139,9 +139,6 @@ export function buildSlots(
139139
hasDynamicSlots = hasScopeRef(node, context.identifiers)
140140
}
141141

142-
let vFor: DirectiveNode | undefined
143-
let vForProps: ExpressionNode | undefined
144-
145142
// 1. Check for slot with slotProps on component itself.
146143
// <Comp v-slot="{ prop }"/>
147144
const onComponentSlot = findDir(node, 'slot', true)
@@ -150,12 +147,10 @@ export function buildSlots(
150147
if (arg && !isStaticExp(arg)) {
151148
hasDynamicSlots = true
152149
}
153-
vFor = findDir(node, 'for')
154-
if (vFor) vForProps = vFor.exp
155150
slotsProperties.push(
156151
createObjectProperty(
157152
arg || createSimpleExpression('default', true),
158-
buildSlotFn(exp, vForProps, children, loc)
153+
buildSlotFn(exp, undefined, children, loc)
159154
)
160155
)
161156
}
@@ -207,12 +202,10 @@ export function buildSlots(
207202
hasDynamicSlots = true
208203
}
209204

210-
vFor = findDir(slotElement, 'for')
211-
if (vFor) vForProps = vFor.exp
212-
else vForProps = undefined
205+
const vFor = findDir(slotElement, 'for')
213206
const slotFunction = buildSlotFn(
214207
slotProps,
215-
vForProps,
208+
vFor?.exp,
216209
slotChildren,
217210
slotLoc
218211
)

packages/compiler-ssr/__tests__/ssrComponent.spec.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,30 @@ describe('ssr: components', () => {
181181
})
182182

183183
test('v-for slot', () => {
184-
expect(
185-
compile(`<foo>
186-
<template v-for="key in names" v-slot:[key]="{ msg }">{{ msg + key + bar }}</template>
187-
</foo>`).code
188-
).toMatchInlineSnapshot(`
184+
const { code } = compile(`<foo>
185+
<template v-for="(key, index) in names" v-slot:[key]="{ msg }">{{ msg + key + index + bar }}</template>
186+
</foo>`)
187+
expect(code).not.toMatch(`_ctx.msg`)
188+
expect(code).not.toMatch(`_ctx.key`)
189+
expect(code).not.toMatch(`_ctx.index`)
190+
expect(code).toMatch(`_ctx.bar`)
191+
expect(code).toMatchInlineSnapshot(`
189192
"const { resolveComponent: _resolveComponent, withCtx: _withCtx, toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, renderList: _renderList, createSlots: _createSlots } = require(\\"vue\\")
190193
const { ssrRenderComponent: _ssrRenderComponent, ssrInterpolate: _ssrInterpolate } = require(\\"vue/server-renderer\\")
191194
192195
return function ssrRender(_ctx, _push, _parent, _attrs) {
193196
const _component_foo = _resolveComponent(\\"foo\\")
194197
195198
_push(_ssrRenderComponent(_component_foo, _attrs, _createSlots({ _: 2 /* DYNAMIC */ }, [
196-
_renderList(_ctx.names, (key) => {
199+
_renderList(_ctx.names, (key, index) => {
197200
return {
198201
name: key,
199202
fn: _withCtx(({ msg }, _push, _parent, _scopeId) => {
200203
if (_push) {
201-
_push(\`\${_ssrInterpolate(msg + key + _ctx.bar)}\`)
204+
_push(\`\${_ssrInterpolate(msg + key + index + _ctx.bar)}\`)
202205
} else {
203206
return [
204-
_createTextVNode(_toDisplayString(msg + key + _ctx.bar), 1 /* TEXT */)
207+
_createTextVNode(_toDisplayString(msg + key + index + _ctx.bar), 1 /* TEXT */)
205208
]
206209
}
207210
})

packages/compiler-ssr/src/transforms/ssrTransformComponent.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
152152
const wipEntries: WIPSlotEntry[] = []
153153
wipMap.set(node, wipEntries)
154154

155-
const buildSSRSlotFn: SlotFnBuilder = (props, _vFor, children, loc) => {
155+
const buildSSRSlotFn: SlotFnBuilder = (props, _vForExp, children, loc) => {
156156
const param0 = (props && stringifyExpression(props)) || `_`
157157
const fn = createFunctionExpression(
158158
[param0, `_push`, `_parent`, `_scopeId`],
@@ -279,7 +279,7 @@ const vnodeDirectiveTransforms = {
279279

280280
function createVNodeSlotBranch(
281281
props: ExpressionNode | undefined,
282-
vFor: ExpressionNode | undefined,
282+
vForExp: ExpressionNode | undefined,
283283
children: TemplateChildNode[],
284284
parentContext: TransformContext
285285
): ReturnStatement {
@@ -306,8 +306,8 @@ function createVNodeSlotBranch(
306306
tag: 'template',
307307
tagType: ElementTypes.TEMPLATE,
308308
isSelfClosing: false,
309-
// important: provide v-slot="props" and v-for="exp" on the wrapper for proper
310-
// scope analysis
309+
// important: provide v-slot="props" and v-for="exp" on the wrapper for
310+
// proper scope analysis
311311
props: [
312312
{
313313
type: NodeTypes.DIRECTIVE,
@@ -320,7 +320,7 @@ function createVNodeSlotBranch(
320320
{
321321
type: NodeTypes.DIRECTIVE,
322322
name: 'for',
323-
exp: vFor,
323+
exp: vForExp,
324324
arg: undefined,
325325
modifiers: [],
326326
loc: locStub

packages/compiler-ssr/src/transforms/ssrTransformSuspense.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function ssrTransformSuspense(
3939
wipEntry.slotsExp = buildSlots(
4040
node,
4141
context,
42-
(_props, _vFor, children, loc) => {
42+
(_props, _vForExp, children, loc) => {
4343
const fn = createFunctionExpression(
4444
[],
4545
undefined, // no return, assign body later

0 commit comments

Comments
 (0)