@@ -447,6 +447,7 @@ import {
447
447
Pattern ,
448
448
PostfixUnaryExpression ,
449
449
PrefixUnaryExpression ,
450
+ PrimitiveLiteral ,
450
451
PrinterOptions ,
451
452
PrintHandlers ,
452
453
PrivateIdentifier ,
@@ -2350,6 +2351,14 @@ export function isLet(node: Node): boolean {
2350
2351
return ( getCombinedNodeFlags ( node ) & NodeFlags . BlockScoped ) === NodeFlags . Let ;
2351
2352
}
2352
2353
2354
+ /** @internal */
2355
+ export function isVarConstLike ( node : VariableDeclaration | VariableDeclarationList ) {
2356
+ const blockScopeKind = getCombinedNodeFlags ( node ) & NodeFlags . BlockScoped ;
2357
+ return blockScopeKind === NodeFlags . Const ||
2358
+ blockScopeKind === NodeFlags . Using ||
2359
+ blockScopeKind === NodeFlags . AwaitUsing ;
2360
+ }
2361
+
2353
2362
/** @internal */
2354
2363
export function isSuperCall ( n : Node ) : n is SuperCall {
2355
2364
return n . kind === SyntaxKind . CallExpression && ( n as CallExpression ) . expression . kind === SyntaxKind . SuperKeyword ;
@@ -10671,3 +10680,66 @@ export function replaceFirstStar(s: string, replacement: string): string {
10671
10680
export function getNameFromImportAttribute ( node : ImportAttribute ) {
10672
10681
return isIdentifier ( node . name ) ? node . name . escapedText : escapeLeadingUnderscores ( node . name . text ) ;
10673
10682
}
10683
+
10684
+ /** @internal */
10685
+ export function isPrimitiveLiteralValue ( node : Expression , includeBigInt = true ) : node is PrimitiveLiteral {
10686
+ Debug . type < PrimitiveLiteral > ( node ) ;
10687
+ switch ( node . kind ) {
10688
+ case SyntaxKind . TrueKeyword :
10689
+ case SyntaxKind . FalseKeyword :
10690
+ case SyntaxKind . NumericLiteral :
10691
+ case SyntaxKind . StringLiteral :
10692
+ case SyntaxKind . NoSubstitutionTemplateLiteral :
10693
+ return true ;
10694
+ case SyntaxKind . BigIntLiteral :
10695
+ return includeBigInt ;
10696
+ case SyntaxKind . PrefixUnaryExpression :
10697
+ if ( node . operator === SyntaxKind . MinusToken ) {
10698
+ return isNumericLiteral ( node . operand ) || ( includeBigInt && isBigIntLiteral ( node . operand ) ) ;
10699
+ }
10700
+ if ( node . operator === SyntaxKind . PlusToken ) {
10701
+ return isNumericLiteral ( node . operand ) ;
10702
+ }
10703
+ return false ;
10704
+ default :
10705
+ assertType < never > ( node ) ;
10706
+ return false ;
10707
+ }
10708
+ }
10709
+
10710
+ /**
10711
+ * @internal
10712
+ *
10713
+ * Clone literal value while normalizing it (converts octals/hex to base 10, uses double quotes strings)
10714
+ */
10715
+ export function clonePrimitiveLiteralValue < T extends PrimitiveLiteral > ( node : T ) : T ;
10716
+ export function clonePrimitiveLiteralValue ( node : PrimitiveLiteral ) : PrimitiveLiteral {
10717
+ switch ( node . kind ) {
10718
+ case SyntaxKind . NumericLiteral :
10719
+ return factory . createNumericLiteral ( node . text ) ;
10720
+ case SyntaxKind . BigIntLiteral :
10721
+ return factory . createBigIntLiteral ( { negative : false , base10Value : parsePseudoBigInt ( node . text ) } ) ;
10722
+ case SyntaxKind . StringLiteral :
10723
+ case SyntaxKind . NoSubstitutionTemplateLiteral :
10724
+ return factory . createStringLiteral ( node . text ) ;
10725
+ case SyntaxKind . FalseKeyword :
10726
+ return factory . createFalse ( ) ;
10727
+ case SyntaxKind . TrueKeyword :
10728
+ return factory . createTrue ( ) ;
10729
+ case SyntaxKind . PrefixUnaryExpression :
10730
+ Debug . assert ( isNumericLiteral ( node . operand ) || isBigIntLiteral ( node . operand ) ) ;
10731
+ if ( node . operator === SyntaxKind . PlusToken ) {
10732
+ return clonePrimitiveLiteralValue ( node . operand ) ;
10733
+ }
10734
+ else if ( node . operator === SyntaxKind . MinusToken ) {
10735
+ return factory . createPrefixUnaryExpression (
10736
+ node . operator ,
10737
+ clonePrimitiveLiteralValue ( node . operand ) ,
10738
+ ) ;
10739
+ }
10740
+ Debug . fail ( `Unable to clone prefixed unary expression with operator ${ Debug . formatSyntaxKind ( node . operator ) } ` ) ;
10741
+ break ;
10742
+ default :
10743
+ Debug . assertNever ( node , `Unable to clone unknown literal type.` ) ;
10744
+ }
10745
+ }
0 commit comments