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