@babel/traverse 7.1.0 → 7.23.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/cache.js +27 -7
- package/lib/cache.js.map +1 -0
- package/lib/context.js +22 -59
- package/lib/context.js.map +1 -0
- package/lib/hub.js +2 -6
- package/lib/hub.js.map +1 -0
- package/lib/index.js +42 -77
- package/lib/index.js.map +1 -0
- package/lib/path/ancestry.js +15 -62
- package/lib/path/ancestry.js.map +1 -0
- package/lib/path/comments.js +31 -24
- package/lib/path/comments.js.map +1 -0
- package/lib/path/context.js +68 -91
- package/lib/path/context.js.map +1 -0
- package/lib/path/conversion.js +275 -273
- package/lib/path/conversion.js.map +1 -0
- package/lib/path/evaluation.js +84 -149
- package/lib/path/evaluation.js.map +1 -0
- package/lib/path/family.js +189 -94
- package/lib/path/family.js.map +1 -0
- package/lib/path/index.js +106 -132
- package/lib/path/index.js.map +1 -0
- package/lib/path/inference/index.js +81 -64
- package/lib/path/inference/index.js.map +1 -0
- package/lib/path/inference/inferer-reference.js +22 -52
- package/lib/path/inference/inferer-reference.js.map +1 -0
- package/lib/path/inference/inferers.js +110 -125
- package/lib/path/inference/inferers.js.map +1 -0
- package/lib/path/inference/util.js +30 -0
- package/lib/path/inference/util.js.map +1 -0
- package/lib/path/introspection.js +182 -168
- package/lib/path/introspection.js.map +1 -0
- package/lib/path/lib/hoister.js +37 -54
- package/lib/path/lib/hoister.js.map +1 -0
- package/lib/path/lib/removal-hooks.js +4 -4
- package/lib/path/lib/removal-hooks.js.map +1 -0
- package/lib/path/lib/virtual-types-validator.js +161 -0
- package/lib/path/lib/virtual-types-validator.js.map +1 -0
- package/lib/path/lib/virtual-types.js +21 -189
- package/lib/path/lib/virtual-types.js.map +1 -0
- package/lib/path/modification.js +103 -98
- package/lib/path/modification.js.map +1 -0
- package/lib/path/removal.js +18 -23
- package/lib/path/removal.js.map +1 -0
- package/lib/path/replacement.js +91 -144
- package/lib/path/replacement.js.map +1 -0
- package/lib/scope/binding.js +28 -16
- package/lib/scope/binding.js.map +1 -0
- package/lib/scope/index.js +407 -414
- package/lib/scope/index.js.map +1 -0
- package/lib/scope/lib/renamer.js +45 -70
- package/lib/scope/lib/renamer.js.map +1 -0
- package/lib/traverse-node.js +29 -0
- package/lib/traverse-node.js.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/visitors.js +77 -113
- package/lib/visitors.js.map +1 -0
- package/package.json +28 -17
@@ -4,23 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = _default;
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
return data;
|
16
|
-
}
|
17
|
-
|
18
|
-
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; } }
|
19
|
-
|
7
|
+
var _t = require("@babel/types");
|
8
|
+
var _util = require("./util.js");
|
9
|
+
const {
|
10
|
+
BOOLEAN_NUMBER_BINARY_OPERATORS,
|
11
|
+
createTypeAnnotationBasedOnTypeof,
|
12
|
+
numberTypeAnnotation,
|
13
|
+
voidTypeAnnotation
|
14
|
+
} = _t;
|
20
15
|
function _default(node) {
|
21
16
|
if (!this.isReferenced()) return;
|
22
17
|
const binding = this.scope.getBinding(node.name);
|
23
|
-
|
24
18
|
if (binding) {
|
25
19
|
if (binding.identifier.typeAnnotation) {
|
26
20
|
return binding.identifier.typeAnnotation;
|
@@ -28,58 +22,48 @@ function _default(node) {
|
|
28
22
|
return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
|
29
23
|
}
|
30
24
|
}
|
31
|
-
|
32
25
|
if (node.name === "undefined") {
|
33
|
-
return
|
26
|
+
return voidTypeAnnotation();
|
34
27
|
} else if (node.name === "NaN" || node.name === "Infinity") {
|
35
|
-
return
|
28
|
+
return numberTypeAnnotation();
|
36
29
|
} else if (node.name === "arguments") {}
|
37
30
|
}
|
38
|
-
|
39
31
|
function getTypeAnnotationBindingConstantViolations(binding, path, name) {
|
40
32
|
const types = [];
|
41
33
|
const functionConstantViolations = [];
|
42
34
|
let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
|
43
35
|
const testType = getConditionalAnnotation(binding, path, name);
|
44
|
-
|
45
36
|
if (testType) {
|
46
37
|
const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
|
47
38
|
constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
|
48
39
|
types.push(testType.typeAnnotation);
|
49
40
|
}
|
50
|
-
|
51
41
|
if (constantViolations.length) {
|
52
|
-
constantViolations
|
53
|
-
|
42
|
+
constantViolations.push(...functionConstantViolations);
|
54
43
|
for (const violation of constantViolations) {
|
55
44
|
types.push(violation.getTypeAnnotation());
|
56
45
|
}
|
57
46
|
}
|
58
|
-
|
59
|
-
|
60
|
-
return t().createUnionTypeAnnotation(types);
|
47
|
+
if (!types.length) {
|
48
|
+
return;
|
61
49
|
}
|
50
|
+
return (0, _util.createUnionType)(types);
|
62
51
|
}
|
63
|
-
|
64
52
|
function getConstantViolationsBefore(binding, path, functions) {
|
65
53
|
const violations = binding.constantViolations.slice();
|
66
54
|
violations.unshift(binding.path);
|
67
55
|
return violations.filter(violation => {
|
68
56
|
violation = violation.resolve();
|
69
|
-
|
70
57
|
const status = violation._guessExecutionStatusRelativeTo(path);
|
71
|
-
|
72
|
-
if (functions && status === "function") functions.push(violation);
|
58
|
+
if (functions && status === "unknown") functions.push(violation);
|
73
59
|
return status === "before";
|
74
60
|
});
|
75
61
|
}
|
76
|
-
|
77
62
|
function inferAnnotationFromBinaryExpression(name, path) {
|
78
63
|
const operator = path.node.operator;
|
79
64
|
const right = path.get("right").resolve();
|
80
65
|
const left = path.get("left").resolve();
|
81
66
|
let target;
|
82
|
-
|
83
67
|
if (left.isIdentifier({
|
84
68
|
name
|
85
69
|
})) {
|
@@ -89,23 +73,18 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
89
73
|
})) {
|
90
74
|
target = left;
|
91
75
|
}
|
92
|
-
|
93
76
|
if (target) {
|
94
77
|
if (operator === "===") {
|
95
78
|
return target.getTypeAnnotation();
|
96
79
|
}
|
97
|
-
|
98
|
-
|
99
|
-
return t().numberTypeAnnotation();
|
80
|
+
if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
81
|
+
return numberTypeAnnotation();
|
100
82
|
}
|
101
|
-
|
102
83
|
return;
|
103
84
|
}
|
104
|
-
|
105
85
|
if (operator !== "===" && operator !== "==") return;
|
106
86
|
let typeofPath;
|
107
87
|
let typePath;
|
108
|
-
|
109
88
|
if (left.isUnaryExpression({
|
110
89
|
operator: "typeof"
|
111
90
|
})) {
|
@@ -117,7 +96,6 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
117
96
|
typeofPath = right;
|
118
97
|
typePath = left;
|
119
98
|
}
|
120
|
-
|
121
99
|
if (!typeofPath) return;
|
122
100
|
if (!typeofPath.get("argument").isIdentifier({
|
123
101
|
name
|
@@ -126,39 +104,31 @@ function inferAnnotationFromBinaryExpression(name, path) {
|
|
126
104
|
if (!typePath.isLiteral()) return;
|
127
105
|
const typeValue = typePath.node.value;
|
128
106
|
if (typeof typeValue !== "string") return;
|
129
|
-
return
|
107
|
+
return createTypeAnnotationBasedOnTypeof(typeValue);
|
130
108
|
}
|
131
|
-
|
132
109
|
function getParentConditionalPath(binding, path, name) {
|
133
110
|
let parentPath;
|
134
|
-
|
135
111
|
while (parentPath = path.parentPath) {
|
136
112
|
if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
|
137
113
|
if (path.key === "test") {
|
138
114
|
return;
|
139
115
|
}
|
140
|
-
|
141
116
|
return parentPath;
|
142
117
|
}
|
143
|
-
|
144
118
|
if (parentPath.isFunction()) {
|
145
119
|
if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
|
146
120
|
}
|
147
|
-
|
148
121
|
path = parentPath;
|
149
122
|
}
|
150
123
|
}
|
151
|
-
|
152
124
|
function getConditionalAnnotation(binding, path, name) {
|
153
125
|
const ifStatement = getParentConditionalPath(binding, path, name);
|
154
126
|
if (!ifStatement) return;
|
155
127
|
const test = ifStatement.get("test");
|
156
128
|
const paths = [test];
|
157
129
|
const types = [];
|
158
|
-
|
159
130
|
for (let i = 0; i < paths.length; i++) {
|
160
131
|
const path = paths[i];
|
161
|
-
|
162
132
|
if (path.isLogicalExpression()) {
|
163
133
|
if (path.node.operator === "&&") {
|
164
134
|
paths.push(path.get("left"));
|
@@ -169,13 +139,13 @@ function getConditionalAnnotation(binding, path, name) {
|
|
169
139
|
if (type) types.push(type);
|
170
140
|
}
|
171
141
|
}
|
172
|
-
|
173
142
|
if (types.length) {
|
174
143
|
return {
|
175
|
-
typeAnnotation:
|
144
|
+
typeAnnotation: (0, _util.createUnionType)(types),
|
176
145
|
ifStatement
|
177
146
|
};
|
178
147
|
}
|
148
|
+
return getConditionalAnnotation(binding, ifStatement, name);
|
149
|
+
}
|
179
150
|
|
180
|
-
|
181
|
-
}
|
151
|
+
//# sourceMappingURL=inferer-reference.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_t","require","_util","BOOLEAN_NUMBER_BINARY_OPERATORS","createTypeAnnotationBasedOnTypeof","numberTypeAnnotation","voidTypeAnnotation","_default","node","isReferenced","binding","scope","getBinding","name","identifier","typeAnnotation","getTypeAnnotationBindingConstantViolations","path","types","functionConstantViolations","constantViolations","getConstantViolationsBefore","testType","getConditionalAnnotation","testConstantViolations","ifStatement","filter","indexOf","push","length","violation","getTypeAnnotation","createUnionType","functions","violations","slice","unshift","resolve","status","_guessExecutionStatusRelativeTo","inferAnnotationFromBinaryExpression","operator","right","get","left","target","isIdentifier","typeofPath","typePath","isUnaryExpression","isLiteral","typeValue","value","getParentConditionalPath","parentPath","isIfStatement","isConditionalExpression","key","isFunction","test","paths","i","isLogicalExpression","isBinaryExpression","type"],"sources":["../../../src/path/inference/inferer-reference.ts"],"sourcesContent":["import type NodePath from \"../index.ts\";\nimport {\n BOOLEAN_NUMBER_BINARY_OPERATORS,\n createTypeAnnotationBasedOnTypeof,\n numberTypeAnnotation,\n voidTypeAnnotation,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Binding from \"../../scope/binding.ts\";\n\nimport { createUnionType } from \"./util.ts\";\n\nexport default function (this: NodePath<t.Identifier>, node: t.Identifier) {\n if (!this.isReferenced()) return;\n\n // check if a binding exists of this value and if so then return a union type of all\n // possible types that the binding could be\n const binding = this.scope.getBinding(node.name);\n if (binding) {\n if (binding.identifier.typeAnnotation) {\n return binding.identifier.typeAnnotation;\n } else {\n return getTypeAnnotationBindingConstantViolations(\n binding,\n this,\n node.name,\n );\n }\n }\n\n // built-in values\n if (node.name === \"undefined\") {\n return voidTypeAnnotation();\n } else if (node.name === \"NaN\" || node.name === \"Infinity\") {\n return numberTypeAnnotation();\n } else if (node.name === \"arguments\") {\n // todo\n }\n}\n\nfunction getTypeAnnotationBindingConstantViolations(\n binding: Binding,\n path: NodePath<t.Identifier>,\n name: string,\n) {\n const types = [];\n\n const functionConstantViolations: NodePath[] = [];\n let constantViolations = getConstantViolationsBefore(\n binding,\n path,\n functionConstantViolations,\n );\n\n const testType = getConditionalAnnotation(binding, path, name);\n if (testType) {\n const testConstantViolations = getConstantViolationsBefore(\n binding,\n testType.ifStatement,\n );\n\n // remove constant violations observed before the IfStatement\n constantViolations = constantViolations.filter(\n path => testConstantViolations.indexOf(path) < 0,\n );\n\n // clear current types and add in observed test type\n types.push(testType.typeAnnotation);\n }\n\n if (constantViolations.length) {\n // pick one constant from each scope which will represent the last possible\n // control flow path that it could've taken/been\n /* This code is broken for the following problems:\n * It thinks that assignments can only happen in scopes.\n * What about conditionals, if statements without block,\n * or guarded assignments.\n * It also checks to see if one of the assignments is in the\n * same scope and uses that as the only \"violation\". However,\n * the binding is returned by `getConstantViolationsBefore` so we for\n * sure always going to return that as the only \"violation\".\n let rawConstantViolations = constantViolations.reverse();\n let visitedScopes = [];\n constantViolations = [];\n for (let violation of (rawConstantViolations: Array<NodePath>)) {\n let violationScope = violation.scope;\n if (visitedScopes.indexOf(violationScope) >= 0) continue;\n\n visitedScopes.push(violationScope);\n constantViolations.push(violation);\n\n if (violationScope === path.scope) {\n constantViolations = [violation];\n break;\n }\n }*/\n\n // add back on function constant violations since we can't track calls\n constantViolations.push(...functionConstantViolations);\n\n // push on inferred types of violated paths\n for (const violation of constantViolations) {\n types.push(violation.getTypeAnnotation());\n }\n }\n\n if (!types.length) {\n return;\n }\n\n return createUnionType(types);\n}\n\nfunction getConstantViolationsBefore(\n binding: Binding,\n path: NodePath,\n functions?: NodePath[],\n) {\n const violations = binding.constantViolations.slice();\n violations.unshift(binding.path);\n return violations.filter(violation => {\n violation = violation.resolve();\n const status = violation._guessExecutionStatusRelativeTo(path);\n if (functions && status === \"unknown\") functions.push(violation);\n return status === \"before\";\n });\n}\n\nfunction inferAnnotationFromBinaryExpression(\n name: string,\n path: NodePath<t.BinaryExpression>,\n) {\n const operator = path.node.operator;\n\n const right = path.get(\"right\").resolve();\n const left = path.get(\"left\").resolve();\n\n let target;\n if (left.isIdentifier({ name })) {\n target = right;\n } else if (right.isIdentifier({ name })) {\n target = left;\n }\n\n if (target) {\n if (operator === \"===\") {\n return target.getTypeAnnotation();\n }\n if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n }\n\n return;\n }\n\n if (operator !== \"===\" && operator !== \"==\") return;\n\n //\n let typeofPath: NodePath<t.UnaryExpression>;\n let typePath: NodePath<t.Expression>;\n if (left.isUnaryExpression({ operator: \"typeof\" })) {\n typeofPath = left;\n typePath = right as NodePath<t.Expression>;\n } else if (right.isUnaryExpression({ operator: \"typeof\" })) {\n typeofPath = right;\n typePath = left as NodePath<t.Expression>;\n }\n\n if (!typeofPath) return;\n // and that the argument of the typeof path references us!\n if (!typeofPath.get(\"argument\").isIdentifier({ name })) return;\n\n // ensure that the type path is a Literal\n typePath = typePath.resolve() as NodePath<t.Expression>;\n if (!typePath.isLiteral()) return;\n\n // and that it's a string so we can infer it\n // @ts-expect-error todo(flow->ts): value is not defined for NullLiteral and some other\n const typeValue = typePath.node.value;\n if (typeof typeValue !== \"string\") return;\n\n // turn type value into a type annotation\n // @ts-expect-error todo(flow->ts): move validation from helper or relax type constraint to just a string\n return createTypeAnnotationBasedOnTypeof(typeValue);\n}\n\nfunction getParentConditionalPath(\n binding: Binding,\n path: NodePath,\n name: string,\n) {\n let parentPath;\n while ((parentPath = path.parentPath)) {\n if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {\n if (path.key === \"test\") {\n return;\n }\n\n return parentPath as NodePath<t.IfStatement | t.ConditionalExpression>;\n }\n if (parentPath.isFunction()) {\n if (parentPath.parentPath.scope.getBinding(name) !== binding) return;\n }\n\n path = parentPath;\n }\n}\n\nfunction getConditionalAnnotation<T extends t.Node>(\n binding: Binding,\n path: NodePath<T>,\n name?: string,\n): {\n typeAnnotation: t.FlowType | t.TSType;\n ifStatement: NodePath<t.IfStatement | t.ConditionalExpression>;\n} {\n const ifStatement = getParentConditionalPath(binding, path, name);\n if (!ifStatement) return;\n\n const test = ifStatement.get(\"test\");\n const paths = [test];\n const types = [];\n\n for (let i = 0; i < paths.length; i++) {\n const path = paths[i];\n\n if (path.isLogicalExpression()) {\n if (path.node.operator === \"&&\") {\n paths.push(path.get(\"left\"));\n paths.push(path.get(\"right\"));\n }\n } else if (path.isBinaryExpression()) {\n const type = inferAnnotationFromBinaryExpression(name, path);\n if (type) types.push(type);\n }\n }\n\n if (types.length) {\n return {\n typeAnnotation: createUnionType(types),\n ifStatement,\n };\n }\n\n return getConditionalAnnotation(binding, ifStatement, name);\n}\n"],"mappings":";;;;;;AACA,IAAAA,EAAA,GAAAC,OAAA;AASA,IAAAC,KAAA,GAAAD,OAAA;AAA4C;EAR1CE,+BAA+B;EAC/BC,iCAAiC;EACjCC,oBAAoB;EACpBC;AAAkB,IAAAN,EAAA;AAOL,SAAAO,SAAwCC,IAAkB,EAAE;EACzE,IAAI,CAAC,IAAI,CAACC,YAAY,CAAC,CAAC,EAAE;EAI1B,MAAMC,OAAO,GAAG,IAAI,CAACC,KAAK,CAACC,UAAU,CAACJ,IAAI,CAACK,IAAI,CAAC;EAChD,IAAIH,OAAO,EAAE;IACX,IAAIA,OAAO,CAACI,UAAU,CAACC,cAAc,EAAE;MACrC,OAAOL,OAAO,CAACI,UAAU,CAACC,cAAc;IAC1C,CAAC,MAAM;MACL,OAAOC,0CAA0C,CAC/CN,OAAO,EACP,IAAI,EACJF,IAAI,CAACK,IACP,CAAC;IACH;EACF;EAGA,IAAIL,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE;IAC7B,OAAOP,kBAAkB,CAAC,CAAC;EAC7B,CAAC,MAAM,IAAIE,IAAI,CAACK,IAAI,KAAK,KAAK,IAAIL,IAAI,CAACK,IAAI,KAAK,UAAU,EAAE;IAC1D,OAAOR,oBAAoB,CAAC,CAAC;EAC/B,CAAC,MAAM,IAAIG,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE,CAEtC;AACF;AAEA,SAASG,0CAA0CA,CACjDN,OAAgB,EAChBO,IAA4B,EAC5BJ,IAAY,EACZ;EACA,MAAMK,KAAK,GAAG,EAAE;EAEhB,MAAMC,0BAAsC,GAAG,EAAE;EACjD,IAAIC,kBAAkB,GAAGC,2BAA2B,CAClDX,OAAO,EACPO,IAAI,EACJE,0BACF,CAAC;EAED,MAAMG,QAAQ,GAAGC,wBAAwB,CAACb,OAAO,EAA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EAAE,EAAE;IACrC,MAAM5C,IAAI,GAAG2C,KAAK,CAACC,CAAC,CAAC;IAErB,IAAI5C,IAAI,CAAC6C,mBAAmB,CAAC,CAAC,EAAE;MAC9B,IAAI7C,IAAI,CAACT,IAAI,CAACiC,QAAQ,KAAK,IAAI,EAAE;QAC/BmB,KAAK,CAAChC,IAAI,CAACX,IAAI,CAAC0B,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5BiB,KAAK,CAAChC,IAAI,CAACX,IAAI,CAAC0B,GAAG,CAAC,OAAO,CAAC,CAAC;MAC/B;IACF,CAAC,MAAM,IAAI1B,IAAI,CAAC8C,kBAAkB,CAAC,CAAC,EAAE;MACpC,MAAMC,IAAI,GAAGxB,mCAAmC,CAAC3B,IAAI,EAAEI,IAAI,CAAC;MAC5D,IAAI+C,IAAI,EAAE9C,KAAK,CAACU,IAAI,CAACoC,IAAI,CAAC;IAC5B;EACF;EAEA,IAAI9C,KAAK,CAACW,MAAM,EAAE;IAChB,OAAO;MACLd,cAAc,EAAE,IAAAiB,qBAAe,EAACd,KAAK,CAAC;MACtCO;IACF,CAAC;EACH;EAEA,OAAOF,wBAAwB,CAACb,OAAO,EAAEe,WAAW,EAAEZ,IAAI,CAAC;AAC7D"}
|
@@ -3,220 +3,205 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.
|
7
|
-
exports.TypeCastExpression = TypeCastExpression;
|
8
|
-
exports.NewExpression = NewExpression;
|
9
|
-
exports.TemplateLiteral = TemplateLiteral;
|
10
|
-
exports.UnaryExpression = UnaryExpression;
|
11
|
-
exports.BinaryExpression = BinaryExpression;
|
12
|
-
exports.LogicalExpression = LogicalExpression;
|
13
|
-
exports.ConditionalExpression = ConditionalExpression;
|
14
|
-
exports.SequenceExpression = SequenceExpression;
|
6
|
+
exports.ArrayExpression = ArrayExpression;
|
15
7
|
exports.AssignmentExpression = AssignmentExpression;
|
16
|
-
exports.
|
17
|
-
exports.StringLiteral = StringLiteral;
|
18
|
-
exports.NumericLiteral = NumericLiteral;
|
8
|
+
exports.BinaryExpression = BinaryExpression;
|
19
9
|
exports.BooleanLiteral = BooleanLiteral;
|
20
|
-
exports.NullLiteral = NullLiteral;
|
21
|
-
exports.RegExpLiteral = RegExpLiteral;
|
22
|
-
exports.ObjectExpression = ObjectExpression;
|
23
|
-
exports.ArrayExpression = ArrayExpression;
|
24
|
-
exports.RestElement = RestElement;
|
25
|
-
exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
|
26
10
|
exports.CallExpression = CallExpression;
|
27
|
-
exports.
|
11
|
+
exports.ConditionalExpression = ConditionalExpression;
|
12
|
+
exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
|
28
13
|
Object.defineProperty(exports, "Identifier", {
|
29
14
|
enumerable: true,
|
30
15
|
get: function () {
|
31
16
|
return _infererReference.default;
|
32
17
|
}
|
33
18
|
});
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
19
|
+
exports.LogicalExpression = LogicalExpression;
|
20
|
+
exports.NewExpression = NewExpression;
|
21
|
+
exports.NullLiteral = NullLiteral;
|
22
|
+
exports.NumericLiteral = NumericLiteral;
|
23
|
+
exports.ObjectExpression = ObjectExpression;
|
24
|
+
exports.ParenthesizedExpression = ParenthesizedExpression;
|
25
|
+
exports.RegExpLiteral = RegExpLiteral;
|
26
|
+
exports.RestElement = RestElement;
|
27
|
+
exports.SequenceExpression = SequenceExpression;
|
28
|
+
exports.StringLiteral = StringLiteral;
|
29
|
+
exports.TSAsExpression = TSAsExpression;
|
30
|
+
exports.TSNonNullExpression = TSNonNullExpression;
|
31
|
+
exports.TaggedTemplateExpression = TaggedTemplateExpression;
|
32
|
+
exports.TemplateLiteral = TemplateLiteral;
|
33
|
+
exports.TypeCastExpression = TypeCastExpression;
|
34
|
+
exports.UnaryExpression = UnaryExpression;
|
35
|
+
exports.UpdateExpression = UpdateExpression;
|
36
|
+
exports.VariableDeclarator = VariableDeclarator;
|
37
|
+
var _t = require("@babel/types");
|
38
|
+
var _infererReference = require("./inferer-reference.js");
|
39
|
+
var _util = require("./util.js");
|
40
|
+
const {
|
41
|
+
BOOLEAN_BINARY_OPERATORS,
|
42
|
+
BOOLEAN_UNARY_OPERATORS,
|
43
|
+
NUMBER_BINARY_OPERATORS,
|
44
|
+
NUMBER_UNARY_OPERATORS,
|
45
|
+
STRING_UNARY_OPERATORS,
|
46
|
+
anyTypeAnnotation,
|
47
|
+
arrayTypeAnnotation,
|
48
|
+
booleanTypeAnnotation,
|
49
|
+
buildMatchMemberExpression,
|
50
|
+
genericTypeAnnotation,
|
51
|
+
identifier,
|
52
|
+
nullLiteralTypeAnnotation,
|
53
|
+
numberTypeAnnotation,
|
54
|
+
stringTypeAnnotation,
|
55
|
+
tupleTypeAnnotation,
|
56
|
+
unionTypeAnnotation,
|
57
|
+
voidTypeAnnotation,
|
58
|
+
isIdentifier
|
59
|
+
} = _t;
|
51
60
|
function VariableDeclarator() {
|
52
|
-
|
53
|
-
|
54
|
-
const init = this.get("init");
|
55
|
-
let type = init.getTypeAnnotation();
|
56
|
-
|
57
|
-
if (type && type.type === "AnyTypeAnnotation") {
|
58
|
-
if (init.isCallExpression() && init.get("callee").isIdentifier({
|
59
|
-
name: "Array"
|
60
|
-
}) && !init.scope.hasBinding("Array", true)) {
|
61
|
-
type = ArrayExpression();
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
return type;
|
61
|
+
if (!this.get("id").isIdentifier()) return;
|
62
|
+
return this.get("init").getTypeAnnotation();
|
66
63
|
}
|
67
|
-
|
68
64
|
function TypeCastExpression(node) {
|
69
65
|
return node.typeAnnotation;
|
70
66
|
}
|
71
|
-
|
72
67
|
TypeCastExpression.validParent = true;
|
73
|
-
|
68
|
+
function TSAsExpression(node) {
|
69
|
+
return node.typeAnnotation;
|
70
|
+
}
|
71
|
+
TSAsExpression.validParent = true;
|
72
|
+
function TSNonNullExpression() {
|
73
|
+
return this.get("expression").getTypeAnnotation();
|
74
|
+
}
|
74
75
|
function NewExpression(node) {
|
75
|
-
if (
|
76
|
-
return
|
76
|
+
if (node.callee.type === "Identifier") {
|
77
|
+
return genericTypeAnnotation(node.callee);
|
77
78
|
}
|
78
79
|
}
|
79
|
-
|
80
80
|
function TemplateLiteral() {
|
81
|
-
return
|
81
|
+
return stringTypeAnnotation();
|
82
82
|
}
|
83
|
-
|
84
83
|
function UnaryExpression(node) {
|
85
84
|
const operator = node.operator;
|
86
|
-
|
87
85
|
if (operator === "void") {
|
88
|
-
return
|
89
|
-
} else if (
|
90
|
-
return
|
91
|
-
} else if (
|
92
|
-
return
|
93
|
-
} else if (
|
94
|
-
return
|
86
|
+
return voidTypeAnnotation();
|
87
|
+
} else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
88
|
+
return numberTypeAnnotation();
|
89
|
+
} else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
90
|
+
return stringTypeAnnotation();
|
91
|
+
} else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
92
|
+
return booleanTypeAnnotation();
|
95
93
|
}
|
96
94
|
}
|
97
|
-
|
98
95
|
function BinaryExpression(node) {
|
99
96
|
const operator = node.operator;
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
return t().booleanTypeAnnotation();
|
97
|
+
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
98
|
+
return numberTypeAnnotation();
|
99
|
+
} else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
100
|
+
return booleanTypeAnnotation();
|
105
101
|
} else if (operator === "+") {
|
106
102
|
const right = this.get("right");
|
107
103
|
const left = this.get("left");
|
108
|
-
|
109
104
|
if (left.isBaseType("number") && right.isBaseType("number")) {
|
110
|
-
return
|
105
|
+
return numberTypeAnnotation();
|
111
106
|
} else if (left.isBaseType("string") || right.isBaseType("string")) {
|
112
|
-
return
|
107
|
+
return stringTypeAnnotation();
|
113
108
|
}
|
114
|
-
|
115
|
-
return t().unionTypeAnnotation([t().stringTypeAnnotation(), t().numberTypeAnnotation()]);
|
109
|
+
return unionTypeAnnotation([stringTypeAnnotation(), numberTypeAnnotation()]);
|
116
110
|
}
|
117
111
|
}
|
118
|
-
|
119
112
|
function LogicalExpression() {
|
120
|
-
|
113
|
+
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
114
|
+
return (0, _util.createUnionType)(argumentTypes);
|
121
115
|
}
|
122
|
-
|
123
116
|
function ConditionalExpression() {
|
124
|
-
|
117
|
+
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
118
|
+
return (0, _util.createUnionType)(argumentTypes);
|
125
119
|
}
|
126
|
-
|
127
120
|
function SequenceExpression() {
|
128
121
|
return this.get("expressions").pop().getTypeAnnotation();
|
129
122
|
}
|
130
|
-
|
123
|
+
function ParenthesizedExpression() {
|
124
|
+
return this.get("expression").getTypeAnnotation();
|
125
|
+
}
|
131
126
|
function AssignmentExpression() {
|
132
127
|
return this.get("right").getTypeAnnotation();
|
133
128
|
}
|
134
|
-
|
135
129
|
function UpdateExpression(node) {
|
136
130
|
const operator = node.operator;
|
137
|
-
|
138
131
|
if (operator === "++" || operator === "--") {
|
139
|
-
return
|
132
|
+
return numberTypeAnnotation();
|
140
133
|
}
|
141
134
|
}
|
142
|
-
|
143
135
|
function StringLiteral() {
|
144
|
-
return
|
136
|
+
return stringTypeAnnotation();
|
145
137
|
}
|
146
|
-
|
147
138
|
function NumericLiteral() {
|
148
|
-
return
|
139
|
+
return numberTypeAnnotation();
|
149
140
|
}
|
150
|
-
|
151
141
|
function BooleanLiteral() {
|
152
|
-
return
|
142
|
+
return booleanTypeAnnotation();
|
153
143
|
}
|
154
|
-
|
155
144
|
function NullLiteral() {
|
156
|
-
return
|
145
|
+
return nullLiteralTypeAnnotation();
|
157
146
|
}
|
158
|
-
|
159
147
|
function RegExpLiteral() {
|
160
|
-
return
|
148
|
+
return genericTypeAnnotation(identifier("RegExp"));
|
161
149
|
}
|
162
|
-
|
163
150
|
function ObjectExpression() {
|
164
|
-
return
|
151
|
+
return genericTypeAnnotation(identifier("Object"));
|
165
152
|
}
|
166
|
-
|
167
153
|
function ArrayExpression() {
|
168
|
-
return
|
154
|
+
return genericTypeAnnotation(identifier("Array"));
|
169
155
|
}
|
170
|
-
|
171
156
|
function RestElement() {
|
172
157
|
return ArrayExpression();
|
173
158
|
}
|
174
|
-
|
175
159
|
RestElement.validParent = true;
|
176
|
-
|
177
160
|
function Func() {
|
178
|
-
return
|
161
|
+
return genericTypeAnnotation(identifier("Function"));
|
179
162
|
}
|
180
|
-
|
181
|
-
const
|
182
|
-
const
|
183
|
-
const
|
184
|
-
const isObjectEntries = t().buildMatchMemberExpression("Object.entries");
|
185
|
-
|
163
|
+
const isArrayFrom = buildMatchMemberExpression("Array.from");
|
164
|
+
const isObjectKeys = buildMatchMemberExpression("Object.keys");
|
165
|
+
const isObjectValues = buildMatchMemberExpression("Object.values");
|
166
|
+
const isObjectEntries = buildMatchMemberExpression("Object.entries");
|
186
167
|
function CallExpression() {
|
187
168
|
const {
|
188
169
|
callee
|
189
170
|
} = this.node;
|
190
|
-
|
191
171
|
if (isObjectKeys(callee)) {
|
192
|
-
return
|
193
|
-
} else if (isArrayFrom(callee) || isObjectValues(callee)
|
194
|
-
|
172
|
+
return arrayTypeAnnotation(stringTypeAnnotation());
|
173
|
+
} else if (isArrayFrom(callee) || isObjectValues(callee) || isIdentifier(callee, {
|
174
|
+
name: "Array"
|
175
|
+
})) {
|
176
|
+
return arrayTypeAnnotation(anyTypeAnnotation());
|
195
177
|
} else if (isObjectEntries(callee)) {
|
196
|
-
return
|
178
|
+
return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]));
|
197
179
|
}
|
198
|
-
|
199
180
|
return resolveCall(this.get("callee"));
|
200
181
|
}
|
201
|
-
|
202
182
|
function TaggedTemplateExpression() {
|
203
183
|
return resolveCall(this.get("tag"));
|
204
184
|
}
|
205
|
-
|
206
185
|
function resolveCall(callee) {
|
207
186
|
callee = callee.resolve();
|
208
|
-
|
209
187
|
if (callee.isFunction()) {
|
210
|
-
|
211
|
-
|
212
|
-
|
188
|
+
const {
|
189
|
+
node
|
190
|
+
} = callee;
|
191
|
+
if (node.async) {
|
192
|
+
if (node.generator) {
|
193
|
+
return genericTypeAnnotation(identifier("AsyncIterator"));
|
213
194
|
} else {
|
214
|
-
return
|
195
|
+
return genericTypeAnnotation(identifier("Promise"));
|
215
196
|
}
|
216
197
|
} else {
|
217
|
-
if (
|
198
|
+
if (node.generator) {
|
199
|
+
return genericTypeAnnotation(identifier("Iterator"));
|
200
|
+
} else if (callee.node.returnType) {
|
218
201
|
return callee.node.returnType;
|
219
202
|
} else {}
|
220
203
|
}
|
221
204
|
}
|
222
|
-
}
|
205
|
+
}
|
206
|
+
|
207
|
+
//# sourceMappingURL=inferers.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_t","require","_infererReference","_util","BOOLEAN_BINARY_OPERATORS","BOOLEAN_UNARY_OPERATORS","NUMBER_BINARY_OPERATORS","NUMBER_UNARY_OPERATORS","STRING_UNARY_OPERATORS","anyTypeAnnotation","arrayTypeAnnotation","booleanTypeAnnotation","buildMatchMemberExpression","genericTypeAnnotation","identifier","nullLiteralTypeAnnotation","numberTypeAnnotation","stringTypeAnnotation","tupleTypeAnnotation","unionTypeAnnotation","voidTypeAnnotation","isIdentifier","VariableDeclarator","get","getTypeAnnotation","TypeCastExpression","node","typeAnnotation","validParent","TSAsExpression","TSNonNullExpression","NewExpression","callee","type","TemplateLiteral","UnaryExpression","operator","indexOf","BinaryExpression","right","left","isBaseType","LogicalExpression","argumentTypes","createUnionType","ConditionalExpression","SequenceExpression","pop","ParenthesizedExpression","AssignmentExpression","UpdateExpression","StringLiteral","NumericLiteral","BooleanLiteral","NullLiteral","RegExpLiteral","ObjectExpression","ArrayExpression","RestElement","Func","isArrayFrom","isObjectKeys","isObjectValues","isObjectEntries","CallExpression","name","resolveCall","TaggedTemplateExpression","resolve","isFunction","async","generator","returnType"],"sources":["../../../src/path/inference/inferers.ts"],"sourcesContent":["import {\n BOOLEAN_BINARY_OPERATORS,\n BOOLEAN_UNARY_OPERATORS,\n NUMBER_BINARY_OPERATORS,\n NUMBER_UNARY_OPERATORS,\n STRING_UNARY_OPERATORS,\n anyTypeAnnotation,\n arrayTypeAnnotation,\n booleanTypeAnnotation,\n buildMatchMemberExpression,\n genericTypeAnnotation,\n identifier,\n nullLiteralTypeAnnotation,\n numberTypeAnnotation,\n stringTypeAnnotation,\n tupleTypeAnnotation,\n unionTypeAnnotation,\n voidTypeAnnotation,\n isIdentifier,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport { default as Identifier } from \"./inferer-reference.ts\";\n\nimport { createUnionType } from \"./util.ts\";\nimport type NodePath from \"../index.ts\";\n\nexport function VariableDeclarator(this: NodePath<t.VariableDeclarator>) {\n if (!this.get(\"id\").isIdentifier()) return;\n return this.get(\"init\").getTypeAnnotation();\n}\n\nexport function TypeCastExpression(node: t.TypeCastExpression) {\n return node.typeAnnotation;\n}\n\nTypeCastExpression.validParent = true;\n\nexport function TSAsExpression(node: t.TSAsExpression) {\n return node.typeAnnotation;\n}\n\nTSAsExpression.validParent = true;\n\nexport function TSNonNullExpression(this: NodePath<t.TSNonNullExpression>) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function NewExpression(\n this: NodePath<t.NewExpression>,\n node: t.NewExpression,\n) {\n if (node.callee.type === \"Identifier\") {\n // only resolve identifier callee\n return genericTypeAnnotation(node.callee);\n }\n}\n\nexport function TemplateLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function UnaryExpression(node: t.UnaryExpression) {\n const operator = node.operator;\n\n if (operator === \"void\") {\n return voidTypeAnnotation();\n } else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return stringTypeAnnotation();\n } else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n }\n}\n\nexport function BinaryExpression(\n this: NodePath<t.BinaryExpression>,\n node: t.BinaryExpression,\n) {\n const operator = node.operator;\n\n if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n } else if (operator === \"+\") {\n const right = this.get(\"right\");\n const left = this.get(\"left\");\n\n if (left.isBaseType(\"number\") && right.isBaseType(\"number\")) {\n // both numbers so this will be a number\n return numberTypeAnnotation();\n } else if (left.isBaseType(\"string\") || right.isBaseType(\"string\")) {\n // one is a string so the result will be a string\n return stringTypeAnnotation();\n }\n\n // unsure if left and right are strings or numbers so stay on the safe side\n return unionTypeAnnotation([\n stringTypeAnnotation(),\n numberTypeAnnotation(),\n ]);\n }\n}\n\nexport function LogicalExpression(this: NodePath<t.LogicalExpression>) {\n const argumentTypes = [\n this.get(\"left\").getTypeAnnotation(),\n this.get(\"right\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function ConditionalExpression(this: NodePath<t.ConditionalExpression>) {\n const argumentTypes = [\n this.get(\"consequent\").getTypeAnnotation(),\n this.get(\"alternate\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function SequenceExpression(this: NodePath<t.SequenceExpression>) {\n return this.get(\"expressions\").pop().getTypeAnnotation();\n}\n\nexport function ParenthesizedExpression(\n this: NodePath<t.ParenthesizedExpression>,\n) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function AssignmentExpression(this: NodePath<t.AssignmentExpression>) {\n return this.get(\"right\").getTypeAnnotation();\n}\n\nexport function UpdateExpression(\n this: NodePath<t.UpdateExpression>,\n node: t.UpdateExpression,\n) {\n const operator = node.operator;\n if (operator === \"++\" || operator === \"--\") {\n return numberTypeAnnotation();\n }\n}\n\nexport function StringLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function NumericLiteral() {\n return numberTypeAnnotation();\n}\n\nexport function BooleanLiteral() {\n return booleanTypeAnnotation();\n}\n\nexport function NullLiteral() {\n return nullLiteralTypeAnnotation();\n}\n\nexport function RegExpLiteral() {\n return genericTypeAnnotation(identifier(\"RegExp\"));\n}\n\nexport function ObjectExpression() {\n return genericTypeAnnotation(identifier(\"Object\"));\n}\n\nexport function ArrayExpression() {\n return genericTypeAnnotation(identifier(\"Array\"));\n}\n\nexport function RestElement() {\n return ArrayExpression();\n}\n\nRestElement.validParent = true;\n\nfunction Func() {\n return genericTypeAnnotation(identifier(\"Function\"));\n}\n\nexport {\n Func as FunctionExpression,\n Func as ArrowFunctionExpression,\n Func as FunctionDeclaration,\n Func as ClassExpression,\n Func as ClassDeclaration,\n};\n\nconst isArrayFrom = buildMatchMemberExpression(\"Array.from\");\nconst isObjectKeys = buildMatchMemberExpression(\"Object.keys\");\nconst isObjectValues = buildMatchMemberExpression(\"Object.values\");\nconst isObjectEntries = buildMatchMemberExpression(\"Object.entries\");\nexport function CallExpression(this: NodePath<t.CallExpression>) {\n const { callee } = this.node;\n if (isObjectKeys(callee)) {\n return arrayTypeAnnotation(stringTypeAnnotation());\n } else if (\n isArrayFrom(callee) ||\n isObjectValues(callee) ||\n // Detect \"var foo = Array()\" calls so we can optimize for arrays vs iterables.\n isIdentifier(callee, { name: \"Array\" })\n ) {\n return arrayTypeAnnotation(anyTypeAnnotation());\n } else if (isObjectEntries(callee)) {\n return arrayTypeAnnotation(\n tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]),\n );\n }\n\n return resolveCall(this.get(\"callee\"));\n}\n\nexport function TaggedTemplateExpression(\n this: NodePath<t.TaggedTemplateExpression>,\n) {\n return resolveCall(this.get(\"tag\"));\n}\n\nfunction resolveCall(callee: NodePath) {\n callee = callee.resolve();\n\n if (callee.isFunction()) {\n const { node } = callee;\n if (node.async) {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"AsyncIterator\"));\n } else {\n return genericTypeAnnotation(identifier(\"Promise\"));\n }\n } else {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"Iterator\"));\n } else if (callee.node.returnType) {\n return callee.node.returnType;\n } else {\n // todo: get union type of all return arguments\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,EAAA,GAAAC,OAAA;AAsBA,IAAAC,iBAAA,GAAAD,OAAA;AAEA,IAAAE,KAAA,GAAAF,OAAA;AAA4C;EAvB1CG,wBAAwB;EACxBC,uBAAuB;EACvBC,uBAAuB;EACvBC,sBAAsB;EACtBC,sBAAsB;EACtBC,iBAAiB;EACjBC,mBAAmB;EACnBC,qBAAqB;EACrBC,0BAA0B;EAC1BC,qBAAqB;EACrBC,UAAU;EACVC,yBAAyB;EACzBC,oBAAoB;EACpBC,oBAAoB;EACpBC,mBAAmB;EACnBC,mBAAmB;EACnBC,kBAAkB;EAClBC;AAAY,IAAArB,EAAA;AASP,SAASsB,kBAAkBA,CAAA,EAAuC;EACvE,IAAI,CAAC,IAAI,CAACC,GAAG,CAAC,IAAI,CAAC,CAACF,YAAY,CAAC,CAAC,EAAE;EACpC,OAAO,IAAI,CAACE,GAAG,CAAC,MAAM,CAAC,CAACC,iBAAiB,CAAC,CAAC;AAC7C;AAEO,SAASC,kBAAkBA,CAACC,IAA0B,EAAE;EAC7D,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAF,kBAAkB,CAACG,WAAW,GAA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oC,OAAO,CAAC,CAAC;EAEzB,IAAIpC,MAAM,CAACqC,UAAU,CAAC,CAAC,EAAE;IACvB,MAAM;MAAE3C;IAAK,CAAC,GAAGM,MAAM;IACvB,IAAIN,IAAI,CAAC4C,KAAK,EAAE;MACd,IAAI5C,IAAI,CAAC6C,SAAS,EAAE;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,eAAe,CAAC,CAAC;MAC3D,CAAC,MAAM;QACL,OAAOD,qBAAqB,CAACC,UAAU,CAAC,SAAS,CAAC,CAAC;MACrD;IACF,CAAC,MAAM;MACL,IAAIY,IAAI,CAAC6C,SAAS,EAAE;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,UAAU,CAAC,CAAC;MACtD,CAAC,MAAM,IAAIkB,MAAM,CAACN,IAAI,CAAC8C,UAAU,EAAE;QACjC,OAAOxC,MAAM,CAACN,IAAI,CAAC8C,UAAU;MAC/B,CAAC,MAAM,CAEP;IACF;EACF;AACF"}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createUnionType = createUnionType;
|
7
|
+
var _t = require("@babel/types");
|
8
|
+
const {
|
9
|
+
createFlowUnionType,
|
10
|
+
createTSUnionType,
|
11
|
+
createUnionTypeAnnotation,
|
12
|
+
isFlowType,
|
13
|
+
isTSType
|
14
|
+
} = _t;
|
15
|
+
function createUnionType(types) {
|
16
|
+
{
|
17
|
+
if (isFlowType(types[0])) {
|
18
|
+
if (createFlowUnionType) {
|
19
|
+
return createFlowUnionType(types);
|
20
|
+
}
|
21
|
+
return createUnionTypeAnnotation(types);
|
22
|
+
} else {
|
23
|
+
if (createTSUnionType) {
|
24
|
+
return createTSUnionType(types);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
//# sourceMappingURL=util.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_t","require","createFlowUnionType","createTSUnionType","createUnionTypeAnnotation","isFlowType","isTSType","createUnionType","types"],"sources":["../../../src/path/inference/util.ts"],"sourcesContent":["import {\n createFlowUnionType,\n createTSUnionType,\n createUnionTypeAnnotation,\n isFlowType,\n isTSType,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport function createUnionType(\n types: Array<t.FlowType | t.TSType>,\n): t.FlowType | t.TSType {\n if (process.env.BABEL_8_BREAKING) {\n if (isFlowType(types[0])) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n if (isTSType(types[0])) {\n return createTSUnionType(types as t.TSType[]);\n }\n } else {\n if (isFlowType(types[0])) {\n if (createFlowUnionType) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n\n return createUnionTypeAnnotation(types as t.FlowType[]);\n } else {\n if (createTSUnionType) {\n return createTSUnionType(types as t.TSType[]);\n }\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,EAAA,GAAAC,OAAA;AAMsB;EALpBC,mBAAmB;EACnBC,iBAAiB;EACjBC,yBAAyB;EACzBC,UAAU;EACVC;AAAQ,IAAAN,EAAA;AAIH,SAASO,eAAeA,CAC7BC,KAAmC,EACZ;EAQhB;IACL,IAAIH,UAAU,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;MACxB,IAAIN,mBAAmB,EAAE;QACvB,OAAOA,mBAAmB,CAACM,KAAqB,CAAC;MACnD;MAEA,OAAOJ,yBAAyB,CAACI,KAAqB,CAAC;IACzD,CAAC,MAAM;MACL,IAAIL,iBAAiB,EAAE;QACrB,OAAOA,iBAAiB,CAACK,KAAmB,CAAC;MAC/C;IACF;EACF;AACF"}
|