diff --git a/packages/babel-traverse/src/index.js b/packages/babel-traverse/src/index.js index 5ec1036000..7eb65732b8 100644 --- a/packages/babel-traverse/src/index.js +++ b/packages/babel-traverse/src/index.js @@ -89,7 +89,7 @@ traverse.clearNode = function (node) { if (key[0] === "_" && node[key] != null) node[key] = undefined; } - traverse.cache.delete(node); + traverse.cache.path.delete(node); let syms: Array = Object.getOwnPropertySymbols(node); for (let sym of syms) { diff --git a/packages/babel-traverse/src/path/cache.js b/packages/babel-traverse/src/path/cache.js index 39dda7a758..08a771a5fc 100644 --- a/packages/babel-traverse/src/path/cache.js +++ b/packages/babel-traverse/src/path/cache.js @@ -1,25 +1,7 @@ -let wm = new WeakMap(); +export let path = new WeakMap(); +export let scope = new WeakMap(); -// To implement clear we need to export a facade. -export default { - clear() { - wm = new WeakMap(); - }, - - delete(k) { - return wm.delete(k); - }, - - get(k) { - return wm.get(k); - }, - - has(k) { - return wm.has(k); - }, - - set(k, v) { - wm.set(k, v); - return wm; - }, -}; +export function clear() { + path = new WeakMap(); + scope = new WeakMap(); +} diff --git a/packages/babel-traverse/src/path/index.js b/packages/babel-traverse/src/path/index.js index fadb928ab6..7547e95581 100644 --- a/packages/babel-traverse/src/path/index.js +++ b/packages/babel-traverse/src/path/index.js @@ -9,7 +9,7 @@ import traverse from "../index"; import assign from "lodash/object/assign"; import Scope from "../scope"; import * as t from "babel-types"; -import cache from "./cache"; +import { path as pathCache } from "./cache"; let debug = buildDebug("babel"); @@ -69,9 +69,9 @@ export default class NodePath { let targetNode = container[key]; - let paths = cache.get(parent) || []; - if (!cache.has(parent)) { - cache.set(parent, paths); + let paths = pathCache.get(parent) || []; + if (!pathCache.has(parent)) { + pathCache.set(parent, paths); } let path; diff --git a/packages/babel-traverse/src/path/modification.js b/packages/babel-traverse/src/path/modification.js index c32745bb9c..9155a97dc6 100644 --- a/packages/babel-traverse/src/path/modification.js +++ b/packages/babel-traverse/src/path/modification.js @@ -1,7 +1,7 @@ /* eslint max-len: 0 */ // This file contains methods that modify the path/node in some ways. -import cache from "./cache"; +import { path as pathCache } from "./cache"; import PathHoister from "./lib/hoister"; import NodePath from "./index"; import * as t from "babel-types"; @@ -136,7 +136,7 @@ export function insertAfter(nodes) { export function updateSiblingKeys(fromIndex, incrementBy) { if (!this.parent) return; - let paths = cache.get(this.parent); + let paths = pathCache.get(this.parent); for (let i = 0; i < paths.length; i++) { let path = paths[i]; if (path.key >= fromIndex) { diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index 05c492f2d4..c4fe62188b 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -10,38 +10,17 @@ import * as messages from "babel-messages"; import Binding from "./binding"; import globals from "globals"; import * as t from "babel-types"; - -// - -const CACHE_SINGLE_KEY = Symbol(); -const CACHE_MULTIPLE_KEY = Symbol(); +import { scope as scopeCache } from "../path/cache"; /** * To avoid creating a new Scope instance for each traversal, we maintain a cache on the * node itself containing all scopes it has been associated with. - * - * We also optimise for the case of there being only a single scope associated with a node. */ function getCache(node, parentScope, self) { - let singleCache = node[CACHE_SINGLE_KEY]; - - if (singleCache) { - // we've only ever associated one scope with this node so let's check it - if (matchesParent(singleCache, parentScope)) { - return singleCache; - } - } else if (!node[CACHE_MULTIPLE_KEY]) { - // no scope has ever been associated with this node - node[CACHE_SINGLE_KEY] = self; - return; - } - - // looks like we have either a single scope association that was never matched or - // multiple assocations, let's find the right one! - return getCacheMultiple(node, parentScope, self, singleCache); -} + let scopes: Array = scopeCache.get(node) || []; +<<<<<<< HEAD function matchesParent(scope, parentScope) { if (scope.parent === parentScope) { return true; @@ -58,11 +37,17 @@ function getCacheMultiple(node, parentScope, self, singleCache) { } // loop through and check each scope to see if it matches our parent +======= +>>>>>>> Move scope cache to the cache module for (let scope of scopes) { - if (matchesParent(scope, parentScope)) return scope; + if (scope.parent === parentScope) return scope; } scopes.push(self); + + if (!scopeCache.has(node)) { + scopeCache.set(node, scopes); + } } // diff --git a/packages/babel-types/src/index.js b/packages/babel-types/src/index.js index 43aa56968a..6c559d87aa 100644 --- a/packages/babel-types/src/index.js +++ b/packages/babel-types/src/index.js @@ -405,8 +405,9 @@ export function inherits(child, parent) { t.inheritsComments(child, parent); - if (traverse.cache.has(parent)) { - traverse.cache.set(child, traverse.cache.get(parent)); + const pathCache = traverse.cache.path; + if (pathCache.has(parent)) { + pathCache.set(child, pathCache.get(parent)); } return child; }