Skip to content

Commit 28f135f

Browse files
committed
fix(compiler-core): adjacent v-else should cause a compiler error
1 parent c486536 commit 28f135f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ export function processIf(
150150
createCompilerError(ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, node.loc),
151151
)
152152
}
153+
// Check that there should not be two adjacent v-else
154+
if (
155+
dir.name === 'else' &&
156+
sibling.branches[sibling.branches.length - 1].condition === undefined
157+
) {
158+
context.onError(
159+
createCompilerError(ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, node.loc),
160+
)
161+
}
153162

154163
// move the node to the if node's branches
155164
context.removeNode()

0 commit comments

Comments
 (0)