@@ -1126,48 +1126,42 @@ export function compileScript(
1126
1126
1127
1127
// walk statements & named exports / variable declarations for top level
1128
1128
// await
1129
- let body = scriptSetupAst . body
1130
1129
if (
1131
1130
( node . type === 'VariableDeclaration' && ! node . declare ) ||
1132
1131
node . type . endsWith ( 'Statement' )
1133
1132
) {
1133
+ const scope : Statement [ ] [ ] = [ scriptSetupAst . body ]
1134
1134
; ( walk as any ) ( node , {
1135
1135
enter ( child : Node , parent : Node ) {
1136
1136
if ( isFunctionType ( child ) ) {
1137
1137
this . skip ( )
1138
1138
}
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
- }
1139
+ if ( child . type === 'BlockStatement' ) {
1140
+ scope . push ( child . body )
1151
1141
}
1152
1142
if ( child . type === 'AwaitExpression' ) {
1153
1143
hasAwait = true
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
1158
- return n . type === 'ExpressionStatement' && n . start === child . start
1144
+ // if the await expression is an expression statement and
1145
+ // - is in the root scope
1146
+ // - or is not the first statement in a nested block scope
1147
+ // then it needs a semicolon before the generated code.
1148
+ const currentScope = scope [ scope . length - 1 ]
1149
+ const needsSemi = currentScope . some ( ( n , i ) => {
1150
+ return (
1151
+ ( scope . length === 1 || i > 0 ) &&
1152
+ n . type === 'ExpressionStatement' &&
1153
+ n . start === child . start
1154
+ )
1159
1155
} )
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
- }
1165
1156
processAwait (
1166
1157
child ,
1167
1158
needsSemi ,
1168
1159
parent . type === 'ExpressionStatement'
1169
1160
)
1170
1161
}
1162
+ } ,
1163
+ exit ( node : Node ) {
1164
+ if ( node . type === 'BlockStatement' ) scope . pop ( )
1171
1165
}
1172
1166
} )
1173
1167
}
0 commit comments