Skip to content

Commit

Permalink
perf: revise traverseNode to use _skipKeysSet
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Sep 22, 2019
1 parent 11e1b29 commit 98ec2c2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
19 changes: 13 additions & 6 deletions packages/babel-traverse/src/index.js
@@ -1,8 +1,8 @@
import TraversalContext from "./context";
import * as visitors from "./visitors";
import includes from "lodash/includes";
import * as t from "@babel/types";
import * as cache from "./cache";
import { traverseNode } from "./traverse-node";

export { default as NodePath } from "./path";
export { default as Scope } from "./scope";
Expand Down Expand Up @@ -33,7 +33,11 @@ export default function traverse(

visitors.explode(opts);

traverse.node(parent, opts, scope, state, parentPath);
if (!t.VISITOR_KEYS[parent.type]) {
return;
}

traverseNode(parent, opts, scope, state, parentPath, new Set());
}

traverse.visitors = visitors;
Expand All @@ -55,11 +59,14 @@ traverse.node = function(
const keys: Array = t.VISITOR_KEYS[node.type];
if (!keys) return;

const context = new TraversalContext(scope, opts, state, parentPath);
for (const key of keys) {
if (skipKeys && skipKeys[key]) continue;
if (context.visit(node, key)) return;
let skipKeysSet;
const skipKeysKeys = Object.keys(skipKeys);
if (skipKeysKeys.length > 0) {
skipKeysSet = new Set(skipKeysKeys.filter(v => skipKeys[v] === true));
} else {
skipKeysSet = new Set();
}
return traverseNode(node, opts, scope, state, parentPath, skipKeysSet);
};

traverse.clearNode = function(node, opts) {
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-traverse/src/path/context.js
@@ -1,6 +1,6 @@
// This file contains methods responsible for maintaining a TraversalContext.

import traverse from "../index";
import { traverseNode } from "../traverse-node";
import { SHOULD_SKIP, SHOULD_STOP } from "./index";

export function call(key): boolean {
Expand Down Expand Up @@ -74,13 +74,13 @@ export function visit(): boolean {
}

this.debug("Recursing into...");
traverse.node(
traverseNode(
this.node,
this.opts,
this.scope,
this.state,
this,
this.skipKeys,
this._skipKeysSet,
);

this.call("exit");
Expand All @@ -93,7 +93,7 @@ export function skip() {
}

export function skipKey(key) {
this.skipKeys[key] = true;
this.skipKeys.add(key);
}

export function stop() {
Expand All @@ -117,7 +117,7 @@ export function setScope() {
}

export function setContext(context) {
this.skipKeys = {};
this._skipKeysSet.clear();
this._traverseFlags = 0;

if (context) {
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-traverse/src/traverse-node.js
@@ -0,0 +1,19 @@
import * as t from "@babel/types";
import TraversalContext from "./context";

export function traverseNode(
node: Object,
opts: Object,
scope: Object,
state: Object,
parentPath: Object,
skipKeys: Set<string>,
) {
const keys: Array = t.VISITOR_KEYS[node.type];

const context = new TraversalContext(scope, opts, state, parentPath);
for (const key of keys) {
if (skipKeys.has(key)) continue;
if (context.visit(node, key)) return;
}
}

0 comments on commit 98ec2c2

Please sign in to comment.