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

Handle side effects correctly in rest params index expressions (#4348) #4674

Merged
merged 1 commit into from Oct 5, 2016
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
23 changes: 19 additions & 4 deletions packages/babel-plugin-transform-es2015-parameters/src/rest.js
Expand Up @@ -17,6 +17,10 @@ let restIndex = template(`
ARGUMENTS.length <= INDEX ? undefined : ARGUMENTS[INDEX]
`);

let restIndexImpure = template(`
REF = INDEX, ARGUMENTS.length <= REF ? undefined : ARGUMENTS[REF]
`);

let restLength = template(`
ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET
`);
Expand Down Expand Up @@ -161,10 +165,21 @@ function optimiseIndexGetter(path, argsId, offset) {
index = t.binaryExpression("+", path.parent.property, t.numericLiteral(offset));
}

path.parentPath.replaceWith(restIndex({
ARGUMENTS: argsId,
INDEX: index,
}));
const { scope } = path;
if (!scope.isPure(index)) {
let temp = scope.generateUidIdentifierBasedOnNode(index);
scope.push({id: temp, kind: "var"});
path.parentPath.replaceWith(restIndexImpure({
ARGUMENTS: argsId,
INDEX: index,
REF: temp
}));
} else {
path.parentPath.replaceWith(restIndex({
ARGUMENTS: argsId,
INDEX: index,
}));
}
}

function optimiseLengthGetter(path, argsId, offset) {
Expand Down
@@ -0,0 +1,4 @@
function first(...values) {
var index = 0;
return values[index++];
}
@@ -0,0 +1,6 @@
function first() {
var _ref;

var index = 0;
return _ref = index++ + 0, arguments.length <= _ref ? undefined : arguments[_ref];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good? wondering where index++ + 0 is coming from (not part of this particular change)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the index of the rest parameter in the parameter list. If the function signature were e.g. function(a, b, ...values), it would be index++ + 2 instead.

Copy link
Member

@hzoo hzoo Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so we could make a change so that if it's 0 then don't add the + 0

#4692

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be a good idea.

}
@@ -1,6 +1,8 @@
var t = function (f) {
var _ref;

arguments.length <= 1 ? undefined : arguments[1];
arguments.length <= (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1 ? undefined : arguments[(arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1];
_ref = (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1, arguments.length <= _ref ? undefined : arguments[_ref];
};

function t(f) {
Expand All @@ -11,4 +13,4 @@ function t(f) {
items;
items[0];
items[items.length - 1];
}
}