1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | const ts = require("./ts-ast");
|
14 | const { parseType, parseParamType } = require('doctrine/lib/typed.js');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function closureTypeToTypeScript(closureType, templateTypes = []) {
|
20 | if (!closureType) {
|
21 | return ts.anyType;
|
22 | }
|
23 | let ast;
|
24 | try {
|
25 | ast = parseType(closureType);
|
26 | }
|
27 | catch (_a) {
|
28 | return ts.anyType;
|
29 | }
|
30 | return convert(ast, templateTypes);
|
31 | }
|
32 | exports.closureTypeToTypeScript = closureTypeToTypeScript;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function closureParamToTypeScript(closureType, templateTypes = []) {
|
39 | if (!closureType) {
|
40 | return { type: ts.anyType, optional: false, rest: false };
|
41 | }
|
42 | let ast;
|
43 | try {
|
44 | ast = parseParamType(closureType);
|
45 | }
|
46 | catch (_a) {
|
47 | return {
|
48 | type: ts.anyType,
|
49 |
|
50 |
|
51 |
|
52 | optional: closureType.endsWith('='),
|
53 | rest: false,
|
54 | };
|
55 | }
|
56 |
|
57 | switch (ast.type) {
|
58 | case 'OptionalType':
|
59 | return {
|
60 | type: convert(ast.expression, templateTypes),
|
61 | optional: true,
|
62 | rest: false,
|
63 | };
|
64 | case 'RestType':
|
65 | return {
|
66 |
|
67 |
|
68 |
|
69 | type: new ts.ArrayType(convert(ast.expression, templateTypes)),
|
70 | optional: false,
|
71 | rest: true,
|
72 | };
|
73 | default:
|
74 | return {
|
75 | type: convert(ast, templateTypes),
|
76 | optional: false,
|
77 | rest: false,
|
78 | };
|
79 | }
|
80 | }
|
81 | exports.closureParamToTypeScript = closureParamToTypeScript;
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function convert(node, templateTypes) {
|
87 | let nullable;
|
88 | if (isNullable(node)) {
|
89 | nullable = true;
|
90 | node = node.expression;
|
91 | }
|
92 | else if (isNonNullable(node)) {
|
93 | nullable = false;
|
94 | node = node.expression;
|
95 | }
|
96 | else if (isName(node) && templateTypes.includes(node.name)) {
|
97 |
|
98 |
|
99 |
|
100 | nullable = false;
|
101 | }
|
102 | else {
|
103 | nullable = nullableByDefault(node);
|
104 | }
|
105 | let t;
|
106 | if (isParameterizedArray(node)) {
|
107 | t = convertArray(node, templateTypes);
|
108 | }
|
109 | else if (isUnion(node)) {
|
110 | t = convertUnion(node, templateTypes);
|
111 | }
|
112 | else if (isFunction(node)) {
|
113 | t = convertFunction(node, templateTypes);
|
114 | }
|
115 | else if (isBareArray(node)) {
|
116 | t = new ts.ArrayType(ts.anyType);
|
117 | }
|
118 | else if (isRecordType(node)) {
|
119 | t = convertRecord(node, templateTypes);
|
120 | }
|
121 | else if (isAllLiteral(node)) {
|
122 | t = ts.anyType;
|
123 | }
|
124 | else if (isNullableLiteral(node)) {
|
125 | t = ts.anyType;
|
126 | }
|
127 | else if (isNullLiteral(node)) {
|
128 | t = ts.nullType;
|
129 | }
|
130 | else if (isUndefinedLiteral(node)) {
|
131 | t = ts.undefinedType;
|
132 | }
|
133 | else if (isVoidLiteral(node)) {
|
134 | t = new ts.NameType('void');
|
135 | }
|
136 | else if (isName(node)) {
|
137 | if (node.name === 'Object') {
|
138 |
|
139 |
|
140 |
|
141 | t = new ts.NameType('object');
|
142 | }
|
143 | else {
|
144 | t = new ts.NameType(node.name);
|
145 | }
|
146 | }
|
147 | else {
|
148 | console.error('Unknown syntax.');
|
149 | return ts.anyType;
|
150 | }
|
151 | if (nullable) {
|
152 | t = new ts.UnionType([t, ts.nullType]);
|
153 | }
|
154 | return t;
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function nullableByDefault(node) {
|
161 | if (isName(node)) {
|
162 | switch (node.name) {
|
163 | case 'string':
|
164 | case 'number':
|
165 | case 'boolean':
|
166 | case 'void':
|
167 | return false;
|
168 | }
|
169 | return true;
|
170 | }
|
171 | return isParameterizedArray(node);
|
172 | }
|
173 | function convertArray(node, templateTypes) {
|
174 | const applications = node.applications;
|
175 | return new ts.ArrayType(applications.length === 1 ? convert(applications[0], templateTypes) :
|
176 | ts.anyType);
|
177 | }
|
178 | function convertUnion(node, templateTypes) {
|
179 | return new ts.UnionType(node.elements.map((element) => convert(element, templateTypes)));
|
180 | }
|
181 | function convertFunction(node, templateTypes) {
|
182 | const params = node.params.map((param, idx) => new ts.ParamType(
|
183 |
|
184 | 'p' + idx, convert(param, templateTypes)));
|
185 | if (node.new) {
|
186 | return new ts.ConstructorType(params,
|
187 |
|
188 |
|
189 |
|
190 | isName(node.this) ? new ts.NameType(node.this.name) : ts.anyType);
|
191 | }
|
192 | else {
|
193 | return new ts.FunctionType(params,
|
194 |
|
195 | node.result ? convert(node.result, templateTypes) : ts.anyType);
|
196 | }
|
197 | }
|
198 | function convertRecord(node, templateTypes) {
|
199 | const fields = [];
|
200 | for (const field of node.fields) {
|
201 | if (field.type !== 'FieldType') {
|
202 | return ts.anyType;
|
203 | }
|
204 | let fieldType = field.value ? convert(field.value, templateTypes) : ts.anyType;
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | let optional = false;
|
211 | if (fieldType.kind === 'union') {
|
212 | fieldType.members = fieldType.members.filter((member) => {
|
213 | if (member.kind === 'name' && member.name === 'undefined') {
|
214 | optional = true;
|
215 | return false;
|
216 | }
|
217 | return true;
|
218 | });
|
219 |
|
220 | fieldType.simplify();
|
221 | }
|
222 | fields.push(new ts.ParamType(field.key, fieldType, optional));
|
223 | }
|
224 | return new ts.RecordType(fields);
|
225 | }
|
226 | function isParameterizedArray(node) {
|
227 | return node.type === 'TypeApplication' &&
|
228 | node.expression.type === 'NameExpression' &&
|
229 | node.expression.name === 'Array';
|
230 | }
|
231 | function isBareArray(node) {
|
232 | return node.type === 'NameExpression' && node.name === 'Array';
|
233 | }
|
234 | function isUnion(node) {
|
235 | return node.type === 'UnionType';
|
236 | }
|
237 | function isFunction(node) {
|
238 | return node.type === 'FunctionType';
|
239 | }
|
240 | function isRecordType(node) {
|
241 | return node.type === 'RecordType';
|
242 | }
|
243 | function isNullable(node) {
|
244 | return node.type === 'NullableType';
|
245 | }
|
246 | function isNonNullable(node) {
|
247 | return node.type === 'NonNullableType';
|
248 | }
|
249 | function isAllLiteral(node) {
|
250 | return node.type === 'AllLiteral';
|
251 | }
|
252 | function isNullLiteral(node) {
|
253 | return node.type === 'NullLiteral';
|
254 | }
|
255 | function isNullableLiteral(node) {
|
256 | return node.type === 'NullableLiteral';
|
257 | }
|
258 | function isUndefinedLiteral(node) {
|
259 | return node.type === 'UndefinedLiteral';
|
260 | }
|
261 | function isVoidLiteral(node) {
|
262 | return node.type === 'VoidLiteral';
|
263 | }
|
264 | function isName(node) {
|
265 | return node.type === 'NameExpression';
|
266 | }
|
267 |
|
\ | No newline at end of file |