fix(compiler-sfc): async transformer doesn't correctly detect need for semicolon in block #5808

This commit is contained in:
liulinboyi 2022-04-27 02:06:24 +08:00 committed by Evan You
parent de7a879cda
commit 6c3b681d23

View File

@ -1126,6 +1126,7 @@ export function compileScript(
// walk statements & named exports / variable declarations for top level // walk statements & named exports / variable declarations for top level
// await // await
let body = scriptSetupAst.body
if ( if (
(node.type === 'VariableDeclaration' && !node.declare) || (node.type === 'VariableDeclaration' && !node.declare) ||
node.type.endsWith('Statement') node.type.endsWith('Statement')
@ -1135,11 +1136,32 @@ export function compileScript(
if (isFunctionType(child)) { if (isFunctionType(child)) {
this.skip() 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') { if (child.type === 'AwaitExpression') {
hasAwait = true 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 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( processAwait(
child, child,
needsSemi, needsSemi,