source: trip-planner-front/node_modules/@babel/traverse/lib/path/inference/index.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getTypeAnnotation = getTypeAnnotation;
7exports._getTypeAnnotation = _getTypeAnnotation;
8exports.isBaseType = isBaseType;
9exports.couldBeBaseType = couldBeBaseType;
10exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
11exports.isGenericType = isGenericType;
12
13var inferers = require("./inferers");
14
15var _t = require("@babel/types");
16
17const {
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
35function 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
42const typeAnnotationInferringNodes = new WeakSet();
43
44function _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
95function isBaseType(baseName, soft) {
96 return _isBaseType(baseName, this.getTypeAnnotation(), soft);
97}
98
99function _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
123function 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
140function 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
151function isGenericType(genericName) {
152 const type = this.getTypeAnnotation();
153 return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
154 name: genericName
155 });
156}
Note: See TracBrowser for help on using the repository browser.