Skip to content

Unintuitive compiled output when using array destructuring to swap variables #7297

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

Closed
joshuacc opened this issue Feb 29, 2016 · 1 comment
Closed
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Revisit An issue worth coming back to

Comments

@joshuacc
Copy link

TypeScript Version:

1.8.2

Code

function whatever (idA, idB) {
    [idA, idB] = [idB, idA];

    return true; 
}

Expected behavior:

function whatever(idA, idB) {
    var _a;
    _a = [idB, idA], idA = _a[0], idB = _a[1];
    return true;
}

Actual behavior:

function whatever(idA, idB) {
    _a = [idB, idA], idA = _a[0], idB = _a[1];
    return true;
    var _a;
}

Thanks to variable hoisting, there is no functional problem here. However, I consider the compiled code unintuitive.

In addition, using Istanbul for code coverage highlights the var _a; declaration as not covered.

@DanielRosenwasser DanielRosenwasser added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Revisit An issue worth coming back to labels Feb 29, 2016
@DanielRosenwasser
Copy link
Member

Like you said, there's no problem here, and aggregating temporaries to emit at the end of a scope makes things a lot simpler during the emit phase. Perhaps when tree rewriting becomes our emit mechanism, this will be easier, so we can revisit this issue in the future. In the mean time, I'd consider filing a bug on Istanbul.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Revisit An issue worth coming back to
Projects
None yet
Development

No branches or pull requests

2 participants