index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.getTypeAnnotation = getTypeAnnotation;
  6. exports._getTypeAnnotation = _getTypeAnnotation;
  7. exports.isBaseType = isBaseType;
  8. exports.couldBeBaseType = couldBeBaseType;
  9. exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
  10. exports.isGenericType = isGenericType;
  11. var _inferers = require("./inferers");
  12. var inferers = _interopRequireWildcard(_inferers);
  13. var _babelTypes = require("babel-types");
  14. var t = _interopRequireWildcard(_babelTypes);
  15. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. function getTypeAnnotation() {
  18. if (this.typeAnnotation) return this.typeAnnotation;
  19. var type = this._getTypeAnnotation() || t.anyTypeAnnotation();
  20. if (t.isTypeAnnotation(type)) type = type.typeAnnotation;
  21. return this.typeAnnotation = type;
  22. }
  23. function _getTypeAnnotation() {
  24. var node = this.node;
  25. if (!node) {
  26. if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
  27. var declar = this.parentPath.parentPath;
  28. var declarParent = declar.parentPath;
  29. if (declar.key === "left" && declarParent.isForInStatement()) {
  30. return t.stringTypeAnnotation();
  31. }
  32. if (declar.key === "left" && declarParent.isForOfStatement()) {
  33. return t.anyTypeAnnotation();
  34. }
  35. return t.voidTypeAnnotation();
  36. } else {
  37. return;
  38. }
  39. }
  40. if (node.typeAnnotation) {
  41. return node.typeAnnotation;
  42. }
  43. var inferer = inferers[node.type];
  44. if (inferer) {
  45. return inferer.call(this, node);
  46. }
  47. inferer = inferers[this.parentPath.type];
  48. if (inferer && inferer.validParent) {
  49. return this.parentPath.getTypeAnnotation();
  50. }
  51. }
  52. function isBaseType(baseName, soft) {
  53. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  54. }
  55. function _isBaseType(baseName, type, soft) {
  56. if (baseName === "string") {
  57. return t.isStringTypeAnnotation(type);
  58. } else if (baseName === "number") {
  59. return t.isNumberTypeAnnotation(type);
  60. } else if (baseName === "boolean") {
  61. return t.isBooleanTypeAnnotation(type);
  62. } else if (baseName === "any") {
  63. return t.isAnyTypeAnnotation(type);
  64. } else if (baseName === "mixed") {
  65. return t.isMixedTypeAnnotation(type);
  66. } else if (baseName === "empty") {
  67. return t.isEmptyTypeAnnotation(type);
  68. } else if (baseName === "void") {
  69. return t.isVoidTypeAnnotation(type);
  70. } else {
  71. if (soft) {
  72. return false;
  73. } else {
  74. throw new Error("Unknown base type " + baseName);
  75. }
  76. }
  77. }
  78. function couldBeBaseType(name) {
  79. var type = this.getTypeAnnotation();
  80. if (t.isAnyTypeAnnotation(type)) return true;
  81. if (t.isUnionTypeAnnotation(type)) {
  82. for (var _iterator = type.types, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  83. var _ref;
  84. if (_isArray) {
  85. if (_i >= _iterator.length) break;
  86. _ref = _iterator[_i++];
  87. } else {
  88. _i = _iterator.next();
  89. if (_i.done) break;
  90. _ref = _i.value;
  91. }
  92. var type2 = _ref;
  93. if (t.isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. } else {
  99. return _isBaseType(name, type, true);
  100. }
  101. }
  102. function baseTypeStrictlyMatches(right) {
  103. var left = this.getTypeAnnotation();
  104. right = right.getTypeAnnotation();
  105. if (!t.isAnyTypeAnnotation(left) && t.isFlowBaseAnnotation(left)) {
  106. return right.type === left.type;
  107. }
  108. }
  109. function isGenericType(genericName) {
  110. var type = this.getTypeAnnotation();
  111. return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, { name: genericName });
  112. }