1
1
import {
2
2
__String ,
3
+ BigIntLiteral ,
3
4
bindSourceFile ,
4
5
CompilerOptions ,
5
6
ComputedPropertyName ,
@@ -14,6 +15,7 @@ import {
14
15
determineIfDeclarationIsVisible ,
15
16
ElementAccessExpression ,
16
17
emptyArray ,
18
+ entityNameToString ,
17
19
EnumDeclaration ,
18
20
EnumMember ,
19
21
ExportSpecifier ,
@@ -35,7 +37,6 @@ import {
35
37
Identifier ,
36
38
InternalSymbolName ,
37
39
isAccessor ,
38
- isBigIntLiteral ,
39
40
isBinaryExpression ,
40
41
isComputedPropertyName ,
41
42
isDeclarationReadonly ,
@@ -54,39 +55,39 @@ import {
54
55
isIdentifier ,
55
56
isInfinityOrNaNString ,
56
57
isModuleDeclaration ,
57
- isNumericLiteral ,
58
- isPrefixUnaryExpression ,
59
58
isPrimitiveLiteralValue ,
60
59
isPropertyAccessExpression ,
61
60
isPropertyName ,
62
61
isSetAccessor ,
63
62
isSetAccessorDeclaration ,
64
63
isStringLiteralLike ,
65
- isTemplateExpression ,
66
64
isVarConst ,
67
65
isVariableDeclaration ,
68
66
LateBoundDeclaration ,
69
- MemberName ,
70
67
ModifierFlags ,
71
68
Node ,
72
69
NodeFlags ,
73
70
nodeIsPresent ,
74
71
NoSubstitutionTemplateLiteral ,
72
+ NumericLiteral ,
75
73
objectAllocator ,
76
74
ParameterDeclaration ,
77
75
parsePseudoBigInt ,
76
+ PrefixUnaryExpression ,
78
77
PropertyAccessExpression ,
79
78
PropertyDeclaration ,
80
79
PropertyName ,
81
80
PropertySignature ,
82
81
skipParentheses ,
83
82
some ,
84
83
SourceFile ,
84
+ StringLiteralLike ,
85
85
Symbol ,
86
86
SymbolAccessibility ,
87
87
SymbolFlags ,
88
88
SymbolTable ,
89
89
SyntaxKind ,
90
+ TemplateExpression ,
90
91
VariableDeclaration ,
91
92
} from "../../_namespaces/ts" ;
92
93
@@ -357,48 +358,44 @@ export function createEmitDeclarationResolver(file: SourceFile, options: Compile
357
358
onNumericLiteral ( ) { } ,
358
359
} ) ;
359
360
function clonePrimitiveLiteralValue ( node : Expression ) : Expression {
360
- if ( isNumericLiteral ( node ) ) {
361
- return factory . createNumericLiteral ( node . text ) ;
362
- }
363
- if ( isBigIntLiteral ( node ) ) {
364
- return factory . createBigIntLiteral ( { negative : false , base10Value : parsePseudoBigInt ( node . text ) } ) ;
365
- }
366
- if ( isStringLiteralLike ( node ) ) {
367
- return factory . createStringLiteral ( node . text ) ;
368
- }
369
-
370
- if ( node . kind === SyntaxKind . FalseKeyword ) {
371
- return factory . createFalse ( ) ;
372
- }
373
-
374
- if ( node . kind === SyntaxKind . TrueKeyword ) {
375
- return factory . createTrue ( ) ;
376
- }
377
-
378
- if ( isPrefixUnaryExpression ( node ) ) {
379
- return factory . createPrefixUnaryExpression (
380
- node . operator ,
381
- clonePrimitiveLiteralValue ( node . operand ) ,
382
- ) ;
383
- }
384
- if ( isTemplateExpression ( node ) ) {
385
- const evaluatedValue = evaluate ( node ) ;
386
- if ( evaluatedValue !== undefined ) {
387
- return factory . createStringLiteral ( evaluatedValue ) ;
388
- }
389
- return factory . createTemplateExpression (
390
- factory . createTemplateHead ( node . head . text , node . head . rawText , node . head . templateFlags ) ,
391
- node . templateSpans . map ( t =>
392
- factory . createTemplateSpan (
393
- clonePrimitiveLiteralValue ( t . expression ) ,
394
- t . literal . kind === SyntaxKind . TemplateMiddle ?
395
- factory . createTemplateMiddle ( t . literal . text , t . literal . rawText , t . literal . templateFlags ) :
396
- factory . createTemplateTail ( t . literal . text , t . literal . rawText , t . literal . templateFlags ) ,
397
- )
398
- ) ,
399
- ) ;
361
+ switch ( node . kind ) {
362
+ case SyntaxKind . NumericLiteral :
363
+ return factory . createNumericLiteral ( ( node as NumericLiteral ) . text ) ;
364
+ case SyntaxKind . BigIntLiteral :
365
+ return factory . createBigIntLiteral ( { negative : false , base10Value : parsePseudoBigInt ( ( node as BigIntLiteral ) . text ) } ) ;
366
+ case SyntaxKind . StringLiteral :
367
+ case SyntaxKind . NoSubstitutionTemplateLiteral :
368
+ return factory . createStringLiteral ( ( node as StringLiteralLike ) . text ) ;
369
+ case SyntaxKind . FalseKeyword :
370
+ return factory . createFalse ( ) ;
371
+ case SyntaxKind . TrueKeyword :
372
+ return factory . createTrue ( ) ;
373
+ case SyntaxKind . PrefixUnaryExpression :
374
+ return factory . createPrefixUnaryExpression (
375
+ ( node as PrefixUnaryExpression ) . operator ,
376
+ clonePrimitiveLiteralValue ( ( node as PrefixUnaryExpression ) . operand ) ,
377
+ ) ;
378
+ case SyntaxKind . TemplateExpression :
379
+ const templateExpression = node as TemplateExpression
380
+ const evaluatedValue = evaluate ( templateExpression ) ;
381
+ if ( evaluatedValue !== undefined ) {
382
+ return factory . createStringLiteral ( evaluatedValue ) ;
383
+ }
384
+ const templateHead = templateExpression . head
385
+ return factory . createTemplateExpression (
386
+ factory . createTemplateHead ( templateHead . text , templateHead . rawText , templateHead . templateFlags ) ,
387
+ templateExpression . templateSpans . map ( t =>
388
+ factory . createTemplateSpan (
389
+ clonePrimitiveLiteralValue ( t . expression ) ,
390
+ t . literal . kind === SyntaxKind . TemplateMiddle ?
391
+ factory . createTemplateMiddle ( t . literal . text , t . literal . rawText , t . literal . templateFlags ) :
392
+ factory . createTemplateTail ( t . literal . text , t . literal . rawText , t . literal . templateFlags ) ,
393
+ )
394
+ ) ,
395
+ ) ;
396
+ default :
397
+ Debug . assert ( false , `Unable to clone unknown literal type. Kind: ${ node . kind } ` ) ;
400
398
}
401
- Debug . assert ( false , `Unable to clone unknown literal type. Kind: ${ node . kind } ` ) ;
402
399
}
403
400
404
401
function isLiteralConstDeclaration ( node : VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration ) : boolean {
@@ -627,37 +624,12 @@ function getSymbolName(name: ElementAccessExpression | PropertyName): __String |
627
624
return getDynamicSymbolName ( name ) ;
628
625
}
629
626
630
- function getEntityNameComponent ( identifier : MemberName ) : __String {
631
- const name = getPropertyNameForPropertyNameNode ( identifier ) ;
632
- if ( name && name . indexOf ( "." ) ) {
633
- return name . replace ( / \. / g, ".." ) as __String ;
634
- }
635
- return name ;
636
- }
637
-
638
627
function getDynamicSymbolName ( name : ElementAccessExpression | PropertyName ) {
639
- let computedName = isComputedPropertyName ( name ) ? name . expression :
628
+ const computedName = isComputedPropertyName ( name ) ? name . expression :
640
629
isElementAccessExpression ( name ) ? name . argumentExpression :
641
630
undefined ;
642
-
643
- if ( computedName ) {
644
- let fullId = "__!" ;
645
- // We only support dotted identifiers as property keys
646
- while ( true ) {
647
- if ( isIdentifier ( computedName ) ) {
648
- const componentName = getEntityNameComponent ( computedName ) ;
649
- fullId += componentName ;
650
- return fullId as __String ;
651
- }
652
- else if ( isPropertyAccessExpression ( computedName ) ) {
653
- const componentName = getEntityNameComponent ( computedName . name ) ;
654
- computedName = computedName . expression ;
655
- fullId += componentName + "." ;
656
- }
657
- else {
658
- return undefined ;
659
- }
660
- }
631
+ if ( computedName && isEntityNameExpression ( computedName ) ) {
632
+ return ( "__!" + entityNameToString ( computedName ) ) as __String ;
661
633
}
662
634
return undefined ;
663
635
}
0 commit comments