@@ -2498,25 +2498,48 @@ module ts {
24982498 }
24992499
25002500 function isInStringOrRegularExpressionLiteral ( previousToken : Node ) : boolean {
2501- if ( previousToken . kind === SyntaxKind . StringLiteral ) {
2501+ if ( previousToken . kind === SyntaxKind . StringLiteral || isTemplateLiteralKind ( previousToken . kind ) ) {
25022502 // The position has to be either: 1. entirely within the token text, or
25032503 // 2. at the end position, and the string literal is not terminated
2504+
25042505 var start = previousToken . getStart ( ) ;
25052506 var end = previousToken . getEnd ( ) ;
2507+
25062508 if ( start < position && position < end ) {
25072509 return true ;
25082510 }
25092511 else if ( position === end ) {
25102512 var width = end - start ;
25112513 var text = previousToken . getSourceFile ( ) . text ;
2512- return width <= 1 ||
2513- text . charCodeAt ( start ) !== text . charCodeAt ( end - 1 ) ||
2514- text . charCodeAt ( end - 2 ) === CharacterCodes . backslash ;
2514+
2515+ // If the token is a single character, or its second-to-last charcter indicates an escape code,
2516+ // then we can immediately say that we are in the middle of an unclosed string.
2517+ if ( width <= 1 || text . charCodeAt ( end - 2 ) === CharacterCodes . backslash ) {
2518+ return true ;
2519+ }
2520+
2521+ // Now check if the last character is a closing character for the token.
2522+ switch ( previousToken . kind ) {
2523+ case SyntaxKind . StringLiteral :
2524+ case SyntaxKind . NoSubstitutionTemplateLiteral :
2525+ return text . charCodeAt ( start ) !== text . charCodeAt ( end - 1 ) ;
2526+
2527+ case SyntaxKind . TemplateHead :
2528+ case SyntaxKind . TemplateMiddle :
2529+ return text . charCodeAt ( end - 1 ) !== CharacterCodes . openBrace
2530+ || text . charCodeAt ( end - 2 ) !== CharacterCodes . $ ;
2531+
2532+ case SyntaxKind . TemplateTail :
2533+ return text . charCodeAt ( end - 1 ) !== CharacterCodes . backtick ;
2534+ }
2535+
2536+ return false ;
25152537 }
25162538 }
25172539 else if ( previousToken . kind === SyntaxKind . RegularExpressionLiteral ) {
25182540 return previousToken . getStart ( ) < position && position < previousToken . getEnd ( ) ;
25192541 }
2542+
25202543 return false ;
25212544 }
25222545
0 commit comments