codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Broken code emitted for conditional return statements #30

Closed motiz88 closed 9 years ago

motiz88 commented 9 years ago

With 1.3.0 and the latest Babel, I encountered this severely broken behavior where some of my functions were returning undefined instead of the expected value, but only with typecheck enabled. I've tracked it down to the following minimal case.

The source

function a(): number
{
    if (true)
        return [].length;
}

is transpiled to

function a() {
    var _length; // Never initialized!

    if (true) return _length;
}

I'm working on a PR with at least a test, and at best a fix if I can manage it.

phpnode commented 9 years ago

oh dear that's horribly nasty, thanks for the report, let me know if you struggle with the PR and I'll take a look

motiz88 commented 9 years ago

My results so far: createReferenceTo() creates the "missing" assignment expression seemingly correctly, but for some reason (which I haven't pinned down) it gets lost inside Babel, in the call to traverser.insertBefore().

This is as far as I am able to debug this issue right now. Hope it helps.

phpnode commented 9 years ago

@motiz88 thanks that's a big help, I'll take a look tonight.