TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

';' is missing after return statement for a non-simple converted loop

Open T18970237136 opened this issue 10 years ago • 2 comments

Hi,

when using current LKG typescriptService.js, when transpiling the following code to ES5:

(function(): number {
    for (let i = 0; i < 10; i++) {
        (() => i)();
        if (i == 5)
            return i;
        else if (i == 6)
            break;
    }
    return null;
})();

it will be transpiled to this JS:

(function () {
    var _loop_1 = function(i) {
        (function () { return i; })();
        if (i == 5)
            return { value: i };
        else if (i == 6)
            return "break";
    };
    for (var i = 0; i < 10; i++) {
        var state_1 = _loop_1(i);
        if (typeof state_1 === "object") return state_1.value
        if (state_1 === "break") break;
    }
    return null;
})();

Note that the semicolon (;) after return state_1.value is missing. ATM this doesn't cause a problem (with "if" or "}" in the next line), but I think the semicolon should be emitted to avoid issues when the next line contains e.g. something like +4;, as this would be interpreted as part of the return statement.

The cause is in emitter.ts, line 3165:

write(`return ${loopResult}.value`);

Thanks!

T18970237136 avatar Feb 02 '16 07:02 T18970237136

PRs are welcomed.

mhegazy avatar Feb 02 '16 07:02 mhegazy

Thanks @T18970237136!

DanielRosenwasser avatar Feb 02 '16 18:02 DanielRosenwasser