suppa-mcp-2 1.3.2 → 1.4.0
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/dist/server.js +1 -1
- package/package.json +1 -1
- package/skills/suppa-entity-code/SKILL.md +48 -7
- package/skills/suppa-entity-code/scripts/validate-suppa-schema.mjs +1527 -1284
- package/skills/suppa-entity-code/templates/entity.template.ts +29 -1
- package/skills/suppa-entity-code/templates/tabular-part.template.ts +99 -94
|
@@ -1,1284 +1,1527 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* validate-suppa-schema.mjs — static checker for Suppa v2 application schema
|
|
3
|
-
* code (entities, seeds, EntityModule registration). AST-only via the
|
|
4
|
-
* TypeScript compiler API; no type-checking, no module resolution.
|
|
5
|
-
*
|
|
6
|
-
* Every rule quotes the migrations doc
|
|
7
|
-
* (docs.modern-expo.com/backend/application-developing/migrations.html):
|
|
8
|
-
* these mistakes compile under tsc and only fail at migration time.
|
|
9
|
-
*
|
|
10
|
-
* Usage: node validate-suppa-schema.mjs <app-root-or-files...>
|
|
11
|
-
* Import: import { validateSchema } from "./validate-suppa-schema.mjs"
|
|
12
|
-
*/
|
|
13
|
-
import { createRequire } from "node:module";
|
|
14
|
-
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
15
|
-
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
16
|
-
import { pathToFileURL } from "node:url";
|
|
17
|
-
|
|
18
|
-
// ── Doc-derived catalogs (fallback when @suppa/sdk typings are absent) ──
|
|
19
|
-
export const SYSTEM_COLUMNS = ["id", "createdAt", "updatedAt", "deletedAt", "createdBy", "removedBy"];
|
|
20
|
-
export const ACCESS_METADATA_KEYS = ["$readAccess", "$updateAccess", "$instanceAccess"];
|
|
21
|
-
export const DOC_ENTITY_OPTIONS = ["name", "title", "comment", "representativeFieldName", "importKeyFields", "key", "icon", "type", "relationEntityName", "options"];
|
|
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
|
-
if (!ts.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return
|
|
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
|
-
if (ts.
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (
|
|
258
|
-
if (ts.
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
function
|
|
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
|
-
function
|
|
786
|
-
const
|
|
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
|
-
if (
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
if (
|
|
822
|
-
findings.push(finding(ts, sf,
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
}
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
const
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
for (const
|
|
1040
|
-
if (
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
if (
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
)
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
if (
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
.
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
}
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1
|
+
/**
|
|
2
|
+
* validate-suppa-schema.mjs — static checker for Suppa v2 application schema
|
|
3
|
+
* code (entities, seeds, EntityModule registration). AST-only via the
|
|
4
|
+
* TypeScript compiler API; no type-checking, no module resolution.
|
|
5
|
+
*
|
|
6
|
+
* Every rule quotes the migrations doc
|
|
7
|
+
* (docs.modern-expo.com/backend/application-developing/migrations.html):
|
|
8
|
+
* these mistakes compile under tsc and only fail at migration time.
|
|
9
|
+
*
|
|
10
|
+
* Usage: node validate-suppa-schema.mjs <app-root-or-files...>
|
|
11
|
+
* Import: import { validateSchema } from "./validate-suppa-schema.mjs"
|
|
12
|
+
*/
|
|
13
|
+
import { createRequire } from "node:module";
|
|
14
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
15
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
16
|
+
import { pathToFileURL } from "node:url";
|
|
17
|
+
|
|
18
|
+
// ── Doc-derived catalogs (fallback when @suppa/sdk typings are absent) ──
|
|
19
|
+
export const SYSTEM_COLUMNS = ["id", "createdAt", "updatedAt", "deletedAt", "createdBy", "removedBy"];
|
|
20
|
+
export const ACCESS_METADATA_KEYS = ["$readAccess", "$updateAccess", "$instanceAccess"];
|
|
21
|
+
export const DOC_ENTITY_OPTIONS = ["name", "title", "comment", "representativeFieldName", "importKeyFields", "key", "icon", "type", "relationEntityName", "options"];
|
|
22
|
+
/**
|
|
23
|
+
* Field options the platform already applies. Writing one of these with its
|
|
24
|
+
* default value adds noise to every field and says nothing — only a DEPARTURE
|
|
25
|
+
* from the default is worth declaring. (W612.)
|
|
26
|
+
*/
|
|
27
|
+
export const DEFAULT_FIELD_OPTIONS = {
|
|
28
|
+
canGroup: true,
|
|
29
|
+
canSort: true,
|
|
30
|
+
showInTable: true,
|
|
31
|
+
editFromTable: true,
|
|
32
|
+
canFilter: true,
|
|
33
|
+
readOnly: false,
|
|
34
|
+
searchable: false,
|
|
35
|
+
};
|
|
36
|
+
export const DOC_COLUMN_OPTIONS = ["type", "subType", "name", "nullable", "primary", "unique", "default", "length", "title", "comment", "key", "minLength", "minValue", "maxValue", "decimalPlaces", "isOnlyPositive", "mask",
|
|
37
|
+
// Field options. The decorator tables do not list them, but migrations.txt:218
|
|
38
|
+
// names them as options a column carries — encryption "makes the searchable,
|
|
39
|
+
// canFilter, canSort and canGroup options become false" — and the builder's
|
|
40
|
+
// Field Options table (api-entity-builder-builder.txt:66-73) gives all seven.
|
|
41
|
+
// Without these the validator hard-errored (E502) on a legitimate
|
|
42
|
+
// `searchable: true`. A repo's own SDK typings still override this fallback.
|
|
43
|
+
...Object.keys(DEFAULT_FIELD_OPTIONS)];
|
|
44
|
+
/**
|
|
45
|
+
* The doc's subType matrix — "it means three unrelated things depending on the
|
|
46
|
+
* base type". Keyed by the CANONICAL FieldTypeEnum value, so E112 can be decided
|
|
47
|
+
* from whatever `checkColumn` resolved `type` to. The Enum/MultiEnum row of that
|
|
48
|
+
* matrix is deliberately absent: `subType` there is the enum's global name, set
|
|
49
|
+
* by @Enum()/@MultiEnum() and never written by hand — that is E113, not E112.
|
|
50
|
+
*/
|
|
51
|
+
export const DOC_SUBTYPES_BY_TYPE = {
|
|
52
|
+
timestamp: ["date", "time", "timestamp"],
|
|
53
|
+
text: ["encrypted"],
|
|
54
|
+
};
|
|
55
|
+
export const DOC_FIELD_TYPES = {
|
|
56
|
+
Text: "text", Numeric: "numeric", Integer: "integer", Serial: "serial",
|
|
57
|
+
Boolean: "boolean", Timestamp: "timestamp", JSON: "json", UUID: "uuid",
|
|
58
|
+
MultiLang: "multi-language", Bytea: "bytea", TsVector: "tsvector",
|
|
59
|
+
MultiType: "multi-type", Icon: "icon",
|
|
60
|
+
};
|
|
61
|
+
const NON_SCALAR_TYPE_STRINGS = new Set([
|
|
62
|
+
"enum", "multi-enum", "file", "multi-file",
|
|
63
|
+
"many-to-one", "one-to-many", "many-to-many", "many-to-many-backref",
|
|
64
|
+
"relation", "attachment",
|
|
65
|
+
]);
|
|
66
|
+
const RELATION_KIND_BY_DECORATOR = {
|
|
67
|
+
ManyToOne: "many-to-one", ManyToMany: "many-to-many",
|
|
68
|
+
ManyToManyBackRef: "many-to-many-backref", OneToMany: "one-to-many",
|
|
69
|
+
};
|
|
70
|
+
const RELATION_KINDS = new Set(Object.values(RELATION_KIND_BY_DECORATOR));
|
|
71
|
+
// `inverse` is REQUIRED on these two (the reverse sides) and OPTIONAL — usually
|
|
72
|
+
// omitted — on @ManyToOne/@ManyToMany; W609 only ever fires on the latter pair.
|
|
73
|
+
const OPTIONAL_INVERSE_DECORATORS = new Set(["ManyToOne", "ManyToMany"]);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* How many rows the property holds — which fixes the TypeScript type it must
|
|
77
|
+
* carry. @ManyToOne points at ONE row (`owner: ProductMatrixEntity`); the other
|
|
78
|
+
* three hold many (`envs: ApplicationEnvsEntity[]`).
|
|
79
|
+
*/
|
|
80
|
+
const RELATION_ARITY = {
|
|
81
|
+
ManyToOne: "one",
|
|
82
|
+
ManyToMany: "many",
|
|
83
|
+
ManyToManyBackRef: "many",
|
|
84
|
+
OneToMany: "many",
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const ENUM_DECORATORS = new Set(["Enum", "MultiEnum"]);
|
|
88
|
+
const FILE_DECORATORS = new Set(["File", "MultiFile"]);
|
|
89
|
+
|
|
90
|
+
// ── typescript resolution: target app, then cwd, then this package ──
|
|
91
|
+
// The app repo is the expected source: a Suppa application compiles to dist/,
|
|
92
|
+
// so it has typescript. This package lists typescript as a devDependency only,
|
|
93
|
+
// so the last candidate resolves in a checkout but never in an install.
|
|
94
|
+
export function loadTypescript(rootDir) {
|
|
95
|
+
const froms = [];
|
|
96
|
+
if (rootDir) froms.push(join(resolve(rootDir), "package.json"));
|
|
97
|
+
froms.push(join(process.cwd(), "package.json"));
|
|
98
|
+
froms.push(import.meta.url);
|
|
99
|
+
let sawStub = false;
|
|
100
|
+
for (const from of froms) {
|
|
101
|
+
let mod;
|
|
102
|
+
try {
|
|
103
|
+
mod = createRequire(from)("typescript");
|
|
104
|
+
} catch {
|
|
105
|
+
continue; // not resolvable from here — try the next candidate
|
|
106
|
+
}
|
|
107
|
+
// Resolvability is NOT proof the compiler API is there. TypeScript 7 — what
|
|
108
|
+
// plain `npm i -D typescript` installs today — maps exports["."] to
|
|
109
|
+
// lib/version.cjs, which exports only { version, versionMajorMinor }; the
|
|
110
|
+
// parser moved behind ./unstable/*. That require SUCCEEDS and used to be
|
|
111
|
+
// handed straight to the parser, which then died on `ts.ScriptTarget.Latest`
|
|
112
|
+
// being undefined. Check the two members this validator actually uses, and
|
|
113
|
+
// keep looking: a usable TS 5 further down the list still rescues the run.
|
|
114
|
+
if (typeof mod?.createSourceFile === "function" && mod?.ScriptTarget) {
|
|
115
|
+
return mod;
|
|
116
|
+
}
|
|
117
|
+
sawStub = true;
|
|
118
|
+
}
|
|
119
|
+
// Tagged so the CLI can report it as an environment problem — one actionable
|
|
120
|
+
// line and exit 2 — instead of an uncaught stack trace.
|
|
121
|
+
throw Object.assign(new Error(
|
|
122
|
+
sawStub
|
|
123
|
+
? "validate-suppa-schema: the 'typescript' found here is TypeScript 7 (or newer), whose " +
|
|
124
|
+
"main entry exports only version info — it has no createSourceFile/ScriptTarget, so " +
|
|
125
|
+
"this validator cannot parse with it. Install the 5.x line in the app repo being " +
|
|
126
|
+
"validated: `npm i -D typescript@5`."
|
|
127
|
+
: "validate-suppa-schema: cannot resolve 'typescript'. It is read from the app repo " +
|
|
128
|
+
"being validated (a Suppa application compiles with tsc, so it normally has it). " +
|
|
129
|
+
"Install it there — `npm i -D typescript@5` — or run this from a directory where it " +
|
|
130
|
+
"resolves.",
|
|
131
|
+
), { code: "SUPPA_VALIDATOR_ENV" });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Path to print for a finding. A relative path is friendlier and clickable, but
|
|
136
|
+
* only when it actually stays inside cwd — across drives or above cwd, Windows
|
|
137
|
+
* `relative()` yields an unusable `..\..\..` chain with no drive letter.
|
|
138
|
+
*/
|
|
139
|
+
export function displayPath(cwd, file) {
|
|
140
|
+
const rel = relative(cwd, file);
|
|
141
|
+
return !rel || rel.startsWith("..") || isAbsolute(rel) ? file : rel;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── small AST helpers ──
|
|
145
|
+
function finding(ts, sf, node, code, message) {
|
|
146
|
+
const { line, character } = sf.getLineAndCharacterOfPosition(node.getStart(sf));
|
|
147
|
+
return {
|
|
148
|
+
file: sf.fileName, line: line + 1, col: character + 1, code,
|
|
149
|
+
level: code.startsWith("W") ? "warning" : "error", message,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function decoratorsOf(ts, node) {
|
|
153
|
+
return (ts.canHaveDecorators?.(node) ? ts.getDecorators(node) : undefined) ?? [];
|
|
154
|
+
}
|
|
155
|
+
function decoratorCall(ts, dec, sf) {
|
|
156
|
+
const e = dec.expression;
|
|
157
|
+
if (ts.isCallExpression(e)) {
|
|
158
|
+
const name = calleeIdentity(ts, sf, e.expression);
|
|
159
|
+
return name ? { name, args: [...e.arguments] } : null;
|
|
160
|
+
}
|
|
161
|
+
const name = calleeIdentity(ts, sf, e);
|
|
162
|
+
return name ? { name, args: [] } : null;
|
|
163
|
+
}
|
|
164
|
+
function objectMap(ts, node) {
|
|
165
|
+
const map = new Map();
|
|
166
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return map;
|
|
167
|
+
for (const p of node.properties) {
|
|
168
|
+
if (ts.isPropertyAssignment(p)) {
|
|
169
|
+
const name =
|
|
170
|
+
ts.isIdentifier(p.name) || ts.isStringLiteral(p.name) ? p.name.text : null;
|
|
171
|
+
if (name !== null) map.set(name, p.initializer);
|
|
172
|
+
} else if (ts.isShorthandPropertyAssignment(p)) {
|
|
173
|
+
map.set(p.name.text, p.name);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return map;
|
|
177
|
+
}
|
|
178
|
+
function stringValue(ts, node) {
|
|
179
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
180
|
+
? node.text
|
|
181
|
+
: null;
|
|
182
|
+
}
|
|
183
|
+
function stringArray(ts, node) {
|
|
184
|
+
if (!node || !ts.isArrayLiteralExpression(node)) return null;
|
|
185
|
+
const out = [];
|
|
186
|
+
for (const el of node.elements) {
|
|
187
|
+
const s = stringValue(ts, el);
|
|
188
|
+
if (s === null) return null;
|
|
189
|
+
out.push(s);
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
function thunkTarget(ts, sf, arg) {
|
|
194
|
+
if (arg && ts.isArrowFunction(arg)) {
|
|
195
|
+
const b = arg.body;
|
|
196
|
+
if (ts.isIdentifier(b)) return resolveLocalName(sf, b.text);
|
|
197
|
+
if (ts.isPropertyAccessExpression(b)) return b.name.text;
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* The `inverse` selector — `(target, inverse, options?)` — is whichever
|
|
203
|
+
* function expression sits in argument position 1; it is distinguished from
|
|
204
|
+
* `(target, options?)` by NOT being an object literal.
|
|
205
|
+
*/
|
|
206
|
+
function isFunctionLike(ts, n) {
|
|
207
|
+
return !!n && (ts.isArrowFunction(n) || ts.isFunctionExpression(n));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The TypeScript type a property declares, reduced to what the relation rules
|
|
211
|
+
* need: the entity name it names, whether it is an array, and whether it is
|
|
212
|
+
* optional. `null` when the property carries no annotation at all.
|
|
213
|
+
*
|
|
214
|
+
* Reads `T`, `T[]`, `Array<T>`, and unions with `null`/`undefined` (`T | null`);
|
|
215
|
+
* anything else yields `{name: null}` so the caller reports "cannot read"
|
|
216
|
+
* rather than inventing a mismatch.
|
|
217
|
+
*/
|
|
218
|
+
function propertyTypeInfo(ts, member) {
|
|
219
|
+
const node = member.type;
|
|
220
|
+
if (!node) return null;
|
|
221
|
+
let t = node;
|
|
222
|
+
let nullable = !!member.questionToken;
|
|
223
|
+
if (ts.isUnionTypeNode(t)) {
|
|
224
|
+
const parts = t.types.filter((p) => {
|
|
225
|
+
const isNullish =
|
|
226
|
+
p.kind === ts.SyntaxKind.NullKeyword ||
|
|
227
|
+
p.kind === ts.SyntaxKind.UndefinedKeyword ||
|
|
228
|
+
(ts.isLiteralTypeNode(p) && p.literal.kind === ts.SyntaxKind.NullKeyword);
|
|
229
|
+
if (isNullish) nullable = true;
|
|
230
|
+
return !isNullish;
|
|
231
|
+
});
|
|
232
|
+
if (parts.length !== 1) return { node, name: null, isArray: false, nullable };
|
|
233
|
+
t = parts[0];
|
|
234
|
+
}
|
|
235
|
+
let isArray = false;
|
|
236
|
+
if (ts.isArrayTypeNode(t)) {
|
|
237
|
+
isArray = true;
|
|
238
|
+
t = t.elementType;
|
|
239
|
+
} else if (
|
|
240
|
+
ts.isTypeReferenceNode(t) &&
|
|
241
|
+
ts.isIdentifier(t.typeName) &&
|
|
242
|
+
t.typeName.text === "Array" &&
|
|
243
|
+
t.typeArguments?.length === 1
|
|
244
|
+
) {
|
|
245
|
+
isArray = true;
|
|
246
|
+
t = t.typeArguments[0];
|
|
247
|
+
}
|
|
248
|
+
let name = null;
|
|
249
|
+
if (ts.isTypeReferenceNode(t)) {
|
|
250
|
+
name = ts.isIdentifier(t.typeName) ? t.typeName.text : t.typeName.getText(member.getSourceFile());
|
|
251
|
+
}
|
|
252
|
+
return { node, name, isArray, nullable };
|
|
253
|
+
}
|
|
254
|
+
/** A boolean literal's value, or null when the node is not one. */
|
|
255
|
+
function booleanValue(ts, node) {
|
|
256
|
+
if (!node) return null;
|
|
257
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
258
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
/** PascalCase: starts upper, letters/digits only, no separators. */
|
|
262
|
+
function isPascalCase(name) {
|
|
263
|
+
return /^[A-Z][A-Za-z0-9]*$/.test(name);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* The property name an inverse selector's body accesses off its own single
|
|
267
|
+
* parameter — `(t) => t.prop` (or `(t) => { return t.prop; }`) -> "prop".
|
|
268
|
+
* Anything else (destructuring, no single-parameter self-access, …) is not a
|
|
269
|
+
* shape this AST-only checker can read, so it returns null rather than guess.
|
|
270
|
+
*/
|
|
271
|
+
function inverseAccessedProperty(ts, fn) {
|
|
272
|
+
if (!fn || fn.parameters.length !== 1) return null;
|
|
273
|
+
const param = fn.parameters[0].name;
|
|
274
|
+
if (!ts.isIdentifier(param)) return null;
|
|
275
|
+
let body = fn.body;
|
|
276
|
+
if (ts.isBlock(body)) {
|
|
277
|
+
const stmts = body.statements;
|
|
278
|
+
if (stmts.length !== 1 || !ts.isReturnStatement(stmts[0]) || !stmts[0].expression) return null;
|
|
279
|
+
body = stmts[0].expression;
|
|
280
|
+
}
|
|
281
|
+
if (ts.isPropertyAccessExpression(body) && ts.isIdentifier(body.expression) &&
|
|
282
|
+
body.expression.text === param.text) {
|
|
283
|
+
return body.name.text;
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
function spreadElements(ts, node) {
|
|
288
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return [];
|
|
289
|
+
return node.properties.filter((p) => ts.isSpreadAssignment(p));
|
|
290
|
+
}
|
|
291
|
+
function arraySpreadElements(ts, node) {
|
|
292
|
+
if (!node || !ts.isArrayLiteralExpression(node)) return [];
|
|
293
|
+
return node.elements.filter((e) => ts.isSpreadElement(e));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ── identity: aliased named imports and '@suppa/sdk' namespace imports ──
|
|
297
|
+
// `import { Things as Thing }` / `import * as sdk from '@suppa/sdk'` name the
|
|
298
|
+
// very same declarations; matching bare identifiers alone misses them.
|
|
299
|
+
function analyzeImports(ts, sf) {
|
|
300
|
+
const alias = new Map(); // local name -> imported (exported) name
|
|
301
|
+
const namespaces = new Set(); // local name of `import * as X from '@suppa/sdk'`
|
|
302
|
+
for (const st of sf.statements) {
|
|
303
|
+
if (!ts.isImportDeclaration(st) || !st.importClause) continue;
|
|
304
|
+
const from = ts.isStringLiteral(st.moduleSpecifier) ? st.moduleSpecifier.text : "";
|
|
305
|
+
const bindings = st.importClause.namedBindings;
|
|
306
|
+
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
307
|
+
if (from === "@suppa/sdk") namespaces.add(bindings.name.text);
|
|
308
|
+
} else if (bindings && ts.isNamedImports(bindings)) {
|
|
309
|
+
for (const el of bindings.elements) {
|
|
310
|
+
alias.set(el.name.text, (el.propertyName ?? el.name).text);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
sf.suppaImports = { alias, namespaces };
|
|
315
|
+
}
|
|
316
|
+
function importsOf(sf) {
|
|
317
|
+
return sf.suppaImports ?? { alias: new Map(), namespaces: new Set() };
|
|
318
|
+
}
|
|
319
|
+
/** local identifier text -> the name it was imported under (or itself) */
|
|
320
|
+
function resolveLocalName(sf, text) {
|
|
321
|
+
return importsOf(sf).alias.get(text) ?? text;
|
|
322
|
+
}
|
|
323
|
+
/** node is `name`, an alias of it, or `<sdkNamespace>.name` */
|
|
324
|
+
function isSdkQualified(ts, sf, node, name) {
|
|
325
|
+
if (!node) return false;
|
|
326
|
+
if (ts.isIdentifier(node)) return resolveLocalName(sf, node.text) === name;
|
|
327
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === name &&
|
|
328
|
+
ts.isIdentifier(node.expression)) {
|
|
329
|
+
return importsOf(sf).namespaces.has(node.expression.text);
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
function calleeIdentity(ts, sf, expr) {
|
|
334
|
+
if (ts.isIdentifier(expr)) return resolveLocalName(sf, expr.text);
|
|
335
|
+
if (ts.isPropertyAccessExpression(expr) && ts.isIdentifier(expr.expression) &&
|
|
336
|
+
importsOf(sf).namespaces.has(expr.expression.text)) {
|
|
337
|
+
return expr.name.text;
|
|
338
|
+
}
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// ── string-ness of an expression (no type checker available) ──
|
|
343
|
+
/** provably NOT a string at runtime */
|
|
344
|
+
function isProvableNonString(ts, n) {
|
|
345
|
+
if (n.kind === ts.SyntaxKind.TrueKeyword || n.kind === ts.SyntaxKind.FalseKeyword ||
|
|
346
|
+
n.kind === ts.SyntaxKind.NullKeyword) return true;
|
|
347
|
+
if (ts.isNumericLiteral(n) || ts.isBigIntLiteral(n)) return true;
|
|
348
|
+
if (ts.isPrefixUnaryExpression(n) &&
|
|
349
|
+
(ts.isNumericLiteral(n.operand) || ts.isBigIntLiteral(n.operand))) return true;
|
|
350
|
+
if (ts.isArrayLiteralExpression(n) || ts.isObjectLiteralExpression(n)) return true;
|
|
351
|
+
if (ts.isNewExpression(n)) return true;
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
/** provably a string at runtime (literal, template, or a `+` chain with one) */
|
|
355
|
+
function isProvableString(ts, n) {
|
|
356
|
+
if (ts.isStringLiteral(n) || ts.isNoSubstitutionTemplateLiteral(n) ||
|
|
357
|
+
ts.isTemplateExpression(n)) return true;
|
|
358
|
+
if (ts.isParenthesizedExpression(n)) return isProvableString(ts, n.expression);
|
|
359
|
+
if (ts.isBinaryExpression(n) && n.operatorToken.kind === ts.SyntaxKind.PlusToken) {
|
|
360
|
+
return isProvableString(ts, n.left) || isProvableString(ts, n.right);
|
|
361
|
+
}
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
/** strip `(x)`, `x as T`, `x!` — they do not change the runtime value */
|
|
365
|
+
function unwrapExpr(ts, n) {
|
|
366
|
+
let cur = n;
|
|
367
|
+
while (cur && (ts.isParenthesizedExpression(cur) || ts.isAsExpression(cur) ||
|
|
368
|
+
ts.isNonNullExpression(cur) || ts.isSatisfiesExpression?.(cur) === true)) {
|
|
369
|
+
cur = cur.expression;
|
|
370
|
+
}
|
|
371
|
+
return cur;
|
|
372
|
+
}
|
|
373
|
+
/** an expression whose value this AST-only checker cannot see */
|
|
374
|
+
function isOpaqueExpression(ts, n) {
|
|
375
|
+
return ts.isIdentifier(n) || ts.isCallExpression(n) ||
|
|
376
|
+
ts.isPropertyAccessExpression(n) || ts.isElementAccessExpression(n) ||
|
|
377
|
+
ts.isAwaitExpression(n) || ts.isConditionalExpression(n);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ── extraction: entities ──
|
|
381
|
+
function analyzeEntities(ts, sf, model) {
|
|
382
|
+
sf.forEachChild(function walk(node) {
|
|
383
|
+
if (ts.isClassDeclaration(node)) {
|
|
384
|
+
const decs = decoratorsOf(ts, node).map((d) => decoratorCall(ts, d, sf)).filter(Boolean);
|
|
385
|
+
const entityDec = decs.find((d) => d.name === "Entity");
|
|
386
|
+
if (entityDec) {
|
|
387
|
+
const className = node.name ? node.name.text : "<anonymous>";
|
|
388
|
+
const options = objectMap(ts, entityDec.args[0]);
|
|
389
|
+
const keysNode = options.get("importKeyFields");
|
|
390
|
+
const info = {
|
|
391
|
+
className,
|
|
392
|
+
entityName: stringValue(ts, options.get("name")) ?? className,
|
|
393
|
+
sf, node, options, optionsNode: entityDec.args[0],
|
|
394
|
+
importKeyFields: stringArray(ts, keysNode),
|
|
395
|
+
// present but not a readable array of string literals: the key checks
|
|
396
|
+
// cannot run, but claiming it is missing would be a lie (CB-8)
|
|
397
|
+
importKeyFieldsUnreadable: !!keysNode && stringArray(ts, keysNode) === null,
|
|
398
|
+
importKeyFieldsNode: keysNode ?? null,
|
|
399
|
+
hasKey: options.has("key"),
|
|
400
|
+
properties: new Map(),
|
|
401
|
+
columnNames: new Set(), // literal `name` options of @Column/relations
|
|
402
|
+
};
|
|
403
|
+
for (const member of node.members) {
|
|
404
|
+
if (!ts.isPropertyDeclaration(member) || !member.name) continue;
|
|
405
|
+
const propName =
|
|
406
|
+
ts.isIdentifier(member.name) || ts.isStringLiteral(member.name)
|
|
407
|
+
? member.name.text
|
|
408
|
+
: member.name.getText(sf);
|
|
409
|
+
for (const d of decoratorsOf(ts, member).map((x) => decoratorCall(ts, x, sf)).filter(Boolean)) {
|
|
410
|
+
if (d.name === "Column") {
|
|
411
|
+
const columnOptions = objectMap(ts, d.args[0]);
|
|
412
|
+
info.properties.set(propName, {
|
|
413
|
+
kind: "column", node: member, decorator: d,
|
|
414
|
+
columnOptions, optionsNode: d.args[0],
|
|
415
|
+
nameNode: columnOptions.get("name") ?? null,
|
|
416
|
+
});
|
|
417
|
+
} else if (ENUM_DECORATORS.has(d.name)) {
|
|
418
|
+
// (globalName?, Enum, titles?, options?) — the options object is
|
|
419
|
+
// the LAST argument when that is an object literal. An inline
|
|
420
|
+
// titles map occupies the same slot when options are omitted, so
|
|
421
|
+
// this node is only ever read for keys options own (E113's
|
|
422
|
+
// `subType`); a titles map never carries one.
|
|
423
|
+
const last = d.args[d.args.length - 1];
|
|
424
|
+
const enumOptionsNode =
|
|
425
|
+
d.args.length >= 2 && last && ts.isObjectLiteralExpression(last) ? last : null;
|
|
426
|
+
info.properties.set(propName, {
|
|
427
|
+
kind: d.name === "Enum" ? "enum" : "multi-enum",
|
|
428
|
+
node: member, decorator: d,
|
|
429
|
+
enumGlobalName: stringValue(ts, d.args[0]),
|
|
430
|
+
optionsNode: enumOptionsNode,
|
|
431
|
+
enumOptions: objectMap(ts, enumOptionsNode),
|
|
432
|
+
});
|
|
433
|
+
} else if (FILE_DECORATORS.has(d.name)) {
|
|
434
|
+
info.properties.set(propName, {
|
|
435
|
+
kind: d.name === "File" ? "file" : "multi-file",
|
|
436
|
+
node: member, decorator: d,
|
|
437
|
+
});
|
|
438
|
+
} else if (d.name in RELATION_KIND_BY_DECORATOR) {
|
|
439
|
+
// (target, options?) or (target, inverse, options?) — the options
|
|
440
|
+
// object is whichever trailing argument is an object literal; the
|
|
441
|
+
// inverse (when present) is argument 1 whenever it is NOT that
|
|
442
|
+
// options object, i.e. it is an arrow/function expression instead.
|
|
443
|
+
const relOptions = objectMap(
|
|
444
|
+
ts, [...d.args].reverse().find((a) => ts.isObjectLiteralExpression(a)),
|
|
445
|
+
);
|
|
446
|
+
const inverseNode = isFunctionLike(ts, d.args[1]) ? d.args[1] : null;
|
|
447
|
+
info.properties.set(propName, {
|
|
448
|
+
kind: RELATION_KIND_BY_DECORATOR[d.name],
|
|
449
|
+
node: member, decorator: d,
|
|
450
|
+
targetClass: thunkTarget(ts, sf, d.args[0]),
|
|
451
|
+
nameNode: relOptions.get("name") ?? null,
|
|
452
|
+
inverseNode,
|
|
453
|
+
relOptions,
|
|
454
|
+
optionsNode: [...d.args].reverse().find((a) => ts.isObjectLiteralExpression(a)) ?? null,
|
|
455
|
+
typeInfo: propertyTypeInfo(ts, member),
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const prop = info.properties.get(propName);
|
|
460
|
+
const colName = prop && stringValue(ts, prop.nameNode);
|
|
461
|
+
if (colName) info.columnNames.add(colName);
|
|
462
|
+
}
|
|
463
|
+
model.entities.set(className, info);
|
|
464
|
+
model.entityByName.set(info.entityName, info);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
ts.forEachChild(node, walk);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// ── rules: tabular parts ──
|
|
472
|
+
/**
|
|
473
|
+
* Doc: "Three things make an entity a tabular part. All three are required,
|
|
474
|
+
* and the platform does not infer any of them: type: 'tabular-part' on
|
|
475
|
+
* @Entity(), relationEntityName — the name of the parent entity, and a
|
|
476
|
+
* @ManyToOne field named owner pointing at the parent class."
|
|
477
|
+
* Conversely, relationEntityName without type: 'tabular-part' is the doc's
|
|
478
|
+
* own mistake row: "type: 'tabular-part' or relationEntityName is missing,
|
|
479
|
+
* so it is listed under /related instead."
|
|
480
|
+
*/
|
|
481
|
+
function checkTabularPart(ts, info, model, findings) {
|
|
482
|
+
const sf = info.sf;
|
|
483
|
+
const anchor = info.node.name ?? info.node;
|
|
484
|
+
const TRIAD = `"All three are required, and the platform does not infer any of them" (type: 'tabular-part', relationEntityName, and a @ManyToOne field named owner)`;
|
|
485
|
+
|
|
486
|
+
const hasType = info.options.has("type");
|
|
487
|
+
const typeNode = info.options.get("type");
|
|
488
|
+
const typeLiteral = hasType ? stringValue(ts, typeNode) : null;
|
|
489
|
+
const typeUnreadable = hasType && typeLiteral === null;
|
|
490
|
+
|
|
491
|
+
const hasRelName = info.options.has("relationEntityName");
|
|
492
|
+
const relNode = info.options.get("relationEntityName");
|
|
493
|
+
const relLiteral = hasRelName ? stringValue(ts, relNode) : null;
|
|
494
|
+
const relUnreadable = hasRelName && relLiteral === null;
|
|
495
|
+
|
|
496
|
+
// an unreadable type/relationEntityName (identifier, expression, …) means
|
|
497
|
+
// this checker cannot tell whether the entity is a tabular part at all —
|
|
498
|
+
// warn instead of guessing E108 one way or the other.
|
|
499
|
+
if (typeUnreadable || relUnreadable) {
|
|
500
|
+
const badNode = typeUnreadable ? typeNode : relNode;
|
|
501
|
+
const badOpt = typeUnreadable ? "type" : "relationEntityName";
|
|
502
|
+
findings.push(finding(ts, sf, badNode, "W606",
|
|
503
|
+
`cannot statically verify '${badOpt}' of '${info.className}' — it is delivered by reference or an expression this checker cannot evaluate, so the tabular-part checks (E108-E110, W607) are skipped; inline the literal to restore checking`));
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const isTabularPart = typeLiteral === "tabular-part";
|
|
508
|
+
// `owner` is a @ManyToOne relation named 'owner' by PROPERTY name, or one
|
|
509
|
+
// whose literal @ManyToOne({ name: 'owner' }) column-name option is 'owner'
|
|
510
|
+
// (F4) — E106 (DA-2) already accepts a column name as a field identity, so
|
|
511
|
+
// E108's notion of "the owner relation" must agree.
|
|
512
|
+
const ownerProp = [...info.properties.entries()].find(
|
|
513
|
+
([propName, p]) => p.kind === "many-to-one" &&
|
|
514
|
+
(propName === "owner" || stringValue(ts, p.nameNode) === "owner"),
|
|
515
|
+
)?.[1];
|
|
516
|
+
const hasOwner = !!ownerProp;
|
|
517
|
+
|
|
518
|
+
// an @Entity() options spread may hide `type`/`relationEntityName` (they are
|
|
519
|
+
// spread properties, not readable PropertyAssignments) — the W606 already
|
|
520
|
+
// fired for the spread promises these doc-rule checks are skipped, so the
|
|
521
|
+
// two option-presence branches below must not also false-positive E108. The
|
|
522
|
+
// missing-`owner` branch is a class member a spread cannot deliver, so it
|
|
523
|
+
// stays ungated.
|
|
524
|
+
const optionsSpread = spreadElements(ts, info.optionsNode).length > 0;
|
|
525
|
+
|
|
526
|
+
if (isTabularPart) {
|
|
527
|
+
if (!hasRelName && !optionsSpread) {
|
|
528
|
+
findings.push(finding(ts, sf, anchor, "E108",
|
|
529
|
+
`tabular part '${info.className}' (type: 'tabular-part') is missing relationEntityName — ${TRIAD}`));
|
|
530
|
+
}
|
|
531
|
+
if (!hasOwner) {
|
|
532
|
+
findings.push(finding(ts, sf, anchor, "E108",
|
|
533
|
+
`tabular part '${info.className}' (type: 'tabular-part') has no @ManyToOne relation named 'owner' pointing at the parent — ${TRIAD}`));
|
|
534
|
+
}
|
|
535
|
+
} else if (hasRelName && !optionsSpread) {
|
|
536
|
+
findings.push(finding(ts, sf, relNode, "E108",
|
|
537
|
+
`entity '${info.className}' sets relationEntityName but not type: 'tabular-part' — "type: 'tabular-part' or relationEntityName is missing, so it is listed under /related instead"`));
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (hasRelName && hasOwner && ownerProp.targetClass) {
|
|
541
|
+
const target = model.entities.get(ownerProp.targetClass);
|
|
542
|
+
if (target && target.entityName !== relLiteral) {
|
|
543
|
+
findings.push(finding(ts, sf, ownerProp.node, "E109",
|
|
544
|
+
`tabular part '${info.className}': owner points at '${target.entityName}' but relationEntityName is '${relLiteral}' — the owner relation's target and relationEntityName must name the same parent entity`));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (isTabularPart && hasOwner && !info.importKeyFieldsUnreadable && info.importKeyFields) {
|
|
549
|
+
const idx = info.importKeyFields.indexOf("owner");
|
|
550
|
+
if (idx === -1) {
|
|
551
|
+
findings.push(finding(ts, sf, info.importKeyFieldsNode, "E110",
|
|
552
|
+
`tabular part '${info.className}' importKeyFields ${JSON.stringify(info.importKeyFields)} does not include 'owner' — "owner is missing from importKeyFields, so the key is matched globally" ⇒ duplicate rows across different parents`));
|
|
553
|
+
} else if (idx !== 0) {
|
|
554
|
+
findings.push(finding(ts, sf, info.importKeyFieldsNode, "W607",
|
|
555
|
+
`tabular part '${info.className}': "Put owner first in importKeyFields" — it is at position ${idx}`));
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// ── rules: entities ──
|
|
561
|
+
function checkEntity(ts, info, sdk, model, findings) {
|
|
562
|
+
const sf = info.sf;
|
|
563
|
+
const anchor = info.node.name ?? info.node;
|
|
564
|
+
|
|
565
|
+
const heritage = (info.node.heritageClauses ?? []).find(
|
|
566
|
+
(h) => h.token === ts.SyntaxKind.ExtendsKeyword,
|
|
567
|
+
);
|
|
568
|
+
const base = heritage?.types?.[0]?.expression;
|
|
569
|
+
if (!isSdkQualified(ts, sf, base, "SystemBaseEntity")) {
|
|
570
|
+
findings.push(finding(ts, sf, anchor, "E101",
|
|
571
|
+
`entity class '${info.className}' must extend SystemBaseEntity — it contributes id/createdAt/updatedAt/deletedAt/createdBy/removedBy (doc: "Every entity must extend SystemBaseEntity")`));
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
for (const [opt, valNode] of info.options) {
|
|
575
|
+
if (!sdk.entityOptions.includes(opt)) {
|
|
576
|
+
findings.push(finding(ts, sf, valNode, "E501",
|
|
577
|
+
`unknown @Entity() option '${opt}' — valid options: ${sdk.entityOptions.join(", ")}. If it isn't in the table, it isn't an option.`));
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
for (const spread of spreadElements(ts, info.optionsNode)) {
|
|
582
|
+
findings.push(finding(ts, sf, spread, "W606",
|
|
583
|
+
`@Entity() options include a spread — options delivered via spread cannot be statically validated: the doc-rule checks (unknown options, default-as-SQL-string, type checks) are skipped for spread content; inline the options to restore checking`));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (info.importKeyFieldsUnreadable) {
|
|
587
|
+
findings.push(finding(ts, sf, info.importKeyFieldsNode, "W606",
|
|
588
|
+
`cannot statically verify importKeyFields of '${info.className}' — it is delivered by reference or spread, so record-level key checks (E106, E205) are skipped; inline the array of field names to restore checking`));
|
|
589
|
+
} else if (info.importKeyFields) {
|
|
590
|
+
for (const f of info.importKeyFields) {
|
|
591
|
+
// a field may be named by its property name or by its @Column({ name }) (DA-2)
|
|
592
|
+
if (!info.properties.has(f) && !info.columnNames.has(f)) {
|
|
593
|
+
findings.push(finding(ts, sf, info.importKeyFieldsNode, "E106",
|
|
594
|
+
`importKeyFields names '${f}' but the class declares no such property or column name — seeds would have nothing to match on`));
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (!info.hasKey) {
|
|
600
|
+
findings.push(finding(ts, sf, anchor, "W602",
|
|
601
|
+
`entity '${info.className}' has no 'key' option — a rename will be treated as drop-and-create instead of a rename (doc TIP)`));
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
checkTabularPart(ts, info, model, findings);
|
|
605
|
+
|
|
606
|
+
for (const [propName, prop] of info.properties) {
|
|
607
|
+
if (SYSTEM_COLUMNS.includes(propName)) {
|
|
608
|
+
findings.push(finding(ts, sf, prop.node, "E102",
|
|
609
|
+
`system column '${propName}' is provided by SystemBaseEntity — "you never declare them yourself"`));
|
|
610
|
+
}
|
|
611
|
+
// the table-level conflict is on the COLUMN name, which `name` overrides
|
|
612
|
+
const colName = stringValue(ts, prop.nameNode);
|
|
613
|
+
if (colName && SYSTEM_COLUMNS.includes(colName)) {
|
|
614
|
+
findings.push(finding(ts, sf, prop.nameNode, "E102",
|
|
615
|
+
`column name '${colName}' on property '${propName}' collides with the system column of the same name provided by SystemBaseEntity — "you never declare them yourself"`));
|
|
616
|
+
}
|
|
617
|
+
if (prop.kind === "column") checkColumn(ts, sf, propName, prop, sdk, findings);
|
|
618
|
+
if (prop.kind === "enum" || prop.kind === "multi-enum") {
|
|
619
|
+
checkEnumSubType(ts, sf, propName, prop, findings);
|
|
620
|
+
}
|
|
621
|
+
checkDefaultFieldOptions(ts, sf, propName, prop, findings);
|
|
622
|
+
if (prop.kind === "enum" || prop.kind === "multi-enum") {
|
|
623
|
+
checkEnumGlobalName(ts, sf, propName, prop, model, findings);
|
|
624
|
+
}
|
|
625
|
+
if (RELATION_KINDS.has(prop.kind)) {
|
|
626
|
+
checkRelationTyping(ts, sf, propName, prop, findings);
|
|
627
|
+
if (prop.targetClass === null) {
|
|
628
|
+
findings.push(finding(ts, sf, prop.node, "E105",
|
|
629
|
+
`relation '${propName}' must receive a thunk returning the target class — write @${prop.decorator.name}(() => Target); a direct class reference breaks circular imports`));
|
|
630
|
+
}
|
|
631
|
+
if (prop.inverseNode) {
|
|
632
|
+
if (OPTIONAL_INVERSE_DECORATORS.has(prop.decorator.name)) {
|
|
633
|
+
findings.push(finding(ts, sf, prop.inverseNode, "W609",
|
|
634
|
+
`'${propName}' (@${prop.decorator.name}) passes an inverse selector — it is optional on @ManyToOne/@ManyToMany and usually omitted; pass only the thunk and the options object (@${prop.decorator.name}(() => Target, { ... })) unless you are deliberately pairing an explicit back-reference declared on the target`));
|
|
635
|
+
}
|
|
636
|
+
checkInverseSelector(ts, sf, propName, prop, model, findings);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* A relation property must declare its TypeScript type, and that type must say
|
|
644
|
+
* the same thing the decorator does: @ManyToOne holds ONE target row, the other
|
|
645
|
+
* three hold many.
|
|
646
|
+
*
|
|
647
|
+
* @ManyToOne(() => ProductMatrixEntity, { nullable: false })
|
|
648
|
+
* owner: ProductMatrixEntity;
|
|
649
|
+
*
|
|
650
|
+
* @ManyToMany(() => ProductMatrixEntity, { … }) @OneToMany(() => X, i, { … })
|
|
651
|
+
* owners: ProductMatrixEntity[]; envs: X[];
|
|
652
|
+
*
|
|
653
|
+
* Without the annotation the property is `any` to every consumer of the entity,
|
|
654
|
+
* and nothing catches a relation read as the wrong shape.
|
|
655
|
+
*/
|
|
656
|
+
function checkRelationTyping(ts, sf, propName, prop, findings) {
|
|
657
|
+
const arity = RELATION_ARITY[prop.decorator.name];
|
|
658
|
+
const target = prop.targetClass;
|
|
659
|
+
const info = prop.typeInfo;
|
|
660
|
+
const expected = target ? (arity === "many" ? `${target}[]` : target) : null;
|
|
661
|
+
|
|
662
|
+
if (!info) {
|
|
663
|
+
findings.push(finding(ts, sf, prop.node, "E114",
|
|
664
|
+
`relation '${propName}' (@${prop.decorator.name}) has no TypeScript type — write ` +
|
|
665
|
+
`${propName}${arity === "one" ? "" : ""}: ${expected ?? (arity === "many" ? "Target[]" : "Target")};` +
|
|
666
|
+
` an untyped relation property is 'any' to every consumer of this entity`));
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (info.name === null) return; // a shape this AST-only checker cannot read
|
|
670
|
+
|
|
671
|
+
if (target && info.name !== target) {
|
|
672
|
+
findings.push(finding(ts, sf, info.node, "E115",
|
|
673
|
+
`relation '${propName}' (@${prop.decorator.name}) points at '${target}' but the property is typed ` +
|
|
674
|
+
`'${info.name}${info.isArray ? "[]" : ""}' — the type must name the same class the thunk returns: ${expected}`));
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
if (arity === "many" && !info.isArray) {
|
|
678
|
+
findings.push(finding(ts, sf, info.node, "E115",
|
|
679
|
+
`'${propName}' (@${prop.decorator.name}) holds MANY rows but is typed '${info.name}' — write '${info.name}[]'`));
|
|
680
|
+
} else if (arity === "one" && info.isArray) {
|
|
681
|
+
findings.push(finding(ts, sf, info.node, "E115",
|
|
682
|
+
`'${propName}' (@ManyToOne) holds ONE row but is typed '${info.name}[]' — write '${info.name}'. ` +
|
|
683
|
+
`The many side is @OneToMany on the other class.`));
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// Optionality should agree with the column: a NOT NULL relation always has a
|
|
687
|
+
// value, anything else may be absent.
|
|
688
|
+
if (arity === "one") {
|
|
689
|
+
const notNull = booleanValue(ts, prop.relOptions?.get("nullable")) === false;
|
|
690
|
+
if (notNull && info.nullable) {
|
|
691
|
+
findings.push(finding(ts, sf, info.node, "W610",
|
|
692
|
+
`'${propName}' is declared nullable: false but typed as optional — drop the '?' (and any '| null'): the column always has a value`));
|
|
693
|
+
} else if (!notNull && !info.nullable) {
|
|
694
|
+
findings.push(finding(ts, sf, info.node, "W610",
|
|
695
|
+
`'${propName}' is nullable (no 'nullable: false') but typed as always present — write '${propName}?: ${info.name}' so callers handle the empty case`));
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Field options the platform already applies by default. Repeating one per
|
|
702
|
+
* field is noise that hides the options that DO differ.
|
|
703
|
+
*/
|
|
704
|
+
function checkDefaultFieldOptions(ts, sf, propName, prop, findings) {
|
|
705
|
+
const scopes = [];
|
|
706
|
+
if (prop.optionsNode) scopes.push(prop.optionsNode);
|
|
707
|
+
// …and a nested `options: { … }` object, whichever shape the SDK takes.
|
|
708
|
+
for (const scope of [...scopes]) {
|
|
709
|
+
const nested = objectMap(ts, scope).get("options");
|
|
710
|
+
if (nested && ts.isObjectLiteralExpression(nested)) scopes.push(nested);
|
|
711
|
+
}
|
|
712
|
+
for (const scope of scopes) {
|
|
713
|
+
for (const [key, valNode] of objectMap(ts, scope)) {
|
|
714
|
+
if (!(key in DEFAULT_FIELD_OPTIONS)) continue;
|
|
715
|
+
const value = booleanValue(ts, valNode);
|
|
716
|
+
if (value !== null && value === DEFAULT_FIELD_OPTIONS[key]) {
|
|
717
|
+
findings.push(finding(ts, sf, valNode, "W612",
|
|
718
|
+
`'${propName}' sets '${key}: ${value}', which is already the platform default — remove it; ` +
|
|
719
|
+
`declare an option only where it DEPARTS from the default (defaults: ` +
|
|
720
|
+
`${Object.entries(DEFAULT_FIELD_OPTIONS).map(([k, v]) => `${k}=${v}`).join(", ")})`));
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* @Enum()/@MultiEnum() take the enum's global name as an optional FIRST
|
|
728
|
+
* argument. A global name is what lets seeds and other entities reference the
|
|
729
|
+
* value set; without one the decorator derives <ClassName>.<propertyName> and
|
|
730
|
+
* the enum stays local to this field. Pass it only when the enum really is
|
|
731
|
+
* shared — so a name nothing references is a name that should not be there.
|
|
732
|
+
*/
|
|
733
|
+
function checkEnumGlobalName(ts, sf, propName, prop, model, findings) {
|
|
734
|
+
const name = prop.enumGlobalName;
|
|
735
|
+
if (!name) return; // local enum — the default, and nothing to check
|
|
736
|
+
// The literal occurs once: here, in this decorator. Nothing — no seed record,
|
|
737
|
+
// no other entity — refers to it, so the name buys nothing.
|
|
738
|
+
const occurrences = model.stringLiteralCounts.get(name) ?? 0;
|
|
739
|
+
if (occurrences <= 1) {
|
|
740
|
+
findings.push(finding(ts, sf, prop.decorator.args[0], "W613",
|
|
741
|
+
`'${propName}' (@${prop.decorator.name}) declares the global enum name '${name}', but nothing in the ` +
|
|
742
|
+
`analyzed files references it. A global name is for enums shared across entities or referenced from ` +
|
|
743
|
+
`seeds ({ name: '${name}', value: … }); for an enum used only by this field, omit the first argument ` +
|
|
744
|
+
`— @${prop.decorator.name}(${prop.decorator.name === "Enum" ? "StatusEnum, StatusTitles, " : "…, "}{ … }) — and the decorator derives the name`));
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* "An inverse must select the property on the target that holds the OTHER
|
|
750
|
+
* SIDE of this relation" — e.g. (task) => task.project. Applies to the
|
|
751
|
+
* `inverse` argument of any of the four relation decorators, required or not.
|
|
752
|
+
* Skipped silently when the target class is not among the analyzed entities
|
|
753
|
+
* (platform/SDK entities such as IconsEntity) — no guessing without a model
|
|
754
|
+
* of that class, and when the selector's shape (destructuring, a computed
|
|
755
|
+
* body, …) is not one this AST-only checker can read.
|
|
756
|
+
*/
|
|
757
|
+
function checkInverseSelector(ts, sf, propName, prop, model, findings) {
|
|
758
|
+
if (!prop.targetClass) return;
|
|
759
|
+
const target = model.entities.get(prop.targetClass);
|
|
760
|
+
if (!target) return;
|
|
761
|
+
const accessed = inverseAccessedProperty(ts, prop.inverseNode);
|
|
762
|
+
if (accessed === null) return;
|
|
763
|
+
const targetProp = target.properties.get(accessed);
|
|
764
|
+
if (targetProp && RELATION_KINDS.has(targetProp.kind)) return; // valid pairing
|
|
765
|
+
|
|
766
|
+
let reason;
|
|
767
|
+
if (SYSTEM_COLUMNS.includes(accessed)) {
|
|
768
|
+
reason = `'${accessed}' is a system column provided by SystemBaseEntity, not a relation`;
|
|
769
|
+
} else if (!targetProp) {
|
|
770
|
+
reason = `'${target.entityName}' declares no property named '${accessed}'`;
|
|
771
|
+
} else {
|
|
772
|
+
reason = `'${target.entityName}.${accessed}' is a '${targetProp.kind}' property, not a relation`;
|
|
773
|
+
}
|
|
774
|
+
findings.push(finding(ts, sf, prop.inverseNode, "E111",
|
|
775
|
+
`inverse selector on '${propName}' (@${prop.decorator.name}) targets '${target.entityName}.${accessed}', but ${reason} — an inverse selector must select the RELATION property on the target that holds the OTHER SIDE of this relation, e.g. (task) => task.project`));
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* E113 — "@Enum() and @MultiEnum() fill it from their first argument […] do not
|
|
780
|
+
* pass subType yourself on an enum field". Passing one by hand either duplicates
|
|
781
|
+
* what the decorator already writes or silently disagrees with it, and the value
|
|
782
|
+
* it disagrees with is the one that resolves { value: 'active' } to an enum
|
|
783
|
+
* record on insert.
|
|
784
|
+
*/
|
|
785
|
+
function checkEnumSubType(ts, sf, propName, prop, findings) {
|
|
786
|
+
const subTypeNode = prop.enumOptions?.get("subType");
|
|
787
|
+
if (!subTypeNode) return;
|
|
788
|
+
findings.push(finding(ts, sf, subTypeNode, "E113",
|
|
789
|
+
`'${propName}' (@${prop.decorator.name}) passes 'subType' by hand — on an enum field subType holds the enum's global name and is "Set by the decorator — never by hand"; @Enum()/@MultiEnum() fill it from their first argument (or derive <ClassName>.<propertyName>). Remove the option.`));
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
function checkColumn(ts, sf, propName, prop, sdk, findings) {
|
|
793
|
+
const opts = prop.columnOptions;
|
|
794
|
+
for (const [opt, valNode] of opts) {
|
|
795
|
+
if (!sdk.columnOptions.includes(opt)) {
|
|
796
|
+
findings.push(finding(ts, sf, valNode, "E502",
|
|
797
|
+
`unknown @Column() option '${opt}' — valid options: ${sdk.columnOptions.join(", ")}. If it isn't in the table, it isn't an option.`));
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
for (const spread of spreadElements(ts, prop.optionsNode)) {
|
|
802
|
+
findings.push(finding(ts, sf, spread, "W606",
|
|
803
|
+
`@Column() options include a spread — options delivered via spread cannot be statically validated: the doc-rule checks (unknown options, default-as-SQL-string, type checks) are skipped for spread content; inline the options to restore checking`));
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
let typeValue = null; // canonical string value, e.g. "text"
|
|
807
|
+
const typeNode = opts.get("type");
|
|
808
|
+
if (typeNode) {
|
|
809
|
+
if (ts.isPropertyAccessExpression(typeNode) &&
|
|
810
|
+
ts.isIdentifier(typeNode.expression) &&
|
|
811
|
+
typeNode.expression.text === "FieldTypeEnum") {
|
|
812
|
+
const member = typeNode.name.text;
|
|
813
|
+
typeValue = sdk.fieldTypes[member] ?? null;
|
|
814
|
+
if (typeValue === null) {
|
|
815
|
+
findings.push(finding(ts, sf, typeNode, "E107",
|
|
816
|
+
`FieldTypeEnum.${member} is not a known member — valid: ${Object.keys(sdk.fieldTypes).join(", ")}`));
|
|
817
|
+
}
|
|
818
|
+
} else {
|
|
819
|
+
const raw = stringValue(ts, typeNode);
|
|
820
|
+
if (raw !== null) {
|
|
821
|
+
if (NON_SCALAR_TYPE_STRINGS.has(raw)) {
|
|
822
|
+
findings.push(finding(ts, sf, typeNode, "E104",
|
|
823
|
+
`@Column type '${raw}' — relations, enums and files are declared with their own decorators (@ManyToOne/@Enum/@File/…) "and never with @Column({ type: ... })"`));
|
|
824
|
+
} else {
|
|
825
|
+
const member = Object.keys(sdk.fieldTypes).find((k) => sdk.fieldTypes[k] === raw);
|
|
826
|
+
if (member) {
|
|
827
|
+
typeValue = raw;
|
|
828
|
+
findings.push(finding(ts, sf, typeNode, "W601",
|
|
829
|
+
`raw string type '${raw}' — use FieldTypeEnum.${member} so a typo fails in the editor instead of at migration time`));
|
|
830
|
+
} else {
|
|
831
|
+
findings.push(finding(ts, sf, typeNode, "E107",
|
|
832
|
+
`unknown column type '${raw}' — "a misspelled one ('txet') compiles too and only fails when the migration runs"`));
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const def = opts.get("default");
|
|
840
|
+
if (def) {
|
|
841
|
+
const val = unwrapExpr(ts, def);
|
|
842
|
+
if (isProvableNonString(ts, val)) {
|
|
843
|
+
findings.push(finding(ts, sf, def, "E103",
|
|
844
|
+
`'default' must be a SQL expression STRING — "Write default: 'false', not default: false, and default: \\"'draft'\\" for a string literal"`));
|
|
845
|
+
} else if (!isProvableString(ts, val)) {
|
|
846
|
+
findings.push(finding(ts, sf, def, "W606",
|
|
847
|
+
`cannot statically verify that 'default' is a SQL expression string — it is delivered by reference or by an expression this checker cannot evaluate, so the check is skipped; inline a literal SQL string to restore checking`));
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
checkSubType(ts, sf, propName, prop, typeValue, findings);
|
|
851
|
+
|
|
852
|
+
if (opts.has("length") && typeValue !== null && typeValue !== "text") {
|
|
853
|
+
findings.push(finding(ts, sf, opts.get("length"), "W603",
|
|
854
|
+
`'length' caps Text columns; this column is '${typeValue}'`));
|
|
855
|
+
}
|
|
856
|
+
if (opts.has("decimalPlaces") && typeValue !== null && typeValue !== "numeric") {
|
|
857
|
+
findings.push(finding(ts, sf, opts.get("decimalPlaces"), "W604",
|
|
858
|
+
`'decimalPlaces' pairs with Numeric columns; this column is '${typeValue}'`));
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* E112 — a subType string that the doc's matrix does not pair with this base
|
|
864
|
+
* type. Only two rows of that matrix belong to @Column(): Timestamp takes
|
|
865
|
+
* 'date' | 'time' | 'timestamp' ("Chooses the physical column type"), Text takes
|
|
866
|
+
* 'encrypted'. Anything else is rejected by the platform, not by tsc — `subType`
|
|
867
|
+
* is typed as a plain string, so a wrong pairing compiles.
|
|
868
|
+
*
|
|
869
|
+
* `subType: null` is the DOCUMENTED way to clear a subtype ("write subType: null
|
|
870
|
+
* explicitly") and is always clean. A value this AST-only checker cannot read
|
|
871
|
+
* takes the usual W606 unverifiable path — never E112. An unresolved `type`
|
|
872
|
+
* leaves nothing to judge the pairing against, so the check is skipped.
|
|
873
|
+
*/
|
|
874
|
+
function checkSubType(ts, sf, propName, prop, typeValue, findings) {
|
|
875
|
+
const node = prop.columnOptions.get("subType");
|
|
876
|
+
if (!node) return;
|
|
877
|
+
const val = unwrapExpr(ts, node);
|
|
878
|
+
if (val.kind === ts.SyntaxKind.NullKeyword) return; // the documented "clear it"
|
|
879
|
+
const raw = stringValue(ts, val);
|
|
880
|
+
if (raw === null) {
|
|
881
|
+
findings.push(finding(ts, sf, node, "W606",
|
|
882
|
+
`cannot statically verify 'subType' of '${propName}' — it is delivered by reference or by an expression this checker cannot evaluate, so the base-type pairing check (E112) is skipped; inline the string literal to restore checking`));
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (typeValue === null) return; // no readable `type` to pair it against
|
|
886
|
+
const allowed = DOC_SUBTYPES_BY_TYPE[typeValue];
|
|
887
|
+
if (allowed && allowed.includes(raw)) return;
|
|
888
|
+
findings.push(finding(ts, sf, node, "E112",
|
|
889
|
+
`subType '${raw}' is not valid on a '${typeValue}' column ('${propName}') — the doc's matrix pairs subType with exactly two @Column base types: Timestamp takes 'date', 'time' or 'timestamp', and Text takes 'encrypted'. No other (type, subType) combination exists.`));
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// ── extraction: string enums (createSeed(EnumName.Member) resolution) ──
|
|
893
|
+
function analyzeEnums(ts, sf, model) {
|
|
894
|
+
sf.forEachChild(function walk(node) {
|
|
895
|
+
// Every string literal in the analyzed set, counted. A global enum name that
|
|
896
|
+
// occurs exactly once is written in its decorator and referenced nowhere —
|
|
897
|
+
// which is what W613 is looking for.
|
|
898
|
+
if (ts.isStringLiteral(node)) {
|
|
899
|
+
model.stringLiteralCounts.set(node.text, (model.stringLiteralCounts.get(node.text) ?? 0) + 1);
|
|
900
|
+
}
|
|
901
|
+
if (ts.isEnumDeclaration(node)) {
|
|
902
|
+
const members = new Map();
|
|
903
|
+
const memberNodes = [];
|
|
904
|
+
for (const m of node.members) {
|
|
905
|
+
const name = ts.isIdentifier(m.name) || ts.isStringLiteral(m.name)
|
|
906
|
+
? m.name.text : m.name.getText(sf);
|
|
907
|
+
memberNodes.push({ name, node: m.name });
|
|
908
|
+
const value = stringValue(ts, m.initializer);
|
|
909
|
+
if (value !== null) members.set(name, value);
|
|
910
|
+
}
|
|
911
|
+
if (members.size) model.stringEnums.set(node.name.text, members);
|
|
912
|
+
model.enumDeclarations.push({ name: node.name.text, sf, members: memberNodes });
|
|
913
|
+
}
|
|
914
|
+
ts.forEachChild(node, walk);
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Enum MEMBER keys are PascalCase — `NotFilled = 'Not filled'`. The key is the
|
|
920
|
+
* name the code reads; the string on the right is what the platform stores, and
|
|
921
|
+
* the two are free to differ. A snake_case or lowercase key reads as data
|
|
922
|
+
* rather than as an identifier and drifts from every other enum in the repo.
|
|
923
|
+
*/
|
|
924
|
+
function checkEnumDeclarations(ts, model, findings) {
|
|
925
|
+
for (const decl of model.enumDeclarations) {
|
|
926
|
+
for (const m of decl.members) {
|
|
927
|
+
if (!isPascalCase(m.name)) {
|
|
928
|
+
findings.push(finding(ts, decl.sf, m.node, "W611",
|
|
929
|
+
`enum member '${decl.name}.${m.name}' is not PascalCase — write '${pascalize(m.name)}'. ` +
|
|
930
|
+
`The member KEY is PascalCase; its stored value is the string on the right and need not match ` +
|
|
931
|
+
`(e.g. NotFilled = 'Not filled')`));
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/** Best-effort PascalCase suggestion for an existing member key. */
|
|
938
|
+
function pascalize(name) {
|
|
939
|
+
return name
|
|
940
|
+
.split(/[^A-Za-z0-9]+/)
|
|
941
|
+
.filter(Boolean)
|
|
942
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
943
|
+
.join("") || name;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
// ── extraction: seeds ──
|
|
947
|
+
function analyzeSeeds(ts, sf, model) {
|
|
948
|
+
sf.forEachChild(function walk(node) {
|
|
949
|
+
if (ts.isVariableStatement(node)) {
|
|
950
|
+
for (const decl of node.declarationList.declarations) {
|
|
951
|
+
const init = decl.initializer;
|
|
952
|
+
if (init && ts.isCallExpression(init) &&
|
|
953
|
+
isSdkQualified(ts, sf, init.expression, "createSeed")) {
|
|
954
|
+
const exportName = ts.isIdentifier(decl.name) ? decl.name.text : decl.name.getText(sf);
|
|
955
|
+
const arg0 = init.arguments[0];
|
|
956
|
+
let entityName = stringValue(ts, arg0);
|
|
957
|
+
let enumRef = null;
|
|
958
|
+
if (entityName === null && arg0 && ts.isPropertyAccessExpression(arg0) &&
|
|
959
|
+
ts.isIdentifier(arg0.expression)) {
|
|
960
|
+
// EntityNamesEnum.Workflows — the VALUE is what createSeed receives;
|
|
961
|
+
// the member name is only a fallback (resolved after every file is read)
|
|
962
|
+
enumRef = { enumName: arg0.expression.text, member: arg0.name.text };
|
|
963
|
+
}
|
|
964
|
+
const arg1 = init.arguments[1];
|
|
965
|
+
let records = null; // null = thin seed (imported payload) — record checks impossible
|
|
966
|
+
if (arg1 && ts.isArrayLiteralExpression(arg1)) {
|
|
967
|
+
records = arg1.elements.map((e) =>
|
|
968
|
+
ts.isObjectLiteralExpression(e)
|
|
969
|
+
? { node: e, map: objectMap(ts, e), hasSpread: spreadElements(ts, e).length > 0, spreads: spreadElements(ts, e), byRef: false }
|
|
970
|
+
: { node: e, map: new Map(), hasSpread: false, spreads: [], byRef: true },
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
model.seeds.push({ exportName, entityName, enumRef, sf, node: init, records });
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
ts.forEachChild(node, walk);
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// ── rules: seeds ──
|
|
982
|
+
/**
|
|
983
|
+
* `{ name: '<global enum name>', value: '<enum value>' }`. Extra keys are legal
|
|
984
|
+
* (the doc gives the shape, it never forbids more). A reference or a spread is
|
|
985
|
+
* unverifiable — warn instead of hard-erroring on code that may well be correct.
|
|
986
|
+
*/
|
|
987
|
+
function checkEnumRef(ts, sf, key, valNode, prop, findings) {
|
|
988
|
+
const root = unwrapExpr(ts, valNode);
|
|
989
|
+
if (root.kind === ts.SyntaxKind.NullKeyword) return;
|
|
990
|
+
const multi = prop.kind === "multi-enum";
|
|
991
|
+
const shapeHint = multi ? " — an array of those for @MultiEnum" : "";
|
|
992
|
+
const unverifiable = () => findings.push(finding(ts, sf, valNode, "W606",
|
|
993
|
+
`cannot statically verify enum reference '${key}' — it is delivered by reference or spread, so the shape check (E206) is skipped; inline { name, value } to restore checking`));
|
|
994
|
+
const wrongShape = (n) => findings.push(finding(ts, sf, n, "E206",
|
|
995
|
+
`enum reference '${key}' must be { name: '<global enum name>', value: '<enum value>' }${shapeHint}`));
|
|
996
|
+
|
|
997
|
+
let refs;
|
|
998
|
+
if (multi) {
|
|
999
|
+
if (!ts.isArrayLiteralExpression(root)) {
|
|
1000
|
+
if (isOpaqueExpression(ts, root)) return unverifiable();
|
|
1001
|
+
return wrongShape(valNode);
|
|
1002
|
+
}
|
|
1003
|
+
if (arraySpreadElements(ts, root).length > 0) return unverifiable();
|
|
1004
|
+
refs = root.elements.map((e) => unwrapExpr(ts, e));
|
|
1005
|
+
} else {
|
|
1006
|
+
refs = [root];
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
for (const ref of refs) {
|
|
1010
|
+
if (!ts.isObjectLiteralExpression(ref)) {
|
|
1011
|
+
if (isOpaqueExpression(ts, ref)) { unverifiable(); continue; }
|
|
1012
|
+
wrongShape(ref);
|
|
1013
|
+
continue;
|
|
1014
|
+
}
|
|
1015
|
+
if (spreadElements(ts, ref).length > 0) { unverifiable(); continue; }
|
|
1016
|
+
const m = objectMap(ts, ref);
|
|
1017
|
+
if (!m.has("name") || !m.has("value")) { wrongShape(ref); continue; }
|
|
1018
|
+
const given = stringValue(ts, m.get("name"));
|
|
1019
|
+
if (prop.enumGlobalName && given !== null && given !== prop.enumGlobalName) {
|
|
1020
|
+
findings.push(finding(ts, sf, m.get("name"), "E206",
|
|
1021
|
+
`enum reference '${key}' names global enum '${given}', but the property is declared as @${multi ? "MultiEnum" : "Enum"}('${prop.enumGlobalName}') — the names must match`));
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
function checkSeed(ts, seed, model, findings) {
|
|
1027
|
+
const sf = seed.sf;
|
|
1028
|
+
const entity = seed.entityName ? model.entityByName.get(seed.entityName) : undefined;
|
|
1029
|
+
if (!entity) {
|
|
1030
|
+
// platform entity, a typo, or simply a file outside the analyzed set — say so
|
|
1031
|
+
findings.push(finding(ts, sf, seed.node, "W606",
|
|
1032
|
+
`target entity '${seed.entityName ?? "<unresolved>"}' of seed '${seed.exportName}' is not among the analyzed entities — record-level checks (E203-E206, W605) are skipped for it`));
|
|
1033
|
+
} else if (!entity.importKeyFieldsUnreadable &&
|
|
1034
|
+
(!entity.importKeyFields || entity.importKeyFields.length === 0)) {
|
|
1035
|
+
findings.push(finding(ts, sf, seed.node, "W605",
|
|
1036
|
+
`entity '${seed.entityName}' has a seed but no importKeyFields — "seeds have nothing to match on and cannot stay idempotent"`));
|
|
1037
|
+
}
|
|
1038
|
+
if (!seed.records) return;
|
|
1039
|
+
for (const rec of seed.records) {
|
|
1040
|
+
if (rec.byRef) {
|
|
1041
|
+
findings.push(finding(ts, sf, rec.node, "W606",
|
|
1042
|
+
`seed record passed by reference cannot be statically validated — E201-E206 and importKeyFields checks are skipped for it; inline the record object to restore checking`));
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
for (const [key, valNode] of rec.map) {
|
|
1046
|
+
if (SYSTEM_COLUMNS.includes(key)) {
|
|
1047
|
+
findings.push(finding(ts, sf, valNode, "E201",
|
|
1048
|
+
`seed record contains system column '${key}' — "they differ per tenant and corrupt matching"`));
|
|
1049
|
+
}
|
|
1050
|
+
if (ACCESS_METADATA_KEYS.includes(key)) {
|
|
1051
|
+
findings.push(finding(ts, sf, valNode, "E202",
|
|
1052
|
+
`seed record contains '${key}' — "access metadata, not data"`));
|
|
1053
|
+
}
|
|
1054
|
+
const prop = entity?.properties.get(key);
|
|
1055
|
+
if (!prop) continue;
|
|
1056
|
+
if (prop.kind === "many-to-one" || prop.kind === "many-to-many") {
|
|
1057
|
+
const isNum = (n) =>
|
|
1058
|
+
ts.isNumericLiteral(n) ||
|
|
1059
|
+
(ts.isPrefixUnaryExpression(n) && ts.isNumericLiteral(n.operand));
|
|
1060
|
+
const bad = isNum(valNode) ||
|
|
1061
|
+
(ts.isArrayLiteralExpression(valNode) && valNode.elements.some(isNum));
|
|
1062
|
+
if (bad) {
|
|
1063
|
+
findings.push(finding(ts, sf, valNode, "E203",
|
|
1064
|
+
`relation '${key}' referenced by numeric id — ids "do not match across tenants"; reference through the target's importKeyFields, e.g. { name: 'Default' }`));
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
if (prop.kind === "one-to-many" || prop.kind === "many-to-many-backref") {
|
|
1068
|
+
findings.push(finding(ts, sf, valNode, "E204",
|
|
1069
|
+
`reverse relation field '${key}' (@${prop.decorator.name}) in a seed (even as []) — "back references cannot be inserted; the import throws". Seed the owning entity with its own seed.`));
|
|
1070
|
+
}
|
|
1071
|
+
if (prop.kind === "enum" || prop.kind === "multi-enum") {
|
|
1072
|
+
checkEnumRef(ts, sf, key, valNode, prop, findings);
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
for (const spread of rec.spreads ?? []) {
|
|
1076
|
+
findings.push(finding(ts, sf, spread, "W606",
|
|
1077
|
+
`seed record content delivered via spread cannot be statically validated — E201-E206 checks are skipped for spread content; inline the record fields to restore checking`));
|
|
1078
|
+
}
|
|
1079
|
+
if (rec.hasSpread) continue; // spread may deliver the import key — E205 would false-positive
|
|
1080
|
+
if (!entity) continue;
|
|
1081
|
+
|
|
1082
|
+
// $key overrides identity per record — but only if it can establish one
|
|
1083
|
+
if (rec.map.has("$key")) {
|
|
1084
|
+
const keyNode = rec.map.get("$key");
|
|
1085
|
+
const keyValue = unwrapExpr(ts, keyNode);
|
|
1086
|
+
if (ts.isObjectLiteralExpression(keyValue)) {
|
|
1087
|
+
if (keyValue.properties.length === 0) {
|
|
1088
|
+
findings.push(finding(ts, sf, keyNode, "E205",
|
|
1089
|
+
`seed record's $key is an empty object — it cannot establish identity for '${seed.entityName}' ⇒ duplicate rows after every restart. Give $key the fields that identify the record.`));
|
|
1090
|
+
}
|
|
1091
|
+
} else if (isOpaqueExpression(ts, keyValue)) {
|
|
1092
|
+
findings.push(finding(ts, sf, keyNode, "W606",
|
|
1093
|
+
`cannot statically verify the $key of this '${seed.entityName}' record — it is delivered by reference, so the identity check (E205) is skipped; inline the $key object to restore checking`));
|
|
1094
|
+
} else {
|
|
1095
|
+
findings.push(finding(ts, sf, keyNode, "E205",
|
|
1096
|
+
`seed record's $key must be an object of identifying fields, e.g. $key: { name: 'Default' } — this value cannot establish identity for '${seed.entityName}' ⇒ duplicate rows after every restart.`));
|
|
1097
|
+
}
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
if (entity.importKeyFields?.length) {
|
|
1102
|
+
for (const f of entity.importKeyFields) {
|
|
1103
|
+
if (!rec.map.has(f)) {
|
|
1104
|
+
findings.push(finding(ts, sf, rec.node, "E205",
|
|
1105
|
+
`seed record is missing importKeyField '${f}' of '${seed.entityName}' — nothing matches ⇒ duplicate rows after every restart. Provide it or override identity with $key.`));
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
// ── extraction: EntityModule.forFeature ──
|
|
1113
|
+
/**
|
|
1114
|
+
* Doc resolution order: "If the entities array is passed AND NOT EMPTY it is
|
|
1115
|
+
* used and entitiesPath is ignored; otherwise entities are loaded from
|
|
1116
|
+
* entitiesPath" — same for seeds/seedsPath. The two sides are independent, so
|
|
1117
|
+
* each gets its own mode: "explicit" | "autoload" | "unverifiable".
|
|
1118
|
+
*/
|
|
1119
|
+
function analyzeModules(ts, sf, model) {
|
|
1120
|
+
sf.forEachChild(function walk(node) {
|
|
1121
|
+
if (ts.isCallExpression(node) &&
|
|
1122
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
1123
|
+
node.expression.name.text === "forFeature" &&
|
|
1124
|
+
isSdkQualified(ts, sf, node.expression.expression, "EntityModule")) {
|
|
1125
|
+
const a0 = node.arguments[0] ? unwrapExpr(ts, node.arguments[0]) : undefined;
|
|
1126
|
+
const a0IsArray = a0 && ts.isArrayLiteralExpression(a0);
|
|
1127
|
+
const a0IsOptions = a0 && ts.isObjectLiteralExpression(a0);
|
|
1128
|
+
// options live in argument 1 next to an entities array, or ARE argument 0
|
|
1129
|
+
const optionsNode = a0IsOptions ? a0 : node.arguments[1];
|
|
1130
|
+
const opts = objectMap(ts, optionsNode);
|
|
1131
|
+
|
|
1132
|
+
const side = (label, arrayNode, pathNode, unreadableNode) => {
|
|
1133
|
+
const s = {
|
|
1134
|
+
label, mode: "autoload", names: [], spreads: [],
|
|
1135
|
+
node, refNode: null, paths: null, pathNode: pathNode ?? null,
|
|
1136
|
+
};
|
|
1137
|
+
if (unreadableNode) {
|
|
1138
|
+
s.mode = "unverifiable";
|
|
1139
|
+
s.refNode = unreadableNode;
|
|
1140
|
+
return s;
|
|
1141
|
+
}
|
|
1142
|
+
if (arrayNode && arrayNode.elements.length > 0) {
|
|
1143
|
+
s.mode = "explicit";
|
|
1144
|
+
s.node = arrayNode;
|
|
1145
|
+
s.names = arrayNode.elements
|
|
1146
|
+
.filter((e) => ts.isIdentifier(e))
|
|
1147
|
+
.map((e) => resolveLocalName(sf, e.text)); // aliases resolve to the exported name
|
|
1148
|
+
s.spreads = arraySpreadElements(ts, arrayNode);
|
|
1149
|
+
return s;
|
|
1150
|
+
}
|
|
1151
|
+
// absent or empty array -> the path decides; read it when it is readable
|
|
1152
|
+
if (pathNode) {
|
|
1153
|
+
const single = stringValue(ts, pathNode);
|
|
1154
|
+
s.paths = single !== null ? [single] : stringArray(ts, pathNode);
|
|
1155
|
+
}
|
|
1156
|
+
return s;
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
const seedsNode = opts.get("seeds") ? unwrapExpr(ts, opts.get("seeds")) : undefined;
|
|
1160
|
+
const m = {
|
|
1161
|
+
sf, node,
|
|
1162
|
+
entities: side(
|
|
1163
|
+
"entitiesPath",
|
|
1164
|
+
a0IsArray ? a0 : null,
|
|
1165
|
+
opts.get("entitiesPath"),
|
|
1166
|
+
a0 && !a0IsArray && !a0IsOptions ? a0 : null,
|
|
1167
|
+
),
|
|
1168
|
+
seeds: side(
|
|
1169
|
+
"seedsPath",
|
|
1170
|
+
seedsNode && ts.isArrayLiteralExpression(seedsNode) ? seedsNode : null,
|
|
1171
|
+
opts.get("seedsPath"),
|
|
1172
|
+
seedsNode && !ts.isArrayLiteralExpression(seedsNode) ? seedsNode
|
|
1173
|
+
: (a0 && !a0IsArray && !a0IsOptions && !optionsNode ? a0 : null),
|
|
1174
|
+
),
|
|
1175
|
+
};
|
|
1176
|
+
model.modules.push(m);
|
|
1177
|
+
}
|
|
1178
|
+
ts.forEachChild(node, walk);
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// ── rules: registration ──
|
|
1183
|
+
/**
|
|
1184
|
+
* E301/E302 fire on the entity/seed source file, far from the module file
|
|
1185
|
+
* that actually needs the edit — name it explicitly so the finding is
|
|
1186
|
+
* actionable instead of just "unregistered". `side` selects which half of
|
|
1187
|
+
* forFeature() (`entities` or `seeds`) determines the classes-shape modules;
|
|
1188
|
+
* paths are shown relative to rootDir when derivable, else just the basename,
|
|
1189
|
+
* deduped, capped at 3 so the message stays short.
|
|
1190
|
+
*/
|
|
1191
|
+
function classShapeModulePaths(modules, side, rootDir) {
|
|
1192
|
+
const shapeModules = modules.filter((m) => m[side].mode === "explicit");
|
|
1193
|
+
if (shapeModules.length === 0) return "";
|
|
1194
|
+
const root = rootDir ? resolve(rootDir).replace(/\\/g, "/") : null;
|
|
1195
|
+
const raw = [...new Set(shapeModules.map((m) => m.sf.fileName))];
|
|
1196
|
+
const shown = raw.map((p) => {
|
|
1197
|
+
const norm = p.replace(/\\/g, "/");
|
|
1198
|
+
if (root && norm.startsWith(`${root}/`)) return norm.slice(root.length + 1);
|
|
1199
|
+
const parts = norm.split("/");
|
|
1200
|
+
return parts[parts.length - 1];
|
|
1201
|
+
});
|
|
1202
|
+
return shown.slice(0, 3).join(", ");
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
function checkRegistration(ts, model, findings, rootDir) {
|
|
1206
|
+
// E303/W606 on autoload paths — only for the side that actually autoloads:
|
|
1207
|
+
// a path the resolution order ignores is dead config, not a mistake.
|
|
1208
|
+
for (const m of model.modules) {
|
|
1209
|
+
for (const side of [m.entities, m.seeds]) {
|
|
1210
|
+
if (side.mode !== "autoload" || !side.pathNode) continue;
|
|
1211
|
+
if (side.paths === null) {
|
|
1212
|
+
findings.push(finding(ts, m.sf, side.pathNode, "W606",
|
|
1213
|
+
`cannot statically verify ${side.label} — it is delivered by reference or by an expression this checker cannot evaluate, so the dist/-vs-src/ check (E303) is skipped; inline the path string to restore checking`));
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
for (const p of side.paths) {
|
|
1217
|
+
if (/(^|\/)src(\/|$)/.test(p.replace(/\\/g, "/"))) {
|
|
1218
|
+
findings.push(finding(ts, m.sf, side.pathNode, "E303",
|
|
1219
|
+
`${side.label} '${p}' points at src/ — autoloading reads compiled output: "entitiesPath / seedsPath points at src/ instead of compiled dist/"`));
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
if (model.entities.size > 0 && model.modules.length === 0) {
|
|
1226
|
+
const first = [...model.entities.values()][0];
|
|
1227
|
+
findings.push(finding(ts, first.sf, first.node.name ?? first.node, "E305",
|
|
1228
|
+
`no EntityModule.forFeature() found — "An entity class that is never passed to forFeature() is invisible — no table is created for it"`));
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
for (const m of model.modules) {
|
|
1233
|
+
for (const spread of m.entities.spreads) {
|
|
1234
|
+
findings.push(finding(ts, m.sf, spread, "W606",
|
|
1235
|
+
`EntityModule.forFeature() entities array includes a spread — membership cannot be statically verified: E301 (unregistered entity) is skipped for entities potentially covered by it; inline the array to restore checking`));
|
|
1236
|
+
}
|
|
1237
|
+
for (const spread of m.seeds.spreads) {
|
|
1238
|
+
findings.push(finding(ts, m.sf, spread, "W606",
|
|
1239
|
+
`EntityModule.forFeature() seeds array includes a spread — membership and ordering cannot be statically verified: E302 (unregistered seed) and E304 (seed ordering) are skipped for seeds potentially covered by it; inline the array to restore checking`));
|
|
1240
|
+
}
|
|
1241
|
+
if (m.entities.mode === "unverifiable") {
|
|
1242
|
+
findings.push(finding(ts, m.sf, m.entities.refNode, "W606",
|
|
1243
|
+
`EntityModule.forFeature() entities are delivered by reference — membership cannot be statically verified: E301 (unregistered entity) is skipped; inline the array to restore checking`));
|
|
1244
|
+
}
|
|
1245
|
+
if (m.seeds.mode === "unverifiable" && m.seeds.refNode !== m.entities.refNode) {
|
|
1246
|
+
findings.push(finding(ts, m.sf, m.seeds.refNode, "W606",
|
|
1247
|
+
`EntityModule.forFeature() seeds are delivered by reference — membership and ordering cannot be statically verified: E302 (unregistered seed) and E304 (seed ordering) are skipped; inline the array to restore checking`));
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// an autoload (or unverifiable) module anywhere may cover any entity/seed not
|
|
1252
|
+
// named explicitly — membership becomes the platform's job, so E301/E302 would
|
|
1253
|
+
// only false-positive.
|
|
1254
|
+
const entitiesUnknown = model.modules.some((m) => m.entities.mode !== "explicit" || m.entities.spreads.length > 0);
|
|
1255
|
+
const seedsUnknown = model.modules.some((m) => m.seeds.mode !== "explicit" || m.seeds.spreads.length > 0);
|
|
1256
|
+
|
|
1257
|
+
if (!entitiesUnknown) {
|
|
1258
|
+
const registeredEntities = new Set(model.modules.flatMap((m) => m.entities.names));
|
|
1259
|
+
const modulePaths = classShapeModulePaths(model.modules, "entities", rootDir);
|
|
1260
|
+
for (const [className, info] of model.entities) {
|
|
1261
|
+
if (!registeredEntities.has(className)) {
|
|
1262
|
+
findings.push(finding(ts, info.sf, info.node.name ?? info.node, "E301",
|
|
1263
|
+
`entity class '${className}' is not in any forFeature() entities array — no table will be created for it` +
|
|
1264
|
+
(modulePaths ? ` — add it to the entities array in ${modulePaths}` : "")));
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
if (!seedsUnknown) {
|
|
1269
|
+
const registeredSeeds = new Set(model.modules.flatMap((m) => m.seeds.names));
|
|
1270
|
+
const modulePaths = classShapeModulePaths(model.modules, "seeds", rootDir);
|
|
1271
|
+
for (const seed of model.seeds) {
|
|
1272
|
+
if (!registeredSeeds.has(seed.exportName)) {
|
|
1273
|
+
findings.push(finding(ts, seed.sf, seed.node, "E302",
|
|
1274
|
+
`seed '${seed.exportName}' is not in any forFeature() seeds array — it will never run` +
|
|
1275
|
+
(modulePaths ? ` — add it to the seeds array in ${modulePaths}` : "")));
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
for (const m of model.modules) {
|
|
1281
|
+
if (m.seeds.mode !== "explicit" || m.seeds.spreads.length > 0) continue;
|
|
1282
|
+
const order = new Map(m.seeds.names.map((n, i) => [n, i]));
|
|
1283
|
+
const reported = new Set();
|
|
1284
|
+
for (const seed of model.seeds) {
|
|
1285
|
+
if (!order.has(seed.exportName) || !seed.records) continue;
|
|
1286
|
+
const entity = seed.entityName ? model.entityByName.get(seed.entityName) : undefined;
|
|
1287
|
+
if (!entity) continue;
|
|
1288
|
+
const myIdx = order.get(seed.exportName);
|
|
1289
|
+
for (const rec of seed.records) {
|
|
1290
|
+
for (const [key] of rec.map) {
|
|
1291
|
+
const prop = entity.properties.get(key);
|
|
1292
|
+
if (!prop || (prop.kind !== "many-to-one" && prop.kind !== "many-to-many") || !prop.targetClass) continue;
|
|
1293
|
+
const target = model.entities.get(prop.targetClass);
|
|
1294
|
+
if (!target || target === entity) continue;
|
|
1295
|
+
// an entity may have several seeds — what matters is the EARLIEST
|
|
1296
|
+
// registered one, since it is the one that creates the rows first
|
|
1297
|
+
const candidates = model.seeds.filter(
|
|
1298
|
+
(s) => s.entityName === target.entityName && order.has(s.exportName),
|
|
1299
|
+
);
|
|
1300
|
+
if (candidates.length === 0) continue;
|
|
1301
|
+
const earliest = candidates.reduce((a, b) =>
|
|
1302
|
+
order.get(a.exportName) <= order.get(b.exportName) ? a : b);
|
|
1303
|
+
const dedupe = `${seed.exportName}->${target.entityName}`;
|
|
1304
|
+
if (order.get(earliest.exportName) > myIdx && !reported.has(dedupe)) {
|
|
1305
|
+
reported.add(dedupe);
|
|
1306
|
+
findings.push(finding(ts, m.sf, m.seeds.node, "E304",
|
|
1307
|
+
`seed '${seed.exportName}' references '${target.entityName}' but '${earliest.exportName}' runs after it — "Register a seed after the seed that creates the records it points at"`));
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// ── rules: tsconfig ──
|
|
1316
|
+
function checkTsconfig(ts, rootDir, findings) {
|
|
1317
|
+
if (!rootDir) return;
|
|
1318
|
+
// ts.readConfigFile compares the path it is given against its own normalized
|
|
1319
|
+
// form while attaching a syntax diagnostic — a Windows path throws a Debug
|
|
1320
|
+
// Failure there, so normalize, and never let this check kill the run.
|
|
1321
|
+
try {
|
|
1322
|
+
const root = resolve(rootDir).replace(/\\/g, "/");
|
|
1323
|
+
const p = `${root}/tsconfig.json`;
|
|
1324
|
+
if (!existsSync(p)) {
|
|
1325
|
+
findings.push({
|
|
1326
|
+
file: root, line: 1, col: 1, code: "W608", level: "warning",
|
|
1327
|
+
message: `no tsconfig.json at ${root} — cannot verify experimentalDecorators/emitDecoratorMetadata, both of which entities rely on`,
|
|
1328
|
+
});
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const read = ts.readConfigFile(p, (f) => readFileSync(f, "utf-8"));
|
|
1332
|
+
if (read.error) return;
|
|
1333
|
+
const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, root);
|
|
1334
|
+
const missing = [];
|
|
1335
|
+
if (!parsed.options.experimentalDecorators) missing.push("experimentalDecorators");
|
|
1336
|
+
if (!parsed.options.emitDecoratorMetadata) missing.push("emitDecoratorMetadata");
|
|
1337
|
+
if (missing.length) {
|
|
1338
|
+
findings.push({
|
|
1339
|
+
file: p, line: 1, col: 1, code: "E401", level: "error",
|
|
1340
|
+
message: `tsconfig.json must enable ${missing.join(" and ")} — entities rely on decorator metadata`,
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
} catch {
|
|
1344
|
+
/* unreadable/malformed tsconfig — degrade silently rather than crash the run */
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// ── SDK catalog: real @suppa/sdk typings when resolvable, doc list otherwise ──
|
|
1349
|
+
function loadSdkCatalog(ts, rootDir) {
|
|
1350
|
+
const catalog = {
|
|
1351
|
+
entityOptions: [...DOC_ENTITY_OPTIONS],
|
|
1352
|
+
columnOptions: [...DOC_COLUMN_OPTIONS],
|
|
1353
|
+
fieldTypes: { ...DOC_FIELD_TYPES },
|
|
1354
|
+
source: "doc",
|
|
1355
|
+
};
|
|
1356
|
+
if (!rootDir) return catalog;
|
|
1357
|
+
let dir = resolve(rootDir);
|
|
1358
|
+
let sdkDir = null;
|
|
1359
|
+
for (let i = 0; i < 8; i++) {
|
|
1360
|
+
const cand = join(dir, "node_modules", "@suppa", "sdk");
|
|
1361
|
+
if (existsSync(cand)) { sdkDir = cand; break; }
|
|
1362
|
+
const parent = dirname(dir);
|
|
1363
|
+
if (parent === dir) break;
|
|
1364
|
+
dir = parent;
|
|
1365
|
+
}
|
|
1366
|
+
if (!sdkDir) return catalog;
|
|
1367
|
+
let typesPath = join(sdkDir, "index.d.ts");
|
|
1368
|
+
try {
|
|
1369
|
+
const pkg = JSON.parse(readFileSync(join(sdkDir, "package.json"), "utf-8"));
|
|
1370
|
+
if (pkg.types || pkg.typings) typesPath = join(sdkDir, pkg.types ?? pkg.typings);
|
|
1371
|
+
} catch { /* fall back to index.d.ts */ }
|
|
1372
|
+
if (!existsSync(typesPath)) return catalog;
|
|
1373
|
+
try {
|
|
1374
|
+
const sf = ts.createSourceFile(typesPath, readFileSync(typesPath, "utf-8"), ts.ScriptTarget.Latest, true);
|
|
1375
|
+
const fieldTypes = {};
|
|
1376
|
+
let entityOptions = [];
|
|
1377
|
+
let columnOptions = [];
|
|
1378
|
+
sf.forEachChild(function walk(node) {
|
|
1379
|
+
if (ts.isEnumDeclaration(node) && node.name.text === "FieldTypeEnum") {
|
|
1380
|
+
for (const m of node.members) {
|
|
1381
|
+
const name = ts.isIdentifier(m.name) || ts.isStringLiteral(m.name)
|
|
1382
|
+
? m.name.text : m.name.getText(sf);
|
|
1383
|
+
fieldTypes[name] = m.initializer && ts.isStringLiteral(m.initializer)
|
|
1384
|
+
? m.initializer.text : name;
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
if (ts.isInterfaceDeclaration(node)) {
|
|
1388
|
+
const props = node.members
|
|
1389
|
+
.filter((mm) => ts.isPropertySignature(mm) && mm.name)
|
|
1390
|
+
.map((mm) => mm.name.getText(sf).replace(/['"]/g, ""));
|
|
1391
|
+
if (/EntityOptions$/i.test(node.name.text)) entityOptions = props;
|
|
1392
|
+
if (/ColumnOptions$/i.test(node.name.text)) columnOptions = props;
|
|
1393
|
+
}
|
|
1394
|
+
ts.forEachChild(node, walk);
|
|
1395
|
+
});
|
|
1396
|
+
let got = 0;
|
|
1397
|
+
// fieldTypes are enum VALUES, not option names — an older SDK enum member
|
|
1398
|
+
// set is the true, current set for this install, so replace outright.
|
|
1399
|
+
// entityOptions/columnOptions are OPTION NAMES: an SDK whose typings
|
|
1400
|
+
// predate a doc-documented option (e.g. relationEntityName) must not make
|
|
1401
|
+
// that option look unknown (E501) — union with the doc list instead of
|
|
1402
|
+
// replacing it (F1).
|
|
1403
|
+
if (Object.keys(fieldTypes).length) { catalog.fieldTypes = fieldTypes; got++; }
|
|
1404
|
+
if (entityOptions.length) {
|
|
1405
|
+
catalog.entityOptions = [...new Set([...entityOptions, ...DOC_ENTITY_OPTIONS])];
|
|
1406
|
+
got++;
|
|
1407
|
+
}
|
|
1408
|
+
if (columnOptions.length) {
|
|
1409
|
+
catalog.columnOptions = [...new Set([...columnOptions, ...DOC_COLUMN_OPTIONS])];
|
|
1410
|
+
got++;
|
|
1411
|
+
}
|
|
1412
|
+
catalog.source = got === 3 ? "sdk" : got > 0 ? "partial-sdk" : "doc";
|
|
1413
|
+
} catch { /* typings unreadable — doc fallback stands */ }
|
|
1414
|
+
return catalog;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
// ── public API ──
|
|
1418
|
+
export function validateSchema({ files = [], rootDir = null } = {}) {
|
|
1419
|
+
const ts = loadTypescript(rootDir);
|
|
1420
|
+
const sdk = loadSdkCatalog(ts, rootDir);
|
|
1421
|
+
const model = {
|
|
1422
|
+
entities: new Map(), entityByName: new Map(),
|
|
1423
|
+
seeds: [], modules: [], stringEnums: new Map(),
|
|
1424
|
+
enumDeclarations: [], stringLiteralCounts: new Map(),
|
|
1425
|
+
};
|
|
1426
|
+
const findings = [];
|
|
1427
|
+
// the CLI deduped; the exported API did not, and duplicated seeds duplicated findings
|
|
1428
|
+
const uniqueFiles = [...new Set(files.map((f) => resolve(f)))];
|
|
1429
|
+
const sources = uniqueFiles.map((f) =>
|
|
1430
|
+
ts.createSourceFile(f, readFileSync(f, "utf-8"), ts.ScriptTarget.Latest, true),
|
|
1431
|
+
);
|
|
1432
|
+
for (const sf of sources) analyzeImports(ts, sf);
|
|
1433
|
+
for (const sf of sources) analyzeEnums(ts, sf, model);
|
|
1434
|
+
for (const sf of sources) {
|
|
1435
|
+
analyzeEntities(ts, sf, model);
|
|
1436
|
+
analyzeSeeds(ts, sf, model);
|
|
1437
|
+
analyzeModules(ts, sf, model);
|
|
1438
|
+
}
|
|
1439
|
+
// createSeed(EnumName.Member) passes the member's VALUE; the member name is
|
|
1440
|
+
// only a fallback for enums declared outside the analyzed file set
|
|
1441
|
+
for (const seed of model.seeds) {
|
|
1442
|
+
if (seed.entityName === null && seed.enumRef) {
|
|
1443
|
+
seed.entityName =
|
|
1444
|
+
model.stringEnums.get(seed.enumRef.enumName)?.get(seed.enumRef.member) ??
|
|
1445
|
+
seed.enumRef.member;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
checkEnumDeclarations(ts, model, findings);
|
|
1449
|
+
for (const info of model.entities.values()) checkEntity(ts, info, sdk, model, findings);
|
|
1450
|
+
for (const seed of model.seeds) checkSeed(ts, seed, model, findings);
|
|
1451
|
+
checkRegistration(ts, model, findings, rootDir);
|
|
1452
|
+
checkTsconfig(ts, rootDir, findings);
|
|
1453
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.col - b.col);
|
|
1454
|
+
return {
|
|
1455
|
+
findings,
|
|
1456
|
+
errors: findings.filter((f) => f.level === "error"),
|
|
1457
|
+
warnings: findings.filter((f) => f.level === "warning"),
|
|
1458
|
+
sdkSource: sdk.source,
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
// ── discovery ──
|
|
1463
|
+
export function discoverFiles(rootDir) {
|
|
1464
|
+
const files = [];
|
|
1465
|
+
(function walk(dir) {
|
|
1466
|
+
for (const name of readdirSync(dir)) {
|
|
1467
|
+
if (name === "node_modules" || name === "dist" || name.startsWith(".")) continue;
|
|
1468
|
+
const p = join(dir, name);
|
|
1469
|
+
if (statSync(p).isDirectory()) {
|
|
1470
|
+
walk(p);
|
|
1471
|
+
} else if (name.endsWith(".entity.ts") || name.endsWith(".seed.ts")) {
|
|
1472
|
+
files.push(p);
|
|
1473
|
+
} else if (name.endsWith(".ts") &&
|
|
1474
|
+
readFileSync(p, "utf-8").includes("EntityModule.forFeature")) {
|
|
1475
|
+
files.push(p);
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
})(resolve(rootDir));
|
|
1479
|
+
return files.sort();
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// ── CLI ──
|
|
1483
|
+
const isMain =
|
|
1484
|
+
process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href;
|
|
1485
|
+
if (isMain) {
|
|
1486
|
+
const args = process.argv.slice(2);
|
|
1487
|
+
if (args.length === 0) {
|
|
1488
|
+
console.error("usage: node validate-suppa-schema.mjs <app-root-or-files...>");
|
|
1489
|
+
process.exit(2);
|
|
1490
|
+
}
|
|
1491
|
+
let rootDir = null;
|
|
1492
|
+
const files = [];
|
|
1493
|
+
for (const a of args) {
|
|
1494
|
+
const p = resolve(a);
|
|
1495
|
+
if (!existsSync(p)) {
|
|
1496
|
+
console.error(`validate-suppa-schema: path not found: ${a}`);
|
|
1497
|
+
process.exit(2);
|
|
1498
|
+
}
|
|
1499
|
+
if (statSync(p).isDirectory()) {
|
|
1500
|
+
rootDir = rootDir ?? p;
|
|
1501
|
+
files.push(...discoverFiles(p));
|
|
1502
|
+
} else {
|
|
1503
|
+
files.push(p);
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
const dedupedFiles = [...new Set(files)];
|
|
1507
|
+
let res;
|
|
1508
|
+
try {
|
|
1509
|
+
res = validateSchema({ files: dedupedFiles, rootDir });
|
|
1510
|
+
} catch (err) {
|
|
1511
|
+
// A missing/unusable toolchain is the caller's to fix: one actionable line,
|
|
1512
|
+
// same exit code as the other argument problems. Anything else is a bug in
|
|
1513
|
+
// this validator and keeps its stack trace.
|
|
1514
|
+
if (err?.code !== "SUPPA_VALIDATOR_ENV") throw err;
|
|
1515
|
+
console.error(err.message);
|
|
1516
|
+
process.exit(2);
|
|
1517
|
+
}
|
|
1518
|
+
for (const f of res.findings) {
|
|
1519
|
+
console.log(
|
|
1520
|
+
`${displayPath(process.cwd(), f.file)}:${f.line}:${f.col} ${f.code} [${f.level}] ${f.message}`,
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
console.log(
|
|
1524
|
+
`\n${res.errors.length} error(s), ${res.warnings.length} warning(s) across ${dedupedFiles.length} file(s) (option catalog: ${res.sdkSource}).`,
|
|
1525
|
+
);
|
|
1526
|
+
process.exit(res.errors.length > 0 ? 1 : 0);
|
|
1527
|
+
}
|