refactor: simplify logic
This commit is contained in:
parent
a0290fe781
commit
be6a0bff98
@ -1126,48 +1126,42 @@ 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')
|
||||||
) {
|
) {
|
||||||
|
const scope: Statement[][] = [scriptSetupAst.body]
|
||||||
;(walk as any)(node, {
|
;(walk as any)(node, {
|
||||||
enter(child: Node, parent: Node) {
|
enter(child: Node, parent: Node) {
|
||||||
if (isFunctionType(child)) {
|
if (isFunctionType(child)) {
|
||||||
this.skip()
|
this.skip()
|
||||||
}
|
}
|
||||||
if (child.type === 'ExpressionStatement') {
|
if (child.type === 'BlockStatement') {
|
||||||
if (
|
scope.push(child.body)
|
||||||
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
|
||||||
// set the AwaitExpression's index in the parent of the AwaitExpression's body to the variable AwaitIndex
|
// if the await expression is an expression statement and
|
||||||
let AwaitIndex = 0
|
// - is in the root scope
|
||||||
let needsSemi = body.some((n, index) => {
|
// - or is not the first statement in a nested block scope
|
||||||
AwaitIndex = index
|
// then it needs a semicolon before the generated code.
|
||||||
return n.type === 'ExpressionStatement' && n.start === child.start
|
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(
|
processAwait(
|
||||||
child,
|
child,
|
||||||
needsSemi,
|
needsSemi,
|
||||||
parent.type === 'ExpressionStatement'
|
parent.type === 'ExpressionStatement'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
exit(node: Node) {
|
||||||
|
if (node.type === 'BlockStatement') scope.pop()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user