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

Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
This commit is contained in:
Jevon 2024-03-29 21:36:19 +08:00 committed by GitHub
parent ba17fb9910
commit 9f8bf4fc82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 5 deletions

View File

@ -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,

View File

@ -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 `)

View File

@ -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,