index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._getTypeAnnotation = _getTypeAnnotation;
  6. exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
  7. exports.couldBeBaseType = couldBeBaseType;
  8. exports.getTypeAnnotation = getTypeAnnotation;
  9. exports.isBaseType = isBaseType;
  10. exports.isGenericType = isGenericType;
  11. var inferers = require("./inferers");
  12. var _t = require("@babel/types");
  13. const {
  14. anyTypeAnnotation,
  15. isAnyTypeAnnotation,
  16. isArrayTypeAnnotation,
  17. isBooleanTypeAnnotation,
  18. isEmptyTypeAnnotation,
  19. isFlowBaseAnnotation,
  20. isGenericTypeAnnotation,
  21. isIdentifier,
  22. isMixedTypeAnnotation,
  23. isNumberTypeAnnotation,
  24. isStringTypeAnnotation,
  25. isTSArrayType,
  26. isTSTypeAnnotation,
  27. isTSTypeReference,
  28. isTupleTypeAnnotation,
  29. isTypeAnnotation,
  30. isUnionTypeAnnotation,
  31. isVoidTypeAnnotation,
  32. stringTypeAnnotation,
  33. voidTypeAnnotation
  34. } = _t;
  35. function getTypeAnnotation() {
  36. let type = this.getData("typeAnnotation");
  37. if (type != null) {
  38. return type;
  39. }
  40. type = this._getTypeAnnotation() || anyTypeAnnotation();
  41. if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
  42. type = type.typeAnnotation;
  43. }
  44. this.setData("typeAnnotation", type);
  45. return type;
  46. }
  47. const typeAnnotationInferringNodes = new WeakSet();
  48. function _getTypeAnnotation() {
  49. const node = this.node;
  50. if (!node) {
  51. if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
  52. const declar = this.parentPath.parentPath;
  53. const declarParent = declar.parentPath;
  54. if (declar.key === "left" && declarParent.isForInStatement()) {
  55. return stringTypeAnnotation();
  56. }
  57. if (declar.key === "left" && declarParent.isForOfStatement()) {
  58. return anyTypeAnnotation();
  59. }
  60. return voidTypeAnnotation();
  61. } else {
  62. return;
  63. }
  64. }
  65. if (node.typeAnnotation) {
  66. return node.typeAnnotation;
  67. }
  68. if (typeAnnotationInferringNodes.has(node)) {
  69. return;
  70. }
  71. typeAnnotationInferringNodes.add(node);
  72. try {
  73. var _inferer;
  74. let inferer =
  75. inferers[node.type];
  76. if (inferer) {
  77. return inferer.call(this, node);
  78. }
  79. inferer = inferers[this.parentPath.type];
  80. if ((_inferer = inferer) != null && _inferer.validParent) {
  81. return this.parentPath.getTypeAnnotation();
  82. }
  83. } finally {
  84. typeAnnotationInferringNodes.delete(node);
  85. }
  86. }
  87. function isBaseType(baseName, soft) {
  88. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  89. }
  90. function _isBaseType(baseName, type, soft) {
  91. if (baseName === "string") {
  92. return isStringTypeAnnotation(type);
  93. } else if (baseName === "number") {
  94. return isNumberTypeAnnotation(type);
  95. } else if (baseName === "boolean") {
  96. return isBooleanTypeAnnotation(type);
  97. } else if (baseName === "any") {
  98. return isAnyTypeAnnotation(type);
  99. } else if (baseName === "mixed") {
  100. return isMixedTypeAnnotation(type);
  101. } else if (baseName === "empty") {
  102. return isEmptyTypeAnnotation(type);
  103. } else if (baseName === "void") {
  104. return isVoidTypeAnnotation(type);
  105. } else {
  106. if (soft) {
  107. return false;
  108. } else {
  109. throw new Error(`Unknown base type ${baseName}`);
  110. }
  111. }
  112. }
  113. function couldBeBaseType(name) {
  114. const type = this.getTypeAnnotation();
  115. if (isAnyTypeAnnotation(type)) return true;
  116. if (isUnionTypeAnnotation(type)) {
  117. for (const type2 of type.types) {
  118. if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. } else {
  124. return _isBaseType(name, type, true);
  125. }
  126. }
  127. function baseTypeStrictlyMatches(rightArg) {
  128. const left = this.getTypeAnnotation();
  129. const right = rightArg.getTypeAnnotation();
  130. if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
  131. return right.type === left.type;
  132. }
  133. return false;
  134. }
  135. function isGenericType(genericName) {
  136. const type = this.getTypeAnnotation();
  137. if (genericName === "Array") {
  138. if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
  139. return true;
  140. }
  141. }
  142. return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
  143. name: genericName
  144. }) || isTSTypeReference(type) && isIdentifier(type.typeName, {
  145. name: genericName
  146. });
  147. }
  148. //# sourceMappingURL=index.js.map