UNPKG

babel-plugin-typecheck

Version:

Transforms flow type annotations into runtime type checks.

835 lines (691 loc) 284 kB
'use strict'; 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"); } }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (_ref59) { var t = _ref59.types; var template = _ref59.template; // constants used when statically verifying types var TYPE_INVALID = 0; var TYPE_VALID = 1; var TYPE_UNKNOWN = 2; /** * Binary Operators that can only produce boolean results. */ var BOOLEAN_BINARY_OPERATORS = ['==', '===', '>=', '<=', '>', '<', 'instanceof']; if (!(Array.isArray(BOOLEAN_BINARY_OPERATORS) && BOOLEAN_BINARY_OPERATORS.every(function (item) { return typeof item === 'string'; }))) { throw new TypeError('Value of variable "BOOLEAN_BINARY_OPERATORS" violates contract, expected string[] got ' + (BOOLEAN_BINARY_OPERATORS === null ? 'null' : (typeof BOOLEAN_BINARY_OPERATORS === 'undefined' ? 'undefined' : _typeof(BOOLEAN_BINARY_OPERATORS)) === 'object' && BOOLEAN_BINARY_OPERATORS.constructor ? BOOLEAN_BINARY_OPERATORS.constructor.name || '[Unknown Object]' : typeof BOOLEAN_BINARY_OPERATORS === 'undefined' ? 'undefined' : _typeof(BOOLEAN_BINARY_OPERATORS))); } var checks = createChecks(); var staticChecks = createStaticChecks(); var checkIsArray = expression('Array.isArray(input)'); if (!(typeof checkIsArray === 'function')) { throw new TypeError('Value of variable "checkIsArray" violates contract, expected () => Node got ' + (checkIsArray === null ? 'null' : (typeof checkIsArray === 'undefined' ? 'undefined' : _typeof(checkIsArray)) === 'object' && checkIsArray.constructor ? checkIsArray.constructor.name || '[Unknown Object]' : typeof checkIsArray === 'undefined' ? 'undefined' : _typeof(checkIsArray))); } var checkIsMap = expression('input instanceof Map'); if (!(typeof checkIsMap === 'function')) { throw new TypeError('Value of variable "checkIsMap" violates contract, expected () => Node got ' + (checkIsMap === null ? 'null' : (typeof checkIsMap === 'undefined' ? 'undefined' : _typeof(checkIsMap)) === 'object' && checkIsMap.constructor ? checkIsMap.constructor.name || '[Unknown Object]' : typeof checkIsMap === 'undefined' ? 'undefined' : _typeof(checkIsMap))); } var checkIsSet = expression('input instanceof Set'); if (!(typeof checkIsSet === 'function')) { throw new TypeError('Value of variable "checkIsSet" violates contract, expected () => Node got ' + (checkIsSet === null ? 'null' : (typeof checkIsSet === 'undefined' ? 'undefined' : _typeof(checkIsSet)) === 'object' && checkIsSet.constructor ? checkIsSet.constructor.name || '[Unknown Object]' : typeof checkIsSet === 'undefined' ? 'undefined' : _typeof(checkIsSet))); } var checkIsObject = expression('input != null && typeof input === \'object\''); if (!(typeof checkIsObject === 'function')) { throw new TypeError('Value of variable "checkIsObject" violates contract, expected () => Node got ' + (checkIsObject === null ? 'null' : (typeof checkIsObject === 'undefined' ? 'undefined' : _typeof(checkIsObject)) === 'object' && checkIsObject.constructor ? checkIsObject.constructor.name || '[Unknown Object]' : typeof checkIsObject === 'undefined' ? 'undefined' : _typeof(checkIsObject))); } var checkNotNull = expression('input != null'); if (!(typeof checkNotNull === 'function')) { throw new TypeError('Value of variable "checkNotNull" violates contract, expected () => Node got ' + (checkNotNull === null ? 'null' : (typeof checkNotNull === 'undefined' ? 'undefined' : _typeof(checkNotNull)) === 'object' && checkNotNull.constructor ? checkNotNull.constructor.name || '[Unknown Object]' : typeof checkNotNull === 'undefined' ? 'undefined' : _typeof(checkNotNull))); } var declareTypeChecker = template('\n const id = function id (input) {\n return check;\n };\n '); if (!(typeof declareTypeChecker === 'function')) { throw new TypeError('Value of variable "declareTypeChecker" violates contract, expected () => Node got ' + (declareTypeChecker === null ? 'null' : (typeof declareTypeChecker === 'undefined' ? 'undefined' : _typeof(declareTypeChecker)) === 'object' && declareTypeChecker.constructor ? declareTypeChecker.constructor.name || '[Unknown Object]' : typeof declareTypeChecker === 'undefined' ? 'undefined' : _typeof(declareTypeChecker))); } var guard = template('\n if (!check) {\n throw new TypeError(message);\n }\n '); if (!(typeof guard === 'function')) { throw new TypeError('Value of variable "guard" violates contract, expected () => Node got ' + (guard === null ? 'null' : (typeof guard === 'undefined' ? 'undefined' : _typeof(guard)) === 'object' && guard.constructor ? guard.constructor.name || '[Unknown Object]' : typeof guard === 'undefined' ? 'undefined' : _typeof(guard))); } var thrower = template('\n if (check) {\n ret;\n }\n else {\n throw new TypeError(message);\n }\n '); if (!(typeof thrower === 'function')) { throw new TypeError('Value of variable "thrower" violates contract, expected () => Node got ' + (thrower === null ? 'null' : (typeof thrower === 'undefined' ? 'undefined' : _typeof(thrower)) === 'object' && thrower.constructor ? thrower.constructor.name || '[Unknown Object]' : typeof thrower === 'undefined' ? 'undefined' : _typeof(thrower))); } var readableName = expression('\n input === null ? \'null\' : typeof input === \'object\' && input.constructor ? input.constructor.name || \'[Unknown Object]\' : typeof input\n '); if (!(typeof readableName === 'function')) { throw new TypeError('Value of variable "readableName" violates contract, expected () => Node got ' + (readableName === null ? 'null' : (typeof readableName === 'undefined' ? 'undefined' : _typeof(readableName)) === 'object' && readableName.constructor ? readableName.constructor.name || '[Unknown Object]' : typeof readableName === 'undefined' ? 'undefined' : _typeof(readableName))); } var checkMapKeys = expression('\n input instanceof Map && Array.from(input.keys()).every(key => keyCheck)\n '); if (!(typeof checkMapKeys === 'function')) { throw new TypeError('Value of variable "checkMapKeys" violates contract, expected () => Node got ' + (checkMapKeys === null ? 'null' : (typeof checkMapKeys === 'undefined' ? 'undefined' : _typeof(checkMapKeys)) === 'object' && checkMapKeys.constructor ? checkMapKeys.constructor.name || '[Unknown Object]' : typeof checkMapKeys === 'undefined' ? 'undefined' : _typeof(checkMapKeys))); } var checkMapValues = expression('\n input instanceof Map && Array.from(input.values()).every(value => valueCheck)\n '); if (!(typeof checkMapValues === 'function')) { throw new TypeError('Value of variable "checkMapValues" violates contract, expected () => Node got ' + (checkMapValues === null ? 'null' : (typeof checkMapValues === 'undefined' ? 'undefined' : _typeof(checkMapValues)) === 'object' && checkMapValues.constructor ? checkMapValues.constructor.name || '[Unknown Object]' : typeof checkMapValues === 'undefined' ? 'undefined' : _typeof(checkMapValues))); } var checkMapEntries = expression('\n input instanceof Map && Array.from(input).every(([key, value]) => keyCheck && valueCheck)\n '); if (!(typeof checkMapEntries === 'function')) { throw new TypeError('Value of variable "checkMapEntries" violates contract, expected () => Node got ' + (checkMapEntries === null ? 'null' : (typeof checkMapEntries === 'undefined' ? 'undefined' : _typeof(checkMapEntries)) === 'object' && checkMapEntries.constructor ? checkMapEntries.constructor.name || '[Unknown Object]' : typeof checkMapEntries === 'undefined' ? 'undefined' : _typeof(checkMapEntries))); } var checkSetEntries = expression('\n input instanceof Set && Array.from(input).every(value => valueCheck)\n '); if (!(typeof checkSetEntries === 'function')) { throw new TypeError('Value of variable "checkSetEntries" violates contract, expected () => Node got ' + (checkSetEntries === null ? 'null' : (typeof checkSetEntries === 'undefined' ? 'undefined' : _typeof(checkSetEntries)) === 'object' && checkSetEntries.constructor ? checkSetEntries.constructor.name || '[Unknown Object]' : typeof checkSetEntries === 'undefined' ? 'undefined' : _typeof(checkSetEntries))); } var visitors = { TypeAlias: function TypeAlias(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } path.replaceWith(createTypeAliasChecks(path)); }, InterfaceDeclaration: function InterfaceDeclaration(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } path.replaceWith(createInterfaceChecks(path)); }, ExportNamedDeclaration: function ExportNamedDeclaration(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var scope = path.scope; if (node.declaration && node.declaration.type === 'TypeAlias') { path.replaceWith(t.exportNamedDeclaration(createTypeAliasChecks(path.get('declaration')), [], null)); } }, ImportDeclaration: function ImportDeclaration(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } if (path.node.importKind !== 'type') { return; } var _path$get$map$reduce = path.get('specifiers').map(function (specifier) { var local = specifier.get('local'); var tmpId = path.scope.generateUidIdentifierBasedOnNode(local.node); var replacement = t.importSpecifier(tmpId, specifier.node.imported); var id = t.identifier(local.node.name); id.isTypeChecker = true; var declarator = t.variableDeclarator(id, tmpId); declarator.isTypeChecker = true; return [declarator, replacement]; }).reduce(function (_ref60, _ref61) { var _ref63 = _slicedToArray(_ref60, 2); var declarators = _ref63[0]; var specifiers = _ref63[1]; var _ref62 = _slicedToArray(_ref61, 2); var declarator = _ref62[0]; var specifier = _ref62[1]; declarators.push(declarator); specifiers.push(specifier); return [declarators, specifiers]; }, [[], []]); var _path$get$map$reduce2 = _slicedToArray(_path$get$map$reduce, 2); var declarators = _path$get$map$reduce2[0]; var specifiers = _path$get$map$reduce2[1]; var declaration = t.variableDeclaration('var', declarators); declaration.isTypeChecker = true; path.replaceWithMultiple([t.importDeclaration(specifiers, path.node.source), declaration]); }, Function: { enter: function enter(path) { var _node$body$body; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var scope = path.scope; var paramChecks = collectParamChecks(path); if (node.type === "ArrowFunctionExpression" && node.expression) { node.expression = false; node.body = t.blockStatement([t.returnStatement(node.body)]); } (_node$body$body = node.body.body).unshift.apply(_node$body$body, _toConsumableArray(paramChecks)); node.savedTypeAnnotation = node.returnType; node.returnCount = 0; }, exit: function exit(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var scope = path.scope; var isVoid = node.savedTypeAnnotation ? maybeNullableAnnotation(node.savedTypeAnnotation) : null; if (!node.returnCount && isVoid === false) { throw path.buildCodeFrameError('Function ' + (node.id ? '"' + node.id.name + '" ' : '') + 'did not return a value, expected ' + humanReadableType(node.savedTypeAnnotation, scope)); } } }, ReturnStatement: function ReturnStatement(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var parent = path.parent; var scope = path.scope; var fn = path.getFunctionParent(); if (!fn) { return; } fn.node.returnCount++; if (node.isTypeChecked) { return; } var returnType = fn.node.returnType; if (!returnType) { return; } if (!node.argument) { if (maybeNullableAnnotation(returnType) === false) { throw path.buildCodeFrameError('Function ' + (fn.node.id ? '"' + fn.node.id.name + '" ' : '') + 'did not return a value, expected ' + humanReadableType(returnType, path.scope)); } return; } var id = undefined; if (node.argument.type === 'Identifier') { id = node.argument; } else { id = scope.generateUidIdentifierBasedOnNode(node.argument); } var ok = staticCheckAnnotation(path.get("argument"), returnType); if (ok === true) { return; } else if (ok === false) { throw path.buildCodeFrameError('Invalid return type, expected ' + humanReadableType(returnType, scope)); } var check = checkAnnotation(id, returnType, scope); if (!check) { return; } if (parent.type !== 'BlockStatement' && parent.type !== 'Program') { var block = []; if (node.argument.type !== 'Identifier') { scope.push({ id: id }); block.push(t.expressionStatement(t.assignmentExpression('=', id, node.argument))); } var ret = t.returnStatement(id); ret.isTypeChecked = true; block.push(thrower({ check: check, ret: ret, message: returnTypeErrorMessage(path, fn.node, id) })); path.replaceWith(t.blockStatement(block)); } else { if (node.argument.type !== 'Identifier') { scope.push({ id: id }); path.insertBefore(t.expressionStatement(t.assignmentExpression('=', id, node.argument))); } var ret = t.returnStatement(id); ret.isTypeChecked = true; path.replaceWith(thrower({ check: check, ret: ret, message: returnTypeErrorMessage(path, fn.node, id) })); } }, VariableDeclaration: function VariableDeclaration(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var scope = path.scope; var collected = []; var declarations = path.get("declarations"); for (var i = 0; i < node.declarations.length; i++) { var declaration = node.declarations[i]; var _id = declaration.id; var init = declaration.init; if (!_id.typeAnnotation || _id.hasBeenTypeChecked) { continue; } _id.savedTypeAnnotation = _id.typeAnnotation; _id.hasBeenTypeChecked = true; var ok = staticCheckAnnotation(declarations[i], _id.typeAnnotation); if (ok === true) { continue; } else if (ok === false) { throw path.buildCodeFrameError('Invalid assignment value, expected ' + humanReadableType(_id.typeAnnotation, scope)); } var check = checkAnnotation(_id, _id.typeAnnotation, scope); if (check) { collected.push(guard({ check: check, message: varTypeErrorMessage(_id, scope) })); } } if (collected.length > 0) { var check = collected.reduce(function (check, branch) { branch.alternate = check; return branch; }); if (path.parent.type === 'Program' || path.parent.type === 'BlockStatement') { path.insertAfter(check); } else if (path.parent.type === 'ExportNamedDeclaration' || path.parent.type === 'ExportDefaultDeclaration' || path.parent.type === 'ExportAllDeclaration' || path.parentPath.isForXStatement()) { path.parentPath.insertAfter(check); } else { path.replaceWith(t.blockStatement([node, check])); } } }, AssignmentExpression: function AssignmentExpression(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var scope = path.scope; if (node.hasBeenTypeChecked || node.left.hasBeenTypeChecked || !t.isIdentifier(node.left)) { return; } var binding = scope.getBinding(node.left.name); if (!binding) { return; } else if (binding.path.type !== 'VariableDeclarator') { return; } var annotation = path.get('left').getTypeAnnotation(); if (annotation.type === 'AnyTypeAnnotation') { var item = binding.path.get('id'); annotation = item.node.savedTypeAnnotation || item.getTypeAnnotation(); } node.hasBeenTypeChecked = true; node.left.hasBeenTypeChecked = true; if (annotation.type === 'AnyTypeAnnotation') { annotation = getAnnotation(path.get('right')); if (!TypeAnnotation(annotation)) { throw new TypeError('Value of variable "annotation" violates contract, expected TypeAnnotation got ' + (annotation === null ? 'null' : (typeof annotation === 'undefined' ? 'undefined' : _typeof(annotation)) === 'object' && annotation.constructor ? annotation.constructor.name || '[Unknown Object]' : typeof annotation === 'undefined' ? 'undefined' : _typeof(annotation))); } if (allowsAny(annotation)) { return; } } var id = node.left; var right = path.get('right'); var ok = staticCheckAnnotation(right, annotation); if (ok === true) { return; } else if (ok === false) { throw path.buildCodeFrameError('Invalid assignment value, expected ' + humanReadableType(annotation, scope)); } var check = checkAnnotation(id, annotation, scope); if (!id.typeAnnotation) { id.typeAnnotation = annotation; } id.hasBeenTypeChecked = true; if (check) { path.getStatementParent().insertAfter(guard({ check: check, message: varTypeErrorMessage(id, scope) })); } }, TypeCastExpression: function TypeCastExpression(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } var node = path.node; var target = undefined; switch (node.expression.type) { case 'Identifier': target = node.expression; break; case 'AssignmentExpression': target = node.expression.left; break; default: // unsupported. return; } var id = path.scope.getBindingIdentifier(target.name); if (!id) { return; } id.savedTypeAnnotation = path.getTypeAnnotation(); } }; return { visitor: { Program: function Program(path) { if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } return path.traverse(visitors); } } }; function createChecks() { return { number: expression('typeof input === \'number\''), boolean: expression('typeof input === \'boolean\''), function: expression('typeof input === \'function\''), string: expression('typeof input === \'string\''), symbol: expression('typeof input === \'symbol\''), undefined: expression('input === undefined'), null: expression('input === null'), void: expression('input == null'), instanceof: expression('input instanceof type'), type: expression('type(input)'), mixed: function mixed() { return null; }, any: function any() { return null; }, union: checkUnion, intersection: checkIntersection, array: checkArray, map: checkMap, set: checkSet, tuple: checkTuple, object: checkObject, nullable: checkNullable, typeof: checkTypeof }; } function createStaticChecks() { return { string: function string(path) { var _maybeStringAnnotatio; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _maybeStringAnnotatio = maybeStringAnnotation(getAnnotation(path)); if (!(_maybeStringAnnotatio == null || typeof _maybeStringAnnotatio === 'boolean')) { throw new TypeError('Value of variable "_maybeStringAnnotatio" violates contract, expected ?bool got ' + (_maybeStringAnnotatio === null ? 'null' : (typeof _maybeStringAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio)) === 'object' && _maybeStringAnnotatio.constructor ? _maybeStringAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeStringAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio))); } if (_maybeStringAnnotatio == null || typeof _maybeStringAnnotatio === 'boolean') { return _maybeStringAnnotatio; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeStringAnnotatio === null ? 'null' : (typeof _maybeStringAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio)) === 'object' && _maybeStringAnnotatio.constructor ? _maybeStringAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeStringAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio))); } }, symbol: function symbol(path) { var _maybeSymbolAnnotatio; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _maybeSymbolAnnotatio = maybeSymbolAnnotation(getAnnotation(path)); if (!(_maybeSymbolAnnotatio == null || typeof _maybeSymbolAnnotatio === 'boolean')) { throw new TypeError('Value of variable "_maybeSymbolAnnotatio" violates contract, expected ?bool got ' + (_maybeSymbolAnnotatio === null ? 'null' : (typeof _maybeSymbolAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeSymbolAnnotatio)) === 'object' && _maybeSymbolAnnotatio.constructor ? _maybeSymbolAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeSymbolAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeSymbolAnnotatio))); } if (_maybeSymbolAnnotatio == null || typeof _maybeSymbolAnnotatio === 'boolean') { return _maybeSymbolAnnotatio; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeSymbolAnnotatio === null ? 'null' : (typeof _maybeSymbolAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeSymbolAnnotatio)) === 'object' && _maybeSymbolAnnotatio.constructor ? _maybeSymbolAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeSymbolAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeSymbolAnnotatio))); } }, number: function number(path) { var _maybeNumberAnnotatio; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _maybeNumberAnnotatio = maybeNumberAnnotation(getAnnotation(path)); if (!(_maybeNumberAnnotatio == null || typeof _maybeNumberAnnotatio === 'boolean')) { throw new TypeError('Value of variable "_maybeNumberAnnotatio" violates contract, expected ?bool got ' + (_maybeNumberAnnotatio === null ? 'null' : (typeof _maybeNumberAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio)) === 'object' && _maybeNumberAnnotatio.constructor ? _maybeNumberAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeNumberAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio))); } if (_maybeNumberAnnotatio == null || typeof _maybeNumberAnnotatio === 'boolean') { return _maybeNumberAnnotatio; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeNumberAnnotatio === null ? 'null' : (typeof _maybeNumberAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio)) === 'object' && _maybeNumberAnnotatio.constructor ? _maybeNumberAnnotatio.constructor.name || '[Unknown Object]' : typeof _maybeNumberAnnotatio === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio))); } }, boolean: function boolean(path) { var _maybeBooleanAnnotati; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _maybeBooleanAnnotati = maybeBooleanAnnotation(getAnnotation(path)); if (!(_maybeBooleanAnnotati == null || typeof _maybeBooleanAnnotati === 'boolean')) { throw new TypeError('Value of variable "_maybeBooleanAnnotati" violates contract, expected ?bool got ' + (_maybeBooleanAnnotati === null ? 'null' : (typeof _maybeBooleanAnnotati === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati)) === 'object' && _maybeBooleanAnnotati.constructor ? _maybeBooleanAnnotati.constructor.name || '[Unknown Object]' : typeof _maybeBooleanAnnotati === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati))); } if (_maybeBooleanAnnotati == null || typeof _maybeBooleanAnnotati === 'boolean') { return _maybeBooleanAnnotati; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeBooleanAnnotati === null ? 'null' : (typeof _maybeBooleanAnnotati === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati)) === 'object' && _maybeBooleanAnnotati.constructor ? _maybeBooleanAnnotati.constructor.name || '[Unknown Object]' : typeof _maybeBooleanAnnotati === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati))); } }, function: function _function(path) { var _maybeFunctionAnnotat; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _maybeFunctionAnnotat = maybeFunctionAnnotation(getAnnotation(path)); if (!(_maybeFunctionAnnotat == null || typeof _maybeFunctionAnnotat === 'boolean')) { throw new TypeError('Value of variable "_maybeFunctionAnnotat" violates contract, expected ?bool got ' + (_maybeFunctionAnnotat === null ? 'null' : (typeof _maybeFunctionAnnotat === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat)) === 'object' && _maybeFunctionAnnotat.constructor ? _maybeFunctionAnnotat.constructor.name || '[Unknown Object]' : typeof _maybeFunctionAnnotat === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat))); } if (_maybeFunctionAnnotat == null || typeof _maybeFunctionAnnotat === 'boolean') { return _maybeFunctionAnnotat; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeFunctionAnnotat === null ? 'null' : (typeof _maybeFunctionAnnotat === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat)) === 'object' && _maybeFunctionAnnotat.constructor ? _maybeFunctionAnnotat.constructor.name || '[Unknown Object]' : typeof _maybeFunctionAnnotat === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat))); } }, any: function any(path) { var _ref; if (!NodePath(path)) { throw new TypeError('Value of argument "path" violates contract, expected NodePath got ' + (path === null ? 'null' : (typeof path === 'undefined' ? 'undefined' : _typeof(path)) === 'object' && path.constructor ? path.constructor.name || '[Unknown Object]' : typeof path === 'undefined' ? 'undefined' : _typeof(path))); } _ref = null; if (!(_ref == null)) { throw new TypeError('Value of variable "_ref" violates contract, expected void got ' + (_ref === null ? 'null' : (typeof _ref === 'undefined' ? 'undefined' : _typeof(_ref)) === 'object' && _ref.constructor ? _ref.constructor.name || '[Unknown Object]' : typeof _ref === 'undefined' ? 'undefined' : _typeof(_ref))); } if (_ref == null || typeof _ref === 'boolean') { return _ref; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref === null ? 'null' : (typeof _ref === 'undefined' ? 'undefined' : _typeof(_ref)) === 'object' && _ref.constructor ? _ref.constructor.name || '[Unknown Object]' : typeof _ref === 'undefined' ? 'undefined' : _typeof(_ref))); } }, instanceof: function _instanceof(_ref64) { var _maybeInstanceOfAnnot; var path = _ref64.path; var type = _ref64.type; var node = path.node; var scope = path.scope; if (type.name === 'Object' && node.type === 'ObjectExpression' && !scope.hasBinding('Object')) { return true; } else if (type.name === 'Map' && !scope.hasBinding('Map')) { var _ref2; _ref2 = null; if (!(_ref2 == null)) { throw new TypeError('Value of variable "_ref2" violates contract, expected void got ' + (_ref2 === null ? 'null' : (typeof _ref2 === 'undefined' ? 'undefined' : _typeof(_ref2)) === 'object' && _ref2.constructor ? _ref2.constructor.name || '[Unknown Object]' : typeof _ref2 === 'undefined' ? 'undefined' : _typeof(_ref2))); } if (_ref2 == null || typeof _ref2 === 'boolean') { return _ref2; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref2 === null ? 'null' : (typeof _ref2 === 'undefined' ? 'undefined' : _typeof(_ref2)) === 'object' && _ref2.constructor ? _ref2.constructor.name || '[Unknown Object]' : typeof _ref2 === 'undefined' ? 'undefined' : _typeof(_ref2))); } } else if (type.name === 'Set' && !scope.hasBinding('Set')) { var _ref3; _ref3 = null; if (!(_ref3 == null)) { throw new TypeError('Value of variable "_ref3" violates contract, expected void got ' + (_ref3 === null ? 'null' : (typeof _ref3 === 'undefined' ? 'undefined' : _typeof(_ref3)) === 'object' && _ref3.constructor ? _ref3.constructor.name || '[Unknown Object]' : typeof _ref3 === 'undefined' ? 'undefined' : _typeof(_ref3))); } if (_ref3 == null || typeof _ref3 === 'boolean') { return _ref3; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref3 === null ? 'null' : (typeof _ref3 === 'undefined' ? 'undefined' : _typeof(_ref3)) === 'object' && _ref3.constructor ? _ref3.constructor.name || '[Unknown Object]' : typeof _ref3 === 'undefined' ? 'undefined' : _typeof(_ref3))); } } _maybeInstanceOfAnnot = maybeInstanceOfAnnotation(getAnnotation(path), type); if (!(_maybeInstanceOfAnnot == null || typeof _maybeInstanceOfAnnot === 'boolean')) { throw new TypeError('Value of variable "_maybeInstanceOfAnnot" violates contract, expected ?bool got ' + (_maybeInstanceOfAnnot === null ? 'null' : (typeof _maybeInstanceOfAnnot === 'undefined' ? 'undefined' : _typeof(_maybeInstanceOfAnnot)) === 'object' && _maybeInstanceOfAnnot.constructor ? _maybeInstanceOfAnnot.constructor.name || '[Unknown Object]' : typeof _maybeInstanceOfAnnot === 'undefined' ? 'undefined' : _typeof(_maybeInstanceOfAnnot))); } if (_maybeInstanceOfAnnot == null || typeof _maybeInstanceOfAnnot === 'boolean') { return _maybeInstanceOfAnnot; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_maybeInstanceOfAnnot === null ? 'null' : (typeof _maybeInstanceOfAnnot === 'undefined' ? 'undefined' : _typeof(_maybeInstanceOfAnnot)) === 'object' && _maybeInstanceOfAnnot.constructor ? _maybeInstanceOfAnnot.constructor.name || '[Unknown Object]' : typeof _maybeInstanceOfAnnot === 'undefined' ? 'undefined' : _typeof(_maybeInstanceOfAnnot))); } }, type: (function (_type) { function type(_x) { return _type.apply(this, arguments); } type.toString = function () { return _type.toString(); }; return type; })(function (_ref65) { var _ref4; var path = _ref65.path; var type = _ref65.type; _ref4 = null; if (!(_ref4 == null)) { throw new TypeError('Value of variable "_ref4" violates contract, expected void got ' + (_ref4 === null ? 'null' : (typeof _ref4 === 'undefined' ? 'undefined' : _typeof(_ref4)) === 'object' && _ref4.constructor ? _ref4.constructor.name || '[Unknown Object]' : typeof _ref4 === 'undefined' ? 'undefined' : _typeof(_ref4))); } if (_ref4 == null || typeof _ref4 === 'boolean') { return _ref4; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref4 === null ? 'null' : (typeof _ref4 === 'undefined' ? 'undefined' : _typeof(_ref4)) === 'object' && _ref4.constructor ? _ref4.constructor.name || '[Unknown Object]' : typeof _ref4 === 'undefined' ? 'undefined' : _typeof(_ref4))); } }), array: function array(_ref66) { var path = _ref66.path; var types = _ref66.types; var node = path.node; if (node.type === 'ArrayExpression') { if (types.length === 0) { return true; } else if (node.elements.length < types.length) { return false; } else { var _ref5; _ref5 = null; if (!(_ref5 == null)) { throw new TypeError('Value of variable "_ref5" violates contract, expected void got ' + (_ref5 === null ? 'null' : (typeof _ref5 === 'undefined' ? 'undefined' : _typeof(_ref5)) === 'object' && _ref5.constructor ? _ref5.constructor.name || '[Unknown Object]' : typeof _ref5 === 'undefined' ? 'undefined' : _typeof(_ref5))); } if (_ref5 == null || typeof _ref5 === 'boolean') { return _ref5; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref5 === null ? 'null' : (typeof _ref5 === 'undefined' ? 'undefined' : _typeof(_ref5)) === 'object' && _ref5.constructor ? _ref5.constructor.name || '[Unknown Object]' : typeof _ref5 === 'undefined' ? 'undefined' : _typeof(_ref5))); } } } else if (maybeArrayAnnotation(getAnnotation(path)) === false) { return false; } else { var _ref6; _ref6 = null; if (!(_ref6 == null)) { throw new TypeError('Value of variable "_ref6" violates contract, expected void got ' + (_ref6 === null ? 'null' : (typeof _ref6 === 'undefined' ? 'undefined' : _typeof(_ref6)) === 'object' && _ref6.constructor ? _ref6.constructor.name || '[Unknown Object]' : typeof _ref6 === 'undefined' ? 'undefined' : _typeof(_ref6))); } if (_ref6 == null || typeof _ref6 === 'boolean') { return _ref6; } else { throw new TypeError('Function return value violates contract, expected ?bool got ' + (_ref6 === null ? 'null' : (typeof _ref6 === 'undefined' ? 'undefined' : _typeof(_ref6)) === 'object' && _ref6.constructor ? _ref6.constructor.name || '[Unknown Object]' : typeof _ref6 === 'undefined' ? 'undefined' : _typeof(_ref6))); } } }, tuple: function tuple(_ref67) { var path = _ref67.path; var types = _ref67.types; var node = path.node; var annotation = getAnnotation(path); if (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation') { annotation = annotation.typeAnnotation; } return maybeTupleAnnotation(annotation); }, union: checkStaticUnion, intersection: checkStaticIntersection, object: checkStaticObject, nullable: checkStaticNullable }; } function compareAnnotations(a, b) { var _maybeStringAnnotatio2, _maybeNumberAnnotatio2, _maybeBooleanAnnotati2, _maybeFunctionAnnotat2, _ref7, _ref8, _compareObjectAnnotat, _compareArrayAnnotati, _compareGenericAnnota, _compareTupleAnnotati, _compareUnionAnnotati, _compareIntersectionA, _compareNullableAnnot, _ref9; if (!TypeAnnotation(a)) { throw new TypeError('Value of argument "a" violates contract, expected TypeAnnotation got ' + (a === null ? 'null' : (typeof a === 'undefined' ? 'undefined' : _typeof(a)) === 'object' && a.constructor ? a.constructor.name || '[Unknown Object]' : typeof a === 'undefined' ? 'undefined' : _typeof(a))); } if (!TypeAnnotation(b)) { throw new TypeError('Value of argument "b" violates contract, expected TypeAnnotation got ' + (b === null ? 'null' : (typeof b === 'undefined' ? 'undefined' : _typeof(b)) === 'object' && b.constructor ? b.constructor.name || '[Unknown Object]' : typeof b === 'undefined' ? 'undefined' : _typeof(b))); } if (a.type === 'TypeAnnotation') { a = a.typeAnnotation; } if (b.type === 'TypeAnnotation') { b = b.typeAnnotation; } switch (a.type) { case 'StringTypeAnnotation': case 'StringLiteralTypeAnnotation': { _maybeStringAnnotatio2 = maybeStringAnnotation(b); if (!(_maybeStringAnnotatio2 == null || typeof _maybeStringAnnotatio2 === 'boolean')) { throw new TypeError('Value of variable "_maybeStringAnnotatio2" violates contract, expected ?bool got ' + (_maybeStringAnnotatio2 === null ? 'null' : (typeof _maybeStringAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio2)) === 'object' && _maybeStringAnnotatio2.constructor ? _maybeStringAnnotatio2.constructor.name || '[Unknown Object]' : typeof _maybeStringAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio2))); } if (_maybeStringAnnotatio2 == null || typeof _maybeStringAnnotatio2 === 'boolean') { return _maybeStringAnnotatio2; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_maybeStringAnnotatio2 === null ? 'null' : (typeof _maybeStringAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio2)) === 'object' && _maybeStringAnnotatio2.constructor ? _maybeStringAnnotatio2.constructor.name || '[Unknown Object]' : typeof _maybeStringAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeStringAnnotatio2))); } } case 'NumberTypeAnnotation': case 'NumericLiteralTypeAnnotation': { _maybeNumberAnnotatio2 = maybeNumberAnnotation(b); if (!(_maybeNumberAnnotatio2 == null || typeof _maybeNumberAnnotatio2 === 'boolean')) { throw new TypeError('Value of variable "_maybeNumberAnnotatio2" violates contract, expected ?bool got ' + (_maybeNumberAnnotatio2 === null ? 'null' : (typeof _maybeNumberAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio2)) === 'object' && _maybeNumberAnnotatio2.constructor ? _maybeNumberAnnotatio2.constructor.name || '[Unknown Object]' : typeof _maybeNumberAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio2))); } if (_maybeNumberAnnotatio2 == null || typeof _maybeNumberAnnotatio2 === 'boolean') { return _maybeNumberAnnotatio2; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_maybeNumberAnnotatio2 === null ? 'null' : (typeof _maybeNumberAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio2)) === 'object' && _maybeNumberAnnotatio2.constructor ? _maybeNumberAnnotatio2.constructor.name || '[Unknown Object]' : typeof _maybeNumberAnnotatio2 === 'undefined' ? 'undefined' : _typeof(_maybeNumberAnnotatio2))); } } case 'BooleanTypeAnnotation': case 'BooleanLiteralTypeAnnotation': { _maybeBooleanAnnotati2 = maybeBooleanAnnotation(b); if (!(_maybeBooleanAnnotati2 == null || typeof _maybeBooleanAnnotati2 === 'boolean')) { throw new TypeError('Value of variable "_maybeBooleanAnnotati2" violates contract, expected ?bool got ' + (_maybeBooleanAnnotati2 === null ? 'null' : (typeof _maybeBooleanAnnotati2 === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati2)) === 'object' && _maybeBooleanAnnotati2.constructor ? _maybeBooleanAnnotati2.constructor.name || '[Unknown Object]' : typeof _maybeBooleanAnnotati2 === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati2))); } if (_maybeBooleanAnnotati2 == null || typeof _maybeBooleanAnnotati2 === 'boolean') { return _maybeBooleanAnnotati2; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_maybeBooleanAnnotati2 === null ? 'null' : (typeof _maybeBooleanAnnotati2 === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati2)) === 'object' && _maybeBooleanAnnotati2.constructor ? _maybeBooleanAnnotati2.constructor.name || '[Unknown Object]' : typeof _maybeBooleanAnnotati2 === 'undefined' ? 'undefined' : _typeof(_maybeBooleanAnnotati2))); } } case 'FunctionTypeAnnotation': { _maybeFunctionAnnotat2 = maybeFunctionAnnotation(b); if (!(_maybeFunctionAnnotat2 == null || typeof _maybeFunctionAnnotat2 === 'boolean')) { throw new TypeError('Value of variable "_maybeFunctionAnnotat2" violates contract, expected ?bool got ' + (_maybeFunctionAnnotat2 === null ? 'null' : (typeof _maybeFunctionAnnotat2 === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat2)) === 'object' && _maybeFunctionAnnotat2.constructor ? _maybeFunctionAnnotat2.constructor.name || '[Unknown Object]' : typeof _maybeFunctionAnnotat2 === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat2))); } if (_maybeFunctionAnnotat2 == null || typeof _maybeFunctionAnnotat2 === 'boolean') { return _maybeFunctionAnnotat2; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_maybeFunctionAnnotat2 === null ? 'null' : (typeof _maybeFunctionAnnotat2 === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat2)) === 'object' && _maybeFunctionAnnotat2.constructor ? _maybeFunctionAnnotat2.constructor.name || '[Unknown Object]' : typeof _maybeFunctionAnnotat2 === 'undefined' ? 'undefined' : _typeof(_maybeFunctionAnnotat2))); } } case 'AnyTypeAnnotation': { _ref7 = null; if (!(_ref7 == null)) { throw new TypeError('Value of variable "_ref7" violates contract, expected void got ' + (_ref7 === null ? 'null' : (typeof _ref7 === 'undefined' ? 'undefined' : _typeof(_ref7)) === 'object' && _ref7.constructor ? _ref7.constructor.name || '[Unknown Object]' : typeof _ref7 === 'undefined' ? 'undefined' : _typeof(_ref7))); } if (_ref7 == null || typeof _ref7 === 'boolean') { return _ref7; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_ref7 === null ? 'null' : (typeof _ref7 === 'undefined' ? 'undefined' : _typeof(_ref7)) === 'object' && _ref7.constructor ? _ref7.constructor.name || '[Unknown Object]' : typeof _ref7 === 'undefined' ? 'undefined' : _typeof(_ref7))); } } case 'MixedTypeAnnotation': { _ref8 = null; if (!(_ref8 == null)) { throw new TypeError('Value of variable "_ref8" violates contract, expected void got ' + (_ref8 === null ? 'null' : (typeof _ref8 === 'undefined' ? 'undefined' : _typeof(_ref8)) === 'object' && _ref8.constructor ? _ref8.constructor.name || '[Unknown Object]' : typeof _ref8 === 'undefined' ? 'undefined' : _typeof(_ref8))); } if (_ref8 == null || typeof _ref8 === 'boolean') { return _ref8; } else { throw new TypeError('Function "compareAnnotations" return value violates contract, expected ?bool got ' + (_ref8 === null ? 'null' : (typeof _ref8 === 'undefined' ? 'undefined' : _typeof(_ref8)) === 'object' && _ref8.constructor ? _ref8.constructor.name || '[Unknown Object]' : typeof _ref8 === 'undefined' ? 'undefined' : _typeof(_ref8))); } } case 'ObjectTypeAnnotation': { _compareObjectAnnotat = compareObjectAnnotation(a, b); if (!(_compareObjectAnnotat == null || typeof _compareObjectAnnotat === 'boolean')) { throw new TypeError('Value of variable "_compareObjectAnnotat" violates contract, expected ?bool got ' + (_compareObjectAnnotat === null ? 'null' : (typeof _compareObjectAnnotat === 'undefined' ? 'undefined'