index.js 3.7 KB

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