Backtestsvuerun-20261021-133314#15495
[compiler-sfc] Top-level await inside switch case generates invalid code (missing semicolon before rewrite)
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The source changes in both patches are functionally identical: push `SwitchCase.consequent` onto the scope stack on enter and pop it on leave, so `needsSemi` is computed against the correct statement list. Both resolve the root cause and both pass all independent tests. The difference is in test coverage. Patch A adds two tests (root-level switch and switch nested in an if) that assert a `;(` appears after the preceding call, plus snapshots. Patch B adds four tests that compile and actually execute the generated setup(), verifying call order at runtime, and covers the additional variants the issue explicitly lists: two consecutive awaits in one case, an await in a `default` case after an expression statement, and a regression guard that an await as the first statement of a case does not get a spurious semicolon. B's tests would fail on base and pass after, and they exercise the behaviour the issue cares about (statements run independently) rather than only the textual shape. Patch B also updates the explanatory comment to mention switch case consequents. Scope and craft are otherwise equivalent, so B wins on test quality and coverage of the issue's stated variants.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
4m 14s
Wall-clock time in the container
Full test suite
No new failures
637 passed · 1 skipped · 638 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
75vs 76 merged
Changed lines, ours vs merged
The issue
View on GitHub[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
Steps to reproduce
- Open the playground link above (or compile the SFC below with
@vue/compiler-sfc). - The component
setup()throwsTypeError: 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
awaitthat follows an expression statement inside a case branch.
No prior issue found for this.
The two patches
packages/compiler-sfc/__tests__/compileScript.spec.tsindex a1cc885cd..38e4f4e85 100644@@ -1045,6 +1045,75 @@ describe('SFC compile <script setup>', () => { )((getAwaitable: () => unknown) => [getAwaitable(), () => {}]) await expect(component.setup({}, { expose() {} })).resolves.toEqual({}) })++ describe('await statements inside switch case', () => {+ async function runSetup(code: string) {+ const { content } = compile(`<script setup>${code}</script>`, {+ genDefaultAs: '_sfc_',+ })+ const calls: string[] = []+ const component = new Function(+ '_withAsyncContext',+ 'calls',+ `${content.replace(/^import .*\n/, '')};return _sfc_`,+ )((getAwaitable: () => unknown) => [getAwaitable(), () => {}], calls)+ await component.setup({}, { expose() {} })+ return { content, calls }+ }++ test('after an expression statement', async () => {+ const { content, calls } = await runSetup(`+ const b = 1+ if (true) {+ switch (b) {+ case 1:+ calls.push('qux')+ await calls.push('bar')+ }+ }`)+ expect(content).toMatch(`calls.push('qux')\n ;(`)+ expect(calls).toEqual(['qux', 'bar'])+ })++ test('consecutive awaits', async () => {+ const { calls } = await runSetup(`+ const b = 1+ switch (b) {+ case 1:+ await calls.push('a')+ await calls.push('b')+ break+ default:+ calls.push('c')+ await calls.push('d')+ }`)+ expect(calls).toEqual(['a', 'b'])+ })++ test('in default case', async () => {+ const { calls } = await runSetup(`+ const b = 2+ switch (b) {+ case 1:+ break+ default:+ calls.push('c')+ await calls.push('d')+ await calls.push('e')+ }`)+ expect(calls).toEqual(['c', 'd', 'e'])+ })++ test('first statement of a case should not prepend semicolon', async () => {+ const { content, calls } = await runSetup(`+ switch (1) {+ case 1:+ await calls.push('a')+ }`)+ expect(content).not.toMatch(`case 1:\n ;(`)+ expect(calls).toEqual(['a'])+ })+ }) }) describe('errors', () => {packages/compiler-sfc/src/compileScript.tsindex 901196235..bdccd05cc 100644@@ -646,12 +646,15 @@ 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 // if the await expression is an expression statement and // - is in the root scope // - or is not the first statement in a nested block scope+ // (block statement body or switch case consequent) // then it needs a semicolon before the generated code. const currentScope = scope[scope.length - 1] const needsSemi = currentScope.some((n, i) => {@@ -670,7 +673,8 @@ export function compileScript( } }, leave(node: Node) {- if (node.type === 'BlockStatement') scope.pop()+ if (node.type === 'BlockStatement' || node.type === 'SwitchCase')+ scope.pop() }, }) }
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()+ } }, }) }