index.js 3.4 KB

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