Skip to content

Commit

Permalink
Optimize simple function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoarrais committed May 9, 2019
1 parent f50bd7a commit 63b7c04
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 18 deletions.
Expand Up @@ -6,42 +6,48 @@ import { types as t } from "@babel/core";
// (a = b, a + e)
const buildOptimizedSequenceExpression = ({ assign, call, path }) => {
const { left: placeholderNode, right: pipelineLeft } = assign;
const { callee: calledExpression } = call;
const { callee: pipelineRight } = call;

let optimizeArrow =
t.isArrowFunctionExpression(calledExpression) &&
t.isExpression(calledExpression.body) &&
!calledExpression.async &&
!calledExpression.generator;
t.isArrowFunctionExpression(pipelineRight) &&
t.isExpression(pipelineRight.body) &&
!pipelineRight.async &&
!pipelineRight.generator;
let param;

if (optimizeArrow) {
const { params } = calledExpression;
const { params } = pipelineRight;
if (params.length === 1 && t.isIdentifier(params[0])) {
param = params[0];
} else if (params.length > 0) {
optimizeArrow = false;
}
} else if (t.isIdentifier(calledExpression, { name: "eval" })) {
} else if (t.isIdentifier(pipelineRight, { name: "eval" })) {
const evalSequence = t.sequenceExpression([
t.numericLiteral(0),
calledExpression,
pipelineRight,
]);

call.callee = evalSequence;

return t.sequenceExpression([assign, call]);
} else if (
(t.isIdentifier(pipelineRight) &&
path.scope.hasBinding(pipelineRight.name)) ||
t.isImmutable(pipelineLeft)
) {
return t.callExpression(pipelineRight, [pipelineLeft]);
}

if (optimizeArrow && !param) {
// Arrow function with 0 arguments
return t.sequenceExpression([pipelineLeft, calledExpression.body]);
return t.sequenceExpression([pipelineLeft, pipelineRight.body]);
}

if (param) {
path.get("right").scope.rename(param.name, placeholderNode.name);

return t.sequenceExpression([assign, calledExpression.body]);
return t.sequenceExpression([assign, pipelineRight.body]);
}

return t.sequenceExpression([assign, call]);
Expand Down
Expand Up @@ -10,6 +10,6 @@ var double = x => x * 2;
var result2 = [4, 9].map(x => {
var _ref4, _x;

return _ref4 = (_x = x, inc(_x)), double(_ref4);
return double(inc(x));
});
expect(result2).toEqual([10, 20]);
Expand Up @@ -2,4 +2,4 @@ var _;

var inc = x => x + 1;

expect((_ = 10, inc(_))).toBe(11);
expect(inc(10)).toBe(11);
Expand Up @@ -10,6 +10,6 @@ var double = x => x * 2;
var result2 = [4, 9].map(x => {
var _ref4, _x;

return _ref4 = (_x = x, inc(_x)), double(_ref4);
return double(inc(x));
});
expect(result2).toEqual([10, 20]);
Expand Up @@ -6,7 +6,7 @@ function then(fn) {
};
}

var result = (_ref = (_ = 1, (async x => (await x) + 1)(_)), then(x => x + 1)(_ref));
var result = (_ref = (async x => (await x) + 1)(1), then(x => x + 1)(_ref));
result.then(val => {
expect(val).toBe(3);
});
Expand Up @@ -2,4 +2,4 @@ var _;

var inc = x => x + 1;

expect((_ = 10, inc(_))).toBe(11);
expect(inc(10)).toBe(11);
Expand Up @@ -4,4 +4,4 @@ var inc = x => x + 1;

var double = x => x * 2;

expect((_ref = (_ = 10, inc(_)), double(_ref))).toBe(22);
expect(double(inc(10))).toBe(22);
Expand Up @@ -2,12 +2,12 @@ var _ref, _ref2, _;

var inc = x => x + 1;

var result = (_ref = 4 || 9, inc(_ref));
var result = inc(4 || 9);
expect(result).toBe(5);

var f = x => x + 10;

var h = x => x + 20;

var result2 = (_ref2 = (_ = 10, (f || h)(_)), inc(_ref2));
var result2 = inc((f || h)(10));
expect(result2).toBe(21);

0 comments on commit 63b7c04

Please sign in to comment.