From: linzhe <40790268+linzhe141@users.noreply.github.com> Date: Fri, 25 Jul 2025 00:30:05 +0000 (+0800) Subject: fix(compiler-core): adjacent v-else should cause a compiler error (#13699) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=911e67045e2a63e0ecbd198ed4f567530f6d1c17;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-core): adjacent v-else should cause a compiler error (#13699) close #13698 --- diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index 2c2fedab0d..73b6e22155 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -301,6 +301,25 @@ describe('compiler: v-if', () => { ]) }) + test('error on adjacent v-else', () => { + const onError = vi.fn() + + const { + node: { branches }, + } = parseWithIfTransform( + `
`, + { 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', () => { const onError = vi.fn() // dynamic diff --git a/packages/compiler-core/src/transforms/vIf.ts b/packages/compiler-core/src/transforms/vIf.ts index 54c505407a..8bf5c6a32f 100644 --- a/packages/compiler-core/src/transforms/vIf.ts +++ b/packages/compiler-core/src/transforms/vIf.ts @@ -141,9 +141,9 @@ export function processIf( } 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 ( - dir.name === 'else-if' && + (dir.name === 'else-if' || dir.name === 'else') && sibling.branches[sibling.branches.length - 1].condition === undefined ) { context.onError(