fix(compiler-core): adjacent v-else should cause a compiler error (#13699)

close #13698
This commit is contained in:
linzhe 2025-07-25 08:30:05 +08:00 committed by GitHub
parent c486536105
commit 911e67045e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -301,6 +301,25 @@ describe('compiler: v-if', () => {
]) ])
}) })
test('error on adjacent v-else', () => {
const onError = vi.fn()
const {
node: { branches },
} = parseWithIfTransform(
`<div v-if="false"/><div v-else/><div v-else/>`,
{ onError },
0,
)
expect(onError.mock.calls[0]).toMatchObject([
{
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
loc: branches[branches.length - 1].loc,
},
])
})
test('error on user key', () => { test('error on user key', () => {
const onError = vi.fn() const onError = vi.fn()
// dynamic // dynamic

View File

@ -141,9 +141,9 @@ export function processIf(
} }
if (sibling && sibling.type === NodeTypes.IF) { if (sibling && sibling.type === NodeTypes.IF) {
// Check if v-else was followed by v-else-if // Check if v-else was followed by v-else-if or there are two adjacent v-else
if ( if (
dir.name === 'else-if' && (dir.name === 'else-if' || dir.name === 'else') &&
sibling.branches[sibling.branches.length - 1].condition === undefined sibling.branches[sibling.branches.length - 1].condition === undefined
) { ) {
context.onError( context.onError(