Skip to content

Incorrect code generated for `for` loop when not in a block

$ bin/buble -v
Bublé version 0.13.1
$ cat for.js 

    var a = [7, 8, 9];
    if (0)
        for (let i = 0; i < a.length; ++i)
            (() => { console.log(a[i]); })();
    console.log('done');
$ node for.js 
done
$ bin/buble for.js 

    var a = [7, 8, 9];
    if (0)
        var loop = function ( i ) {
                (function () { console.log(a[i]); })();
            };

            for (var i = 0; i < a.length; ++i)
            loop( i );
    console.log('done');
$ bin/buble for.js | node
[stdin]:9
            loop( i );
            ^

TypeError: loop is not a function

The test case above happens to use an if statement, but the problem would occur in any blockless else, while, do/while, for, for in and for of. try/catch/finally is not an issue because the ES grammar requires blocks. switch also appears to be immune to this issue. This problem would also be applicable to blockless with statements, but buble sets strict mode by default and doesn't allow that construct.

There may be other transforms with similar block issues.