@@ -421,91 +421,49 @@ namespace ts {
421
421
* This method replace comment content by whitespace rather than completely remove them to keep positions in json parsing error reporting accurate.
422
422
*/
423
423
function removeComments ( jsonText : string ) : string {
424
- let result = "" ;
425
- let processingString = false ;
426
- let processingSingleLineComment = false ;
427
- let processingMultiLineComment = false ;
428
- for ( let i = 0 ; i < jsonText . length ; i ++ ) {
429
- let currentCharCode = jsonText . charCodeAt ( i ) ;
430
- let currentChar = jsonText . charAt ( i ) ;
431
- let nextCharCode = ( i + 1 < jsonText . length ) ? jsonText . charCodeAt ( i + 1 ) : undefined ;
432
- if ( processingString ) {
433
- if ( currentCharCode === CharacterCodes . backslash
434
- && nextCharCode !== undefined ) {
435
- // Found an escaped character
436
- // consume the \ and the escaped char
437
- result += currentChar ;
438
- result += jsonText . charAt ( i + 1 ) ;
439
- i += 1 ;
440
- }
441
- else if ( currentCharCode === CharacterCodes . doubleQuote ) {
442
- // End of string
443
- result += currentChar ;
444
- processingString = false ;
445
- }
446
- else {
447
- // String content
448
- result += currentChar ;
449
- }
450
- }
451
- else if ( processingSingleLineComment ) {
452
- if ( isLineBreak ( currentCharCode ) ) {
453
- // End of single line comment
454
- processingSingleLineComment = false ;
455
- // Keep the line breaks to keep line numbers aligned
456
- result += currentChar ;
457
- }
458
- else {
459
- // replace comment content by whitespaces
460
- result += " " ;
461
- }
424
+ let output = "" ;
425
+ let scanner = createScanner ( ScriptTarget . ES5 , /* skipTrivia */ false , LanguageVariant . Standard , jsonText ) ;
426
+ let token : SyntaxKind ;
427
+ while ( ( token = scanner . scan ( ) ) !== SyntaxKind . EndOfFileToken ) {
428
+ switch ( token ) {
429
+ case SyntaxKind . SingleLineCommentTrivia :
430
+ case SyntaxKind . MultiLineCommentTrivia :
431
+ // replace comments with whitespaces to preserve original characters position
432
+ output += replaceWithWhitespaces ( scanner . getTokenText ( ) ) ;
433
+ break ;
434
+ default :
435
+ output += scanner . getTokenText ( ) ;
436
+ break ;
462
437
}
463
- else if ( processingMultiLineComment ) {
464
- if ( currentCharCode === CharacterCodes . asterisk && nextCharCode === CharacterCodes . slash ) {
465
- // End of comment */
466
- result += " " ;
467
- i += 1 ;
468
- processingMultiLineComment = false ;
469
- }
470
- else if ( isLineBreak ( currentCharCode ) ) {
471
- // Keep the line breaks to Keep line aligned
472
- result += currentChar ;
438
+ }
439
+ return output ;
440
+
441
+ function replaceWithWhitespaces ( commentTokenText : string ) : string {
442
+ let result = "" ;
443
+ let pos = 0 ;
444
+ let start = 0 ;
445
+ while ( pos < commentTokenText . length ) {
446
+ if ( isLineBreak ( commentTokenText . charCodeAt ( pos ) ) ) {
447
+ let nbCharToReplace = pos - start ;
448
+ result += nSpaces ( nbCharToReplace ) ;
449
+ result += commentTokenText . charAt ( pos ) ;
450
+ pos += 1 ;
451
+ start = pos ;
473
452
}
474
453
else {
475
- // replace comment content by whitespaces
476
- result += " " ;
454
+ pos += 1 ;
477
455
}
478
456
}
479
- else if ( currentCharCode === CharacterCodes . doubleQuote ) {
480
- // String start
481
- result += currentChar ;
482
- processingString = true ;
483
- }
484
- else if ( currentCharCode === CharacterCodes . hash ) {
485
- // Start of # comment
486
- result += " " ;
487
- processingSingleLineComment = true ;
488
- }
489
- else if ( currentCharCode === CharacterCodes . slash && nextCharCode === CharacterCodes . slash ) {
490
- // Start of // comment
491
- result += " " ;
492
- i += 1 ;
493
- processingSingleLineComment = true ;
494
- }
495
- else if ( currentCharCode === CharacterCodes . slash && nextCharCode === CharacterCodes . asterisk ) {
496
- // Start of /**/ comment
497
- result += " " ;
498
- i += 1 ;
499
- processingMultiLineComment = true ;
500
- }
501
- else {
502
- // Keep other characters
503
- result += currentChar ;
504
- }
457
+ result += nSpaces ( pos - start ) ;
458
+ return result ;
459
+
460
+ function nSpaces ( n : number ) : string {
461
+ return new Array ( n + 1 ) . join ( " " ) ;
462
+ } ;
505
463
}
506
- return result ;
507
464
}
508
465
466
+
509
467
/**
510
468
* Parse the contents of a config file (tsconfig.json).
511
469
* @param json The contents of the config file to parse
0 commit comments