@@ -945,10 +945,8 @@ module ts {
945945 }
946946
947947 export function createSourceFile ( filename : string , sourceText : string , languageVersion : ScriptTarget , version : string , isOpen : boolean = false ) : SourceFile {
948- var scanner : Scanner ;
949948 var token : SyntaxKind ;
950949 var parsingContext : ParsingContext ;
951- var commentRanges : TextRange [ ] ;
952950 var identifiers : Map < string > = { } ;
953951 var identifierCount = 0 ;
954952 var nodeCount = 0 ;
@@ -1128,10 +1126,6 @@ module ts {
11281126 parseErrorAtPosition ( pos , 0 , message ) ;
11291127 }
11301128
1131- function onComment ( pos : number , end : number ) {
1132- if ( commentRanges ) commentRanges . push ( { pos : pos , end : end } ) ;
1133- }
1134-
11351129 function getNodePos ( ) : number {
11361130 return scanner . getStartPos ( ) ;
11371131 }
@@ -4201,14 +4195,25 @@ module ts {
42014195 }
42024196
42034197 function processReferenceComments ( ) : ReferenceComments {
4198+ var triviaScanner = createScanner ( languageVersion , /*skipTrivia*/ false , sourceText ) ;
42044199 var referencedFiles : FileReference [ ] = [ ] ;
42054200 var amdDependencies : string [ ] = [ ] ;
42064201 var amdModuleName : string ;
4207- commentRanges = [ ] ;
4208- token = scanner . scan ( ) ;
42094202
4210- for ( var i = 0 ; i < commentRanges . length ; i ++ ) {
4211- var range = commentRanges [ i ] ;
4203+ // Keep scanning all the leading trivia in the file until we get to something that
4204+ // isn't trivia. Any single line comment will be analyzed to see if it is a
4205+ // reference comment.
4206+ while ( true ) {
4207+ var kind = triviaScanner . scan ( ) ;
4208+ if ( kind === SyntaxKind . WhitespaceTrivia || kind === SyntaxKind . NewLineTrivia || kind === SyntaxKind . MultiLineCommentTrivia ) {
4209+ continue ;
4210+ }
4211+ if ( kind !== SyntaxKind . SingleLineCommentTrivia ) {
4212+ break ;
4213+ }
4214+
4215+ var range = { pos : triviaScanner . getTokenPos ( ) , end : triviaScanner . getTextPos ( ) } ;
4216+
42124217 var comment = sourceText . substring ( range . pos , range . end ) ;
42134218 var referencePathMatchResult = getFileReferenceFromReferencePath ( comment , range ) ;
42144219 if ( referencePathMatchResult ) {
@@ -4239,7 +4244,7 @@ module ts {
42394244 }
42404245 }
42414246 }
4242- commentRanges = undefined ;
4247+
42434248 return {
42444249 referencedFiles,
42454250 amdDependencies,
@@ -4275,7 +4280,6 @@ module ts {
42754280 return syntacticDiagnostics ;
42764281 }
42774282
4278- scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError , onComment ) ;
42794283 var rootNodeFlags : NodeFlags = 0 ;
42804284 if ( fileExtensionIs ( filename , ".d.ts" ) ) {
42814285 rootNodeFlags = NodeFlags . DeclarationFile ;
@@ -4301,6 +4305,10 @@ module ts {
43014305 sourceFile . amdDependencies = referenceComments . amdDependencies ;
43024306 sourceFile . amdModuleName = referenceComments . amdModuleName ;
43034307
4308+ // Create and prime the scanner before parsing the source elements.
4309+ var scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError ) ;
4310+ nextToken ( ) ;
4311+
43044312 sourceFile . statements = parseList ( ParsingContext . SourceElements , /*checkForStrictMode*/ true , parseSourceElement ) ;
43054313 Debug . assert ( token === SyntaxKind . EndOfFileToken ) ;
43064314 sourceFile . endOfFileToken = parseTokenNode ( ) ;
0 commit comments