Skip to content

Commit 5addef8

Browse files
authored
fix(compiler-core): add check when v-else-if is behind v-else (#4603)
1 parent 5aa4255 commit 5addef8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('compiler: v-if', () => {
248248
])
249249
})
250250

251-
test('error on v-else-if missing adjacent v-if', () => {
251+
test('error on v-else-if missing adjacent v-if or v-else-if', () => {
252252
const onError = jest.fn()
253253

254254
const { node: node1 } = parseWithIfTransform(`<div v-else-if="foo"/>`, {
@@ -284,6 +284,21 @@ describe('compiler: v-if', () => {
284284
loc: node3.loc
285285
}
286286
])
287+
288+
const {
289+
node: { branches }
290+
} = parseWithIfTransform(
291+
`<div v-if="notOk"/><div v-else/><div v-else-if="ok"/>`,
292+
{ onError },
293+
0
294+
)
295+
296+
expect(onError.mock.calls[3]).toMatchObject([
297+
{
298+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
299+
loc: branches[branches.length - 1].loc
300+
}
301+
])
287302
})
288303

289304
test('error on user key', () => {

packages/compiler-core/src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
149149
// transform errors
150150
[ErrorCodes.X_V_IF_NO_EXPRESSION]: `v-if/v-else-if is missing expression.`,
151151
[ErrorCodes.X_V_IF_SAME_KEY]: `v-if/else branches must use unique keys.`,
152-
[ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if.`,
152+
[ErrorCodes.X_V_ELSE_NO_ADJACENT_IF]: `v-else/v-else-if has no adjacent v-if or v-else-if.`,
153153
[ErrorCodes.X_V_FOR_NO_EXPRESSION]: `v-for is missing expression.`,
154154
[ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression.`,
155155
[ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT]: `<template v-for> key should be placed on the <template> tag.`,

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

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ export function processIf(
145145
}
146146

147147
if (sibling && sibling.type === NodeTypes.IF) {
148+
// Check if v-else was followed by v-else-if
149+
if (
150+
dir.name === 'else-if' &&
151+
sibling.branches[sibling.branches.length - 1].condition === undefined
152+
) {
153+
context.onError(
154+
createCompilerError(ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, node.loc)
155+
)
156+
}
157+
148158
// move the node to the if node's branches
149159
context.removeNode()
150160
const branch = createIfBranch(node, dir)

0 commit comments

Comments
 (0)