Skip to content

Commit

Permalink
Revert "perf: Improve scope information collection performance" (#17005)
Browse files Browse the repository at this point in the history
Revert "perf: Improve scope information collection performance (#16923)"

This reverts commit ded1571.
  • Loading branch information
JLHwung authored Dec 5, 2024
1 parent 651bc0b commit f33704a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 126 deletions.
23 changes: 0 additions & 23 deletions packages/babel-traverse/src/path/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,6 @@ export function stop(this: NodePath) {
this._traverseFlags |= SHOULD_SKIP | SHOULD_STOP;
}

export function _forceSetScope(current: NodePath) {
let path = current.parentPath;

if (
// Skip method scope if is computed method key or decorator expression
((current.key === "key" || current.listKey === "decorators") &&
path.isMethod()) ||
// Skip switch scope if for discriminant (`x` in `switch (x) {}`).
(current.key === "discriminant" && path.isSwitchStatement())
) {
path = path.parentPath;
}

let target;
while (path && !target) {
target = path.scope;
path = path.parentPath;
}

current.scope = current.getScope(target);
current.scope?.init();
}

export function setScope(this: NodePath) {
if (this.opts?.noScope) return;

Expand Down
11 changes: 2 additions & 9 deletions packages/babel-traverse/src/path/lib/virtual-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import type * as t from "@babel/types";

export interface VirtualTypeAliases {
BindingIdentifier: t.Identifier;
BlockScoped:
| t.FunctionDeclaration
| t.ClassDeclaration
| t.VariableDeclaration;
BlockScoped: t.Node;
ExistentialTypeParam: t.ExistsTypeAnnotation;
Expression: t.Expression;
Flow: t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier;
Expand Down Expand Up @@ -45,11 +42,7 @@ export const Scope: VirtualTypeMapping = ["Scopable", "Pattern"] as const;

export const Referenced: VirtualTypeMapping = null;

export const BlockScoped: VirtualTypeMapping = [
"FunctionDeclaration",
"ClassDeclaration",
"VariableDeclaration",
] as const;
export const BlockScoped: VirtualTypeMapping = null;

export const Var: VirtualTypeMapping = ["VariableDeclaration"];

Expand Down
6 changes: 4 additions & 2 deletions packages/babel-traverse/src/scope/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Renamer from "./lib/renamer.ts";
import type NodePath from "../path/index.ts";
import traverse from "../index.ts";
import traverseForScope from "./traverseForScope.ts";
import Binding from "./binding.ts";
import type { BindingKind } from "./binding.ts";
import globals from "globals";
Expand Down Expand Up @@ -931,14 +930,17 @@ class Scope {
// traverse does not visit the root node, here we explicitly collect
// root node binding info when the root is not a Program.
if (path.type !== "Program" && isExplodedVisitor(collectorVisitor)) {
for (const visit of collectorVisitor.enter) {
visit.call(state, path, state);
}
const typeVisitors = collectorVisitor[path.type];
if (typeVisitors) {
for (const visit of typeVisitors.enter) {
visit.call(state, path, state);
}
}
}
traverseForScope(path, collectorVisitor, state);
path.traverse(collectorVisitor, state);
this.crawling = false;

// register assignments
Expand Down
89 changes: 0 additions & 89 deletions packages/babel-traverse/src/scope/traverseForScope.ts

This file was deleted.

10 changes: 7 additions & 3 deletions packages/babel-traverse/src/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,13 @@ function explode$1<S>(visitor: Visitor<S>): ExplodedVisitor<S> {
const types = virtualTypes[nodeType];
if (types !== null) {
for (const type of types) {
// @ts-expect-error Expression produces too complex union
visitor[type] ??= {};
mergePair(visitor[type], fns);
// merge the visitor if necessary or just put it back in
if (visitor[type]) {
mergePair(visitor[type], fns);
} else {
// @ts-expect-error Expression produces too complex union
visitor[type] = fns;
}
}
} else {
mergePair(visitor, fns);
Expand Down

0 comments on commit f33704a

Please sign in to comment.