-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
856 lines (750 loc) · 30.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import { promises as fs } from 'fs'
import { dirname, join, resolve } from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import walk from 'klaw'
import ts from 'typescript'
import {
ConfigService,
fileUtil,
Service,
VanillaConfig,
type SymbolMap,
} from '@spyglassmc/core'
import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js'
import * as mcdoc from '@spyglassmc/mcdoc'
const cache_root = join(dirname(fileURLToPath(import.meta.url)), 'cache')
const project_path = resolve(process.cwd(), 'mcdoc')
const resolved_modules = new Map<string, string[]>()
const module_files = new Map<string, ts.TypeAliasDeclaration[]>()
await fileUtil.ensureDir(NodeJsExternals, project_path)
const service = new Service({
logger: {
log: (...log_args: any[]) => console.log(...log_args),
warn: (...log_args: any[]) => console.warn(...log_args),
error: (...log_args: any[]) => console.error(...log_args),
info: (...log_args: any[]) => console.info(...log_args),
},
project: {
cacheRoot: fileUtil.ensureEndingSlash(
pathToFileURL(cache_root).toString(),
),
defaultConfig: ConfigService.merge(VanillaConfig, {
env: { dependencies: [] },
}),
externals: NodeJsExternals,
initializers: [mcdoc.initialize],
projectRoot: fileUtil.ensureEndingSlash(
pathToFileURL(project_path).toString(),
),
},
})
await service.project.ready()
await service.project.cacheService.save()
const generated_path = 'types'
for await (const doc_file of walk(project_path)) {
if (doc_file.path.endsWith('.mcdoc')) {
const DocumentUri = pathToFileURL(doc_file.path).toString()
const doc_contents = await fs.readFile(doc_file.path, 'utf-8')
await service.project.onDidOpen(
DocumentUri,
'mcdoc',
0,
doc_contents,
)
await service.project.ensureClientManagedChecked(
DocumentUri,
)
await service.project.ensureBindingStarted(
DocumentUri,
)
}
}
function camel_case(name: string) {
const words = name.split('_')
if (words.length === 1) return name
return words[0] + words
.slice(1)
.map((word) => word[0].toUpperCase() + word.slice(1))
.join('')
}
const symbols = service.project.symbols.getVisibleSymbols('mcdoc')
const dispatchers = service.project.symbols.getVisibleSymbols('mcdoc/dispatcher')
const resources = dispatchers['minecraft:resource']!.members!
for await (const [resource_type, resource] of Object.entries(resources)) {
const local_path = resource.definition![0].uri.match(/mcdoc\/java\/(\w+)\/([\w\/]+)/)!
const pack_type = local_path[1] === 'data' ? 'datapack' : 'resourcepack'
const type_path = join(generated_path, pack_type, `${camel_case(resource_type)}.ts`)
const typeDef = (resource.data! as any).typeDef as mcdoc.McdocType
let file = `const original = ${JSON.stringify(typeDef, null, 4)}\n\n`
const resolved_resource_types = resolveRootTypes(resource_type.replaceAll('/', '_'), typeDef, `java/${local_path[1]}/${local_path[2]}`)
file += compileTypes(resolved_resource_types)
Bun.write(type_path, file)
}
function resolveRootTypes(name: string, type: mcdoc.McdocType, current_location: string) {
if (type.kind === 'struct') {
const result = createStruct(type, current_location)
const types = [ts.factory.createTypeAliasDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(name),
undefined,
result.type,
)] as (ts.TypeAliasDeclaration | ts.ImportDeclaration | ts.EnumDeclaration)[]
if (result.modules.length > 0) {
types.push(...result.modules)
}
if (result.imports.length > 0) {
types.unshift(...result.imports)
}
return types
} else {
return []
}
}
function resolveTypes(name: string, type: mcdoc.McdocType, current_location: string) {
if (type.kind === 'struct') {
const result = createStruct(type, current_location)
return {
type: ts.factory.createTypeAliasDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier(name),
undefined,
result.type,
),
imports: result.imports,
modules: result.modules
}
} else {
return {
type: undefined,
imports: [],
modules: []
}
}
}
function compileTypes(nodes: any[]) {
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const resultFile = ts.createSourceFile(
"code.ts",
"",
ts.ScriptTarget.Latest,
false,
ts.ScriptKind.TS
);
return printer.printList(ts.ListFormat.MultiLine, nodes as unknown as ts.NodeArray<ts.Node>, resultFile)
}
type StructMember = {
type: ts.IndexSignatureDeclaration | ts.PropertySignature | ts.Identifier;
add_import?: ts.ImportDeclaration;
module?: ts.TypeAliasDeclaration;
}
let dispatcher_count = 0
function createStruct(typeDef: mcdoc.StructType, current_location: string) {
const anyFallback = ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
const member_types: (ts.IndexSignatureDeclaration | ts.PropertySignature | ts.Identifier)[] = []
const imports: ts.ImportDeclaration[] = []
const modules: (ts.TypeAliasDeclaration | ts.EnumDeclaration)[] = []
for (const field of typeDef.fields) {
if (field.attributes !== undefined && field.attributes.includes((attr: mcdoc.Attribute) => attr.name === 'until')) {
continue
}
if (field.kind === 'pair') {
const resolvePair: () => StructMember = () => {
if (typeof field.key === 'string') {
let value
if (field.type.kind === 'struct') {
const struct = createStruct(field.type, current_location)
value = struct.type
if (struct.imports.length > 0) {
imports.push(...struct.imports)
}
if (struct.modules.length > 0) {
modules.push(...struct.modules)
}
} else {
value = anyFallback
}
return {
type: ts.factory.createPropertySignature(
undefined,
bindKey(field.key),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
value,
)
}
} else if (field.key.kind === 'string') {
if (field.key.attributes && field.key.attributes.includes((attr: mcdoc.Attribute) => attr.name === 'id')) {
console.log('hello???')
let value
if (field.type.kind === 'struct') {
const struct = createStruct(field.type, current_location)
value = struct.type
if (struct.imports.length > 0) {
imports.push(...struct.imports)
}
if (struct.modules.length > 0) {
modules.push(...struct.modules)
}
} else {
value = anyFallback
}
return {
type: ts.factory.createIndexSignature(
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier('id'),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
)
],
value,
)
}
}
let index = (value: ts.TypeNode) => ts.factory.createIndexSignature(
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier('key'),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
)
],
value,
)
if (field.type.kind === 'struct') {
const struct = createStruct(field.type, current_location)
if (struct.imports.length > 0) {
imports.push(...struct.imports)
}
if (struct.modules.length > 0) {
modules.push(...struct.modules)
}
return {
type: index(struct.type)
}
}
if (field.type.kind === 'reference') {
const resolved = resolveReference(field.type, current_location)
if (resolved.import) return {
add_import: resolved.import,
type: ts.factory.createIdentifier(resolved.name)
}
return {
type: ts.factory.createIdentifier(resolved.name),
...(resolved.module ? { module: resolved.module } : {})
}
}
return {
type: index(anyFallback)
}
} else if (field.key.kind === 'dispatcher') {
const value = resolveDispatcher(field.key, current_location)
if (value) {
if (value.import) {
imports.push(value.import)
}
if (typeof value.module !== 'boolean') {
modules.push(value.module)
}
const name = typeof value.name === 'string' ? value.name : `dispatcher_${dispatcher_count++}`
return {
type: ts.factory.createPropertySignature(
undefined,
bindKey('dispatcher'),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
ts.factory.createTypeReferenceNode(name),
)
}
}
}
const key = field.key as mcdoc.ReferenceType
let value = resolveValueType(field.type, current_location)!
if (value.imports?.length > 0) {
imports.push(...value.imports)
}
if (value.modules?.length > 0) {
modules.push(...value.modules)
}
return {
type: ts.factory.createIndexSignature(
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier(key.path!.split('::').pop()!),
undefined,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
)
],
value.type,
)
}
}
const pair = resolvePair()
member_types.push(pair.type)
if (pair.add_import) {
imports.push(pair.add_import)
}
if (pair.module) {
modules.push(pair.module)
}
}
if (field.kind === 'spread') {
continue
}
if (typeof field.key === 'string') {
if (field.type.kind === 'struct') {
const struct = createStruct(field.type, current_location)
member_types.push(ts.factory.createPropertySignature(
undefined,
bindKey(field.key),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
struct.type,
))
if (struct.imports.length > 0) {
imports.push(...struct.imports)
}
if (struct.modules.length > 0) {
modules.push(...struct.modules)
}
continue
}
if (field.type.kind === 'reference') {
const resolved = resolveReference(field.type, current_location)
member_types.push(ts.factory.createPropertySignature(
undefined,
bindKey(field.key),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
ts.factory.createTypeReferenceNode(resolved.name),
))
if (resolved.import) {
imports.push(resolved.import)
} else if (resolved.module) {
modules.push(resolved.module)
}
continue
}
if (field.type.kind === 'any') {
member_types.push(ts.factory.createPropertySignature(
undefined,
bindKey(field.key),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
anyFallback,
))
continue
}
}
}
/* @ts-ignore */
if (member_types.length === 0) console.log(typeDef.fields[0].key)
return {
/* @ts-ignore */ // TODO: Make sure this is okay
type: ts.factory.createTypeLiteralNode(member_types),
imports,
modules
}
}
let inline_enum_count = 0
/**
* TODO: Remember to add this!!!
* ```js
* type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift'
* type FixedLengthArray<T, L extends number, TObj = [T, ...Array<T>]> =
* Pick<TObj, Exclude<keyof TObj, ArrayLengthMutationKeys>>
* & {
* readonly length: L
* [ I : number ] : T
* [Symbol.iterator]: () => IterableIterator<T>
* }
* ```
*/
function resolveValueType(type: mcdoc.McdocType, current_location: string): {
type: ts.TypeLiteralNode | ts.TypeReferenceNode | ts.KeywordTypeNode | ts.UnionTypeNode | ts.LiteralTypeNode;
imports: ts.ImportDeclaration[] | never[];
modules: (ts.TypeAliasDeclaration | ts.EnumDeclaration)[] | never[];
} | undefined {
switch (type.kind) {
case 'struct':
return createStruct(type, current_location)
case 'reference':
const resolved = resolveReference(type, current_location)
return {
type: ts.factory.createTypeReferenceNode(resolved.name),
imports: resolved.import ? [resolved.import] : [],
modules: resolved.module ? [resolved.module] : []
}
case 'boolean':
return {
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword),
imports: [],
modules: []
}
case 'string':
return {
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
imports: [],
modules: []
}
case 'byte':
return {
type: ts.factory.createTypeReferenceNode('NBTByte'),
imports: [ bindImport('NBTByte', 'sandstone/variables/nbt') ],
modules: []
}
case 'short':
return {
type: ts.factory.createTypeReferenceNode('NBTShort'),
imports: [ bindImport('NBTShort', 'sandstone/variables/nbt') ],
modules: []
}
case 'int':
return {
type: ts.factory.createTypeReferenceNode('NBTInt'),
imports: [ bindImport('NBTInt', 'sandstone/variables/nbt') ],
modules: []
}
case 'long':
return {
type: ts.factory.createTypeReferenceNode('NBTLong'),
imports: [ bindImport('NBTLong', 'sandstone/variables/nbt') ],
modules: []
}
case 'float':
return {
type: ts.factory.createTypeReferenceNode('NBTFloat'),
imports: [ bindImport('NBTFloat', 'sandstone/variables/nbt') ],
modules: []
}
case 'double':
return {
type: ts.factory.createTypeReferenceNode('NBTDouble'),
imports: [ bindImport('NBTDouble', 'sandstone/variables/nbt') ],
modules: []
}
case 'list': {
const item = resolveValueType(type.item, current_location)!
if (type.lengthRange) {
const range = bindRangedInt(type.lengthRange)
if (range) {
return {
type: ts.factory.createTypeReferenceNode('FixedLengthArray', [item.type, {
...range,
_typeNodeBrand: ''
}]),
imports: [ bindImport('FixedLengthArray', 'sandstone/utils'), ...item.imports ],
modules: item.modules
}
}
}
return {
type: ts.factory.createTypeReferenceNode('Array', [item.type]),
imports: item.imports,
modules: item.modules
}
}
case 'byte_array': {
return {
type: ts.factory.createTypeReferenceNode('NBTByteArray'),
imports: [ bindImport('NBTByteArray', 'sandstone/variables/nbt') ],
modules: []
}
}
case 'int_array': {
return {
type: ts.factory.createTypeReferenceNode('NBTIntArray'),
imports: [ bindImport('NBTIntArray', 'sandstone/variables/nbt') ],
modules: []
}
}
case 'long_array': {
return {
type: ts.factory.createTypeReferenceNode('NBTLongArray'),
imports: [ bindImport('NBTLongArray', 'sandstone/variables/nbt') ],
modules: []
}
}
case 'union': {
const types = type.members.map((type) => resolveValueType(type, current_location)!)
return {
type: ts.factory.createUnionTypeNode(types.map((type) => type.type)),
imports: types.flatMap((type) => type.imports),
modules: types.flatMap((type) => type.modules)
}
}
case 'enum': {
const enum_identifier = ts.factory.createIdentifier(`inlineEnum${inline_enum_count++}`) // :husk:
return {
type: ts.factory.createTypeReferenceNode(enum_identifier),
imports: [],
modules: [ createEnum(enum_identifier, type) ]
}
}
case 'literal': {
switch (type.value.kind) {
case 'boolean':
return {
type: type.value.value ?
ts.factory.createLiteralTypeNode(ts.factory.createTrue())
: ts.factory.createLiteralTypeNode(ts.factory.createFalse()),
imports: [],
modules: []
}
case 'string':
return {
type: ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(type.value.value)),
imports: [],
modules: []
}
case 'byte': {
return {
type: ts.factory.createTypeReferenceNode('NBTByte', [{
...ts.factory.createNumericLiteral(type.value.value),
_typeNodeBrand: ''
}]),
imports: [
bindImport('NBTByte', 'sandstone/variables/nbt')
],
modules: []
}
}
case 'short': {
return {
type: ts.factory.createTypeReferenceNode('NBTShort', [{
...ts.factory.createNumericLiteral(type.value.value),
_typeNodeBrand: ''
}]),
imports: [
bindImport('NBTShort', 'sandstone/variables/nbt')
],
modules: []
}
}
case 'float': {
return {
type: ts.factory.createTypeReferenceNode('NBTFloat', [{
...ts.factory.createNumericLiteral(type.value.value),
_typeNodeBrand: ''
}]),
imports: [
bindImport('NBTFloat', 'sandstone/variables/nbt')
],
modules: []
}
}
default: // This is a hack, but it works. `double` is the default decimal SNBT value type, `int` is the default integer SNBT value type.
return {
type: ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(type.value.value)),
imports: [],
modules: []
}
}
}
default:
return {
type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
imports: [],
modules: []
}
}
}
function createEnum(name: ts.Identifier, _enum: mcdoc.EnumType) {
if (_enum.enumKind === 'string') {
return ts.factory.createEnumDeclaration(
undefined,
name,
_enum.values.map((value) => ts.factory.createEnumMember(value.identifier, ts.factory.createStringLiteral(value.value as string))),
)
}
return ts.factory.createEnumDeclaration(
undefined,
name,
_enum.values.map((value) => ts.factory.createEnumMember(value.identifier, ts.factory.createNumericLiteral(value.value as number))),
)
}
function bindRangedInt(range: mcdoc.NumericRange) {
const rangeString = mcdoc.NumericRange.toString(range)
if (/^[0-9]+$/.test(mcdoc.NumericRange.toString(range))) {
return ts.factory.createNumericLiteral(parseInt(rangeString))
}
if (range.min && range.max) {
if (range.max - range.min > 100) {
return
}
let values = []
if (mcdoc.RangeKind.isRightExclusive(range.kind)) {
range.max--
}
if (mcdoc.RangeKind.isLeftExclusive(range.kind)) {
range.min++
}
for (let i = range.min; i <= range.max; i++) {
values.push({
...ts.factory.createNumericLiteral(i),
_typeNodeBrand: ''
})
}
return ts.factory.createUnionTypeNode(values)
}
}
function bindKey(key: string | mcdoc.McdocType) {
if (typeof key === 'string') return ts.factory.createIdentifier(key)
return ts.factory.createComputedPropertyName(ts.factory.createIdentifier('string'))
}
function bindImport(module_name: string, module_path: string) {
return ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
true,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(module_name))
])
),
ts.factory.createStringLiteral(module_path)
)
}
function resolveReference(ref: mcdoc.ReferenceType, current_location: string): (
{ import: false, name: string, module: ts.TypeAliasDeclaration | false } |
{ import: ts.ImportDeclaration, name: string, module: false }
) {
const ref_path = ref.path!.split('::').slice(1)
const ref_name = ref_path.slice(-1)[0]
const location = ref_path.slice(0, -1).join('/')
const resolve_module = () => resolveTypes(ref_name, (symbols[ref.path!].data! as any).typeDef as mcdoc.McdocType, current_location)
if (location === current_location) {
const module = resolve_module()
return {
import: false,
name: ref_name,
module: module.type ?? false
}
}
const module_path = `${generated_path}/${location}/${ref_name}.ts`
const module_import = ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
true,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(ref_name))
])
),
ts.factory.createStringLiteral(module_path)
)
if (resolved_modules.has(location) && resolved_modules.get(location)!.includes(module_path)) return {
import: module_import,
name: ref_name,
module: false
}
const module = resolve_module()
if (!resolved_modules.has(location)) {
resolved_modules.set(location, [module_path])
if (module.type !== undefined) {
module_files.set(location, [module.type])
}
return {
import: module_import,
name: ref_name,
module: false
}
}
resolved_modules.get(location)!.push(module_path)
if (module.type !== undefined) {
if (!module_files.has(location)) {
module_files.set(location, [module.type])
} else {
module_files.get(location)!.push(module.type)
}
}
return {
import: module_import,
name: ref_name,
module: false
}
}
// Kill me. This is the reason I didn't want mcdoc/runtime to be a thing. All of this would ideally be resolved statically AoT by the language server.
function resolveDispatcher(dispatcher: mcdoc.DispatcherType, parent_struct: string, parent_key?: mcdoc.StructKeyNode) {
const index = dispatcher.parallelIndices[0]
if (index.kind === 'static') {
const module_import = ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
true,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(index.value))
])
),
ts.factory.createStringLiteral(generated_path + `/${dispatcher.registry.split(':')[1]}/${index.value}.ts`)
)
return {
import: module_import,
name: index.value,
module: false
}
} else {
if (typeof index.accessor[0] === 'object') {
if (index.accessor[0].keyword === 'key') {
const registry = dispatcher.registry.replaceAll('/', '_').split(':')[1]
const registry_import = ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(
true,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(registry))
])
),
ts.factory.createStringLiteral(generated_path +`/${dispatcher.registry.split(':')[1]}/index.ts`)
)
return {
import: registry_import,
name: false,
module: ts.factory.createTypeAliasDeclaration(
undefined,
ts.factory.createIdentifier(`${parent_struct}_${registry}`),
undefined,
ts.factory.createMappedTypeNode(
undefined,
ts.factory.createTypeParameterDeclaration(
undefined,
ts.factory.createIdentifier('Key'),
ts.factory.createTypeOperatorNode(
ts.SyntaxKind.KeyOfKeyword,
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(registry), undefined)
),
undefined
),
undefined,
ts.factory.createToken(ts.SyntaxKind.QuestionToken),
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(registry), [
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Key'), undefined)
]),
undefined
)
)
}
} else {
let parentCount = 1
let path: string[] = []
for (const accessor of index.accessor.slice(1)) {
if (typeof accessor === 'object') {
if (accessor.keyword === 'parent') {
parentCount++
} else {
throw new Error('Invalid accessor keyword')
}
}
path.push(accessor as string)
}
// Kill me
}
}
}
}