From bc87123af9ce9cfb14a886fdb6c8b0916af1668d Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 13 Feb 2020 10:06:22 +0800 Subject: [PATCH] fix @babel/traverse can't use path.remove() with noScope --- packages/babel-traverse/src/path/removal.js | 4 +++- packages/babel-traverse/test/removal.js | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/babel-traverse/src/path/removal.js b/packages/babel-traverse/src/path/removal.js index 9ddf0a201448..83e792e9be62 100644 --- a/packages/babel-traverse/src/path/removal.js +++ b/packages/babel-traverse/src/path/removal.js @@ -7,7 +7,9 @@ export function remove() { this._assertUnremoved(); this.resync(); - this._removeFromScope(); + if (this.opts && !this.opts.noScope) { + this._removeFromScope(); + } if (this._callRemovalHooks()) { this._markRemoved(); diff --git a/packages/babel-traverse/test/removal.js b/packages/babel-traverse/test/removal.js index f500eb37ddd0..8ed424e374ed 100644 --- a/packages/babel-traverse/test/removal.js +++ b/packages/babel-traverse/test/removal.js @@ -6,7 +6,7 @@ function getPath(code) { const ast = parse(code); let path; traverse(ast, { - Program: function(_path) { + Program: function (_path) { path = _path; _path.stop(); }, @@ -19,9 +19,9 @@ function generateCode(path) { return generate(path.node).code; } -describe("removal", function() { - describe("ArrowFunction", function() { - it("remove body", function() { +describe("removal", function () { + describe("ArrowFunction", function () { + it("remove body", function () { const rootPath = getPath("x = () => b;"); const path = rootPath .get("body")[0] @@ -33,4 +33,15 @@ describe("removal", function() { expect(generateCode(rootPath)).toBe("x = () => {};"); }); }); + + it("remove with noScope", function () { + const ast = parse("a=1"); + traverse(ast, { + AssignmentExpression: function (path) { + path.remove(); + }, + }); + + expect(generate(ast).code).toBe(""); + }); });