From: liulinboyi <814921718@qq.com> Date: Tue, 26 Apr 2022 18:06:24 +0000 (+0800) Subject: fix(compiler-sfc): async transformer doesn't correctly detect need for semicolon... X-Git-Tag: v3.2.34-beta.1~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c3b681d235bc70293827c535572f90be1ab6c68;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): async transformer doesn't correctly detect need for semicolon in block #5808 --- diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 1139f71a7a..7442e23c26 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1126,6 +1126,7 @@ export function compileScript( // walk statements & named exports / variable declarations for top level // await + let body = scriptSetupAst.body if ( (node.type === 'VariableDeclaration' && !node.declare) || node.type.endsWith('Statement') @@ -1135,11 +1136,32 @@ export function compileScript( if (isFunctionType(child)) { this.skip() } + if (child.type === 'ExpressionStatement') { + if ( + child.expression.type === 'AwaitExpression' || + child.expression.type === 'BinaryExpression' + ) { + // set the parent of the AwaitExpression's body to the variable body + if (parent && parent.type === 'BlockStatement') { + body = parent.body + } else { + body = scriptSetupAst.body + } + } + } if (child.type === 'AwaitExpression') { hasAwait = true - const needsSemi = scriptSetupAst.body.some(n => { + // set the AwaitExpression's index in the parent of the AwaitExpression's body to the variable AwaitIndex + let AwaitIndex = 0 + let needsSemi = body.some((n, index) => { + AwaitIndex = index return n.type === 'ExpressionStatement' && n.start === child.start }) + // if the variable body is not equal scriptSetupAst.body + if (body !== scriptSetupAst.body) { + // judge the AwaitExpression is not in the first of the parent of the AwaitExpression's body + needsSemi = needsSemi && AwaitIndex > 0 + } processAwait( child, needsSemi,