[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports._getTypeAnnotation = _getTypeAnnotation;
|
---|
| 7 | exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
|
---|
| 8 | exports.couldBeBaseType = couldBeBaseType;
|
---|
| 9 | exports.getTypeAnnotation = getTypeAnnotation;
|
---|
| 10 | exports.isBaseType = isBaseType;
|
---|
| 11 | exports.isGenericType = isGenericType;
|
---|
| 12 | var inferers = require("./inferers.js");
|
---|
| 13 | var _t = require("@babel/types");
|
---|
| 14 | const {
|
---|
| 15 | anyTypeAnnotation,
|
---|
| 16 | isAnyTypeAnnotation,
|
---|
| 17 | isArrayTypeAnnotation,
|
---|
| 18 | isBooleanTypeAnnotation,
|
---|
| 19 | isEmptyTypeAnnotation,
|
---|
| 20 | isFlowBaseAnnotation,
|
---|
| 21 | isGenericTypeAnnotation,
|
---|
| 22 | isIdentifier,
|
---|
| 23 | isMixedTypeAnnotation,
|
---|
| 24 | isNumberTypeAnnotation,
|
---|
| 25 | isStringTypeAnnotation,
|
---|
| 26 | isTSArrayType,
|
---|
| 27 | isTSTypeAnnotation,
|
---|
| 28 | isTSTypeReference,
|
---|
| 29 | isTupleTypeAnnotation,
|
---|
| 30 | isTypeAnnotation,
|
---|
| 31 | isUnionTypeAnnotation,
|
---|
| 32 | isVoidTypeAnnotation,
|
---|
| 33 | stringTypeAnnotation,
|
---|
| 34 | voidTypeAnnotation
|
---|
| 35 | } = _t;
|
---|
| 36 | function getTypeAnnotation() {
|
---|
| 37 | let type = this.getData("typeAnnotation");
|
---|
| 38 | if (type != null) {
|
---|
| 39 | return type;
|
---|
| 40 | }
|
---|
| 41 | type = _getTypeAnnotation.call(this) || anyTypeAnnotation();
|
---|
| 42 | if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
|
---|
| 43 | type = type.typeAnnotation;
|
---|
| 44 | }
|
---|
| 45 | this.setData("typeAnnotation", type);
|
---|
| 46 | return type;
|
---|
| 47 | }
|
---|
| 48 | const typeAnnotationInferringNodes = new WeakSet();
|
---|
| 49 | function _getTypeAnnotation() {
|
---|
| 50 | const node = this.node;
|
---|
| 51 | if (!node) {
|
---|
| 52 | if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
|
---|
| 53 | const declar = this.parentPath.parentPath;
|
---|
| 54 | const declarParent = declar.parentPath;
|
---|
| 55 | if (declar.key === "left" && declarParent.isForInStatement()) {
|
---|
| 56 | return stringTypeAnnotation();
|
---|
| 57 | }
|
---|
| 58 | if (declar.key === "left" && declarParent.isForOfStatement()) {
|
---|
| 59 | return anyTypeAnnotation();
|
---|
| 60 | }
|
---|
| 61 | return voidTypeAnnotation();
|
---|
| 62 | } else {
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | if (node.typeAnnotation) {
|
---|
| 67 | return node.typeAnnotation;
|
---|
| 68 | }
|
---|
| 69 | if (typeAnnotationInferringNodes.has(node)) {
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
| 72 | typeAnnotationInferringNodes.add(node);
|
---|
| 73 | try {
|
---|
| 74 | var _inferer;
|
---|
| 75 | let inferer = inferers[node.type];
|
---|
| 76 | if (inferer) {
|
---|
| 77 | return inferer.call(this, node);
|
---|
| 78 | }
|
---|
| 79 | inferer = inferers[this.parentPath.type];
|
---|
| 80 | if ((_inferer = inferer) != null && _inferer.validParent) {
|
---|
| 81 | return this.parentPath.getTypeAnnotation();
|
---|
| 82 | }
|
---|
| 83 | } finally {
|
---|
| 84 | typeAnnotationInferringNodes.delete(node);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | function isBaseType(baseName, soft) {
|
---|
| 88 | return _isBaseType(baseName, this.getTypeAnnotation(), soft);
|
---|
| 89 | }
|
---|
| 90 | function _isBaseType(baseName, type, soft) {
|
---|
| 91 | if (baseName === "string") {
|
---|
| 92 | return isStringTypeAnnotation(type);
|
---|
| 93 | } else if (baseName === "number") {
|
---|
| 94 | return isNumberTypeAnnotation(type);
|
---|
| 95 | } else if (baseName === "boolean") {
|
---|
| 96 | return isBooleanTypeAnnotation(type);
|
---|
| 97 | } else if (baseName === "any") {
|
---|
| 98 | return isAnyTypeAnnotation(type);
|
---|
| 99 | } else if (baseName === "mixed") {
|
---|
| 100 | return isMixedTypeAnnotation(type);
|
---|
| 101 | } else if (baseName === "empty") {
|
---|
| 102 | return isEmptyTypeAnnotation(type);
|
---|
| 103 | } else if (baseName === "void") {
|
---|
| 104 | return isVoidTypeAnnotation(type);
|
---|
| 105 | } else {
|
---|
| 106 | if (soft) {
|
---|
| 107 | return false;
|
---|
| 108 | } else {
|
---|
| 109 | throw new Error(`Unknown base type ${baseName}`);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | function couldBeBaseType(name) {
|
---|
| 114 | const type = this.getTypeAnnotation();
|
---|
| 115 | if (isAnyTypeAnnotation(type)) return true;
|
---|
| 116 | if (isUnionTypeAnnotation(type)) {
|
---|
| 117 | for (const type2 of type.types) {
|
---|
| 118 | if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
|
---|
| 119 | return true;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | return false;
|
---|
| 123 | } else {
|
---|
| 124 | return _isBaseType(name, type, true);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | function baseTypeStrictlyMatches(rightArg) {
|
---|
| 128 | const left = this.getTypeAnnotation();
|
---|
| 129 | const right = rightArg.getTypeAnnotation();
|
---|
| 130 | if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {
|
---|
| 131 | return right.type === left.type;
|
---|
| 132 | }
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | function isGenericType(genericName) {
|
---|
| 136 | const type = this.getTypeAnnotation();
|
---|
| 137 | if (genericName === "Array") {
|
---|
| 138 | if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
|
---|
| 143 | name: genericName
|
---|
| 144 | }) || isTSTypeReference(type) && isIdentifier(type.typeName, {
|
---|
| 145 | name: genericName
|
---|
| 146 | });
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | //# sourceMappingURL=index.js.map
|
---|