Skip to content

Commit 6c3b681

Browse files
liulinboyiyyx990803
authored andcommitted
fix(compiler-sfc): async transformer doesn't correctly detect need for semicolon in block #5808
1 parent de7a879 commit 6c3b681

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/compiler-sfc/src/compileScript.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ export function compileScript(
11261126

11271127
// walk statements & named exports / variable declarations for top level
11281128
// await
1129+
let body = scriptSetupAst.body
11291130
if (
11301131
(node.type === 'VariableDeclaration' && !node.declare) ||
11311132
node.type.endsWith('Statement')
@@ -1135,11 +1136,32 @@ export function compileScript(
11351136
if (isFunctionType(child)) {
11361137
this.skip()
11371138
}
1139+
if (child.type === 'ExpressionStatement') {
1140+
if (
1141+
child.expression.type === 'AwaitExpression' ||
1142+
child.expression.type === 'BinaryExpression'
1143+
) {
1144+
// set the parent of the AwaitExpression's body to the variable body
1145+
if (parent && parent.type === 'BlockStatement') {
1146+
body = parent.body
1147+
} else {
1148+
body = scriptSetupAst.body
1149+
}
1150+
}
1151+
}
11381152
if (child.type === 'AwaitExpression') {
11391153
hasAwait = true
1140-
const needsSemi = scriptSetupAst.body.some(n => {
1154+
// set the AwaitExpression's index in the parent of the AwaitExpression's body to the variable AwaitIndex
1155+
let AwaitIndex = 0
1156+
let needsSemi = body.some((n, index) => {
1157+
AwaitIndex = index
11411158
return n.type === 'ExpressionStatement' && n.start === child.start
11421159
})
1160+
// if the variable body is not equal scriptSetupAst.body
1161+
if (body !== scriptSetupAst.body) {
1162+
// judge the AwaitExpression is not in the first of the parent of the AwaitExpression's body
1163+
needsSemi = needsSemi && AwaitIndex > 0
1164+
}
11431165
processAwait(
11441166
child,
11451167
needsSemi,

0 commit comments

Comments
 (0)