@tsplc/compiler 0.2.17 → 0.2.19
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/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -1,36 +1,28 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
import { primitiveTypes } from "@tsplc/compiler/ir";
|
|
2
3
|
import type { CapturedValue } from "./graph.ts";
|
|
4
|
+
import { ModuleImports } from "./imports.ts";
|
|
3
5
|
import type { Model } from "./model.ts";
|
|
4
6
|
|
|
5
7
|
/** IEC names a program can write as types; they keep their width rather than becoming DINT. */
|
|
6
|
-
const iecNames = new Set(
|
|
7
|
-
"BOOL",
|
|
8
|
-
"SINT",
|
|
9
|
-
"INT",
|
|
10
|
-
"DINT",
|
|
11
|
-
"LINT",
|
|
12
|
-
"USINT",
|
|
13
|
-
"UINT",
|
|
14
|
-
"UDINT",
|
|
15
|
-
"ULINT",
|
|
16
|
-
"BYTE",
|
|
17
|
-
"WORD",
|
|
18
|
-
"DWORD",
|
|
19
|
-
"LWORD",
|
|
20
|
-
"REAL",
|
|
21
|
-
"LREAL",
|
|
22
|
-
"TIME",
|
|
23
|
-
"DATE",
|
|
24
|
-
"TIME_OF_DAY",
|
|
25
|
-
"DATE_AND_TIME",
|
|
26
|
-
]);
|
|
8
|
+
const iecNames = new Set(primitiveTypes.map((type) => type.id));
|
|
27
9
|
|
|
28
10
|
/** A union of string literals: a PLC enum. */
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
11
|
+
interface LiteralEnum {
|
|
12
|
+
name: string;
|
|
13
|
+
members: string[];
|
|
14
|
+
/** The union also allows undefined, stored as this extra member. */
|
|
15
|
+
optional: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A union of object types told apart by a string-literal field, the tag. */
|
|
19
|
+
interface TaggedUnion {
|
|
20
|
+
tag: string;
|
|
21
|
+
variants: ts.Type[];
|
|
22
|
+
/** Each variant's tag, in the order of the variants. */
|
|
23
|
+
tags: string[];
|
|
24
|
+
/** The union also allows undefined. */
|
|
25
|
+
optional: boolean;
|
|
34
26
|
}
|
|
35
27
|
|
|
36
28
|
/**
|
|
@@ -39,370 +31,585 @@ export interface LiteralEnum {
|
|
|
39
31
|
* optional record, and arrays take their captured length. Declarations it needs collect in `declarations`.
|
|
40
32
|
*/
|
|
41
33
|
export class Layouts {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
34
|
+
readonly declarations = new Map<string, string>();
|
|
35
|
+
/** Enums the declarations need from kept modules. */
|
|
36
|
+
readonly imports = new ModuleImports();
|
|
37
|
+
/** Types the declarations need from kept modules. */
|
|
38
|
+
readonly typeImports = new ModuleImports();
|
|
39
|
+
private readonly enums = new Map<string, LiteralEnum>();
|
|
40
|
+
private readonly records = new Map<string, string>();
|
|
41
|
+
private readonly names = new Map<string, number>();
|
|
42
|
+
|
|
43
|
+
/** Exported types of kept modules by the type they declare, for types that lost their alias name. */
|
|
44
|
+
private readonly keptTypes = new Map<
|
|
45
|
+
ts.Type,
|
|
46
|
+
{ file: string; name: string }
|
|
47
|
+
>();
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
private readonly model: Model,
|
|
51
|
+
private readonly checker: ts.TypeChecker,
|
|
52
|
+
/** Modules the compiled program keeps, whose named types and enums it imports rather than copies. */
|
|
53
|
+
private readonly kept: (file: string) => boolean,
|
|
54
|
+
keptSources: readonly ts.SourceFile[] = [],
|
|
55
|
+
) {
|
|
56
|
+
for (const source of keptSources) {
|
|
57
|
+
for (const statement of source.statements) {
|
|
58
|
+
if (
|
|
59
|
+
(ts.isTypeAliasDeclaration(statement) ||
|
|
60
|
+
ts.isInterfaceDeclaration(statement)) &&
|
|
61
|
+
isExported(statement)
|
|
62
|
+
) {
|
|
63
|
+
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
64
|
+
if (symbol) {
|
|
65
|
+
this.keptTypes.set(checker.getDeclaredTypeOfSymbol(symbol), {
|
|
66
|
+
file: source.fileName,
|
|
67
|
+
name: statement.name.text,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private unique(base: string): string {
|
|
76
|
+
const count = this.names.get(base) ?? 0;
|
|
77
|
+
this.names.set(base, count + 1);
|
|
78
|
+
return count ? `${base}_${count + 1}` : base;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Each string-literal union's enum, with the string each member stands for. */
|
|
82
|
+
get literalEnums(): { name: string; members: Record<string, string> }[] {
|
|
83
|
+
return [...this.enums.values()].map((declared) => ({
|
|
84
|
+
name: declared.name,
|
|
85
|
+
members: Object.fromEntries(
|
|
86
|
+
declared.members.map((member) => [enumMember(member), member]),
|
|
87
|
+
),
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Generated enums, which modules import as values. */
|
|
92
|
+
get enumNames(): string[] {
|
|
93
|
+
return [...this.declarations]
|
|
94
|
+
.filter(([, text]) => text.startsWith("export enum"))
|
|
95
|
+
.map(([name]) => name);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The string literals of a union of them, without undefined. */
|
|
99
|
+
literals(
|
|
100
|
+
type: ts.Type,
|
|
101
|
+
): { members: string[]; optional: boolean } | undefined {
|
|
102
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
103
|
+
const defined = parts.filter(
|
|
104
|
+
(part) => !(part.flags & ts.TypeFlags.Undefined),
|
|
105
|
+
);
|
|
106
|
+
const members = defined.flatMap((part) =>
|
|
107
|
+
part.isStringLiteral() ? [part.value] : [],
|
|
108
|
+
);
|
|
109
|
+
if (!members.length || members.length !== defined.length) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return { members, optional: defined.length !== parts.length };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The enum a string-literal union compiles to. */
|
|
116
|
+
literalEnum(type: ts.Type): LiteralEnum | undefined {
|
|
117
|
+
const literals = this.literals(type);
|
|
118
|
+
if (!literals) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const key = [...literals.members].sort().join("|");
|
|
122
|
+
const existing = this.enums.get(key);
|
|
123
|
+
if (existing) {
|
|
124
|
+
existing.optional ||= literals.optional;
|
|
125
|
+
this.writeEnum(existing);
|
|
126
|
+
return existing;
|
|
127
|
+
}
|
|
128
|
+
const alias = (
|
|
129
|
+
type.aliasSymbol ?? this.checker.getNonNullableType(type).aliasSymbol
|
|
130
|
+
)?.name;
|
|
131
|
+
const declared: LiteralEnum = {
|
|
132
|
+
name: this.unique(alias ?? pascalCase(literals.members.join("_or_"))),
|
|
133
|
+
members: literals.members,
|
|
134
|
+
optional: literals.optional,
|
|
135
|
+
};
|
|
136
|
+
this.enums.set(key, declared);
|
|
137
|
+
this.writeEnum(declared);
|
|
138
|
+
return declared;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private writeEnum(declared: LiteralEnum): void {
|
|
142
|
+
const members = [
|
|
143
|
+
...(declared.optional ? ["Undefined = 0"] : []),
|
|
144
|
+
...declared.members.map(
|
|
145
|
+
(member, index) => `${enumMember(member)} = ${index + 1}`,
|
|
146
|
+
),
|
|
147
|
+
];
|
|
148
|
+
this.declarations.set(
|
|
149
|
+
declared.name,
|
|
150
|
+
`export enum ${declared.name} { ${members.join(", ")} }`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** `Enum.Member` text for a string literal of a literal union. */
|
|
155
|
+
member(declared: LiteralEnum, value: string | undefined): string {
|
|
156
|
+
return `${declared.name}.${value === undefined ? "Undefined" : enumMember(value)}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The object types of a union of them, without undefined, when it has a literal tag field. */
|
|
160
|
+
tagged(type: ts.Type): TaggedUnion | undefined {
|
|
161
|
+
if (!type.isUnion()) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const variants = type.types.filter(
|
|
165
|
+
(part) => !(part.flags & ts.TypeFlags.Undefined),
|
|
166
|
+
);
|
|
167
|
+
const [first, ...others] = variants;
|
|
168
|
+
if (
|
|
169
|
+
!first ||
|
|
170
|
+
!others.length ||
|
|
171
|
+
variants.some((part) => !(part.flags & ts.TypeFlags.Object))
|
|
172
|
+
) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const literal = (variant: ts.Type, name: string) => {
|
|
176
|
+
const own = variant.getProperty(name);
|
|
177
|
+
const value = own && this.checker.getTypeOfSymbol(own);
|
|
178
|
+
return value?.isStringLiteral() ? value.value : undefined;
|
|
179
|
+
};
|
|
180
|
+
for (const property of first.getProperties()) {
|
|
181
|
+
const tags = variants.map((variant) => literal(variant, property.name));
|
|
182
|
+
if (tags.every((tag): tag is string => tag !== undefined)) {
|
|
183
|
+
return {
|
|
184
|
+
tag: property.name,
|
|
185
|
+
variants,
|
|
186
|
+
tags,
|
|
187
|
+
optional: variants.length !== type.types.length,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The fields of a tagged union's record: the tag, then every variant's fields. */
|
|
195
|
+
taggedFields(tagged: TaggedUnion): string[] {
|
|
196
|
+
const names = [tagged.tag];
|
|
197
|
+
for (const variant of tagged.variants) {
|
|
198
|
+
for (const property of variant.getProperties()) {
|
|
199
|
+
if (!names.includes(property.name)) {
|
|
200
|
+
names.push(property.name);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return names;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The declared type of a field of a tagged union, from the first variant that has it. */
|
|
208
|
+
taggedFieldType(
|
|
209
|
+
declared: ts.Type,
|
|
210
|
+
tagged: TaggedUnion,
|
|
211
|
+
name: string,
|
|
212
|
+
): ts.Type {
|
|
213
|
+
for (const variant of tagged.variants) {
|
|
214
|
+
const property = variant.getProperty(name);
|
|
215
|
+
if (property) {
|
|
216
|
+
return this.checker.getTypeOfSymbol(property);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
throw new Error(
|
|
220
|
+
`${name} is in no variant of ${this.checker.typeToString(declared)}.`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The type of a tagged union's tag, once the union's record and tag enum are declared. */
|
|
225
|
+
tagType(declared: ts.Type, tagged: TaggedUnion): ts.Type {
|
|
226
|
+
this.text(declared);
|
|
227
|
+
const property = this.checker.getPropertyOfType(declared, tagged.tag);
|
|
228
|
+
return property ? this.checker.getTypeOfSymbol(property) : declared;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The name of the member of a TypeScript enum type that has `value`. */
|
|
232
|
+
enumMemberName(type: ts.Type, value: number): string | undefined {
|
|
233
|
+
return this.enumDeclaration(type)
|
|
234
|
+
?.members.find(
|
|
235
|
+
(member) => this.checker.getConstantValue(member) === value,
|
|
236
|
+
)
|
|
237
|
+
?.name.getText();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Whether `T | undefined` is stored as `{ has, value }`; a literal union has an Undefined member instead. */
|
|
241
|
+
isOptionalRecord(type: ts.Type): boolean {
|
|
242
|
+
if (
|
|
243
|
+
!type.isUnion() ||
|
|
244
|
+
!type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
|
|
245
|
+
) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return !this.literalEnum(type);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The dialect type text for `type`, holding `value` when it was captured. */
|
|
252
|
+
text(type: ts.Type, value?: CapturedValue, hint?: string): string {
|
|
253
|
+
const checker = this.checker;
|
|
254
|
+
const alias = type.aliasSymbol?.name;
|
|
255
|
+
if (alias && iecNames.has(alias)) {
|
|
256
|
+
return alias;
|
|
257
|
+
}
|
|
258
|
+
if (type.flags & ts.TypeFlags.BooleanLike) {
|
|
259
|
+
return "boolean";
|
|
260
|
+
}
|
|
261
|
+
const enumDeclaration = this.enumDeclaration(type);
|
|
262
|
+
if (enumDeclaration) {
|
|
263
|
+
return this.enumName(enumDeclaration);
|
|
264
|
+
}
|
|
265
|
+
if (type.flags & ts.TypeFlags.NumberLike) {
|
|
266
|
+
return "number";
|
|
267
|
+
}
|
|
268
|
+
if (type.flags & ts.TypeFlags.BigIntLike) {
|
|
269
|
+
return "LINT";
|
|
270
|
+
}
|
|
271
|
+
const literal = this.literalEnum(type);
|
|
272
|
+
if (literal) {
|
|
273
|
+
return literal.name;
|
|
274
|
+
}
|
|
275
|
+
const named = this.namedKept(type);
|
|
276
|
+
if (named) {
|
|
277
|
+
return named;
|
|
278
|
+
}
|
|
279
|
+
if (type.flags & ts.TypeFlags.StringLike) {
|
|
280
|
+
return "string";
|
|
281
|
+
}
|
|
282
|
+
const tagged = this.tagged(type);
|
|
283
|
+
if (tagged) {
|
|
284
|
+
return this.taggedRecord(type, tagged);
|
|
285
|
+
}
|
|
286
|
+
if (type.isUnion()) {
|
|
287
|
+
const defined = checker.getNonNullableType(type);
|
|
288
|
+
if (defined !== type) {
|
|
289
|
+
return this.optional(defined, value);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const element = this.model.elementType(type);
|
|
293
|
+
if (
|
|
294
|
+
element &&
|
|
295
|
+
(checker.isArrayType(type) ||
|
|
296
|
+
checker.isTupleType(type) ||
|
|
297
|
+
isReadonlyArray(type))
|
|
298
|
+
) {
|
|
299
|
+
const node = value && this.model.node(value);
|
|
300
|
+
if (!node || node.captured.kind !== "array") {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`${checker.typeToString(type)} needs a length: build it during setup.`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const items = node.captured.items;
|
|
306
|
+
return `FixedArray<${this.text(element, items[0] ?? this.sample(element))}, ${Math.max(items.length, 1)}>`;
|
|
307
|
+
}
|
|
308
|
+
const enumKey = this.enumRecord(type);
|
|
309
|
+
if (enumKey) {
|
|
310
|
+
return enumKey;
|
|
311
|
+
}
|
|
312
|
+
if (type.flags & ts.TypeFlags.Object) {
|
|
313
|
+
return this.record(type, value, hint);
|
|
314
|
+
}
|
|
315
|
+
throw new Error(`${checker.typeToString(type)} has no PLC layout.`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** The TypeScript enum a type is, or is a union of members of. */
|
|
319
|
+
enumDeclaration(type: ts.Type): ts.EnumDeclaration | undefined {
|
|
320
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
321
|
+
const [first] = parts;
|
|
322
|
+
if (!first || !parts.every((part) => part.flags & ts.TypeFlags.EnumLike)) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const symbol = first.symbol;
|
|
326
|
+
const declaration =
|
|
327
|
+
symbol?.declarations?.find(ts.isEnumDeclaration) ??
|
|
328
|
+
symbol?.valueDeclaration?.parent;
|
|
329
|
+
return declaration && ts.isEnumDeclaration(declaration)
|
|
330
|
+
? declaration
|
|
331
|
+
: undefined;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* The IEC type a declaration names, such as `WORD`: TypeScript keeps no alias name for an alias
|
|
336
|
+
* of a primitive, so the width is read from the declaration's annotation.
|
|
337
|
+
*/
|
|
338
|
+
iecName(declaration: ts.Node | undefined): string | undefined {
|
|
339
|
+
return iecTypeName(annotation(declaration));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** The IEC width an array declaration gives its elements, as `data: Array<BYTE>` does. */
|
|
343
|
+
elementIec(declaration: ts.Node | undefined): string | undefined {
|
|
344
|
+
const node = annotation(declaration);
|
|
345
|
+
if (node && ts.isArrayTypeNode(node)) {
|
|
346
|
+
return iecTypeName(node.elementType);
|
|
347
|
+
}
|
|
348
|
+
if (node && ts.isTypeReferenceNode(node)) {
|
|
349
|
+
return iecTypeName(node.typeArguments?.[0]);
|
|
350
|
+
}
|
|
351
|
+
return undefined;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** A declared field's layout, keeping the IEC widths its annotation names. */
|
|
355
|
+
declaredText(
|
|
356
|
+
declaration: ts.Node | undefined,
|
|
357
|
+
type: ts.Type,
|
|
358
|
+
value?: CapturedValue,
|
|
359
|
+
hint?: string,
|
|
360
|
+
): string {
|
|
361
|
+
const iec = this.iecName(declaration);
|
|
362
|
+
if (iec) {
|
|
363
|
+
return iec;
|
|
364
|
+
}
|
|
365
|
+
const elementName = iecTypeName(arrayElement(annotation(declaration)));
|
|
366
|
+
if (elementName) {
|
|
367
|
+
const captured = value && this.model.node(value);
|
|
368
|
+
if (captured?.captured.kind !== "array") {
|
|
369
|
+
throw new Error(
|
|
370
|
+
`${this.checker.typeToString(type)} needs a length: build it during setup.`,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
return `FixedArray<${elementName}, ${Math.max(captured.captured.items.length, 1)}>`;
|
|
374
|
+
}
|
|
375
|
+
return this.text(type, value, hint);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** A record field's layout, with the lengths setup gave its arrays. */
|
|
379
|
+
fieldText(property: ts.Symbol): string {
|
|
380
|
+
return this.declaredText(
|
|
381
|
+
property.valueDeclaration,
|
|
382
|
+
this.checker.getTypeOfSymbol(property),
|
|
383
|
+
this.fieldSample(property),
|
|
384
|
+
pascalCase(property.name),
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** A captured value of a field, from any captured record that has it. */
|
|
389
|
+
private fieldSample(property: ts.Symbol): CapturedValue | undefined {
|
|
390
|
+
for (const node of this.model.nodes) {
|
|
391
|
+
const captured = node.captured;
|
|
392
|
+
if (
|
|
393
|
+
(captured.kind === "object" || captured.kind === "instance") &&
|
|
394
|
+
node.type?.getProperty(property.name) === property
|
|
395
|
+
) {
|
|
396
|
+
return captured.fields[property.name];
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Some captured value of `type`, giving the lengths of an empty array's element. */
|
|
403
|
+
private sample(type: ts.Type): CapturedValue | undefined {
|
|
404
|
+
const node = this.model.nodes.find((candidate) => candidate.type === type);
|
|
405
|
+
return node && { kind: "node", id: node.id };
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** A named type from a module the program keeps, such as a service's procedure record. */
|
|
409
|
+
private namedKept(type: ts.Type): string | undefined {
|
|
410
|
+
const declared = this.keptTypes.get(type);
|
|
411
|
+
if (declared) {
|
|
412
|
+
return this.typeImports.add(declared.file, declared.name);
|
|
413
|
+
}
|
|
414
|
+
const symbol = type.aliasSymbol ?? type.getSymbol();
|
|
415
|
+
const declaration = symbol?.declarations?.[0];
|
|
416
|
+
if (!symbol || !declaration) {
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
if (
|
|
420
|
+
!ts.isTypeAliasDeclaration(declaration) &&
|
|
421
|
+
!ts.isInterfaceDeclaration(declaration)
|
|
422
|
+
) {
|
|
423
|
+
return undefined;
|
|
424
|
+
}
|
|
425
|
+
const file = declaration.getSourceFile().fileName;
|
|
426
|
+
if (!this.kept(file) || !isExported(declaration)) {
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
return this.typeImports.add(file, symbol.name);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private enumName(declaration: ts.EnumDeclaration): string {
|
|
433
|
+
const file = declaration.getSourceFile().fileName;
|
|
434
|
+
if (!this.kept(file)) {
|
|
435
|
+
throw new Error(
|
|
436
|
+
`${declaration.name.text}: declare PLC enums in a module of enums and constants, so the server and PLC share them.`,
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
return this.imports.add(file, declaration.name.text);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** `Record<Enum, T>`: an object type whose properties are exactly an enum's members. */
|
|
443
|
+
private enumRecord(type: ts.Type): string | undefined {
|
|
444
|
+
const record = recordTypes(type);
|
|
445
|
+
if (
|
|
446
|
+
!record ||
|
|
447
|
+
(!this.enumDeclaration(record.key) && !this.literalEnum(record.key))
|
|
448
|
+
) {
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
return `Record<${this.text(record.key)}, ${this.text(record.value, undefined)}>`;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** `{ has, value }` for `T | undefined`. */
|
|
455
|
+
private optional(defined: ts.Type, value: CapturedValue | undefined): string {
|
|
456
|
+
const inner = this.text(
|
|
457
|
+
defined,
|
|
458
|
+
value?.kind === "undefined" ? undefined : value,
|
|
459
|
+
);
|
|
460
|
+
const name = `Optional_${inner.replace(/\W+/g, "_")}`;
|
|
461
|
+
if (!this.declarations.has(name)) {
|
|
462
|
+
this.declarations.set(
|
|
463
|
+
name,
|
|
464
|
+
`export interface ${name} { has: boolean; value: ${inner}; }`,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
return name;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** One record for a tagged union: the tag as an enum, and every variant's fields. */
|
|
471
|
+
private taggedRecord(type: ts.Type, tagged: TaggedUnion): string {
|
|
472
|
+
const key = `tagged:${this.checker.typeToString(type)}`;
|
|
473
|
+
const known = this.records.get(key);
|
|
474
|
+
if (known) {
|
|
475
|
+
return known;
|
|
476
|
+
}
|
|
477
|
+
const name = this.unique(type.aliasSymbol?.name ?? "Variant");
|
|
478
|
+
this.records.set(key, name);
|
|
479
|
+
const tags = tagged.tags;
|
|
480
|
+
const tagEnum: LiteralEnum = {
|
|
481
|
+
name: `${name}Kind`,
|
|
482
|
+
members: tags,
|
|
483
|
+
optional: tagged.optional,
|
|
484
|
+
};
|
|
485
|
+
this.enums.set([...tags].sort().join("|"), tagEnum);
|
|
486
|
+
this.writeEnum(tagEnum);
|
|
487
|
+
const fields = new Map<string, string>();
|
|
488
|
+
for (const variant of tagged.variants) {
|
|
489
|
+
for (const property of variant.getProperties()) {
|
|
490
|
+
if (property.name === tagged.tag || fields.has(property.name)) {
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
fields.set(
|
|
494
|
+
property.name,
|
|
495
|
+
this.declaredText(
|
|
496
|
+
property.valueDeclaration,
|
|
497
|
+
this.checker.getTypeOfSymbol(property),
|
|
498
|
+
),
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
this.declarations.set(
|
|
503
|
+
name,
|
|
504
|
+
`export interface ${name} { ${tagged.tag}: ${tagEnum.name}; ${[...fields]
|
|
505
|
+
.map(([field, text]) => `${field}: ${text};`)
|
|
506
|
+
.join(" ")} }`,
|
|
507
|
+
);
|
|
508
|
+
return name;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** A record of an object type; captured arrays inside it can give it distinct variants. */
|
|
512
|
+
private record(
|
|
513
|
+
type: ts.Type,
|
|
514
|
+
value: CapturedValue | undefined,
|
|
515
|
+
hint?: string,
|
|
516
|
+
): string {
|
|
517
|
+
const node = value && this.model.node(value);
|
|
518
|
+
const fields = this.checker.getPropertiesOfType(type).map((property) => {
|
|
519
|
+
const captured =
|
|
520
|
+
node &&
|
|
521
|
+
(node.captured.kind === "object" || node.captured.kind === "instance")
|
|
522
|
+
? node.captured.fields[property.name]
|
|
523
|
+
: undefined;
|
|
524
|
+
return `${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured ?? this.fieldSample(property), pascalCase(property.name))};`;
|
|
525
|
+
});
|
|
526
|
+
const body = fields.join(" ");
|
|
527
|
+
const base = type.aliasSymbol?.name ?? type.getSymbol()?.name;
|
|
528
|
+
const key = `${base}:${body}`;
|
|
529
|
+
const known = this.records.get(key);
|
|
530
|
+
if (known) {
|
|
531
|
+
return known;
|
|
532
|
+
}
|
|
533
|
+
const name = this.unique(
|
|
534
|
+
base && base !== "__type" && base !== "__object"
|
|
535
|
+
? base
|
|
536
|
+
: (hint ?? "Fields"),
|
|
537
|
+
);
|
|
538
|
+
this.records.set(key, name);
|
|
539
|
+
this.declarations.set(name, `export interface ${name} { ${body} }`);
|
|
540
|
+
return name;
|
|
541
|
+
}
|
|
394
542
|
}
|
|
395
543
|
|
|
544
|
+
/** The key and value types of a `Record<K, V>`. */
|
|
545
|
+
export const recordTypes = (
|
|
546
|
+
type: ts.Type,
|
|
547
|
+
): { key: ts.Type; value: ts.Type } | undefined => {
|
|
548
|
+
const [key, value] = type.aliasTypeArguments ?? [];
|
|
549
|
+
return type.aliasSymbol?.name === "Record" && key && value
|
|
550
|
+
? { key, value }
|
|
551
|
+
: undefined;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/** A declaration's type annotation. Every declaration kind that can have one calls it `type`. */
|
|
555
|
+
const annotation = (
|
|
556
|
+
declaration: ts.Node | undefined,
|
|
557
|
+
): ts.TypeNode | undefined =>
|
|
558
|
+
declaration && (declaration as { type?: ts.TypeNode }).type;
|
|
559
|
+
|
|
560
|
+
/** The element type an annotation `T[]`, `Array<T>` or `ReadonlyArray<T>` names. */
|
|
561
|
+
const arrayElement = (
|
|
562
|
+
node: ts.TypeNode | undefined,
|
|
563
|
+
): ts.TypeNode | undefined => {
|
|
564
|
+
if (node && ts.isArrayTypeNode(node)) {
|
|
565
|
+
return node.elementType;
|
|
566
|
+
}
|
|
567
|
+
if (
|
|
568
|
+
node &&
|
|
569
|
+
ts.isTypeReferenceNode(node) &&
|
|
570
|
+
["Array", "ReadonlyArray"].includes(node.typeName.getText())
|
|
571
|
+
) {
|
|
572
|
+
return node.typeArguments?.[0];
|
|
573
|
+
}
|
|
574
|
+
return undefined;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
/** The IEC type an annotation names, such as `WORD`. */
|
|
578
|
+
const iecTypeName = (node: ts.TypeNode | undefined): string | undefined =>
|
|
579
|
+
node && ts.isTypeReferenceNode(node) && iecNames.has(node.typeName.getText())
|
|
580
|
+
? node.typeName.getText()
|
|
581
|
+
: undefined;
|
|
582
|
+
|
|
396
583
|
const isExported = (declaration: ts.Declaration): boolean =>
|
|
397
|
-
|
|
584
|
+
Boolean(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export);
|
|
398
585
|
|
|
399
|
-
const isReadonlyArray = (type: ts.Type): boolean =>
|
|
586
|
+
const isReadonlyArray = (type: ts.Type): boolean =>
|
|
587
|
+
type.getSymbol()?.name === "ReadonlyArray";
|
|
400
588
|
|
|
401
|
-
|
|
402
|
-
|
|
589
|
+
/** Text in PascalCase, as generated names use it: `bin_full` and `bin full` become `BinFull`. */
|
|
590
|
+
export const pascalCase = (text: string): string =>
|
|
591
|
+
text.replace(/(^|[^A-Za-z0-9]+)([A-Za-z0-9])/g, (_, __, letter: string) =>
|
|
592
|
+
letter.toUpperCase(),
|
|
593
|
+
);
|
|
594
|
+
|
|
595
|
+
/** `base`, or else the first of `base_2`, `base_3`… that `taken` does not hold. */
|
|
596
|
+
export const unusedName = (
|
|
597
|
+
base: string,
|
|
598
|
+
taken: (name: string) => boolean,
|
|
599
|
+
): string => {
|
|
600
|
+
if (!taken(base)) {
|
|
601
|
+
return base;
|
|
602
|
+
}
|
|
603
|
+
for (let index = 2; ; index++) {
|
|
604
|
+
const name = `${base}_${index}`;
|
|
605
|
+
if (!taken(name)) {
|
|
606
|
+
return name;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
403
610
|
|
|
404
611
|
/** A string literal as an enum member name. */
|
|
405
612
|
export const enumMember = (value: string): string => {
|
|
406
|
-
|
|
407
|
-
|
|
613
|
+
const name = pascalCase(value);
|
|
614
|
+
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
408
615
|
};
|