Skip to content

Commit 015e1ee

Browse files
committed
fix(compiler-vapor): move next, child and nthChild before the setInsertionState
1 parent c50fb0e commit 015e1ee

File tree

8 files changed

+61
-19
lines changed

8 files changed

+61
-19
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function render(_ctx) {
2626
`;
2727

2828
exports[`compile > custom directive > component 1`] = `
29-
"import { resolveComponent as _resolveComponent, resolveDirective as _resolveDirective, createComponentWithFallback as _createComponentWithFallback, withVaporDirectives as _withVaporDirectives, setInsertionState as _setInsertionState, createIf as _createIf, template as _template } from 'vue';
29+
"import { resolveComponent as _resolveComponent, resolveDirective as _resolveDirective, setInsertionState as _setInsertionState, createComponentWithFallback as _createComponentWithFallback, withVaporDirectives as _withVaporDirectives, createIf as _createIf, template as _template } from 'vue';
3030
const t0 = _template("<div></div>")
3131
3232
export function render(_ctx) {
@@ -38,9 +38,9 @@ export function render(_ctx) {
3838
"default": () => {
3939
const n0 = _createIf(() => (true), () => {
4040
const n3 = t0()
41+
_setInsertionState(n3)
4142
const n2 = _createComponentWithFallback(_component_Bar)
4243
_withVaporDirectives(n2, [[_directive_hello, void 0, void 0, { world: true }]])
43-
_setInsertionState(n3)
4444
return n3
4545
})
4646
return n0
@@ -149,17 +149,17 @@ export function render(_ctx, $props, $emit, $attrs, $slots) {
149149
`;
150150
151151
exports[`compile > directives > v-pre > should not affect siblings after it 1`] = `
152-
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, setInsertionState as _setInsertionState, child as _child, toDisplayString as _toDisplayString, setText as _setText, setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
152+
"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createComponentWithFallback as _createComponentWithFallback, child as _child, toDisplayString as _toDisplayString, setText as _setText, setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
153153
const t0 = _template("<div :id=\\"foo\\"><Comp></Comp>{{ bar }}</div>")
154154
const t1 = _template("<div> </div>")
155155
156156
export function render(_ctx, $props, $emit, $attrs, $slots) {
157157
const _component_Comp = _resolveComponent("Comp")
158158
const n0 = t0()
159159
const n3 = t1()
160-
const n1 = _createComponentWithFallback(_component_Comp)
161-
_setInsertionState(n3, 0)
162160
const n2 = _child(n3)
161+
_setInsertionState(n3, 0)
162+
const n1 = _createComponentWithFallback(_component_Comp)
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/__snapshots__/transformChildren.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compiler: children transform > anchor insertion in middle 1`] = `
4-
"import { child as _child, next as _next, createIf as _createIf, setInsertionState as _setInsertionState, template as _template } from 'vue';
4+
"import { child as _child, next as _next, setInsertionState as _setInsertionState, createIf as _createIf, template as _template } from 'vue';
55
const t0 = _template("<div></div>")
66
const t1 = _template("<div><div></div><!><div></div></div>", true)
77
88
export function render(_ctx) {
99
const n4 = t1()
1010
const n3 = _next(_child(n4))
11+
_setInsertionState(n4, n3)
1112
const n0 = _createIf(() => (1), () => {
1213
const n2 = t0()
1314
return n2
1415
}, null, true)
15-
_setInsertionState(n4, n3)
1616
return n4
1717
}"
1818
`;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ export function render(_ctx) {
6565
`;
6666

6767
exports[`compiler: v-for > nested v-for 1`] = `
68-
"import { child as _child, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, createFor as _createFor, setInsertionState as _setInsertionState, template as _template } from 'vue';
68+
"import { setInsertionState as _setInsertionState, child as _child, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, createFor as _createFor, template as _template } from 'vue';
6969
const t0 = _template("<span> </span>")
7070
const t1 = _template("<div></div>", true)
7171
7272
export function render(_ctx) {
7373
const n0 = _createFor(() => (_ctx.list), (_for_item0) => {
7474
const n5 = t1()
75+
_setInsertionState(n5)
7576
const n2 = _createFor(() => (_for_item0.value), (_for_item1) => {
7677
const n4 = t0()
7778
const x4 = _child(n4)
7879
_renderEffect(() => _setText(x4, _toDisplayString(_for_item1.value+_for_item0.value)))
7980
return n4
8081
}, null, 1)
81-
_setInsertionState(n5)
8282
return n5
8383
})
8484
return n0

packages/compiler-vapor/__tests__/transforms/__snapshots__/vOnce.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export function render(_ctx) {
3636
`;
3737

3838
exports[`compiler: v-once > on component 1`] = `
39-
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, setInsertionState as _setInsertionState, template as _template } from 'vue';
39+
"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
4040
const t0 = _template("<div></div>", true)
4141
4242
export function render(_ctx) {
4343
const _component_Comp = _resolveComponent("Comp")
4444
const n1 = t0()
45-
const n0 = _createComponentWithFallback(_component_Comp, { id: () => (_ctx.foo) }, null, null, true)
4645
_setInsertionState(n1)
46+
const n0 = _createComponentWithFallback(_component_Comp, { id: () => (_ctx.foo) }, null, null, true)
4747
return n1
4848
}"
4949
`;

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/operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export function genOperationWithInsertionState(
4343
context: CodegenContext,
4444
): CodeFragment[] {
4545
const [frag, push] = buildCodeFragment()
46-
push(...genOperation(oper, context))
4746
if (isBlockOperation(oper) && oper.parent) {
4847
push(...genInsertionstate(oper, context))
4948
}
49+
push(...genOperation(oper, context))
5050
return frag
5151
}
5252

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)