Add clearCache and clearPath as separate APIs under traverse (#4835)

This commit is contained in:
Boopathi Rajaa 2016-11-15 16:48:59 +01:00 committed by Henry Zhu
parent d0c884b9d4
commit dbc1371ba9
3 changed files with 79 additions and 55 deletions

View File

@ -2,6 +2,14 @@ export let path = new WeakMap();
export let scope = new WeakMap();
export function clear() {
path = new WeakMap();
scope = new WeakMap();
clearPath();
clearScope();
}
export function clearPath() {
path = new WeakMap;
}
export function clearScope() {
scope = new WeakMap;
}

View File

@ -98,6 +98,9 @@ traverse.clearCache = function() {
cache.clear();
};
traverse.clearCache.clearPath = cache.clearPath;
traverse.clearCache.clearScope = cache.clearScope;
traverse.copyCache = function(source, destination) {
if (cache.path.has(source)) {
cache.path.set(destination, cache.path.get(source));

View File

@ -1,63 +1,23 @@
let traverse = require("../lib").default;
let assert = require("assert");
let _ = require("lodash");
let parse = require("babylon").parse;
describe("traverse", function () {
let ast = {
type: "Program",
body: [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "foo",
},
"init": {
"type": "StringLiteral",
"value": "bar",
"raw": "\'bar\'"
}
}
],
"kind": "var"
},
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "ThisExpression"
},
"property": {
"type": "Identifier",
"name": "test"
}
},
"right": {
"type": "StringLiteral",
"value": "wow",
"raw": "\'wow\'"
}
}
}
]
};
let body = ast.body;
let code = `
var foo = "bar";
this.test = "wow";
`;
let ast = parse(code);
let program = ast.program;
let body = program.body;
it("traverse replace", function () {
let replacement = {
type: "StringLiteral",
value: "foo"
};
let ast2 = _.cloneDeep(ast);
let ast2 = _.cloneDeep(program);
traverse(ast2, {
enter: function (path) {
@ -76,7 +36,7 @@ describe("traverse", function () {
let actual = [];
traverse(ast, {
traverse(program, {
enter: function (path) {
actual.push(path.node);
}
@ -101,7 +61,7 @@ describe("traverse", function () {
let actual = [];
traverse(ast, {
traverse(program, {
blacklist: ["MemberExpression"],
enter: function (path) {
actual.push(path.node);
@ -126,17 +86,46 @@ describe("traverse", function () {
it("clearCache", function () {
let paths = [];
let scopes = [];
traverse(ast, {
enter: function (path) {
enter(path) {
scopes.push(path.scope);
paths.push(path);
path.stop();
}
});
traverse.clearCache();
let paths2 = [];
let scopes2 = [];
traverse(ast, {
enter: function (path) {
enter(path) {
scopes2.push(path.scope);
paths2.push(path);
path.stop();
}
});
scopes2.forEach(function (_, i) {
assert.notStrictEqual(scopes[i], scopes2[i]);
assert.notStrictEqual(paths[i], paths2[i]);
});
});
it("clearPath", function () {
let paths = [];
traverse(ast, {
enter(path) {
paths.push(path);
}
});
traverse.clearCache.clearPath();
let paths2 = [];
traverse(ast, {
enter(path) {
paths2.push(path);
}
});
@ -145,4 +134,28 @@ describe("traverse", function () {
assert.notStrictEqual(p, paths[i]);
});
});
it("clearScope", function () {
let scopes = [];
traverse(ast, {
enter(path) {
scopes.push(path.scope);
path.stop();
}
});
traverse.clearCache.clearScope();
let scopes2 = [];
traverse(ast, {
enter(path) {
scopes2.push(path.scope);
path.stop();
}
});
scopes2.forEach(function (p, i) {
assert.notStrictEqual(p, scopes[i]);
});
});
});