]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): adjacent v-else should cause a compiler error (#13699)
authorlinzhe <40790268+linzhe141@users.noreply.github.com>
Fri, 25 Jul 2025 00:30:05 +0000 (08:30 +0800)
committerGitHub <noreply@github.com>
Fri, 25 Jul 2025 00:30:05 +0000 (08:30 +0800)
close #13698

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

index 2c2fedab0d5850cc718a668b046646998c0ebe4f..73b6e221554dbb6d017aa680c7c7f79c28b0cbb0 100644 (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', () => {
       const onError = vi.fn()
       // dynamic
index 54c505407a363783a66753dd187278139e3ec388..8bf5c6a32ff0a127465e9595b7205489a52ec7d6 100644 (file)
@@ -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(