From 6254bc1550329567aa39736e580091e225f100da Mon Sep 17 00:00:00 2001 From: wujingchang <1957503379@qq.com> Date: Wed, 15 Sep 2021 23:35:33 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): shouldn't add `;` when use top level await with single-line if condition --- .../compiler-sfc/__tests__/compileScript.spec.ts | 8 ++++++++ packages/compiler-sfc/src/compileScript.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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..0f6e81aeaaa 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -913,6 +913,13 @@ export function compileScript( (node.type === 'VariableDeclaration' && !node.declare) || node.type.endsWith('Statement') ) { + let isIfExpStmt = false + if ( + node.type === 'IfStatement' && + node.consequent.type === 'ExpressionStatement' + ) { + isIfExpStmt = true + } ;(walk as any)(node, { enter(child: Node, parent: Node) { if (isFunctionType(child)) { @@ -920,7 +927,10 @@ export function compileScript( } if (child.type === 'AwaitExpression') { hasAwait = true - processAwait(child, parent.type === 'ExpressionStatement') + processAwait( + child, + parent.type === 'ExpressionStatement' && !isIfExpStmt + ) } } }) From b9c26b8f7f9896fa96bdfeb4569a6105ad0de300 Mon Sep 17 00:00:00 2001 From: wujingchang <1957503379@qq.com> Date: Thu, 16 Sep 2021 08:00:01 +0800 Subject: [PATCH 2/2] chore: optimized code --- packages/compiler-sfc/src/compileScript.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 0f6e81aeaaa..9ea8a01528a 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -913,13 +913,9 @@ export function compileScript( (node.type === 'VariableDeclaration' && !node.declare) || node.type.endsWith('Statement') ) { - let isIfExpStmt = false - if ( + const isIfExpStmt = node.type === 'IfStatement' && node.consequent.type === 'ExpressionStatement' - ) { - isIfExpStmt = true - } ;(walk as any)(node, { enter(child: Node, parent: Node) { if (isFunctionType(child)) {