Skip to content

Commit 08f9c1d

Browse files
authored
fix(compiler-vapor): properly locate last if node (#13399)
1 parent 1ef6e6e commit 08f9c1d

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,29 @@ export function render(_ctx) {
134134
return n0
135135
}"
136136
`;
137+
138+
exports[`compiler: v-if > v-if + v-if / v-else[-if] 1`] = `
139+
"import { setInsertionState as _setInsertionState, createIf as _createIf, template as _template } from 'vue';
140+
const t0 = _template("<span>foo</span>")
141+
const t1 = _template("<span>bar</span>")
142+
const t2 = _template("<span>baz</span>")
143+
const t3 = _template("<div></div>", true)
144+
145+
export function render(_ctx) {
146+
const n8 = t3()
147+
_setInsertionState(n8)
148+
const n0 = _createIf(() => (_ctx.foo), () => {
149+
const n2 = t0()
150+
return n2
151+
})
152+
_setInsertionState(n8)
153+
const n3 = _createIf(() => (_ctx.bar), () => {
154+
const n5 = t1()
155+
return n5
156+
}, () => {
157+
const n7 = t2()
158+
return n7
159+
})
160+
return n8
161+
}"
162+
`;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,25 @@ export function render(_ctx) {
252252
return n1
253253
}"
254254
`;
255+
256+
exports[`compiler: transform slot > slot + v-if / v-else[-if] should not cause error 1`] = `
257+
"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createSlot as _createSlot, createComponentWithFallback as _createComponentWithFallback, createIf as _createIf, template as _template } from 'vue';
258+
const t0 = _template("<div></div>", true)
259+
260+
export function render(_ctx) {
261+
const _component_Foo = _resolveComponent("Foo")
262+
const _component_Bar = _resolveComponent("Bar")
263+
const n6 = t0()
264+
_setInsertionState(n6)
265+
const n0 = _createSlot("foo", null)
266+
_setInsertionState(n6)
267+
const n1 = _createIf(() => (true), () => {
268+
const n3 = _createComponentWithFallback(_component_Foo)
269+
return n3
270+
}, () => {
271+
const n5 = _createComponentWithFallback(_component_Bar)
272+
return n5
273+
})
274+
return n6
275+
}"
276+
`;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ describe('compiler: v-if', () => {
215215
})
216216
})
217217

218+
test('v-if + v-if / v-else[-if]', () => {
219+
const { code } = compileWithVIf(
220+
`<div>
221+
<span v-if="foo">foo</span>
222+
<span v-if="bar">bar</span>
223+
<span v-else>baz</span>
224+
</div>`,
225+
)
226+
expect(code).toMatchSnapshot()
227+
})
228+
218229
test('comment between branches', () => {
219230
const { code, ir } = compileWithVIf(`
220231
<div v-if="ok"/>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ describe('compiler: transform slot', () => {
371371
})
372372
})
373373

374+
test('slot + v-if / v-else[-if] should not cause error', () => {
375+
const { code } = compileWithSlots(
376+
`<div>
377+
<slot name="foo"></slot>
378+
<Foo v-if="true"></Foo>
379+
<Bar v-else />
380+
</div>`,
381+
)
382+
expect(code).toMatchSnapshot()
383+
})
384+
374385
test('quote slot name', () => {
375386
const { code } = compileWithSlots(
376387
`<Comp><template #nav-bar-title-before></template></Comp>`,

packages/compiler-vapor/src/transforms/vIf.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export function processIf(
6565
if (siblings) {
6666
let i = siblings.length
6767
while (i--) {
68-
if (siblings[i].operation) lastIfNode = siblings[i].operation
68+
if (
69+
siblings[i].operation &&
70+
siblings[i].operation!.type === IRNodeTypes.IF
71+
) {
72+
lastIfNode = siblings[i].operation
73+
break
74+
}
6975
}
7076
}
7177

0 commit comments

Comments
 (0)