Просмотр исходного кода

fix(compiler-core): generate `updateEffect` for nested v-for (#171)

Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
Jevon 2 лет назад
Родитель
Сommit
9f8bf4fc82

+ 5 - 1
packages/compiler-vapor/src/generate.ts

@@ -41,6 +41,11 @@ export class CodegenContext {
   delegates = new Set<string>()
 
   identifiers: Record<string, string[]> = Object.create(null)
+
+  genEffects: Array<
+    (effects: IREffect[], context: CodegenContext) => CodeFragment[]
+  > = []
+
   withId = <T>(fn: () => T, map: Record<string, string | null>): T => {
     const { identifiers } = this
     const ids = Object.keys(map)
@@ -55,7 +60,6 @@ export class CodegenContext {
 
     return ret
   }
-  genEffect?: (effects: IREffect[]) => CodeFragment[]
 
   constructor(
     public ir: RootIRNode,

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

@@ -50,7 +50,11 @@ export function genBlockContent(
   }
 
   push(...genOperations(operation, context))
-  push(...(context.genEffect || genEffects)(effect, context))
+  push(
+    ...(context.genEffects.length
+      ? context.genEffects[context.genEffects.length - 1]
+      : genEffects)(effect, context),
+  )
 
   push(NEWLINE, `return `)
 

+ 4 - 3
packages/compiler-vapor/src/generators/for.ts

@@ -18,7 +18,7 @@ export function genFor(
   oper: ForIRNode,
   context: CodegenContext,
 ): CodeFragment[] {
-  const { vaporHelper } = context
+  const { vaporHelper, genEffects } = context
   const { source, value, key, index, render, keyProp } = oper
 
   const rawValue = value && value.content
@@ -27,7 +27,8 @@ export function genFor(
 
   const sourceExpr = ['() => (', ...genExpression(source, context), ')']
   let updateFn = '_updateEffect'
-  context.genEffect = genEffectInFor
+
+  genEffects.push(genEffectInFor)
 
   const idMap: Record<string, string> = {}
   if (rawValue) idMap[rawValue] = `_block.s[0]`
@@ -65,7 +66,7 @@ export function genFor(
     ]
   }
 
-  context.genEffect = undefined
+  genEffects.pop()
 
   return [
     NEWLINE,