Skip to content

Commit fd16f2b

Browse files
committed
refactor: avoid global scope parentStack
1 parent 3ea85c1 commit fd16f2b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/compiler-sfc/src/compileScript.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ export function compileScript(
739739
if (enableRefSugar && Object.keys(refBindings).length) {
740740
for (const node of scriptSetupAst) {
741741
if (node.type !== 'ImportDeclaration') {
742-
walkIdentifiers(node, (id, parent) => {
742+
walkIdentifiers(node, (id, parent, parentStack) => {
743743
if (refBindings[id.name] && !refIdentifiers.has(id)) {
744744
if (isStaticProperty(parent) && parent.shorthand) {
745745
// let binding used in a property shorthand
@@ -1337,8 +1337,6 @@ function genRuntimeEmits(emits: Set<string>) {
13371337
: ``
13381338
}
13391339

1340-
const parentStack: Node[] = []
1341-
13421340
/**
13431341
* Walk an AST and find identifiers that are variable references.
13441342
* This is largely the same logic with `transformExpressions` in compiler-core
@@ -1347,15 +1345,19 @@ const parentStack: Node[] = []
13471345
*/
13481346
function walkIdentifiers(
13491347
root: Node,
1350-
onIdentifier: (node: Identifier, parent: Node) => void
1348+
onIdentifier: (node: Identifier, parent: Node, parentStack: Node[]) => void
13511349
) {
1350+
const parentStack: Node[] = []
13521351
const knownIds: Record<string, number> = Object.create(null)
13531352
;(walk as any)(root, {
13541353
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
13551354
parent && parentStack.push(parent)
13561355
if (node.type === 'Identifier') {
1357-
if (!knownIds[node.name] && isRefIdentifier(node, parent!)) {
1358-
onIdentifier(node, parent!)
1356+
if (
1357+
!knownIds[node.name] &&
1358+
isRefIdentifier(node, parent!, parentStack)
1359+
) {
1360+
onIdentifier(node, parent!, parentStack)
13591361
}
13601362
} else if (isFunction(node)) {
13611363
// walk function expressions and add its arguments to known identifiers
@@ -1411,7 +1413,7 @@ function walkIdentifiers(
14111413
})
14121414
}
14131415

1414-
function isRefIdentifier(id: Identifier, parent: Node) {
1416+
function isRefIdentifier(id: Identifier, parent: Node, parentStack: Node[]) {
14151417
// declaration id
14161418
if (
14171419
(parent.type === 'VariableDeclarator' ||

0 commit comments

Comments
 (0)