@@ -53,7 +53,7 @@ export function isDynamicValue(value: any): value is DynamicValue {
53
53
* non-primitive value, or a special `DynamicValue` type which indicates the value was not
54
54
* available statically.
55
55
*/
56
- export type ResolvedValue = number | boolean | string | null | undefined | Reference |
56
+ export type ResolvedValue = number | boolean | string | null | undefined | Reference | EnumValue |
57
57
ResolvedValueArray | ResolvedValueMap | DynamicValue ;
58
58
59
59
/**
@@ -72,6 +72,15 @@ export interface ResolvedValueArray extends Array<ResolvedValue> {}
72
72
* `ResolvedValue`.
73
73
*/ export interface ResolvedValueMap extends Map < string , ResolvedValue > { }
74
74
75
+ /**
76
+ * A value member of an enumeration.
77
+ *
78
+ * Contains a `Reference` to the enumeration itself, and the name of the referenced member.
79
+ */
80
+ export class EnumValue {
81
+ constructor ( readonly enumRef : Reference < ts . EnumDeclaration > , readonly name : string ) { }
82
+ }
83
+
75
84
/**
76
85
* Tracks the scope of a function body, which includes `ResolvedValue`s for the parameters of that
77
86
* body.
@@ -438,13 +447,27 @@ class StaticInterpreter {
438
447
return context . scope . get ( node ) ! ;
439
448
} else if ( ts . isExportAssignment ( node ) ) {
440
449
return this . visitExpression ( node . expression , context ) ;
450
+ } else if ( ts . isEnumDeclaration ( node ) ) {
451
+ return this . visitEnumDeclaration ( node , context ) ;
441
452
} else if ( ts . isSourceFile ( node ) ) {
442
453
return this . visitSourceFile ( node , context ) ;
443
454
} else {
444
455
return this . getReference ( node , context ) ;
445
456
}
446
457
}
447
458
459
+ private visitEnumDeclaration ( node : ts . EnumDeclaration , context : Context ) : ResolvedValue {
460
+ const enumRef = this . getReference ( node , context ) as Reference < ts . EnumDeclaration > ;
461
+ const map = new Map < string , EnumValue > ( ) ;
462
+ node . members . forEach ( member => {
463
+ const name = this . stringNameFromPropertyName ( member . name , context ) ;
464
+ if ( name !== undefined ) {
465
+ map . set ( name , new EnumValue ( enumRef , name ) ) ;
466
+ }
467
+ } ) ;
468
+ return map ;
469
+ }
470
+
448
471
private visitElementAccessExpression ( node : ts . ElementAccessExpression , context : Context ) :
449
472
ResolvedValue {
450
473
const lhs = this . visitExpression ( node . expression , context ) ;
@@ -689,6 +712,8 @@ function literal(value: ResolvedValue): any {
689
712
function identifierOfDeclaration ( decl : ts . Declaration ) : ts . Identifier | undefined {
690
713
if ( ts . isClassDeclaration ( decl ) ) {
691
714
return decl . name ;
715
+ } else if ( ts . isEnumDeclaration ( decl ) ) {
716
+ return decl . name ;
692
717
} else if ( ts . isFunctionDeclaration ( decl ) ) {
693
718
return decl . name ;
694
719
} else if ( ts . isVariableDeclaration ( decl ) && ts . isIdentifier ( decl . name ) ) {
0 commit comments