Move scope cache to the cache module
This commit is contained in:
parent
3c148148bc
commit
bf91a68375
@ -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<Symbol> = Object.getOwnPropertySymbols(node);
|
||||
for (let sym of syms) {
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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<Scope> = 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);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user