index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. isBooleanTypeAnnotation,
  17. isEmptyTypeAnnotation,
  18. isFlowBaseAnnotation,
  19. isGenericTypeAnnotation,
  20. isIdentifier,
  21. isMixedTypeAnnotation,
  22. isNumberTypeAnnotation,
  23. isStringTypeAnnotation,
  24. isTypeAnnotation,
  25. isUnionTypeAnnotation,
  26. isVoidTypeAnnotation,
  27. stringTypeAnnotation,
  28. voidTypeAnnotation
  29. } = _t;
  30. function getTypeAnnotation() {
  31. if (this.typeAnnotation) return this.typeAnnotation;
  32. let type = this._getTypeAnnotation() || anyTypeAnnotation();
  33. if (isTypeAnnotation(type)) type = type.typeAnnotation;
  34. return this.typeAnnotation = type;
  35. }
  36. const typeAnnotationInferringNodes = new WeakSet();
  37. function _getTypeAnnotation() {
  38. const node = this.node;
  39. if (!node) {
  40. if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
  41. const declar = this.parentPath.parentPath;
  42. const declarParent = declar.parentPath;
  43. if (declar.key === "left" && declarParent.isForInStatement()) {
  44. return stringTypeAnnotation();
  45. }
  46. if (declar.key === "left" && declarParent.isForOfStatement()) {
  47. return anyTypeAnnotation();
  48. }
  49. return voidTypeAnnotation();
  50. } else {
  51. return;
  52. }
  53. }
  54. if (node.typeAnnotation) {
  55. return node.typeAnnotation;
  56. }
  57. if (typeAnnotationInferringNodes.has(node)) {
  58. return;
  59. }
  60. typeAnnotationInferringNodes.add(node);
  61. try {
  62. var _inferer;
  63. let inferer = inferers[node.type];
  64. if (inferer) {
  65. return inferer.call(this, node);
  66. }
  67. inferer = inferers[this.parentPath.type];
  68. if ((_inferer = inferer) != null && _inferer.validParent) {
  69. return this.parentPath.getTypeAnnotation();
  70. }
  71. } finally {
  72. typeAnnotationInferringNodes.delete(node);
  73. }
  74. }
  75. function isBaseType(baseName, soft) {
  76. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  77. }
  78. function _isBaseType(baseName, type, soft) {
  79. if (baseName === "string") {
  80. return isStringTypeAnnotation(type);
  81. } else if (baseName === "number") {
  82. return isNumberTypeAnnotation(type);
  83. } else if (baseName === "boolean") {
  84. return isBooleanTypeAnnotation(type);
  85. } else if (baseName === "any") {
  86. return isAnyTypeAnnotation(type);
  87. } else if (baseName === "mixed") {
  88. return isMixedTypeAnnotation(type);
  89. } else if (baseName === "empty") {
  90. return isEmptyTypeAnnotation(type);
  91. } else if (baseName === "void") {
  92. return isVoidTypeAnnotation(type);
  93. } else {
  94. if (soft) {
  95. return false;
  96. } else {
  97. throw new Error(`Unknown base type ${baseName}`);
  98. }
  99. }
  100. }
  101. function couldBeBaseType(name) {
  102. const type = this.getTypeAnnotation();
  103. if (isAnyTypeAnnotation(type)) return true;
  104. if (isUnionTypeAnnotation(type)) {
  105. for (const type2 of type.types) {
  106. if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. } else {
  112. return _isBaseType(name, type, true);
  113. }
  114. }
  115. function baseTypeStrictlyMatches(rightArg) {
  116. const left = this.getTypeAnnotation();
  117. const right = rightArg.getTypeAnnotation();
  118. if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
  119. return right.type === left.type;
  120. }
  121. return false;
  122. }
  123. function isGenericType(genericName) {
  124. const type = this.getTypeAnnotation();
  125. return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
  126. name: genericName
  127. });
  128. }