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

Bad check #86

Closed Riim closed 8 years ago

Riim commented 8 years ago
this._position = [].concat(_toConsumableArray(coordinates), [0]);

if (!(Array.isArray(this._position) && this._position.length >= 2 && this._position[1] === 0)) {
    throw new TypeError('Value of "this._position" violates contract, expected Array<any, 0> got ' + (this._position === null ? 'null' : _typeof(this._position) === 'object' && this._position.constructor ? this._position.constructor.name || '[Unknown Object]' : _typeof(this._position)));
}

_toConsumableArray returns an array of any length and check impassable.

phpnode commented 8 years ago

@Riim do you have any type annotations for the properties / variables here? It would be really helpful if you could create a failing test case (just copy one of the existing ones) or provide a bit more context (I need to figure out why it thinks that this.position's type is Array<any, 0>). Thanks

Riim commented 8 years ago

Added to the project in which there are no annotations. Unfortunately much code I can't show you, I hope this will be enough:

module.exports = Layer.extend({
    options: {
        /*
        text: (String) optional
        */
        sprite: new Sprite(),
        font: 'sans-serif',
        fontSize: 10,
        fontColor: '#000000',
        interactive: true,
        sizeThresholds: [20, 21, 22]
    },

    initialize(coordinates, options) {
        Util.setOptions(this, options);

        this._sprite = this.options.sprite;

        // Будет создан при добавлении на карту
        this._object = null;

        // Состояние маркера, которое определяет его внешний вид:
        this._position = [...coordinates, 0];

Output:

var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })();

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

module.exports = Layer.extend({
    options: {
        /*
        text: (String) optional
        */
        sprite: new Sprite(),
        font: 'sans-serif',
        fontSize: 10,
        fontColor: '#000000',
        interactive: true,
        sizeThresholds: [20, 21, 22]
    },

    initialize: function initialize(coordinates, options) {
        Util.setOptions(this, options);

        this._sprite = this.options.sprite;

        // Будет создан при добавлении на карту
        this._object = null;

        // Состояние маркера, которое определяет его внешний вид:
        if (!(this._object == null)) {
            throw new TypeError('Value of "this._object" violates contract, expected void got ' + (this._object === null ? 'null' : _typeof(this._object) === 'object' && this._object.constructor ? this._object.constructor.name || '[Unknown Object]' : _typeof(this._object)));
        }

        this._position = [].concat(_toConsumableArray(coordinates), [0]);

        if (!(Array.isArray(this._position) && this._position.length >= 2 && this._position[1] === 0)) {
            throw new TypeError('Value of "this._position" violates contract, expected Array<any, 0> got ' + (this._position === null ? 'null' : _typeof(this._position) === 'object' && this._position.constructor ? this._position.constructor.name || '[Unknown Object]' : _typeof(this._position)));
        }
phpnode commented 8 years ago

Should be enough, will take a look later today, thanks!