@@ -2501,47 +2501,21 @@ module ts {
25012501 }
25022502
25032503 function isInStringOrRegularExpressionOrTemplateLiteral ( previousToken : Node ) : boolean {
2504- if ( previousToken . kind === SyntaxKind . StringLiteral || isTemplateLiteralKind ( previousToken . kind ) ) {
2504+ if ( previousToken . kind === SyntaxKind . StringLiteral
2505+ || previousToken . kind === SyntaxKind . RegularExpressionLiteral
2506+ || isTemplateLiteralKind ( previousToken . kind ) ) {
25052507 // The position has to be either: 1. entirely within the token text, or
2506- // 2. at the end position, and the string literal is not terminated
2507-
2508+ // 2. at the end position of an unterminated token.
25082509 var start = previousToken . getStart ( ) ;
25092510 var end = previousToken . getEnd ( ) ;
25102511
25112512 if ( start < position && position < end ) {
25122513 return true ;
25132514 }
25142515 else if ( position === end ) {
2515- var width = end - start ;
2516- var text = previousToken . getSourceFile ( ) . text ;
2517-
2518- // If the token is a single character, or its second-to-last charcter indicates an escape code,
2519- // then we can immediately say that we are in the middle of an unclosed string.
2520- if ( width <= 1 || text . charCodeAt ( end - 2 ) === CharacterCodes . backslash ) {
2521- return true ;
2522- }
2523-
2524- // Now check if the last character is a closing character for the token.
2525- switch ( previousToken . kind ) {
2526- case SyntaxKind . StringLiteral :
2527- case SyntaxKind . NoSubstitutionTemplateLiteral :
2528- return text . charCodeAt ( start ) !== text . charCodeAt ( end - 1 ) ;
2529-
2530- case SyntaxKind . TemplateHead :
2531- case SyntaxKind . TemplateMiddle :
2532- return text . charCodeAt ( end - 1 ) !== CharacterCodes . openBrace
2533- || text . charCodeAt ( end - 2 ) !== CharacterCodes . $ ;
2534-
2535- case SyntaxKind . TemplateTail :
2536- return text . charCodeAt ( end - 1 ) !== CharacterCodes . backtick ;
2537- }
2538-
2539- return false ;
2516+ return ! ! ( < LiteralExpression > previousToken ) . isUnterminated ;
25402517 }
25412518 }
2542- else if ( previousToken . kind === SyntaxKind . RegularExpressionLiteral ) {
2543- return previousToken . getStart ( ) < position && position < previousToken . getEnd ( ) ;
2544- }
25452519
25462520 return false ;
25472521 }
@@ -5665,23 +5639,29 @@ module ts {
56655639 addResult ( end - start , classFromKind ( token ) ) ;
56665640
56675641 if ( end >= text . length ) {
5668- // We're at the end.
56695642 if ( token === SyntaxKind . StringLiteral ) {
56705643 // Check to see if we finished up on a multiline string literal.
56715644 var tokenText = scanner . getTokenText ( ) ;
5672- if ( tokenText . length > 0 && tokenText . charCodeAt ( tokenText . length - 1 ) === CharacterCodes . backslash ) {
5673- var quoteChar = tokenText . charCodeAt ( 0 ) ;
5674- result . finalLexState = quoteChar === CharacterCodes . doubleQuote
5675- ? EndOfLineState . InDoubleQuoteStringLiteral
5676- : EndOfLineState . InSingleQuoteStringLiteral ;
5645+ if ( scanner . isUnterminated ( ) ) {
5646+ var lastCharIndex = tokenText . length - 1 ;
5647+
5648+ var numBackslashes = 0 ;
5649+ while ( tokenText . charCodeAt ( lastCharIndex - numBackslashes ) === CharacterCodes . backslash ) {
5650+ numBackslashes ++ ;
5651+ }
5652+
5653+ // If we have an odd number of backslashes, then the multiline string is unclosed
5654+ if ( numBackslashes & 1 ) {
5655+ var quoteChar = tokenText . charCodeAt ( 0 ) ;
5656+ result . finalLexState = quoteChar === CharacterCodes . doubleQuote
5657+ ? EndOfLineState . InDoubleQuoteStringLiteral
5658+ : EndOfLineState . InSingleQuoteStringLiteral ;
5659+ }
56775660 }
56785661 }
56795662 else if ( token === SyntaxKind . MultiLineCommentTrivia ) {
56805663 // Check to see if the multiline comment was unclosed.
5681- var tokenText = scanner . getTokenText ( )
5682- if ( ! ( tokenText . length > 3 && // need to avoid catching '/*/'
5683- tokenText . charCodeAt ( tokenText . length - 2 ) === CharacterCodes . asterisk &&
5684- tokenText . charCodeAt ( tokenText . length - 1 ) === CharacterCodes . slash ) ) {
5664+ if ( scanner . isUnterminated ( ) ) {
56855665 result . finalLexState = EndOfLineState . InMultiLineCommentTrivia ;
56865666 }
56875667 }
0 commit comments