Skip to content

Commit b44ca85

Browse files
zhiyuanzmjsxzz
andauthored
feat(compiler-vapor): support v-for without prefixIdentifiers (#259)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
1 parent 5eb43b0 commit b44ca85

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ export function render(_ctx) {
3030
}"
3131
`;
3232

33+
exports[`compiler: v-for > function params w/ prefixIdentifiers: false 1`] = `
34+
"import { renderEffect as _renderEffect, setText as _setText, createFor as _createFor, template as _template } from 'vue/vapor';
35+
const t0 = _template("<div></div>")
36+
37+
export function render(_ctx) {
38+
const n0 = _createFor(() => (items), ([item, __, k]) => {
39+
const n2 = t0()
40+
_renderEffect(() => _setText(n2, item))
41+
return n2
42+
}, (item, __, k) => (k))
43+
return n0
44+
}"
45+
`;
46+
3347
exports[`compiler: v-for > multi effect 1`] = `
3448
"import { renderEffect as _renderEffect, setDynamicProp as _setDynamicProp, createFor as _createFor, template as _template } from 'vue/vapor';
3549
const t0 = _template("<div></div>")

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,17 @@ describe('compiler: v-for', () => {
223223
index: undefined,
224224
})
225225
})
226+
227+
test('function params w/ prefixIdentifiers: false', () => {
228+
const { code } = compileWithVFor(
229+
`<div v-for="(item, , k) of items" :key="k">{{ item }}</div>`,
230+
{
231+
prefixIdentifiers: false,
232+
},
233+
)
234+
235+
expect(code).contains(`_createFor(() => (items), ([item, __, k]) => {`)
236+
expect(code).contain(`_setText(n2, item)`)
237+
expect(code).matchSnapshot()
238+
})
226239
})

packages/compiler-vapor/src/generators/for.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ export function genFor(
4242
}
4343

4444
const [depth, exitScope] = context.enterScope()
45-
const propsName = `_ctx${depth}`
45+
let propsName: string
4646
const idMap: Record<string, string | null> = {}
47-
Array.from(idsOfValue).forEach(
48-
(id, idIndex) => (idMap[id] = `${propsName}[${idIndex}]`),
49-
)
50-
if (rawKey) idMap[rawKey] = `${propsName}[${idsOfValue.size}]`
51-
if (rawIndex) idMap[rawIndex] = `${propsName}[${idsOfValue.size + 1}]`
47+
if (context.options.prefixIdentifiers) {
48+
propsName = `_ctx${depth}`
49+
Array.from(idsOfValue).forEach(
50+
(id, idIndex) => (idMap[id] = `${propsName}[${idIndex}]`),
51+
)
52+
if (rawKey) idMap[rawKey] = `${propsName}[${idsOfValue.size}]`
53+
if (rawIndex) idMap[rawIndex] = `${propsName}[${idsOfValue.size + 1}]`
54+
} else {
55+
propsName = `[${[rawValue || ((rawKey || rawIndex) && '_'), rawKey || (rawIndex && '__'), rawIndex].filter(Boolean).join(', ')}]`
56+
}
5257

5358
let blockFn = context.withId(
5459
() => genBlock(render, context, [propsName]),

0 commit comments

Comments
 (0)