Skip to content

fix(compiler-vapor): properly locate last if node #13399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,29 @@ export function render(_ctx) {
return n0
}"
`;

exports[`compiler: v-if > v-if + v-if / v-else[-if] 1`] = `
"import { setInsertionState as _setInsertionState, createIf as _createIf, template as _template } from 'vue';
const t0 = _template("<span>foo</span>")
const t1 = _template("<span>bar</span>")
const t2 = _template("<span>baz</span>")
const t3 = _template("<div></div>", true)
export function render(_ctx) {
const n8 = t3()
_setInsertionState(n8)
const n0 = _createIf(() => (_ctx.foo), () => {
const n2 = t0()
return n2
})
_setInsertionState(n8)
const n3 = _createIf(() => (_ctx.bar), () => {
const n5 = t1()
return n5
}, () => {
const n7 = t2()
return n7
})
return n8
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,25 @@ export function render(_ctx) {
return n1
}"
`;

exports[`compiler: transform slot > slot + v-if / v-else[-if] should not cause error 1`] = `
"import { resolveComponent as _resolveComponent, setInsertionState as _setInsertionState, createSlot as _createSlot, createComponentWithFallback as _createComponentWithFallback, createIf as _createIf, template as _template } from 'vue';
const t0 = _template("<div></div>", true)

export function render(_ctx) {
const _component_Foo = _resolveComponent("Foo")
const _component_Bar = _resolveComponent("Bar")
const n6 = t0()
_setInsertionState(n6)
const n0 = _createSlot("foo", null)
_setInsertionState(n6)
const n1 = _createIf(() => (true), () => {
const n3 = _createComponentWithFallback(_component_Foo)
return n3
}, () => {
const n5 = _createComponentWithFallback(_component_Bar)
return n5
})
return n6
}"
`;
11 changes: 11 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vIf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ describe('compiler: v-if', () => {
})
})

test('v-if + v-if / v-else[-if]', () => {
const { code } = compileWithVIf(
`<div>
<span v-if="foo">foo</span>
<span v-if="bar">bar</span>
<span v-else>baz</span>
</div>`,
)
expect(code).toMatchSnapshot()
})

test('comment between branches', () => {
const { code, ir } = compileWithVIf(`
<div v-if="ok"/>
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ describe('compiler: transform slot', () => {
})
})

test('slot + v-if / v-else[-if] should not cause error', () => {
const { code } = compileWithSlots(
`<div>
<slot name="foo"></slot>
<Foo v-if="true"></Foo>
<Bar v-else />
</div>`,
)
expect(code).toMatchSnapshot()
})

test('quote slot name', () => {
const { code } = compileWithSlots(
`<Comp><template #nav-bar-title-before></template></Comp>`,
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-vapor/src/transforms/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export function processIf(
if (siblings) {
let i = siblings.length
while (i--) {
if (siblings[i].operation) lastIfNode = siblings[i].operation
if (
siblings[i].operation &&
siblings[i].operation!.type === IRNodeTypes.IF
) {
lastIfNode = siblings[i].operation
break
}
}
}

Expand Down