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

Post-increment in transpiled code is executed twice (T7432) #4348

Closed
babel-bot opened this issue Jun 15, 2016 · 1 comment
Closed

Post-increment in transpiled code is executed twice (T7432) #4348

babel-bot opened this issue Jun 15, 2016 · 1 comment
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects

Comments

@babel-bot
Copy link
Collaborator

Issue originally made by @mariusschulz

Bug information

  • Babel version: 6.7.7

Input code

function first(...values) {
    let index = 0;
    return values[index++];
}

console.log(first(1, 2));

Description

Here's the code that Babel emits for the above input:

"use strict";

function first() {
    var index = 0;
    return arguments.length <= index++ + 0 ? undefined : arguments[index++ + 0];
}

console.log(first(1, 2));

As you can see, index++ is executed twice, both within the condition of the conditional expression and within its second branch.

  • Actual: 2 is logged to the console
  • Expected: 1 is logged to the console

Emitting an additional local variable and a sequence expression would solve the problem:

"use strict";

function first() {
    var index = 0, _index;
    return _index = index++, arguments.length <= _index + 0 ? undefined : arguments[_index + 0];
}

console.log(first(1, 2));
@motiz88
Copy link
Contributor

motiz88 commented Oct 6, 2016

Closed via #4674.

@motiz88 motiz88 closed this as completed Oct 6, 2016
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
No open projects
@motiz88
Verified issues
Development

No branches or pull requests

2 participants