Skip to content

Commit 911e670

Browse files
authored
fix(compiler-core): adjacent v-else should cause a compiler error (#13699)
close #13698
1 parent c486536 commit 911e670

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,25 @@ describe('compiler: v-if', () => {
301301
])
302302
})
303303

304+
test('error on adjacent v-else', () => {
305+
const onError = vi.fn()
306+
307+
const {
308+
node: { branches },
309+
} = parseWithIfTransform(
310+
`<div v-if="false"/><div v-else/><div v-else/>`,
311+
{ onError },
312+
0,
313+
)
314+
315+
expect(onError.mock.calls[0]).toMatchObject([
316+
{
317+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
318+
loc: branches[branches.length - 1].loc,
319+
},
320+
])
321+
})
322+
304323
test('error on user key', () => {
305324
const onError = vi.fn()
306325
// dynamic

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ export function processIf(
141141
}
142142

143143
if (sibling && sibling.type === NodeTypes.IF) {
144-
// Check if v-else was followed by v-else-if
144+
// Check if v-else was followed by v-else-if or there are two adjacent v-else
145145
if (
146-
dir.name === 'else-if' &&
146+
(dir.name === 'else-if' || dir.name === 'else') &&
147147
sibling.branches[sibling.branches.length - 1].condition === undefined
148148
) {
149149
context.onError(

0 commit comments

Comments
 (0)