-
Notifications
You must be signed in to change notification settings - Fork 242
/
validator.ts
825 lines (785 loc) · 24.9 KB
/
validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import * as spec from '@jsii/spec';
import * as assert from 'assert';
import * as Case from 'case';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import deepEqual = require('deep-equal');
import * as ts from 'typescript';
import { Emitter } from './emitter';
import { JsiiDiagnostic } from './jsii-diagnostic';
import { getRelatedNode } from './node-bindings';
import * as bindings from './node-bindings';
import { ProjectInfo } from './project-info';
export class Validator implements Emitter {
public static VALIDATIONS: ValidationFunction[] = _defaultValidations();
private _diagnostics = new Array<JsiiDiagnostic>();
public constructor(
public readonly projectInfo: ProjectInfo,
public readonly assembly: spec.Assembly,
) {}
public async emit(): Promise<ts.EmitResult> {
this._diagnostics = [];
for (const validation of Validator.VALIDATIONS) {
validation(this, this.assembly, (diag) => this._diagnostics.push(diag));
}
try {
return await Promise.resolve({
diagnostics: this._diagnostics,
emitSkipped: this._diagnostics.some(
(diag) => diag.category === ts.DiagnosticCategory.Error,
),
});
} finally {
// Clearing ``this._diagnostics`` to allow contents to be garbage-collected.
delete this._diagnostics;
}
}
}
export type DiagnosticEmitter = (diag: JsiiDiagnostic) => void;
export type ValidationFunction = (
validator: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) => void;
function _defaultValidations(): ValidationFunction[] {
return [
_typeNamesMustUsePascalCase,
_enumMembersMustUserUpperSnakeCase,
_memberNamesMustUseCamelCase,
_staticConstantNamesMustUseUpperSnakeCase,
_memberNamesMustNotLookLikeJavaGettersOrSetters,
_allTypeReferencesAreValid,
_inehritanceDoesNotChangeContracts,
_staticMembersAndNestedTypesMustNotSharePascalCaseName,
];
function _typeNamesMustUsePascalCase(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const type of _allTypes(assembly)) {
if (type.name !== Case.pascal(type.name)) {
diagnostic(
JsiiDiagnostic.JSII_8000_PASCAL_CASED_TYPE_NAMES.createDetached(
type.name,
),
);
}
}
}
function _enumMembersMustUserUpperSnakeCase(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const type of _allTypes(assembly)) {
if (!spec.isEnumType(type)) {
continue;
}
for (const member of type.members) {
if (member.name && !isConstantCase(member.name)) {
diagnostic(
JsiiDiagnostic.JSII_8001_ALL_CAPS_ENUM_MEMBERS.createDetached(
member.name,
type.fqn,
),
);
}
}
}
}
function _memberNamesMustUseCamelCase(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const { member, type } of _allMembers(assembly)) {
if (member.static && (member as spec.Property).const) {
continue;
}
if (member.name && member.name !== Case.camel(member.name)) {
diagnostic(
JsiiDiagnostic.JSII_8002_CAMEL_CASED_MEMBERS.createDetached(
member.name,
type.fqn,
),
);
}
}
}
function _staticConstantNamesMustUseUpperSnakeCase(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const { member, type } of _allMembers(assembly)) {
if (!member.static || !(member as spec.Property).const) {
continue;
}
if (
member.name &&
!isConstantCase(member.name) &&
member.name !== Case.pascal(member.name) &&
member.name !== Case.camel(member.name)
) {
diagnostic(
JsiiDiagnostic.JSII_8003_STATIC_CONST_CASING.createDetached(
member.name,
type.name,
),
);
}
}
}
function _memberNamesMustNotLookLikeJavaGettersOrSetters(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const { member, type } of _allMembers(assembly)) {
if (!member.name) {
continue;
}
const snakeName = Case.snake(member.name);
if (
snakeName.startsWith('get_') &&
_isEmpty((member as spec.Method).parameters)
) {
diagnostic(
JsiiDiagnostic.JSII_5000_JAVA_GETTERS.createDetached(
member.name,
type.name,
),
);
} else if (
snakeName.startsWith('set_') &&
((member as spec.Method).parameters ?? []).length === 1
) {
diagnostic(
JsiiDiagnostic.JSII_5001_JAVA_SETTERS.createDetached(
member.name,
type.name,
),
);
}
}
}
function _allTypeReferencesAreValid(
validator: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const typeRef of _allTypeReferences(assembly)) {
const [assm] = typeRef.fqn.split('.');
if (assembly.name === assm) {
if (!(typeRef.fqn in (assembly.types ?? {}))) {
diagnostic(
JsiiDiagnostic.JSII_3000_EXPORTED_API_USES_HIDDEN_TYPE.create(
typeRef.node!, // Pretend there is always a value
typeRef.fqn,
),
);
}
continue;
}
const foreignAssm = validator.projectInfo.dependencyClosure.find(
(dep) => dep.name === assm,
);
if (!foreignAssm) {
diagnostic(
JsiiDiagnostic.JSII_9000_UNKNOWN_MODULE.createDetached(assm),
);
continue;
}
if (!(typeRef.fqn in (foreignAssm.types ?? {}))) {
diagnostic(
JsiiDiagnostic.JSII_9001_TYPE_NOT_FOUND.createDetached(typeRef),
);
}
}
}
function _inehritanceDoesNotChangeContracts(
validator: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const type of _allTypes(assembly)) {
if (spec.isClassType(type)) {
for (const method of type.methods ?? []) {
_validateMethodOverride(method, type);
}
for (const property of type.properties ?? []) {
_validatePropertyOverride(property, type);
}
}
if (
spec.isClassOrInterfaceType(type) &&
(type.interfaces?.length ?? 0) > 0
) {
for (const method of _allImplementations(type, (t) => t.methods)) {
_validateMethodImplementation(method, type);
}
for (const property of _allImplementations(type, (t) => t.properties)) {
_validatePropertyImplementation(property, type);
}
}
}
/**
* Lists all "implementations" from the given type, using the provided
* implementation getter. Note that abstract members may be part of the
* result (in particular, if `type` is an interface type, or if it's an
* abstract class with unimplemented members) -- I just couldn't come up
* with a name that actually describes this.
*
* @param type the type which implemented members are needed.
* @param getter the getter to obtain methods or properties from the type.
*
* @returns a list of members (possibly empty, always defined)
*/
function _allImplementations<T extends spec.Property | spec.Method>(
type: spec.ClassType | spec.InterfaceType,
getter: (type: spec.ClassType | spec.InterfaceType) => T[] | undefined,
): T[] {
const result = new Array<T>();
const known = new Set<string>();
for (const member of getter(type) ?? []) {
result.push(member);
known.add(member.name);
}
if (spec.isClassType(type) && type.base) {
// We have a parent class, collect their concrete members, too (recursively)...
const base = _dereference(type.base, assembly, validator);
assert(base != null && spec.isClassType(base));
for (const member of _allImplementations(base, getter)) {
if (known.has(member.name)) {
continue;
}
result.push(member);
known.add(member.name);
}
}
return result;
}
function _validateMethodOverride(
method: spec.Method,
type: spec.ClassType,
): boolean {
if (!type.base) {
return false;
}
const baseType = _dereference(
type.base,
assembly,
validator,
) as spec.ClassType;
if (!baseType) {
return false;
}
const overridden = (baseType.methods ?? []).find(
(m) => m.name === method.name,
);
if (!overridden) {
return _validateMethodOverride(method, baseType);
}
_assertSignaturesMatch(
overridden,
method,
`${type.fqn}#${method.name}`,
`overriding ${baseType.fqn}`,
);
method.overrides = baseType.fqn;
return true;
}
function _validatePropertyOverride(
property: spec.Property,
type: spec.ClassType,
): boolean {
if (!type.base) {
return false;
}
const baseType = _dereference(
type.base,
assembly,
validator,
) as spec.ClassType;
if (!baseType) {
return false;
}
const overridden = (baseType.properties ?? []).find(
(p) => p.name === property.name,
);
if (!overridden) {
return _validatePropertyOverride(property, baseType);
}
_assertPropertiesMatch(
overridden,
property,
`${type.fqn}#${property.name}`,
`overriding ${baseType.fqn}`,
);
property.overrides = baseType.fqn;
return true;
}
function _validateMethodImplementation(
method: spec.Method,
type: spec.ClassType | spec.InterfaceType,
): boolean {
if (!type.interfaces) {
// Abstract classes may not directly implement all members, need to check their supertypes...
if (spec.isClassType(type) && type.base && type.abstract) {
return _validateMethodImplementation(
method,
_dereference(type.base, assembly, validator) as spec.ClassType,
);
}
return false;
}
for (const iface of type.interfaces) {
const ifaceType = _dereference(
iface,
assembly,
validator,
) as spec.InterfaceType;
const implemented = (ifaceType.methods ?? []).find(
(m) => m.name === method.name,
);
if (implemented) {
_assertSignaturesMatch(
implemented,
method,
`${type.fqn}#${method.name}`,
`implementing ${ifaceType.fqn}`,
);
// We won't replace a previous overrides declaration from a method override, as those have
// higher precedence than an initial implementation.
method.overrides = method.overrides ?? iface;
return true;
}
if (_validateMethodImplementation(method, ifaceType)) {
return true;
}
}
return false;
}
function _validatePropertyImplementation(
property: spec.Property,
type: spec.ClassType | spec.InterfaceType,
): boolean {
if (!type.interfaces) {
// Abstract classes may not directly implement all members, need to check their supertypes...
if (spec.isClassType(type) && type.base && type.abstract) {
return _validatePropertyImplementation(
property,
_dereference(type.base, assembly, validator) as spec.ClassType,
);
}
return false;
}
for (const iface of type.interfaces) {
const ifaceType = _dereference(
iface,
assembly,
validator,
) as spec.InterfaceType;
const implemented = (ifaceType.properties ?? []).find(
(p) => p.name === property.name,
);
if (implemented) {
_assertPropertiesMatch(
implemented,
property,
`${type.fqn}#${property.name}`,
`implementing ${ifaceType.fqn}`,
);
// We won't replace a previous overrides declaration from a property override, as those
// have higher precedence than an initial implementation.
property.overrides = property.overrides ?? ifaceType.fqn;
return true;
}
if (_validatePropertyImplementation(property, ifaceType)) {
return true;
}
}
return false;
}
function _assertSignaturesMatch(
expected: spec.Method,
actual: spec.Method,
label: string,
action: string,
) {
if (!!expected.protected !== !!actual.protected) {
const expVisibility = expected.protected ? 'protected' : 'public';
const actVisibility = actual.protected ? 'protected' : 'public';
diagnostic(
JsiiDiagnostic.JSII_5002_OVERRIDE_CHANGES_VISIBILITY.createDetached(
label,
action,
actVisibility,
expVisibility,
),
);
}
if (!deepEqual(actual.returns, expected.returns)) {
const expType = spec.describeTypeReference(expected.returns?.type);
const actType = spec.describeTypeReference(actual.returns?.type);
diagnostic(
JsiiDiagnostic.JSII_5003_OVERRIDE_CHANGES_RETURN_TYPE.createDetached(
label,
action,
actType,
expType,
),
);
}
const expectedParams = expected.parameters ?? [];
const actualParams = actual.parameters ?? [];
if (expectedParams.length !== actualParams.length) {
diagnostic(
JsiiDiagnostic.JSII_5005_OVERRIDE_CHANGES_PARAM_COUNT.createDetached(
label,
action,
actualParams.length,
expectedParams.length,
),
);
return;
}
for (let i = 0; i < expectedParams.length; i++) {
const expParam = expectedParams[i];
const actParam = actualParams[i];
if (!deepEqual(expParam.type, actParam.type)) {
diagnostic(
JsiiDiagnostic.JSII_5006_OVERRIDE_CHANGES_PARAM_TYPE.createDetached(
label,
action,
actParam,
expParam,
),
);
}
// Not-ing those to force the values to a strictly boolean context (they're optional, undefined means false)
if (expParam.variadic !== actParam.variadic) {
diagnostic(
JsiiDiagnostic.JSII_5007_OVERRIDE_CHANGES_VARIADIC.createDetached(
label,
action,
actParam.variadic,
expParam.variadic,
),
);
}
if (expParam.optional !== actParam.optional) {
diagnostic(
JsiiDiagnostic.JSII_5008_OVERRIDE_CHANGES_PARAM_OPTIONAL.createDetached(
label,
action,
actParam,
expParam,
),
);
}
}
}
function _assertPropertiesMatch(
expected: spec.Property,
actual: spec.Property,
label: string,
action: string,
) {
const actualNode = bindings.getPropertyRelatedNode(actual);
const expectedNode = bindings.getPropertyRelatedNode(expected);
if (!!expected.protected !== !!actual.protected) {
const expVisibility = expected.protected ? 'protected' : 'public';
const actVisibility = actual.protected ? 'protected' : 'public';
diagnostic(
JsiiDiagnostic.JSII_5002_OVERRIDE_CHANGES_VISIBILITY.create(
actualNode?.modifiers?.find(
(mod) =>
mod.kind === ts.SyntaxKind.PublicKeyword ||
mod.kind === ts.SyntaxKind.ProtectedKeyword,
) ?? declarationName(actualNode),
label,
action,
actVisibility,
expVisibility,
).maybeAddRelatedInformation(
expectedNode?.modifiers?.find(
(mod) =>
mod.kind === ts.SyntaxKind.PublicKeyword ||
mod.kind === ts.SyntaxKind.ProtectedKeyword,
) ?? declarationName(expectedNode),
'The implemented delcaration is here.',
),
);
}
if (!deepEqual(expected.type, actual.type)) {
diagnostic(
JsiiDiagnostic.JSII_5004_OVERRIDE_CHANGES_PROP_TYPE.create(
actualNode?.type ?? declarationName(actualNode),
label,
action,
actual.type,
expected.type,
).maybeAddRelatedInformation(
expectedNode?.type ?? declarationName(expectedNode),
'The implemented delcaration is here.',
),
);
}
if (expected.immutable !== actual.immutable) {
diagnostic(
JsiiDiagnostic.JSII_5010_OVERRIDE_CHANGES_MUTABILITY.create(
actualNode?.modifiers?.find(
(mod) => mod.kind === ts.SyntaxKind.ReadonlyKeyword,
) ?? declarationName(actualNode),
label,
action,
actual.immutable,
expected.immutable,
).maybeAddRelatedInformation(
expectedNode?.modifiers?.find(
(mod) => mod.kind === ts.SyntaxKind.ReadonlyKeyword,
) ?? declarationName(expectedNode),
'The implemented delcaration is here.',
),
);
}
if (expected.optional !== actual.optional) {
diagnostic(
JsiiDiagnostic.JSII_5009_OVERRIDE_CHANGES_PROP_OPTIONAL.create(
actualNode?.questionToken ??
actualNode?.type ??
declarationName(actualNode),
label,
action,
actual.optional,
expected.optional,
).maybeAddRelatedInformation(
expectedNode?.questionToken ??
expectedNode?.type ??
declarationName(expectedNode),
'The implemented delcaration is here.',
),
);
}
}
}
function _staticMembersAndNestedTypesMustNotSharePascalCaseName(
_: Validator,
assembly: spec.Assembly,
diagnostic: DiagnosticEmitter,
) {
for (const nestedType of Object.values(assembly.types ?? {})) {
if (nestedType.namespace == null) {
continue;
}
const nestingType =
assembly.types![`${assembly.name}.${nestedType.namespace}`];
if (nestingType == null) {
continue;
}
const nestedTypeName = Case.pascal(nestedType.name);
for (const { name, member } of staticMembers(nestingType)) {
if (name === nestedTypeName) {
let diag =
JsiiDiagnostic.JSII_5020_STATIC_MEMBER_CONFLICTS_WITH_NESTED_TYPE.create(
getRelatedNode(member)!,
nestingType,
member,
nestedType,
);
const nestedTypeNode = getRelatedNode(nestedType);
if (nestedTypeNode != null) {
diag = diag.addRelatedInformation(
nestedTypeNode,
'This is the conflicting nested type declaration',
);
}
diagnostic(diag);
}
}
}
function staticMembers(type: spec.Type) {
if (spec.isClassOrInterfaceType(type)) {
return [
...(type.methods?.filter((method) => method.static) ?? []),
...(type.properties?.filter((prop) => prop.static) ?? []),
].map((member) => ({ name: Case.pascal(member.name), member }));
}
return type.members.map((member) => ({ name: member.name, member }));
}
}
}
function _allTypes(assm: spec.Assembly): spec.Type[] {
return Object.values(assm.types ?? {});
}
function _allMethods(
assm: spec.Assembly,
): Array<{ member: spec.Method; type: spec.Type }> {
const methods = new Array<{ member: spec.Method; type: spec.Type }>();
for (const type of _allTypes(assm)) {
if (!spec.isClassOrInterfaceType(type)) {
continue;
}
if (!type.methods) {
continue;
}
type.methods.forEach((method) => methods.push({ member: method, type }));
}
return methods;
}
function _allProperties(
assm: spec.Assembly,
): Array<{ member: spec.Property; type: spec.Type }> {
const properties = new Array<{ member: spec.Property; type: spec.Type }>();
for (const type of _allTypes(assm)) {
if (!spec.isClassOrInterfaceType(type)) {
continue;
}
if (!type.properties) {
continue;
}
type.properties.forEach((property) =>
properties.push({ member: property, type }),
);
}
return properties;
}
function _allMembers(
assm: spec.Assembly,
): Array<{ member: spec.Property | spec.Method; type: spec.Type }> {
return [..._allMethods(assm), ..._allProperties(assm)];
}
interface AnnotatedTypeReference extends spec.NamedTypeReference {
readonly node: ts.Node | undefined;
}
function _allTypeReferences(
assm: spec.Assembly,
): readonly AnnotatedTypeReference[] {
const typeReferences = new Array<AnnotatedTypeReference>();
for (const type of _allTypes(assm)) {
if (!spec.isClassOrInterfaceType(type)) {
continue;
}
if (spec.isClassType(type)) {
const node = bindings.getClassRelatedNode(type);
if (type.base) {
typeReferences.push({
fqn: type.base,
node: node?.heritageClauses?.find(
(hc) => hc.token === ts.SyntaxKind.ExtendsKeyword,
)?.types[0],
});
}
if (type.initializer?.parameters) {
for (const param of type.initializer.parameters) {
_collectTypeReferences(
param.type,
bindings.getParameterRelatedNode(param)?.type,
);
}
}
}
if (type.interfaces) {
const node = bindings.getClassOrInterfaceRelatedNode(type);
type.interfaces.forEach((iface) =>
typeReferences.push({
fqn: iface,
node: node?.heritageClauses?.find(
(hc) =>
hc.token ===
(spec.isInterfaceType(type)
? ts.SyntaxKind.ImplementsKeyword
: ts.SyntaxKind.ExtendsKeyword),
),
}),
);
}
}
for (const { member: prop } of _allProperties(assm)) {
_collectTypeReferences(
prop.type,
bindings.getPropertyRelatedNode(prop)?.type,
);
}
for (const { member: meth } of _allMethods(assm)) {
if (meth.returns) {
_collectTypeReferences(
meth.returns.type,
bindings.getMethodRelatedNode(meth)?.type,
);
}
for (const param of meth.parameters ?? []) {
_collectTypeReferences(
param.type,
bindings.getParameterRelatedNode(param)?.type,
);
}
}
return typeReferences;
function _collectTypeReferences(
type: spec.TypeReference,
node: ts.Node | undefined,
): void {
if (spec.isNamedTypeReference(type)) {
typeReferences.push({ ...type, node });
} else if (spec.isCollectionTypeReference(type)) {
_collectTypeReferences(type.collection.elementtype, node);
} else if (spec.isUnionTypeReference(type)) {
type.union.types.forEach((type) => _collectTypeReferences(type, node));
}
}
}
function _dereference(
typeRef: string | spec.NamedTypeReference,
assembly: spec.Assembly,
validator: Validator,
): spec.Type | undefined {
if (typeof typeRef !== 'string') {
typeRef = typeRef.fqn;
}
const [assm] = typeRef.split('.');
if (assembly.name === assm) {
return assembly.types?.[typeRef];
}
const foreignAssm = validator.projectInfo.dependencyClosure.find(
(dep) => dep.name === assm,
);
return foreignAssm?.types?.[typeRef];
}
function _isEmpty(array: undefined | any[]): array is undefined {
return array == null || array.length === 0;
}
/**
* Return whether an identifier only consists of upperchase characters, digits and underscores
*
* We have our own check here (isConstantCase) which is more lenient than what
* `case.constant()` prescribes. We also want to allow combinations of letters
* and digits without underscores: `C5A`, which `case` would force to `C5_A`.
* The hint we print will still use `case.constant()` but that is fine.
*/
function isConstantCase(x: string) {
return !/[^A-Z0-9_]/.exec(x);
}
/**
* Obtains the name of the given declaration, if it has one, or returns the declaration itself.
* This function is meant to be used as a convenience to obtain the `ts.Node` to bind a
* `JsiiDianostic` instance on.
*
* It may return `undefined` but is typed as `ts.Node` so that it is easier to use with
* `JsiiDiagnostic` factories.
*
* @param decl the declaration which name is needed.
*
* @returns the name of the declaration if it has one, or the declaration itself. Might return
* `undefined` if the provided declaration is undefined.
*/
function declarationName(
decl: ts.Declaration | ts.Expression | undefined,
): ts.Node {
if (decl == null) {
// Pretend we returned a node - this is used to create diagnostics, worst case it'll be unbound.
return decl as any;
}
return ts.getNameOfDeclaration(decl) ?? decl;
}