Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize removal-hooks for ArrowFunctions #5076

Merged
merged 1 commit into from Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/babel-traverse/package.json
Expand Up @@ -17,5 +17,8 @@
"globals": "^9.0.0",
"invariant": "^2.2.0",
"lodash": "^4.2.0"
},
"devDependencies": {
"babel-generator": "^6.21.0"
}
}
9 changes: 1 addition & 8 deletions packages/babel-traverse/src/path/lib/removal-hooks.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions 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");
});
});
});