diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index d3a1282f4c5..638a4f038cf 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -1118,6 +1118,14 @@ const emit = defineEmits(['a', 'b']) false ) }) + + test('if statement with expression statement consequent type', () => { + assertAwaitDetection(`if (ok) await foo`, code => { + return code.includes( + `if (ok) (([__temp,__restore]=_withAsyncContext(()=>(foo))),__temp=await __temp,__restore(),__temp)` + ) + }) + }) }) describe('errors', () => { diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 609244232ef..9ea8a01528a 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -913,6 +913,9 @@ export function compileScript( (node.type === 'VariableDeclaration' && !node.declare) || node.type.endsWith('Statement') ) { + const isIfExpStmt = + node.type === 'IfStatement' && + node.consequent.type === 'ExpressionStatement' ;(walk as any)(node, { enter(child: Node, parent: Node) { if (isFunctionType(child)) { @@ -920,7 +923,10 @@ export function compileScript( } if (child.type === 'AwaitExpression') { hasAwait = true - processAwait(child, parent.type === 'ExpressionStatement') + processAwait( + child, + parent.type === 'ExpressionStatement' && !isIfExpStmt + ) } } })