index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.js");
  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 = inferers[node.type];
  75. if (inferer) {
  76. return inferer.call(this, node);
  77. }
  78. inferer = inferers[this.parentPath.type];
  79. if ((_inferer = inferer) != null && _inferer.validParent) {
  80. return this.parentPath.getTypeAnnotation();
  81. }
  82. } finally {
  83. typeAnnotationInferringNodes.delete(node);
  84. }
  85. }
  86. function isBaseType(baseName, soft) {
  87. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  88. }
  89. function _isBaseType(baseName, type, soft) {
  90. if (baseName === "string") {
  91. return isStringTypeAnnotation(type);
  92. } else if (baseName === "number") {
  93. return isNumberTypeAnnotation(type);
  94. } else if (baseName === "boolean") {
  95. return isBooleanTypeAnnotation(type);
  96. } else if (baseName === "any") {
  97. return isAnyTypeAnnotation(type);
  98. } else if (baseName === "mixed") {
  99. return isMixedTypeAnnotation(type);
  100. } else if (baseName === "empty") {
  101. return isEmptyTypeAnnotation(type);
  102. } else if (baseName === "void") {
  103. return isVoidTypeAnnotation(type);
  104. } else {
  105. if (soft) {
  106. return false;
  107. } else {
  108. throw new Error(`Unknown base type ${baseName}`);
  109. }
  110. }
  111. }
  112. function couldBeBaseType(name) {
  113. const type = this.getTypeAnnotation();
  114. if (isAnyTypeAnnotation(type)) return true;
  115. if (isUnionTypeAnnotation(type)) {
  116. for (const type2 of type.types) {
  117. if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. } else {
  123. return _isBaseType(name, type, true);
  124. }
  125. }
  126. function baseTypeStrictlyMatches(rightArg) {
  127. const left = this.getTypeAnnotation();
  128. const right = rightArg.getTypeAnnotation();
  129. if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
  130. return right.type === left.type;
  131. }
  132. return false;
  133. }
  134. function isGenericType(genericName) {
  135. const type = this.getTypeAnnotation();
  136. if (genericName === "Array") {
  137. if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
  138. return true;
  139. }
  140. }
  141. return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
  142. name: genericName
  143. }) || isTSTypeReference(type) && isIdentifier(type.typeName, {
  144. name: genericName
  145. });
  146. }
  147. //# sourceMappingURL=index.js.map