Skip to content

Commit bc679b4

Browse files
authored
perf(transform): mayContainAwaitExpr end traversal early (#65)
1 parent 376ff89 commit bc679b4

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

packages/compiler/src/transform.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { AwaitExpression, Node } from '@babel/types'
1111
import {
1212
CSS_VARS_HELPER,
1313
DEFINE_COMPONENT_HELPER,
14+
EXPECTED_ERROR,
1415
TO_REFS_HELPER,
1516
UN_REF_HELPER,
1617
USE_DEFAULTS_HELPER,
@@ -43,21 +44,27 @@ function wrapWithAsyncContext(
4344

4445
function mayContainAwaitExpr(targetNode: Node) {
4546
let awaitExpr: AwaitExpression | undefined
46-
let isMayCotainAwaitExpr = (
47+
if (!(
4748
isVariableDeclaration(targetNode)
4849
|| isAssignmentExpression(targetNode)
4950
|| isExpressionStatement(targetNode)
50-
)
51-
if (!isMayCotainAwaitExpr) {
51+
)) {
5252
return false
5353
}
54-
traverse(targetNode, (descendant) => {
55-
if (isAwaitExpression(descendant)) {
56-
isMayCotainAwaitExpr = true
57-
awaitExpr = descendant
54+
try {
55+
traverse(targetNode, (descendant) => {
56+
if (isAwaitExpression(descendant)) {
57+
awaitExpr = descendant
58+
throw new Error(EXPECTED_ERROR)
59+
}
60+
})
61+
}
62+
catch (error) {
63+
if (error === EXPECTED_ERROR) {
64+
return awaitExpr
5865
}
59-
})
60-
return isMayCotainAwaitExpr && awaitExpr
66+
throw error
67+
}
6168
}
6269

6370
function registerImport(
@@ -89,7 +96,7 @@ function registerImport(
8996
* We need to remove all imports from the original code, one by one, and then prepend the
9097
* merged imports to the code, based on our analysis result.
9198
*
92-
* 2. - Transform every Vine comonent function to be an IIFE.
99+
* 2. - Transform every Vine component function to be an IIFE.
93100
* it's for creating a independent scope, so we can put those statements can be hosted.
94101
*/
95102
export function transformFile(
@@ -169,7 +176,7 @@ export function transformFile(
169176

170177
// Replace the original function delcaration start to its body's first statement's start,
171178
// and the last statement's end to the function declaration end.
172-
// Wrap all body statemnts into a `setup(...) { ... }`
179+
// Wrap all body statements into a `setup(...) { ... }`
173180
ms.remove(vineCompFnStart, firstStmt.start!)
174181
ms.remove(lastStmt.end!, vineCompFnEnd)
175182

0 commit comments

Comments
 (0)