Explorar el Código

feat(compiler-vapor): support v-for without prefixIdentifiers (#259)

Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
zhiyuanzmj hace 1 año
padre
commit
b44ca85cb1

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

@@ -30,6 +30,20 @@ export function render(_ctx) {
 }"
 `;
 
+exports[`compiler: v-for > function params w/ prefixIdentifiers: false 1`] = `
+"import { renderEffect as _renderEffect, setText as _setText, createFor as _createFor, template as _template } from 'vue/vapor';
+const t0 = _template("<div></div>")
+
+export function render(_ctx) {
+  const n0 = _createFor(() => (items), ([item, __, k]) => {
+    const n2 = t0()
+    _renderEffect(() => _setText(n2, item))
+    return n2
+  }, (item, __, k) => (k))
+  return n0
+}"
+`;
+
 exports[`compiler: v-for > multi effect 1`] = `
 "import { renderEffect as _renderEffect, setDynamicProp as _setDynamicProp, createFor as _createFor, template as _template } from 'vue/vapor';
 const t0 = _template("<div></div>")

+ 13 - 0
packages/compiler-vapor/__tests__/transforms/vFor.spec.ts

@@ -223,4 +223,17 @@ describe('compiler: v-for', () => {
       index: undefined,
     })
   })
+
+  test('function params w/ prefixIdentifiers: false', () => {
+    const { code } = compileWithVFor(
+      `<div v-for="(item, , k) of items" :key="k">{{ item }}</div>`,
+      {
+        prefixIdentifiers: false,
+      },
+    )
+
+    expect(code).contains(`_createFor(() => (items), ([item, __, k]) => {`)
+    expect(code).contain(`_setText(n2, item)`)
+    expect(code).matchSnapshot()
+  })
 })

+ 11 - 6
packages/compiler-vapor/src/generators/for.ts

@@ -42,13 +42,18 @@ export function genFor(
   }
 
   const [depth, exitScope] = context.enterScope()
-  const propsName = `_ctx${depth}`
+  let propsName: string
   const idMap: Record<string, string | null> = {}
-  Array.from(idsOfValue).forEach(
-    (id, idIndex) => (idMap[id] = `${propsName}[${idIndex}]`),
-  )
-  if (rawKey) idMap[rawKey] = `${propsName}[${idsOfValue.size}]`
-  if (rawIndex) idMap[rawIndex] = `${propsName}[${idsOfValue.size + 1}]`
+  if (context.options.prefixIdentifiers) {
+    propsName = `_ctx${depth}`
+    Array.from(idsOfValue).forEach(
+      (id, idIndex) => (idMap[id] = `${propsName}[${idIndex}]`),
+    )
+    if (rawKey) idMap[rawKey] = `${propsName}[${idsOfValue.size}]`
+    if (rawIndex) idMap[rawIndex] = `${propsName}[${idsOfValue.size + 1}]`
+  } else {
+    propsName = `[${[rawValue || ((rawKey || rawIndex) && '_'), rawKey || (rawIndex && '__'), rawIndex].filter(Boolean).join(', ')}]`
+  }
 
   let blockFn = context.withId(
     () => genBlock(render, context, [propsName]),