TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Incorrect transpile for destructuring assignment of block-scoped loop variable in closure

Open T18970237136 opened this issue 10 years ago • 0 comments

Hi, it seems the fix for https://github.com/Microsoft/TypeScript/issues/6877 missed destructuring assignments.

TypeScript Version: master (f06423b)

Code Running the following code in tsc or in the playground:

(function() {
    "use strict";

    for (let i = 0; i < 4; i++) {
        (() => [i] = [i + 1])();
        console.log(i);
    }
})();

when targeting ES5 will be compiled to:

(function () {
    "use strict";
    var _loop_1 = function(i) {
        (function () { return (_a = [i + 1], i = _a[0], _a); var _a; })();
        console.log(i);
    };
    for (var i = 0; i < 4; i++) {
        _loop_1(i);
    }
})();

Actual behavior when running the compiled snipped:

1
2
3
4

Expected behavior (running the original snipped in Firefox or Chrome):

1
3

Expected compilation result:

(function () {
    "use strict";
    var _loop_1 = function(i) {
        (function () { return (_a = [i + 1], i = _a[0], _a); var _a; })();
        console.log(i);
        out_i_1 = i;
    };
    var out_i_1;
    for (var i = 0; i < 4; i++) {
        _loop_1(i);
        i = out_i_1;
    }
})();

Note that the same problem exists for object desctructuring.

Thanks!

T18970237136 avatar Mar 10 '16 07:03 T18970237136