@@ -562,10 +562,17 @@ namespace ts {
562
562
return emitNode && emitNode . flags || 0 ;
563
563
}
564
564
565
- export function getLiteralText ( node : LiteralLikeNode , sourceFile : SourceFile , neverAsciiEscape : boolean | undefined , jsxAttributeEscape : boolean ) {
565
+ export const enum GetLiteralTextFlags {
566
+ None = 0 ,
567
+ NeverAsciiEscape = 1 << 0 ,
568
+ JsxAttributeEscape = 1 << 1 ,
569
+ TerminateUnterminatedLiterals = 1 << 2 ,
570
+ }
571
+
572
+ export function getLiteralText ( node : LiteralLikeNode , sourceFile : SourceFile , flags : GetLiteralTextFlags ) {
566
573
// If we don't need to downlevel and we can reach the original source text using
567
574
// the node's parent reference, then simply get the text as it was originally written.
568
- if ( ! nodeIsSynthesized ( node ) && node . parent && ! (
575
+ if ( ! nodeIsSynthesized ( node ) && node . parent && ! ( flags & GetLiteralTextFlags . TerminateUnterminatedLiterals && node . isUnterminated ) && ! (
569
576
( isNumericLiteral ( node ) && node . numericLiteralFlags & TokenFlags . ContainsSeparator ) ||
570
577
isBigIntLiteral ( node )
571
578
) ) {
@@ -576,8 +583,8 @@ namespace ts {
576
583
// or a (possibly escaped) quoted form of the original text if it's string-like.
577
584
switch ( node . kind ) {
578
585
case SyntaxKind . StringLiteral : {
579
- const escapeText = jsxAttributeEscape ? escapeJsxAttributeString :
580
- neverAsciiEscape || ( getEmitFlags ( node ) & EmitFlags . NoAsciiEscaping ) ? escapeString :
586
+ const escapeText = flags & GetLiteralTextFlags . JsxAttributeEscape ? escapeJsxAttributeString :
587
+ flags & GetLiteralTextFlags . NeverAsciiEscape || ( getEmitFlags ( node ) & EmitFlags . NoAsciiEscaping ) ? escapeString :
581
588
escapeNonAsciiString ;
582
589
if ( ( < StringLiteral > node ) . singleQuote ) {
583
590
return "'" + escapeText ( node . text , CharacterCodes . singleQuote ) + "'" ;
@@ -592,7 +599,7 @@ namespace ts {
592
599
case SyntaxKind . TemplateTail : {
593
600
// If a NoSubstitutionTemplateLiteral appears to have a substitution in it, the original text
594
601
// had to include a backslash: `not \${a} substitution`.
595
- const escapeText = neverAsciiEscape || ( getEmitFlags ( node ) & EmitFlags . NoAsciiEscaping ) ? escapeString :
602
+ const escapeText = flags & GetLiteralTextFlags . NeverAsciiEscape || ( getEmitFlags ( node ) & EmitFlags . NoAsciiEscaping ) ? escapeString :
596
603
escapeNonAsciiString ;
597
604
598
605
const rawText = ( < TemplateLiteralLikeNode > node ) . rawText || escapeTemplateSubstitution ( escapeText ( node . text , CharacterCodes . backtick ) ) ;
@@ -610,7 +617,11 @@ namespace ts {
610
617
}
611
618
case SyntaxKind . NumericLiteral :
612
619
case SyntaxKind . BigIntLiteral :
620
+ return node . text ;
613
621
case SyntaxKind . RegularExpressionLiteral :
622
+ if ( flags & GetLiteralTextFlags . TerminateUnterminatedLiterals && node . isUnterminated ) {
623
+ return node . text + ( node . text . charCodeAt ( node . text . length - 1 ) === CharacterCodes . backslash ? " /" : "/" ) ;
624
+ }
614
625
return node . text ;
615
626
}
616
627
@@ -1858,7 +1869,7 @@ namespace ts {
1858
1869
1859
1870
export function getExternalModuleRequireArgument ( node : Node ) {
1860
1871
return isRequireVariableDeclaration ( node , /*requireStringLiteralLikeArgument*/ true )
1861
- && ( getLeftmostPropertyAccessExpression ( node . initializer ) as CallExpression ) . arguments [ 0 ] as StringLiteral ;
1872
+ && ( getLeftmostAccessExpression ( node . initializer ) as CallExpression ) . arguments [ 0 ] as StringLiteral ;
1862
1873
}
1863
1874
1864
1875
export function isInternalModuleImportEqualsDeclaration ( node : Node ) : node is ImportEqualsDeclaration {
@@ -1929,7 +1940,7 @@ namespace ts {
1929
1940
export function isRequireVariableDeclaration ( node : Node , requireStringLiteralLikeArgument : boolean ) : node is VariableDeclaration ;
1930
1941
export function isRequireVariableDeclaration ( node : Node , requireStringLiteralLikeArgument : boolean ) : node is VariableDeclaration {
1931
1942
node = getRootDeclaration ( node ) ;
1932
- return isVariableDeclaration ( node ) && ! ! node . initializer && isRequireCall ( getLeftmostPropertyAccessExpression ( node . initializer ) , requireStringLiteralLikeArgument ) ;
1943
+ return isVariableDeclaration ( node ) && ! ! node . initializer && isRequireCall ( getLeftmostAccessExpression ( node . initializer ) , requireStringLiteralLikeArgument ) ;
1933
1944
}
1934
1945
1935
1946
export function isRequireVariableStatement ( node : Node , requireStringLiteralLikeArgument = true ) : node is RequireVariableStatement {
@@ -5452,8 +5463,8 @@ namespace ts {
5452
5463
return node . kind === SyntaxKind . NamedImports || node . kind === SyntaxKind . NamedExports ;
5453
5464
}
5454
5465
5455
- export function getLeftmostPropertyAccessExpression ( expr : Expression ) : Expression {
5456
- while ( isPropertyAccessExpression ( expr ) ) {
5466
+ export function getLeftmostAccessExpression ( expr : Expression ) : Expression {
5467
+ while ( isAccessExpression ( expr ) ) {
5457
5468
expr = expr . expression ;
5458
5469
}
5459
5470
return expr ;
@@ -5922,6 +5933,10 @@ namespace ts {
5922
5933
return compilerOptions [ flag ] === undefined ? ! ! compilerOptions . strict : ! ! compilerOptions [ flag ] ;
5923
5934
}
5924
5935
5936
+ export function getAllowJSCompilerOption ( compilerOptions : CompilerOptions ) : boolean {
5937
+ return compilerOptions . allowJs === undefined ? ! ! compilerOptions . checkJs : compilerOptions . allowJs ;
5938
+ }
5939
+
5925
5940
export function compilerOptionsAffectSemanticDiagnostics ( newOptions : CompilerOptions , oldOptions : CompilerOptions ) : boolean {
5926
5941
return oldOptions !== newOptions &&
5927
5942
semanticDiagnosticsOptionDeclarations . some ( option => ! isJsonEqual ( getCompilerOptionValue ( oldOptions , option ) , getCompilerOptionValue ( newOptions , option ) ) ) ;
@@ -6375,7 +6390,7 @@ namespace ts {
6375
6390
export function getSupportedExtensions ( options ?: CompilerOptions ) : readonly Extension [ ] ;
6376
6391
export function getSupportedExtensions ( options ?: CompilerOptions , extraFileExtensions ?: readonly FileExtensionInfo [ ] ) : readonly string [ ] ;
6377
6392
export function getSupportedExtensions ( options ?: CompilerOptions , extraFileExtensions ?: readonly FileExtensionInfo [ ] ) : readonly string [ ] {
6378
- const needJsExtensions = options && options . allowJs ;
6393
+ const needJsExtensions = options && getAllowJSCompilerOption ( options ) ;
6379
6394
6380
6395
if ( ! extraFileExtensions || extraFileExtensions . length === 0 ) {
6381
6396
return needJsExtensions ? allSupportedExtensions : supportedTSExtensions ;
0 commit comments