index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. const typeAnnotationInferringNodes = new WeakSet();
  22. function _getTypeAnnotation() {
  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. if (typeAnnotationInferringNodes.has(node)) {
  43. return;
  44. }
  45. typeAnnotationInferringNodes.add(node);
  46. try {
  47. var _inferer;
  48. let inferer = inferers[node.type];
  49. if (inferer) {
  50. return inferer.call(this, node);
  51. }
  52. inferer = inferers[this.parentPath.type];
  53. if ((_inferer = inferer) != null && _inferer.validParent) {
  54. return this.parentPath.getTypeAnnotation();
  55. }
  56. } finally {
  57. typeAnnotationInferringNodes.delete(node);
  58. }
  59. }
  60. function isBaseType(baseName, soft) {
  61. return _isBaseType(baseName, this.getTypeAnnotation(), soft);
  62. }
  63. function _isBaseType(baseName, type, soft) {
  64. if (baseName === "string") {
  65. return t.isStringTypeAnnotation(type);
  66. } else if (baseName === "number") {
  67. return t.isNumberTypeAnnotation(type);
  68. } else if (baseName === "boolean") {
  69. return t.isBooleanTypeAnnotation(type);
  70. } else if (baseName === "any") {
  71. return t.isAnyTypeAnnotation(type);
  72. } else if (baseName === "mixed") {
  73. return t.isMixedTypeAnnotation(type);
  74. } else if (baseName === "empty") {
  75. return t.isEmptyTypeAnnotation(type);
  76. } else if (baseName === "void") {
  77. return t.isVoidTypeAnnotation(type);
  78. } else {
  79. if (soft) {
  80. return false;
  81. } else {
  82. throw new Error(`Unknown base type ${baseName}`);
  83. }
  84. }
  85. }
  86. function couldBeBaseType(name) {
  87. const type = this.getTypeAnnotation();
  88. if (t.isAnyTypeAnnotation(type)) return true;
  89. if (t.isUnionTypeAnnotation(type)) {
  90. for (const type2 of type.types) {
  91. if (t.isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. } else {
  97. return _isBaseType(name, type, true);
  98. }
  99. }
  100. function baseTypeStrictlyMatches(rightArg) {
  101. const left = this.getTypeAnnotation();
  102. const right = rightArg.getTypeAnnotation();
  103. if (!t.isAnyTypeAnnotation(left) && t.isFlowBaseAnnotation(left)) {
  104. return right.type === left.type;
  105. }
  106. return false;
  107. }
  108. function isGenericType(genericName) {
  109. const type = this.getTypeAnnotation();
  110. return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, {
  111. name: genericName
  112. });
  113. }