Backtestsvuerun-20261013-093701#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 are functionally identical. Both push `SwitchCase.consequent` onto the scope stack on enter and pop it on leave, which fixes the root cause the issue identifies: `needsSemi` was computed against the wrong statement list for awaits inside case branches. The only source difference is cosmetic: A wraps the `leave` body in braces and B uses an unbraced multi-line `if`. That is a minor point for A. The tests are the meaningful difference. Patch A adds two snapshot tests with a regex check that a `;(` follows the preceding call. Both would fail before the fix. However, they only cover the basic case named in the issue. They do not exercise the 'consecutive awaits' variant the issue explicitly lists, and they never run the generated code. Patch B compiles the SFC, executes `setup()` with a stub `_withAsyncContext`, and records the order of side effects. This checks runtime correctness directly, catching the `qux()(...)` call-merge bug and the reported `TypeError`, not just output shape. Its cases cover: - an expression statement followed by an await inside a nested `if`/`switch` (the issue's exact repro); - consecutive awaits in one case (the issue's listed variant); - an await in a `default` case; - a nested block inside a case followed by more case statements, which checks that push/pop stays balanced; - an await as the first statement of a case, which checks that no spurious semicolon is emitted. These would fail before the fix and give much stronger regression protection. Scope is equivalent and tight in both. Since correctness is identical, B's more thorough, behavior-level tests that cover the issue's variants make it the better patch.
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
3m 40s
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
79vs 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..5da0f699c 100644@@ -1045,6 +1045,80 @@ describe('SFC compile <script setup>', () => { )((getAwaitable: () => unknown) => [getAwaitable(), () => {}]) await expect(component.setup({}, { expose() {} })).resolves.toEqual({}) })++ describe('await statements inside switch cases', () => {+ async function runSetup(code: string) {+ const { content } = compile(`<script setup>${code}</script>`, {+ genDefaultAs: '_sfc_',+ })+ const calls: unknown[] = []+ const component = new Function(+ '_withAsyncContext',+ 'record',+ `${content.replace(/^import .*\n/, '')};return _sfc_`,+ )(+ (getAwaitable: () => unknown) => [getAwaitable(), () => {}],+ (v: unknown) => calls.push(v),+ )+ 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:+ record('qux')+ await Promise.resolve(record('bar'))+ }+ }`)+ expect(content).toMatch(/record\('qux'\)\s*;\(/)+ expect(calls).toEqual(['qux', 'bar'])+ })++ test('consecutive awaits', async () => {+ const { content, calls } = await runSetup(`+ switch (1) {+ case 1:+ await Promise.resolve(record(1))+ await Promise.resolve(record(2))+ break+ default:+ record('default')+ await Promise.resolve(record(3))+ }`)+ // the default branch is not executed, so assert on the output+ expect(content).toMatch(/record\('default'\)\s*;\(/)+ expect(calls).toEqual([1, 2])+ })++ test('in default case and nested block', async () => {+ const { calls } = await runSetup(`+ switch (0) {+ case 1:+ break+ default: {+ record('a')+ await Promise.resolve(record('b'))+ }+ record('c')+ await Promise.resolve(record('d'))+ }`)+ expect(calls).toEqual(['a', 'b', 'c', 'd'])+ })++ test('as first statement of a case', async () => {+ const { content, calls } = await runSetup(`+ switch (1) {+ case 1:+ await Promise.resolve(record(1))+ }`)+ expect(content).not.toMatch(/case 1:\s*;\(/)+ expect(calls).toEqual([1])+ })+ }) }) describe('errors', () => {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() }, }) }
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()+ } }, }) }