Skip to content

Commit 2f00264

Browse files
authored
fix(compiler-vapor): move next, child and nthChild before setInsertionState (#13246)
1 parent 42a421a commit 2f00264

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
157157
const _component_Comp = _resolveComponent("Comp")
158158
const n0 = t0()
159159
const n3 = t1()
160+
const n2 = _child(n3)
160161
_setInsertionState(n3, 0)
161162
const n1 = _createComponentWithFallback(_component_Comp)
162-
const n2 = _child(n3)
163163
_renderEffect(() => {
164164
_setText(n2, _toDisplayString(_ctx.bar))
165165
_setProp(n3, "id", _ctx.foo)
@@ -230,6 +230,30 @@ export function render(_ctx) {
230230
}"
231231
`;
232232
233+
exports[`compile > setInsertionState > next, child and nthChild should be above the setInsertionState 1`] = `
234+
"import { resolveComponent as _resolveComponent, child as _child, next as _next, setInsertionState as _setInsertionState, createComponentWithFallback as _createComponentWithFallback, nthChild as _nthChild, createIf as _createIf, setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
235+
const t0 = _template("<div></div>")
236+
const t1 = _template("<div><div></div><!><div></div><!><div><button></button></div></div>", true)
237+
238+
export function render(_ctx) {
239+
const _component_Comp = _resolveComponent("Comp")
240+
const n6 = t1()
241+
const n5 = _next(_child(n6))
242+
const n7 = _nthChild(n6, 3)
243+
const p0 = _next(n7)
244+
const n4 = _child(p0)
245+
_setInsertionState(n6, n5)
246+
const n0 = _createComponentWithFallback(_component_Comp)
247+
_setInsertionState(n6, n7)
248+
const n1 = _createIf(() => (true), () => {
249+
const n3 = t0()
250+
return n3
251+
})
252+
_renderEffect(() => _setProp(n4, "disabled", _ctx.foo))
253+
return n6
254+
}"
255+
`;
256+
233257
exports[`compile > static + dynamic root 1`] = `
234258
"import { toDisplayString as _toDisplayString, setText as _setText, template as _template } from 'vue';
235259
const t0 = _template(" ")

packages/compiler-vapor/__tests__/compile.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,21 @@ describe('compile', () => {
220220
expect(code).matchSnapshot()
221221
})
222222
})
223+
224+
describe('setInsertionState', () => {
225+
test('next, child and nthChild should be above the setInsertionState', () => {
226+
const code = compile(`
227+
<div>
228+
<div />
229+
<Comp />
230+
<div />
231+
<div v-if="true" />
232+
<div>
233+
<button :disabled="foo" />
234+
</div>
235+
</div>
236+
`)
237+
expect(code).toMatchSnapshot()
238+
})
239+
})
223240
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ describe('compiler: children transform', () => {
6969
</div>`,
7070
)
7171
// ensure the insertion anchor is generated before the insertion statement
72-
expect(code).toMatch(`const n3 = _next(_child(n4))
73-
_setInsertionState(n4, n3)`)
72+
expect(code).toMatch(`const n3 = _next(_child(n4))`)
73+
expect(code).toMatch(`_setInsertionState(n4, n3)`)
7474
expect(code).toMatchSnapshot()
7575
})
7676
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function genBlockContent(
5252
push(...genSelf(child, context))
5353
}
5454
for (const child of dynamic.children) {
55-
push(...genChildren(child, context, `n${child.id!}`))
55+
push(...genChildren(child, context, push, `n${child.id!}`))
5656
}
5757

5858
push(...genOperations(operation, context))

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function genSelf(
4141
export function genChildren(
4242
dynamic: IRDynamicInfo,
4343
context: CodegenContext,
44+
pushBlock: (...items: CodeFragment[]) => number,
4445
from: string = `n${dynamic.id}`,
4546
): CodeFragment[] {
4647
const { helper } = context
@@ -72,17 +73,17 @@ export function genChildren(
7273
// p for "placeholder" variables that are meant for possible reuse by
7374
// other access paths
7475
const variable = id === undefined ? `p${context.block.tempId++}` : `n${id}`
75-
push(NEWLINE, `const ${variable} = `)
76+
pushBlock(NEWLINE, `const ${variable} = `)
7677

7778
if (prev) {
7879
if (elementIndex - prev[1] === 1) {
79-
push(...genCall(helper('next'), prev[0]))
80+
pushBlock(...genCall(helper('next'), prev[0]))
8081
} else {
81-
push(...genCall(helper('nthChild'), from, String(elementIndex)))
82+
pushBlock(...genCall(helper('nthChild'), from, String(elementIndex)))
8283
}
8384
} else {
8485
if (elementIndex === 0) {
85-
push(...genCall(helper('child'), from))
86+
pushBlock(...genCall(helper('child'), from))
8687
} else {
8788
// check if there's a node that we can reuse from
8889
let init = genCall(helper('child'), from)
@@ -91,7 +92,7 @@ export function genChildren(
9192
} else if (elementIndex > 1) {
9293
init = genCall(helper('nthChild'), from, String(elementIndex))
9394
}
94-
push(...init)
95+
pushBlock(...init)
9596
}
9697
}
9798

@@ -109,7 +110,7 @@ export function genChildren(
109110

110111
if (childrenToGen.length) {
111112
for (const [child, from] of childrenToGen) {
112-
push(...genChildren(child, context, from))
113+
push(...genChildren(child, context, pushBlock, from))
113114
}
114115
}
115116

0 commit comments

Comments
 (0)