Skip to content

Commit 3fa17ef

Browse files
Cleaning up the new rename-comments/strings code.
Mohamed rightly pointed out we were triplicating work and we could just roll the new checks into the existing tree walking code.
1 parent 6b76a6d commit 3fa17ef

File tree

1 file changed

+51
-86
lines changed

1 file changed

+51
-86
lines changed

src/services/services.ts

Lines changed: 51 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@ module ts {
26372637

26382638
if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.ThisKeyword || node.kind === SyntaxKind.SuperKeyword ||
26392639
isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) {
2640-
return getReferencesForNode(node, [sourceFile]);
2640+
return getReferencesForNode(node, [sourceFile], /*findInStrings:*/ false, /*findInComments:*/ false);
26412641
}
26422642

26432643
switch (node.kind) {
@@ -3098,90 +3098,10 @@ module ts {
30983098
}
30993099

31003100
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);
31823102
}
31833103

3184-
function getReferencesForNode(node: Node, sourceFiles: SourceFile[]): ReferenceEntry[] {
3104+
function getReferencesForNode(node: Node, sourceFiles: SourceFile[], findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
31853105
// Labels
31863106
if (isLabelName(node)) {
31873107
if (isJumpStatementTarget(node)) {
@@ -3231,15 +3151,15 @@ module ts {
32313151

32323152
if (scope) {
32333153
result = [];
3234-
getReferencesInNode(scope, symbol, symbolName, node, searchMeaning, result);
3154+
getReferencesInNode(scope, symbol, symbolName, node, searchMeaning, findInStrings, findInComments, result);
32353155
}
32363156
else {
32373157
forEach(sourceFiles, sourceFile => {
32383158
cancellationToken.throwIfCancellationRequested();
32393159

32403160
if (lookUp(sourceFile.identifiers, symbolName)) {
32413161
result = result || [];
3242-
getReferencesInNode(sourceFile, symbol, symbolName, node, searchMeaning, result);
3162+
getReferencesInNode(sourceFile, symbol, symbolName, node, searchMeaning, findInStrings, findInComments, result);
32433163
}
32443164
});
32453165
}
@@ -3391,8 +3311,16 @@ module ts {
33913311
* tuple of(searchSymbol, searchText, searchLocation, and searchMeaning).
33923312
* searchLocation: a node where the search value
33933313
*/
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 {
33953322
var sourceFile = container.getSourceFile();
3323+
var tripleSlashDirectivePrefixRegex = /^\/\/\/\s*</
33963324

33973325
var possiblePositions = getPossibleSymbolReferencePositions(sourceFile, searchText, container.getStart(), container.getEnd());
33983326

@@ -3405,6 +3333,17 @@ module ts {
34053333

34063334
var referenceLocation = getTouchingPropertyName(sourceFile, position);
34073335
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+
}
34083347
return;
34093348
}
34103349

@@ -3425,6 +3364,32 @@ module ts {
34253364
}
34263365
});
34273366
}
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+
}
34283393
}
34293394

34303395
function getReferencesForSuperKeyword(superKeyword: Node): ReferenceEntry[]{

0 commit comments

Comments
 (0)