Explorar o código

chore: Merge branch 'edison/fix/codegenOrder' into edison/testVapor

daiwei hai 11 meses
pai
achega
6b360d371c

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

@@ -212,6 +212,20 @@ export function render(_ctx) {
 }"
 `;
 
+exports[`compile > execution order > with insertionState 1`] = `
+"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createSlot as _createSlot, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
+const t0 = _template("<div></div>")
+
+export function render(_ctx) {
+  const _component_Comp = _resolveComponent("Comp")
+  const n1 = t0()
+  _setInsertionState(n1)
+  const n0 = _createSlot("default", null)
+  const n2 = _createComponentWithFallback(_component_Comp)
+  return [n1, n2]
+}"
+`;
+
 exports[`compile > execution order > with v-once 1`] = `
 "import { child as _child, next as _next, nthChild as _nthChild, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
 const t0 = _template("<div><span> </span> <br> </div>", true)

+ 6 - 0
packages/compiler-vapor/__tests__/compile.spec.ts

@@ -247,6 +247,7 @@ describe('compile', () => {
     _setText(x0, _toDisplayString(_ctx.bar))`,
       )
     })
+
     test('with v-once', () => {
       const code = compile(
         `<div>
@@ -261,5 +262,10 @@ describe('compile', () => {
     _setText(n2, " " + _toDisplayString(_ctx.baz))`,
       )
     })
+
+    test('with insertionState', () => {
+      const code = compile(`<div><slot /></div><Comp/>`)
+      expect(code).matchSnapshot()
+    })
   })
 })

+ 1 - 1
packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

@@ -343,8 +343,8 @@ const t2 = _template("<form></form>")
 
 export function render(_ctx) {
   const n1 = t1()
-  const n3 = t2()
   const n0 = t0()
+  const n3 = t2()
   const n2 = t2()
   _insert(n0, n1)
   _insert(n2, n3)

+ 3 - 1
packages/compiler-vapor/src/generators/block.ts

@@ -70,7 +70,9 @@ export function genBlockContent(
     push(...genSelf(child, context))
   }
   for (const child of dynamic.children) {
-    push(...genChildren(child, context, push, `n${child.id!}`))
+    if (!child.hasDynamicChild) {
+      push(...genChildren(child, context, push, `n${child.id!}`))
+    }
   }
 
   push(...genOperations(operation, context))

+ 7 - 2
packages/compiler-vapor/src/generators/template.ts

@@ -24,7 +24,8 @@ export function genSelf(
   context: CodegenContext,
 ): CodeFragment[] {
   const [frag, push] = buildCodeFragment()
-  const { id, template, operation, dynamicChildOffset } = dynamic
+  const { id, template, operation, dynamicChildOffset, hasDynamicChild } =
+    dynamic
 
   if (id !== undefined && template !== undefined) {
     push(NEWLINE, `const n${id} = t${template}(${dynamicChildOffset || ''})`)
@@ -35,6 +36,10 @@ export function genSelf(
     push(...genOperationWithInsertionState(operation, context))
   }
 
+  if (hasDynamicChild) {
+    push(...genChildren(dynamic, context, push, `n${id}`))
+  }
+
   return frag
 }
 
@@ -109,7 +114,7 @@ export function genChildren(
       }
     }
 
-    if (id === child.anchor) {
+    if (id === child.anchor && !child.hasDynamicChild) {
       push(...genSelf(child, context))
     }
 

+ 3 - 1
packages/runtime-vapor/src/apiCreateFor.ts

@@ -288,6 +288,7 @@ export const createFor = (
           const previousKeyIndexMap = new Map(previousKeyIndexPairs)
           const operations: (() => void)[] = []
 
+          let mountCounter = 0
           const relocateOrMountBlock = (
             blockIndex: number,
             blockItem: ReturnType<typeof getItem>,
@@ -312,6 +313,7 @@ export const createFor = (
                 )
               }
             } else {
+              mountCounter++
               operations.push(() =>
                 mount(
                   source,
@@ -342,7 +344,7 @@ export const createFor = (
             relocateOrMountBlock(i, blockItem, blockKey, -1)
           }
 
-          const useFastRemove = operations.length === newLength
+          const useFastRemove = mountCounter === newLength
 
           for (const leftoverIndex of previousKeyIndexMap.values()) {
             unmount(