index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  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 = _interopRequireWildcard(require("./inferers"));
  12. var t = _interopRequireWildcard(require("@babel/types"));
  13. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  14. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  15. function getTypeAnnotation() {
  16. if (this.typeAnnotation) return this.typeAnnotation;
  17. let type = this._getTypeAnnotation() || t.anyTypeAnnotation();
  18. if (t.isTypeAnnotation(type)) type = type.typeAnnotation;
  19. return this.typeAnnotation = type;
  20. }
  21. function _getTypeAnnotation() {
  22. var _inferer;
  23. const node = this.node;
  24. if (!node) {
  25. if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
  26. const declar = this.parentPath.parentPath;
  27. const declarParent = declar.parentPath;
  28. if (declar.key === "left" && declarParent.isForInStatement()) {
  29. return t.stringTypeAnnotation();
  30. }
  31. if (declar.key === "left" && declarParent.isForOfStatement()) {
  32. return t.anyTypeAnnotation();
  33. }
  34. return t.voidTypeAnnotation();
  35. } else {
  36. return;
  37. }
  38. }
  39. if (node.typeAnnotation) {
  40. return node.typeAnnotation;
  41. }
  42. let inferer = inferers[node.type];
  43. if (inferer) {
  44. return inferer.call(this, node);
  45. }
  46. inferer = inferers[this.parentPath.type];
  47. if ((_inferer = inferer) == null ? void 0 : _inferer.validParent) {
  48. return this.parentPath.getTypeAnnotation();
  49. }
  50. }
  51. function isBaseType(baseName, soft) {
  52. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  53. }
  54. function _isBaseType(baseName, type, soft) {
  55. if (baseName === "string") {
  56. return t.isStringTypeAnnotation(type);
  57. } else if (baseName === "number") {
  58. return t.isNumberTypeAnnotation(type);
  59. } else if (baseName === "boolean") {
  60. return t.isBooleanTypeAnnotation(type);
  61. } else if (baseName === "any") {
  62. return t.isAnyTypeAnnotation(type);
  63. } else if (baseName === "mixed") {
  64. return t.isMixedTypeAnnotation(type);
  65. } else if (baseName === "empty") {
  66. return t.isEmptyTypeAnnotation(type);
  67. } else if (baseName === "void") {
  68. return t.isVoidTypeAnnotation(type);
  69. } else {
  70. if (soft) {
  71. return false;
  72. } else {
  73. throw new Error(`Unknown base type ${baseName}`);
  74. }
  75. }
  76. }
  77. function couldBeBaseType(name) {
  78. const type = this.getTypeAnnotation();
  79. if (t.isAnyTypeAnnotation(type)) return true;
  80. if (t.isUnionTypeAnnotation(type)) {
  81. for (const type2 of type.types) {
  82. if (t.isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. } else {
  88. return _isBaseType(name, type, true);
  89. }
  90. }
  91. function baseTypeStrictlyMatches(right) {
  92. const left = this.getTypeAnnotation();
  93. right = right.getTypeAnnotation();
  94. if (!t.isAnyTypeAnnotation(left) && t.isFlowBaseAnnotation(left)) {
  95. return right.type === left.type;
  96. }
  97. }
  98. function isGenericType(genericName) {
  99. const type = this.getTypeAnnotation();
  100. return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, {
  101. name: genericName
  102. });
  103. }