@@ -2637,7 +2637,7 @@ module ts {
2637
2637
2638
2638
if ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . ThisKeyword || node . kind === SyntaxKind . SuperKeyword ||
2639
2639
isLiteralNameOfPropertyDeclarationOrIndexAccess ( node ) || isNameOfExternalModuleImportOrDeclaration ( node ) ) {
2640
- return getReferencesForNode ( node , [ sourceFile ] ) ;
2640
+ return getReferencesForNode ( node , [ sourceFile ] , /*findInStrings:*/ false , /*findInComments:*/ false ) ;
2641
2641
}
2642
2642
2643
2643
switch ( node . kind ) {
@@ -3098,90 +3098,10 @@ module ts {
3098
3098
}
3099
3099
3100
3100
Debug . assert ( node . kind === SyntaxKind . Identifier || node . kind === SyntaxKind . NumericLiteral || node . kind === SyntaxKind . StringLiteral ) ;
3101
- var references = getReferencesForNode ( node , program . getSourceFiles ( ) ) ;
3102
-
3103
- var name = ( < Identifier > node ) . text ;
3104
- var tripleSlashDirectivePrefixRegex = / ^ \/ \/ \/ \s * < /
3105
-
3106
- if ( findInStrings ) {
3107
- forEach ( program . getSourceFiles ( ) , addStringReferences ) ;
3108
- }
3109
-
3110
- if ( findInComments ) {
3111
- forEach ( program . getSourceFiles ( ) , addCommentReferences ) ;
3112
- }
3113
-
3114
- return references ;
3115
-
3116
- function addReferencesInRawText ( rawText : string , rawTextPositionInSourceText : number , sourceText : string ) {
3117
- var matchIndex = 0 ;
3118
- while ( ( matchIndex = rawText . indexOf ( name , matchIndex ) ) >= 0 ) {
3119
- // Only consider it a match if there isn't a letter/number before or after
3120
- // the match.
3121
- var indexInSourceText = rawTextPositionInSourceText + matchIndex ;
3122
-
3123
- if ( indexInSourceText === 0 || ! isIdentifierPart ( sourceText . charCodeAt ( indexInSourceText - 1 ) , ScriptTarget . ES5 ) ) {
3124
- var matchEnd = indexInSourceText + name . length ;
3125
- if ( matchEnd >= sourceText . length || ! isIdentifierPart ( sourceText . charCodeAt ( matchEnd ) , ScriptTarget . ES5 ) ) {
3126
-
3127
- references . push ( {
3128
- fileName : sourceFile . filename ,
3129
- textSpan : new TypeScript . TextSpan ( indexInSourceText , name . length ) ,
3130
- isWriteAccess : false
3131
- } ) ;
3132
- }
3133
- }
3134
-
3135
- // Advance the matchIndex forward (if we don't, then we'll simply find the same
3136
- // match at the same position again).
3137
- matchIndex ++ ;
3138
- }
3139
- }
3140
-
3141
- function addCommentReferences ( sourceFile : SourceFile ) {
3142
- var sourceText = sourceFile . text ;
3143
- forEachChild ( sourceFile , addCommentReferencesInNode ) ;
3144
-
3145
- function addCommentReferencesInNode ( node : Node ) {
3146
- if ( isToken ( node ) ) {
3147
- // Found a token, walk its comments (if it has any) for matches).
3148
- forEach ( getLeadingCommentRanges ( sourceText , node . pos ) , addReferencesInCommentRange ) ;
3149
- }
3150
- else {
3151
- forEach ( node . getChildren ( ) , addCommentReferencesInNode ) ;
3152
- }
3153
- }
3154
-
3155
- function addReferencesInCommentRange ( range : CommentRange ) {
3156
- var commentText = sourceText . substring ( range . pos , range . end ) ;
3157
-
3158
- // Don't add matches in ///<reference comments. We don't want to
3159
- // unintentionally update a file name.
3160
- if ( ! tripleSlashDirectivePrefixRegex . test ( commentText ) ) {
3161
- addReferencesInRawText ( commentText , range . pos , sourceText ) ;
3162
- }
3163
- }
3164
- }
3165
-
3166
- function addStringReferences ( sourceFile : SourceFile ) {
3167
- var sourceText = sourceFile . text ;
3168
-
3169
- forEachChild ( sourceFile , addStringReferencesInNode ) ;
3170
-
3171
- function addStringReferencesInNode ( node : Node ) {
3172
- if ( node . kind === SyntaxKind . StringLiteral ) {
3173
- // Found a string literal. See if we can find any matches in it.
3174
- addReferencesInRawText ( getTextOfNodeFromSourceText ( sourceText , node ) , node . getStart ( sourceFile ) , sourceText ) ;
3175
- }
3176
- else {
3177
- // Recurse and keep looking for references in strings.
3178
- forEachChild ( node , addStringReferencesInNode ) ;
3179
- }
3180
- }
3181
- }
3101
+ return getReferencesForNode ( node , program . getSourceFiles ( ) , findInStrings , findInComments ) ;
3182
3102
}
3183
3103
3184
- function getReferencesForNode ( node : Node , sourceFiles : SourceFile [ ] ) : ReferenceEntry [ ] {
3104
+ function getReferencesForNode ( node : Node , sourceFiles : SourceFile [ ] , findInStrings : boolean , findInComments : boolean ) : ReferenceEntry [ ] {
3185
3105
// Labels
3186
3106
if ( isLabelName ( node ) ) {
3187
3107
if ( isJumpStatementTarget ( node ) ) {
@@ -3231,15 +3151,15 @@ module ts {
3231
3151
3232
3152
if ( scope ) {
3233
3153
result = [ ] ;
3234
- getReferencesInNode ( scope , symbol , symbolName , node , searchMeaning , result ) ;
3154
+ getReferencesInNode ( scope , symbol , symbolName , node , searchMeaning , findInStrings , findInComments , result ) ;
3235
3155
}
3236
3156
else {
3237
3157
forEach ( sourceFiles , sourceFile => {
3238
3158
cancellationToken . throwIfCancellationRequested ( ) ;
3239
3159
3240
3160
if ( lookUp ( sourceFile . identifiers , symbolName ) ) {
3241
3161
result = result || [ ] ;
3242
- getReferencesInNode ( sourceFile , symbol , symbolName , node , searchMeaning , result ) ;
3162
+ getReferencesInNode ( sourceFile , symbol , symbolName , node , searchMeaning , findInStrings , findInComments , result ) ;
3243
3163
}
3244
3164
} ) ;
3245
3165
}
@@ -3391,8 +3311,16 @@ module ts {
3391
3311
* tuple of(searchSymbol, searchText, searchLocation, and searchMeaning).
3392
3312
* searchLocation: a node where the search value
3393
3313
*/
3394
- function getReferencesInNode ( container : Node , searchSymbol : Symbol , searchText : string , searchLocation : Node , searchMeaning : SearchMeaning , result : ReferenceEntry [ ] ) : void {
3314
+ function getReferencesInNode ( container : Node ,
3315
+ searchSymbol : Symbol ,
3316
+ searchText : string ,
3317
+ searchLocation : Node ,
3318
+ searchMeaning : SearchMeaning ,
3319
+ findInStrings : boolean ,
3320
+ findInComments : boolean ,
3321
+ result : ReferenceEntry [ ] ) : void {
3395
3322
var sourceFile = container . getSourceFile ( ) ;
3323
+ var tripleSlashDirectivePrefixRegex = / ^ \/ \/ \/ \s * < /
3396
3324
3397
3325
var possiblePositions = getPossibleSymbolReferencePositions ( sourceFile , searchText , container . getStart ( ) , container . getEnd ( ) ) ;
3398
3326
@@ -3405,6 +3333,17 @@ module ts {
3405
3333
3406
3334
var referenceLocation = getTouchingPropertyName ( sourceFile , position ) ;
3407
3335
if ( ! isValidReferencePosition ( referenceLocation , searchText ) ) {
3336
+ // This wasn't the start of a token. Check to see if it might be a
3337
+ // match in a comment or string if that's what the caller is asking
3338
+ // for.
3339
+ if ( ( findInStrings && isInString ( position ) ) ||
3340
+ ( findInComments && isInComment ( position ) ) ) {
3341
+ result . push ( {
3342
+ fileName : sourceFile . filename ,
3343
+ textSpan : new TypeScript . TextSpan ( position , searchText . length ) ,
3344
+ isWriteAccess : false
3345
+ } ) ;
3346
+ }
3408
3347
return ;
3409
3348
}
3410
3349
@@ -3425,6 +3364,32 @@ module ts {
3425
3364
}
3426
3365
} ) ;
3427
3366
}
3367
+
3368
+ function isInString ( position : number ) {
3369
+ var token = getTokenAtPosition ( sourceFile , position ) ;
3370
+ return token && token . kind === SyntaxKind . StringLiteral && position > token . getStart ( ) ;
3371
+ }
3372
+
3373
+ function isInComment ( position : number ) {
3374
+ var token = getTokenAtPosition ( sourceFile , position ) ;
3375
+ if ( token && position < token . getStart ( ) ) {
3376
+ // First, we have to see if this position actually landed in a comment.
3377
+ var commentRanges = getLeadingCommentRanges ( sourceFile . text , token . pos ) ;
3378
+
3379
+ // Then we want to make sure that it wasn't in a "///<" directive comment
3380
+ // We don't want to unintentionally update a file name.
3381
+ return forEach ( commentRanges , c => {
3382
+ if ( c . pos < position && position < c . end ) {
3383
+ var commentText = sourceFile . text . substring ( c . pos , c . end ) ;
3384
+ if ( ! tripleSlashDirectivePrefixRegex . test ( commentText ) ) {
3385
+ return true ;
3386
+ }
3387
+ }
3388
+ } ) ;
3389
+ }
3390
+
3391
+ return false ;
3392
+ }
3428
3393
}
3429
3394
3430
3395
function getReferencesForSuperKeyword ( superKeyword : Node ) : ReferenceEntry [ ] {
0 commit comments