From be6a0bff9887e58c9547c7cc0ee5aefe222d1f56 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 13 May 2022 15:40:53 +0800 Subject: [PATCH] refactor: simplify logic --- packages/compiler-sfc/src/compileScript.ts | 40 +++++++++------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 7442e23c..0e1431c6 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1126,48 +1126,42 @@ 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') ) { + const scope: Statement[][] = [scriptSetupAst.body] ;(walk as any)(node, { enter(child: Node, parent: Node) { 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 === 'BlockStatement') { + scope.push(child.body) } if (child.type === 'AwaitExpression') { hasAwait = true - // 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 await expression is an expression statement and + // - is in the root scope + // - or is not the first statement in a nested block scope + // then it needs a semicolon before the generated code. + const currentScope = scope[scope.length - 1] + const needsSemi = currentScope.some((n, i) => { + return ( + (scope.length === 1 || i > 0) && + 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, parent.type === 'ExpressionStatement' ) } + }, + exit(node: Node) { + if (node.type === 'BlockStatement') scope.pop() } }) }