Skip to content

Commit f1fa30a

Browse files
committed
refactor(ref-transform): improve algorithm into one pass
1 parent 06051c4 commit f1fa30a

File tree

4 files changed

+437
-409
lines changed

4 files changed

+437
-409
lines changed

packages/compiler-core/src/babelUtils.ts

+13-22
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export function walkIdentifiers(
1818
isReference: boolean,
1919
isLocal: boolean
2020
) => void,
21-
onNode?: (node: Node, parent: Node, parentStack: Node[]) => void | boolean,
2221
includeAll = false,
23-
analyzeScope = true,
2422
parentStack: Node[] = [],
2523
knownIds: Record<string, number> = Object.create(null)
2624
) {
@@ -41,11 +39,8 @@ export function walkIdentifiers(
4139
) {
4240
return this.skip()
4341
}
44-
if (onNode && onNode(node, parent!, parentStack) === false) {
45-
return this.skip()
46-
}
4742
if (node.type === 'Identifier') {
48-
const isLocal = analyzeScope && !!knownIds[node.name]
43+
const isLocal = !!knownIds[node.name]
4944
const isRefed = isReferencedIdentifier(node, parent!, parentStack)
5045
if (includeAll || (isRefed && !isLocal)) {
5146
onIdentifier(node, parent!, parentStack, isRefed, isLocal)
@@ -56,24 +51,20 @@ export function walkIdentifiers(
5651
) {
5752
// mark property in destructure pattern
5853
;(node as any).inPattern = true
59-
} else if (analyzeScope) {
60-
if (isFunctionType(node)) {
61-
// walk function expressions and add its arguments to known identifiers
62-
// so that we don't prefix them
63-
walkFunctionParams(node, id =>
64-
markScopeIdentifier(node, id, knownIds)
65-
)
66-
} else if (node.type === 'BlockStatement') {
67-
// #3445 record block-level local variables
68-
walkBlockDeclarations(node, id =>
69-
markScopeIdentifier(node, id, knownIds)
70-
)
71-
}
54+
} else if (isFunctionType(node)) {
55+
// walk function expressions and add its arguments to known identifiers
56+
// so that we don't prefix them
57+
walkFunctionParams(node, id => markScopeIdentifier(node, id, knownIds))
58+
} else if (node.type === 'BlockStatement') {
59+
// #3445 record block-level local variables
60+
walkBlockDeclarations(node, id =>
61+
markScopeIdentifier(node, id, knownIds)
62+
)
7263
}
7364
},
7465
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
7566
parent && parentStack.pop()
76-
if (analyzeScope && node !== rootExp && node.scopeIds) {
67+
if (node !== rootExp && node.scopeIds) {
7768
for (const id of node.scopeIds) {
7869
knownIds[id]--
7970
if (knownIds[id] === 0) {
@@ -189,13 +180,13 @@ export function extractIdentifiers(
189180
break
190181

191182
case 'ObjectPattern':
192-
param.properties.forEach(prop => {
183+
for (const prop of param.properties) {
193184
if (prop.type === 'RestElement') {
194185
extractIdentifiers(prop.argument, nodes)
195186
} else {
196187
extractIdentifiers(prop.value, nodes)
197188
}
198-
})
189+
}
199190
break
200191

201192
case 'ArrayPattern':

packages/compiler-core/src/transforms/transformExpression.ts

-2
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ export function processExpression(
282282
ids.push(node as QualifiedId)
283283
}
284284
},
285-
undefined,
286285
true, // invoke on ALL identifiers
287-
true, // isLocal scope analysis
288286
parentStack,
289287
knownIds
290288
)

0 commit comments

Comments
 (0)