diff --git a/packages/babel-traverse/package.json b/packages/babel-traverse/package.json index f88baac088..cca9351535 100644 --- a/packages/babel-traverse/package.json +++ b/packages/babel-traverse/package.json @@ -17,5 +17,8 @@ "globals": "^9.0.0", "invariant": "^2.2.0", "lodash": "^4.2.0" + }, + "devDependencies": { + "babel-generator": "^6.21.0" } } diff --git a/packages/babel-traverse/src/path/lib/removal-hooks.js b/packages/babel-traverse/src/path/lib/removal-hooks.js index e016c712f3..07cc98ab46 100644 --- a/packages/babel-traverse/src/path/lib/removal-hooks.js +++ b/packages/babel-traverse/src/path/lib/removal-hooks.js @@ -5,13 +5,6 @@ */ export let hooks = [ - function (self, parent) { - if (self.key === "body" && parent.isArrowFunctionExpression()) { - self.replaceWith(self.scope.buildUndefinedNode()); - return true; - } - }, - function (self, parent) { let removeParent = false; @@ -69,7 +62,7 @@ export let hooks = [ function (self, parent) { if ( (parent.isIfStatement() && (self.key === "consequent" || self.key === "alternate")) || - (parent.isLoop() && self.key === "body") + (self.key === "body" && (parent.isLoop() || parent.isArrowFunctionExpression())) ) { self.replaceWith({ type: "BlockStatement", diff --git a/packages/babel-traverse/test/removal.js b/packages/babel-traverse/test/removal.js new file mode 100644 index 0000000000..d09091db0e --- /dev/null +++ b/packages/babel-traverse/test/removal.js @@ -0,0 +1,34 @@ +import traverse from "../lib"; +import assert from "assert"; +import { parse } from "babylon"; +import generate from "babel-generator"; + +function getPath(code) { + const ast = parse(code); + let path; + traverse(ast, { + Program: function (_path) { + path = _path; + _path.stop(); + } + }); + + return path; +} + +function generateCode(path) { + return generate(path.node).code; +} + +describe("removal", function () { + describe("ArrowFunction", function () { + it("remove body", function () { + const rootPath = getPath("x = () => b;"); + const path = rootPath.get("body")[0].get("expression").get("right"); + const body = path.get("body"); + body.remove(); + + assert.equal(generateCode(rootPath), "x = () => {};", "body should be replaced with BlockStatement"); + }); + }); +});