@@ -42,6 +42,10 @@ namespace ts {
4242 context . onEmitNode = onEmitNode ;
4343 context . onSubstituteNode = onSubstituteNode ;
4444
45+ // Enable substitution for property/element access to emit const enum values.
46+ context . enableSubstitution ( SyntaxKind . PropertyAccessExpression ) ;
47+ context . enableSubstitution ( SyntaxKind . ElementAccessExpression ) ;
48+
4549 // These variables contain state that changes as we descend into the tree.
4650 let currentSourceFile : SourceFile ;
4751 let currentNamespace : ModuleDeclaration ;
@@ -3294,17 +3298,15 @@ namespace ts {
32943298 switch ( node . kind ) {
32953299 case SyntaxKind . Identifier :
32963300 return substituteExpressionIdentifier ( < Identifier > node ) ;
3297- }
3298-
3299- if ( enabledSubstitutions & TypeScriptSubstitutionFlags . AsyncMethodsWithSuper ) {
3300- switch ( node . kind ) {
3301- case SyntaxKind . CallExpression :
3301+ case SyntaxKind . PropertyAccessExpression :
3302+ return substitutePropertyAccessExpression ( < PropertyAccessExpression > node ) ;
3303+ case SyntaxKind . ElementAccessExpression :
3304+ return substituteElementAccessExpression ( < ElementAccessExpression > node ) ;
3305+ case SyntaxKind . CallExpression :
3306+ if ( enabledSubstitutions & TypeScriptSubstitutionFlags . AsyncMethodsWithSuper ) {
33023307 return substituteCallExpression ( < CallExpression > node ) ;
3303- case SyntaxKind . PropertyAccessExpression :
3304- return substitutePropertyAccessExpression ( < PropertyAccessExpression > node ) ;
3305- case SyntaxKind . ElementAccessExpression :
3306- return substituteElementAccessExpression ( < ElementAccessExpression > node ) ;
3307- }
3308+ }
3309+ break ;
33083310 }
33093311
33103312 return node ;
@@ -3381,7 +3383,7 @@ namespace ts {
33813383 }
33823384
33833385 function substitutePropertyAccessExpression ( node : PropertyAccessExpression ) {
3384- if ( node . expression . kind === SyntaxKind . SuperKeyword ) {
3386+ if ( enabledSubstitutions & TypeScriptSubstitutionFlags . AsyncMethodsWithSuper && node . expression . kind === SyntaxKind . SuperKeyword ) {
33853387 const flags = getSuperContainerAsyncMethodFlags ( ) ;
33863388 if ( flags ) {
33873389 return createSuperAccessInAsyncMethod (
@@ -3392,11 +3394,11 @@ namespace ts {
33923394 }
33933395 }
33943396
3395- return node ;
3397+ return substituteConstantValue ( node ) ;
33963398 }
33973399
33983400 function substituteElementAccessExpression ( node : ElementAccessExpression ) {
3399- if ( node . expression . kind === SyntaxKind . SuperKeyword ) {
3401+ if ( enabledSubstitutions & TypeScriptSubstitutionFlags . AsyncMethodsWithSuper && node . expression . kind === SyntaxKind . SuperKeyword ) {
34003402 const flags = getSuperContainerAsyncMethodFlags ( ) ;
34013403 if ( flags ) {
34023404 return createSuperAccessInAsyncMethod (
@@ -3407,9 +3409,39 @@ namespace ts {
34073409 }
34083410 }
34093411
3412+ return substituteConstantValue ( node ) ;
3413+ }
3414+
3415+ function substituteConstantValue ( node : PropertyAccessExpression | ElementAccessExpression ) : LeftHandSideExpression {
3416+ const constantValue = tryGetConstEnumValue ( node ) ;
3417+ if ( constantValue !== undefined ) {
3418+ const substitute = createLiteral ( constantValue ) ;
3419+ setSourceMapRange ( substitute , node ) ;
3420+ setCommentRange ( substitute , node ) ;
3421+ if ( ! compilerOptions . removeComments ) {
3422+ const propertyName = isPropertyAccessExpression ( node )
3423+ ? declarationNameToString ( node . name )
3424+ : getTextOfNode ( node . argumentExpression ) ;
3425+ substitute . trailingComment = ` ${ propertyName } ` ;
3426+ }
3427+
3428+ setConstantValue ( node , constantValue ) ;
3429+ return substitute ;
3430+ }
3431+
34103432 return node ;
34113433 }
34123434
3435+ function tryGetConstEnumValue ( node : Node ) : number {
3436+ if ( compilerOptions . isolatedModules ) {
3437+ return undefined ;
3438+ }
3439+
3440+ return isPropertyAccessExpression ( node ) || isElementAccessExpression ( node )
3441+ ? resolver . getConstantValue ( < PropertyAccessExpression | ElementAccessExpression > node )
3442+ : undefined ;
3443+ }
3444+
34133445 function createSuperAccessInAsyncMethod ( argumentExpression : Expression , flags : NodeCheckFlags , location : TextRange ) : LeftHandSideExpression {
34143446 if ( flags & NodeCheckFlags . AsyncMethodWithSuperBinding ) {
34153447 return createPropertyAccess (
0 commit comments