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

No errors at a compilation time when type is wrong #164

Closed mhf-ir closed 8 years ago

mhf-ir commented 8 years ago

I have project with babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "plugins": [
    "typecheck",
    "syntax-flow",
    "transform-flow-strip-types"
  ]
}

This is my invalid file

// @flow

function bar(x : number): number {
  return x + x;
}

bar('a');
// Error will appear in ATOM :  Flow IDE  Warning string This type is incompatible with numberat line 7 col 5

But i compile it

sweb@sweb-laptop:~/www/start1$ ./node_modules/.bin/babel -d .dist ./src/ -s
src/index.js -> .dist/index.js

Compile without any error :cry:

What i missed ?

./.dist/index.js

'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

function bar(x) {
  function _ref(_id) {
    if (!(typeof _id === 'number')) {
      throw new TypeError('Function "bar" return value violates contract.\n\nExpected:\nnumber\n\nGot:\n' + _inspect(_id));
    }

    return _id;
  }

  if (!(typeof x === 'number')) {
    throw new TypeError('Value of argument "x" violates contract.\n\nExpected:\nnumber\n\nGot:\n' + _inspect(x));
  }

  return _ref(x + x);
}

bar('a');

function _inspect(input, depth) {
  var maxDepth = 4;
  var maxKeys = 15;

  if (depth === undefined) {
    depth = 0;
  }

  depth += 1;

  if (input === null) {
    return 'null';
  } else if (input === undefined) {
    return 'void';
  } else if (typeof input === 'string' || typeof input === 'number' || typeof input === 'boolean') {
    return typeof input === 'undefined' ? 'undefined' : _typeof(input);
  } else if (Array.isArray(input)) {
    if (input.length > 0) {
      var _ret = function () {
        if (depth > maxDepth) return {
            v: '[...]'
          };

        var first = _inspect(input[0], depth);

        if (input.every(function (item) {
          return _inspect(item, depth) === first;
        })) {
          return {
            v: first.trim() + '[]'
          };
        } else {
          return {
            v: '[' + input.slice(0, maxKeys).map(function (item) {
              return _inspect(item, depth);
            }).join(', ') + (input.length >= maxKeys ? ', ...' : '') + ']'
          };
        }
      }();

      if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
    } else {
      return 'Array';
    }
  } else {
    var keys = Object.keys(input);

    if (!keys.length) {
      if (input.constructor && input.constructor.name && input.constructor.name !== 'Object') {
        return input.constructor.name;
      } else {
        return 'Object';
      }
    }

    if (depth > maxDepth) return '{...}';
    var indent = '  '.repeat(depth - 1);
    var entries = keys.slice(0, maxKeys).map(function (key) {
      return (/^([A-Z_$][A-Z0-9_$]*)$/i.test(key) ? key : JSON.stringify(key)) + ': ' + _inspect(input[key], depth) + ';';
    }).join('\n  ' + indent);

    if (keys.length >= maxKeys) {
      entries += '\n  ' + indent + '...';
    }

    if (input.constructor && input.constructor.name && input.constructor.name !== 'Object') {
      return input.constructor.name + ' {\n  ' + indent + entries + '\n' + indent + '}';
    } else {
      return '{\n  ' + indent + entries + '\n' + indent + '}';
    }
  }
}
//# sourceMappingURL=index.js.map
gcanti commented 8 years ago

@mhf-ir type checks are executed at runtime

gajus commented 8 years ago

As @gcanti said, babel-plugin-typecheck provides runtime type checking. For static analyses, refer to https://flowtype.org/.