Backtestsvuerun-20261005-155915#15495

Issue #15495 Merged fix PR #15498 Run 5 Oct 2026 · run-20261005-155915 vuejs/core

[compiler-sfc] Top-level await inside switch case generates invalid code (missing semicolon before rewrite)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

The production change is functionally identical in both patches. Each pushes `SwitchCase.consequent` onto the scope stack on enter and pops it on leave. This fixes the root cause: `needsSemi` now sees the case's statement list, so awaits following other statements in a case get the leading `;`. The only code difference is cosmetic (braces around `scope.pop()` in B). The deciding factor is test coverage. Patch A tests the reported scenario: a switch nested in `if`, with an expression statement followed by an await. It also tests the 'consecutive awaits in one case' variant the issue explicitly lists as affected. A additionally checks that the first statement of a case gets no semicolon, across multiple cases including `default`. That guards against over-inserting and against scope leaking between cases. Patch B tests only the single expression-then-await pattern, at top level and nested in `if`. It leaves the consecutive-awaits variant and the first-statement-of-case behavior unverified. Both patches' tests would fail before the fix, but A's assertions are more precise and cover more of the issue's described variants.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

7m 52s

Wall-clock time in the container

Full test suite

No new failures

3,741 passed · 6 skipped · 3,747 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

106vs 76 merged

Changed lines, ours vs merged

[compiler-sfc] Top-level await inside switch case generates invalid code (missing semicolon before rewrite)

Vue version

3.5.42 — also reproduced on main (5409708)

Link to minimal reproduction

https://play.vuejs.org/#N4IgbgpgTgzglgewHYgFwgMwDoCsWAsATCADQgBmcANhDGqDFAMYD0AggA4dZgCuE9EE2QAXCEhFoQAHjEBbDlQCGYgHwAdJAAIt0gCZwwqqBA5QE0lgaObL8xSogaktmEyhwOIrTAgjeHM5wCghQ3sBaJuRaAL5a5OZyWgDkfBDJmprCSDDeSloAvJEQ5AAUIlD8AJRZyLlaAEaFxWUAjDUuSOS8SEwiiNoAjrwAHqVVWsCaOiwsxf5QOVo9eiVwSBB6WshaHLxQHAi+mjGZSEowAJ698T19A41KUOOT0-P72kQnZ3DRpUo8JRUaqvbQ+ADucBETAAFlpSg1AcCIBMpmCdEwLhAtK1UG8dDphmMOgSCUpwUooY9niTYt8kJY3B4vM5SCA4DA2PdIGgKvwYmRGKw5JSkFgRHRUKBsmIJFJgocwpMtO4II5OBxYvFEik0hkkArQt4NdqEElklh2FwePx9VkTOquKUNVUsHIED0RKVkgBiJRcZIdNkcrn9HmoPkQGICkAILwDSXAaNAA

Steps to reproduce

  1. Open the playground link above (or compile the SFC below with @vue/compiler-sfc).
  2. The component setup() throws TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)).

SFC:

<script setup>
const a = true
const b = 1
function qux() {}
async function bar() { return 42 }
if (a) {
  switch (b) {
    case 1:
      qux()
      await bar()
  }
}
</script>

What is expected?

The compiled setup() should run fine: qux() and await bar() should be two independent statements.

What is actually happening?

The top-level await rewrite in compileScript emits the withAsyncContext wrapper expression without a leading semicolon when the await is inside a switch case that is not the first statement:

async setup(__props, { expose: __expose }) {
  ...
  if (a) {
    switch (b) {
      case 1:
        qux()
        (
  ([__temp,__restore] = _withAsyncContext(() => bar())),
  await __temp,
  __restore()
)
    }
  }
}

Because the next line starts with (, ASI does not insert a semicolon, so the two lines are parsed as a single call expression qux()(...). bar() is never awaited in the intended way and the runtime throws TypeError: object is not iterable.

The same code inside a plain block (not a switch) is compiled correctly with a leading ;:

if (a) {
  qux()
  ;(
  ([__temp,__restore] = _withAsyncContext(() => bar())),
  await __temp,
  __restore()
)
}

Additional comments

Root cause: in packages/compiler-sfc/src/compileScript.ts, the scope stack used to decide whether the rewrite needs a semicolon only pushes/pops on BlockStatement. A switch statement's SwitchCase.consequent statement list is not tracked, so needsSemi is misjudged as false for await expressions inside case branches (unless the await happens to be the very first statement of the case).

Related variants also affected:

  • Two consecutive top-level awaits inside one case produce (...)(...) back-to-back (second one also parsed as a call of the first result).
  • Any await that follows an expression statement inside a case branch.

No prior issue found for this.

The two patches

Our patch+105 −1
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snapindex 795f0ae88..dfb867046 100644@@ -206,6 +206,72 @@ return { n, get x() { return x } } })" `; +exports[`SFC compile <script setup> > async/await detection > await after expression statement in switch case 1`] = `+"import { withAsyncContext as _withAsyncContext } from 'vue'++export default {+  async setup(__props, { expose: __expose }) {+  __expose();++let __temp, __restore+if (a) {+  switch (b) {+    case 1:+      qux()+      ;(+  ([__temp,__restore] = _withAsyncContext(() => bar())),+  await __temp,+  __restore()+)+  }+}+return {  }+}++}"+`;++exports[`SFC compile <script setup> > async/await detection > consecutive awaits in switch case 1`] = `+"import { withAsyncContext as _withAsyncContext } from 'vue'++export default {+  async setup(__props, { expose: __expose }) {+  __expose();++let __temp, __restore+switch (b) {+  case 1:+    (+  ([__temp,__restore] = _withAsyncContext(() => foo())),+  await __temp,+  __restore()+)+    ;(+  ([__temp,__restore] = _withAsyncContext(() => bar())),+  await __temp,+  __restore()+)+    break+  case 2:+    (+  ([__temp,__restore] = _withAsyncContext(() => baz())),+  await __temp,+  __restore()+)+  default:+    qux()+    ;(+  ([__temp,__restore] = _withAsyncContext(() => qux())),+  await __temp,+  __restore()+)+}+return {  }+}++}"+`;+ exports[`SFC compile <script setup> > async/await detection > expression statement 1`] = ` "import { withAsyncContext as _withAsyncContext } from 'vue' packages/compiler-sfc/__tests__/compileScript.spec.tsindex a1cc885cd..6c6e2a170 100644@@ -960,6 +960,41 @@ describe('SFC compile <script setup>', () => {       assertAwaitDetection(`if (ok) { await foo } else { await bar }`)     }) +    // should prepend semicolon inside switch case consequent+    test('await after expression statement in switch case', () => {+      const code = assertAwaitDetection(`if (a) {+  switch (b) {+    case 1:+      qux()+      await bar()+  }+}`)+      expect(code).toMatch(`qux()\n      ;(`)+      expect(code).not.toMatch(`qux()\n      (`)+    })++    test('consecutive awaits in switch case', () => {+      const code = assertAwaitDetection(`switch (b) {+  case 1:+    await foo()+    await bar()+    break+  case 2:+    await baz()+  default:+    qux()+    await qux()+}`)+      // first statement of a case follows `:` and needs no semicolon+      expect(code).toMatch(`case 1:\n    (`)+      expect(code).toMatch(`case 2:\n    (`)+      // subsequent statements in the same case need one+      expect(code).toMatch(+        `\n)\n    ;(\n  ([__temp,__restore] = _withAsyncContext(() => bar()))`,+      )+      expect(code).toMatch(`qux()\n    ;(`)+    })+     test('multiple `if` nested statements', () => {       assertAwaitDetection(`if (ok) {         let a = 'foo'packages/compiler-sfc/src/compileScript.tsindex 901196235..9337d5cd1 100644@@ -646,6 +646,8 @@ export function compileScript(           }           if (child.type === 'BlockStatement') {             scope.push(child.body)+          } else if (child.type === 'SwitchCase') {+            scope.push(child.consequent)           }           if (child.type === 'AwaitExpression') {             hasAwait = true@@ -670,7 +672,8 @@ export function compileScript(           }         },         leave(node: Node) {-          if (node.type === 'BlockStatement') scope.pop()+          if (node.type === 'BlockStatement' || node.type === 'SwitchCase')+            scope.pop()         },       })     } 
Merged fix · PR #15498+75 −1
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snapindex 795f0ae88..c718e7690 100644@@ -206,6 +206,54 @@ return { n, get x() { return x } } })" `; +exports[`SFC compile <script setup> > async/await detection > await in switch case 1`] = `+"import { withAsyncContext as _withAsyncContext } from 'vue'++export default {+  async setup(__props, { expose: __expose }) {+  __expose();++let __temp, __restore+switch (a) {+        case 1:+          foo()+          ;(+  ([__temp,__restore] = _withAsyncContext(() => bar())),+  await __temp,+  __restore()+)+      }+return {  }+}++}"+`;++exports[`SFC compile <script setup> > async/await detection > await in switch case nested in a block 1`] = `+"import { withAsyncContext as _withAsyncContext } from 'vue'++export default {+  async setup(__props, { expose: __expose }) {+  __expose();++let __temp, __restore+if (a) {+        switch (b) {+          case 1:+            qux()+            ;(+  ([__temp,__restore] = _withAsyncContext(() => bar())),+  await __temp,+  __restore()+)+        }+      }+return {  }+}++}"+`;+ exports[`SFC compile <script setup> > async/await detection > expression statement 1`] = ` "import { withAsyncContext as _withAsyncContext } from 'vue' packages/compiler-sfc/__tests__/compileScript.spec.tsindex a1cc885cd..caaae92b4 100644@@ -960,6 +960,28 @@ describe('SFC compile <script setup>', () => {       assertAwaitDetection(`if (ok) { await foo } else { await bar }`)     }) +    // #15495+    test('await in switch case', () => {+      const code = assertAwaitDetection(`switch (a) {+        case 1:+          foo()+          await bar()+      }`)+      expect(code).toMatch(/foo\(\)\s*;\(/)+    })++    // #15495+    test('await in switch case nested in a block', () => {+      const code = assertAwaitDetection(`if (a) {+        switch (b) {+          case 1:+            qux()+            await bar()+        }+      }`)+      expect(code).toMatch(/qux\(\)\s*;\(/)+    })+     test('multiple `if` nested statements', () => {       assertAwaitDetection(`if (ok) {         let a = 'foo'packages/compiler-sfc/src/compileScript.tsindex 901196235..3c070a0e0 100644@@ -646,6 +646,8 @@ export function compileScript(           }           if (child.type === 'BlockStatement') {             scope.push(child.body)+          } else if (child.type === 'SwitchCase') {+            scope.push(child.consequent)           }           if (child.type === 'AwaitExpression') {             hasAwait = true@@ -670,7 +672,9 @@ export function compileScript(           }         },         leave(node: Node) {-          if (node.type === 'BlockStatement') scope.pop()+          if (node.type === 'BlockStatement' || node.type === 'SwitchCase') {+            scope.pop()+          }         },       })     }