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

Returning string literals #68

Closed sgodwin424 closed 8 years ago

sgodwin424 commented 8 years ago

The v3.3.0 update generates an error on the following code when running node bar:

bar.js

require('babel-core/register')();
require('./foo.js');

foo.js

function foo(): string
{
    return '';
}
C:\Users\Scott\Documents\GitHub\test\node_modules\babel-core\lib\transformation\file\index.js:540
      throw err;
      ^

SyntaxError: C:/Users/Scott/Documents/GitHub/test/foo.js: Function "foo" did not return a value, expected string
←[0m> 1 | ←[36mfunction←[39m foo←[94m←[1m(←[22m←[39m←[94m←[1m)←[22m←[39m←[1m:←[22m string
    | ^
  2 | ←[32m{←[39m
  3 |     ←[36mreturn←[39m ←[31m''←[39m←[1m;←[22m
  4 | ←[32m}←[39m←[0m
    at File.buildCodeFrameError (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-core\lib\transformation\file\index.js:407:15)
    at NodePath.buildCodeFrameError (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\path\index.js:148:26)
    at exit (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-plugin-typecheck\lib\index.js:160:22)
    at NodePath._call (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\path\context.js:74:18)
    at NodePath.call (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\path\context.js:46:17)
    at NodePath.visit (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\path\context.js:116:8)
    at TraversalContext.visitQueue (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\context.js:153:16)
    at TraversalContext.visitMultiple (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\context.js:108:17)
    at TraversalContext.visit (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\context.js:195:19)
    at Function.traverse.node (C:\Users\Scott\Documents\GitHub\test\node_modules\babel-traverse\lib\index.js:139:17)

If I just do babel foo.js --out-dir dist, I can run the resulting file node foo without any errors.

phpnode commented 8 years ago

@sgodwin424 does it only occur when returning an empty string?

sgodwin424 commented 8 years ago

It doesn't seem to matter what the string is, it will cause the error. However, the following examples won't:

function foo(): string
{
    let a = '';
    return a;
}
function foo(): number
{
    return 22;
}
phpnode commented 8 years ago

ok will try and fix it later. For now you can work around this by using a pragma:

// @fixme typecheck: ignore statement because of bug #68
function foo(): string {
    return '';
}
sgodwin424 commented 8 years ago

I found another scenario in which it occurs:

foo.js

class Foo
{
    constructor()
    {
        this.ret = '';
    }

    MyFunction(): string
    {
        return this.ret;
    }
}

This is a bit unrelated to string literals, since this will throw an error regardless of what property of this you are returning.

SyntaxError: C:/Users/Scott/Documents/GitHub/test/foo.js: Function did not return a value, expected string
←[0m   6 |     ←[32m}←[39m
   7 |
>  8 |     MyFunction←[94m←[1m(←[22m←[39m←[94m←[1m)←[22m←[39m←[1m:←[22m string
     |     ^
   9 |     ←[32m{←[39m
  10 |         ←[36mreturn←[39m ←[36mthis←[39m←[1m.←[22mretsdfgsdfg←[1m;←[22m
  11 |     ←[32m}←[39m←[0m

With same stacktrace.

EDIT:

Just to clarify, this only happens with v3.3.0. v3.2.1 does not cause this problem.

phpnode commented 8 years ago

I couldn't replicate this but I saw a possible way it could have happened and worked around it in 3.4.0. If it doesn't work, please let me know and I'll reopen this issue.

sgodwin424 commented 8 years ago

It solved the issue. Thanks.