@@ -951,10 +951,8 @@ module ts {
951951 }
952952
953953 export function createSourceFile ( filename : string , sourceText : string , languageVersion : ScriptTarget , version : string , isOpen : boolean = false ) : SourceFile {
954- var scanner : Scanner ;
955954 var token : SyntaxKind ;
956955 var parsingContext : ParsingContext ;
957- var commentRanges : TextRange [ ] ;
958956 var identifiers : Map < string > = { } ;
959957 var identifierCount = 0 ;
960958 var nodeCount = 0 ;
@@ -1136,10 +1134,6 @@ module ts {
11361134 parseErrorAtPosition ( pos , 0 , message ) ;
11371135 }
11381136
1139- function onComment ( pos : number , end : number ) {
1140- if ( commentRanges ) commentRanges . push ( { pos : pos , end : end } ) ;
1141- }
1142-
11431137 function getNodePos ( ) : number {
11441138 return scanner . getStartPos ( ) ;
11451139 }
@@ -4173,14 +4167,25 @@ module ts {
41734167 }
41744168
41754169 function processReferenceComments ( ) : ReferenceComments {
4170+ var triviaScanner = createScanner ( languageVersion , /*skipTrivia*/ false , sourceText ) ;
41764171 var referencedFiles : FileReference [ ] = [ ] ;
41774172 var amdDependencies : string [ ] = [ ] ;
41784173 var amdModuleName : string ;
4179- commentRanges = [ ] ;
4180- token = scanner . scan ( ) ;
41814174
4182- for ( var i = 0 ; i < commentRanges . length ; i ++ ) {
4183- var range = commentRanges [ i ] ;
4175+ // Keep scanning all the leading trivia in the file until we get to something that
4176+ // isn't trivia. Any single line comment will be analyzed to see if it is a
4177+ // reference comment.
4178+ while ( true ) {
4179+ var kind = triviaScanner . scan ( ) ;
4180+ if ( kind === SyntaxKind . WhitespaceTrivia || kind === SyntaxKind . NewLineTrivia || kind === SyntaxKind . MultiLineCommentTrivia ) {
4181+ continue ;
4182+ }
4183+ if ( kind !== SyntaxKind . SingleLineCommentTrivia ) {
4184+ break ;
4185+ }
4186+
4187+ var range = { pos : triviaScanner . getTokenPos ( ) , end : triviaScanner . getTextPos ( ) } ;
4188+
41844189 var comment = sourceText . substring ( range . pos , range . end ) ;
41854190 var referencePathMatchResult = getFileReferenceFromReferencePath ( comment , range ) ;
41864191 if ( referencePathMatchResult ) {
@@ -4211,7 +4216,7 @@ module ts {
42114216 }
42124217 }
42134218 }
4214- commentRanges = undefined ;
4219+
42154220 return {
42164221 referencedFiles,
42174222 amdDependencies,
@@ -4247,7 +4252,6 @@ module ts {
42474252 return syntacticDiagnostics ;
42484253 }
42494254
4250- scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError , onComment ) ;
42514255 var rootNodeFlags : NodeFlags = 0 ;
42524256 if ( fileExtensionIs ( filename , ".d.ts" ) ) {
42534257 rootNodeFlags = NodeFlags . DeclarationFile ;
@@ -4273,6 +4277,10 @@ module ts {
42734277 sourceFile . amdDependencies = referenceComments . amdDependencies ;
42744278 sourceFile . amdModuleName = referenceComments . amdModuleName ;
42754279
4280+ // Create and prime the scanner before parsing the source elements.
4281+ var scanner = createScanner ( languageVersion , /*skipTrivia*/ true , sourceText , scanError ) ;
4282+ nextToken ( ) ;
4283+
42764284 sourceFile . statements = parseList ( ParsingContext . SourceElements , /*checkForStrictMode*/ true , parseSourceElement ) ;
42774285 Debug . assert ( token === SyntaxKind . EndOfFileToken ) ;
42784286 sourceFile . endOfFileToken = parseTokenNode ( ) ;
0 commit comments