Skip to content

Commit

Permalink
fix @babel/traverse can't use path.remove() with noScope
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Feb 13, 2020
1 parent 3c6a8ae commit bc87123
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/babel-traverse/src/path/removal.js
Expand Up @@ -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();
Expand Down
19 changes: 15 additions & 4 deletions packages/babel-traverse/test/removal.js
Expand Up @@ -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();
},
Expand All @@ -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]
Expand All @@ -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("");
});
});

0 comments on commit bc87123

Please sign in to comment.