diff --git a/packages/babel-traverse/src/cache.js b/packages/babel-traverse/src/cache.js index 08a771a5fc..d1a3c265d4 100644 --- a/packages/babel-traverse/src/cache.js +++ b/packages/babel-traverse/src/cache.js @@ -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; } diff --git a/packages/babel-traverse/src/index.js b/packages/babel-traverse/src/index.js index 6c1ac70650..6a3d4660a4 100644 --- a/packages/babel-traverse/src/index.js +++ b/packages/babel-traverse/src/index.js @@ -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)); diff --git a/packages/babel-traverse/test/traverse.js b/packages/babel-traverse/test/traverse.js index f8446a11ae..cee7157150 100644 --- a/packages/babel-traverse/test/traverse.js +++ b/packages/babel-traverse/test/traverse.js @@ -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]); + }); + }); });