@@ -972,10 +972,8 @@ module ts {
972972 }
973973
974974 export function createSourceFile ( filename : string , sourceText : string , languageVersion : ScriptTarget , version : string , isOpen : boolean = false ) : SourceFile {
975- var scanner : Scanner ;
976975 var token : SyntaxKind ;
977976 var parsingContext : ParsingContext ;
978- var commentRanges : TextRange [ ] ;
979977 var identifiers : Map < string > = { } ;
980978 var identifierCount = 0 ;
981979 var nodeCount = 0 ;
@@ -1184,10 +1182,6 @@ module ts {
11841182 parseErrorAtPosition ( pos , 0 , message ) ;
11851183 }
11861184
1187- function onComment ( pos : number , end : number ) {
1188- if ( commentRanges ) commentRanges . push ( { pos : pos , end : end } ) ;
1189- }
1190-
11911185 function getNodePos ( ) : number {
11921186 return scanner . getStartPos ( ) ;
11931187 }
@@ -4267,14 +4261,25 @@ module ts {
42674261 }
42684262
42694263 function processReferenceComments ( ) : ReferenceComments {
4264+ var triviaScanner = createScanner ( languageVersion , /*skipTrivia*/ false , sourceText ) ;
42704265 var referencedFiles : FileReference [ ] = [ ] ;
42714266 var amdDependencies : string [ ] = [ ] ;
42724267 var amdModuleName : string ;
4273- commentRanges = [ ] ;
4274- token = scanner . scan ( ) ;
42754268
4276- for ( var i = 0 ; i < commentRanges . length ; i ++ ) {
4277- var range = commentRanges [ i ] ;
4269+ // Keep scanning all the leading trivia in the file until we get to something that
4270+ // isn't trivia. Any single line comment will be analyzed to see if it is a
4271+ // reference comment.
4272+ while ( true ) {
4273+ var kind = triviaScanner . scan ( ) ;
4274+ if ( kind === SyntaxKind . WhitespaceTrivia || kind === SyntaxKind . NewLineTrivia || kind === SyntaxKind . MultiLineCommentTrivia ) {
4275+ continue ;
4276+ }
4277+ if ( kind !== SyntaxKind . SingleLineCommentTrivia ) {
4278+ break ;
4279+ }
4280+
4281+ var range = { pos : triviaScanner . getTokenPos ( ) , end : triviaScanner . getTextPos ( ) } ;
4282+
42784283 var comment = sourceText . substring ( range . pos , range . end ) ;
42794284 var referencePathMatchResult = getFileReferenceFromReferencePath ( comment , range ) ;
42804285 if ( referencePathMatchResult ) {
@@ -4305,7 +4310,7 @@ module ts {
43054310 }
43064311 }
43074312 }
4308- commentRanges = undefined ;
4313+
43094314 return {
43104315 referencedFiles,
43114316 amdDependencies,
@@ -4341,7 +4346,6 @@ module ts {
43414346 return syntacticDiagnostics ;
43424347 }
43434348
4344- scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError , onComment ) ;
43454349 var rootNodeFlags : NodeFlags = 0 ;
43464350 if ( fileExtensionIs ( filename , ".d.ts" ) ) {
43474351 rootNodeFlags = NodeFlags . DeclarationFile ;
@@ -4367,6 +4371,10 @@ module ts {
43674371 sourceFile . amdDependencies = referenceComments . amdDependencies ;
43684372 sourceFile . amdModuleName = referenceComments . amdModuleName ;
43694373
4374+ // Create and prime the scanner before parsing the source elements.
4375+ var scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError ) ;
4376+ nextToken ( ) ;
4377+
43704378 sourceFile . statements = parseList ( ParsingContext . SourceElements , /*checkForStrictMode*/ true , parseSourceElement ) ;
43714379 Debug . assert ( token === SyntaxKind . EndOfFileToken ) ;
43724380 sourceFile . endOfFileToken = parseTokenNode ( ) ;
0 commit comments