From 3fa17eff27d9f68aabcf7e6b84a3fa96d994d168 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Fri, 3 Oct 2014 17:33:03 -0700 Subject: [PATCH] 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. --- src/services/services.ts | 137 +++++++++++++++------------------------ 1 file changed, 51 insertions(+), 86 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index bb3fe8e3bf4..33bdd4956ec 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2637,7 +2637,7 @@ module ts { if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.ThisKeyword || node.kind === SyntaxKind.SuperKeyword || isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) { - return getReferencesForNode(node, [sourceFile]); + return getReferencesForNode(node, [sourceFile], /*findInStrings:*/ false, /*findInComments:*/ false); } switch (node.kind) { @@ -3098,90 +3098,10 @@ module ts { } Debug.assert(node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.NumericLiteral || node.kind === SyntaxKind.StringLiteral); - var references = getReferencesForNode(node, program.getSourceFiles()); - - var name = (node).text; - var tripleSlashDirectivePrefixRegex = /^\/\/\/\s*= 0) { - // Only consider it a match if there isn't a letter/number before or after - // the match. - var indexInSourceText = rawTextPositionInSourceText + matchIndex; - - if (indexInSourceText === 0 || !isIdentifierPart(sourceText.charCodeAt(indexInSourceText - 1), ScriptTarget.ES5)) { - var matchEnd = indexInSourceText + name.length; - if (matchEnd >= sourceText.length || !isIdentifierPart(sourceText.charCodeAt(matchEnd), ScriptTarget.ES5)) { - - references.push({ - fileName: sourceFile.filename, - textSpan: new TypeScript.TextSpan(indexInSourceText, name.length), - isWriteAccess: false - }); - } - } - - // Advance the matchIndex forward (if we don't, then we'll simply find the same - // match at the same position again). - matchIndex++; - } - } - - function addCommentReferences(sourceFile: SourceFile) { - var sourceText = sourceFile.text; - forEachChild(sourceFile, addCommentReferencesInNode); - - function addCommentReferencesInNode(node: Node) { - if (isToken(node)) { - // Found a token, walk its comments (if it has any) for matches). - forEach(getLeadingCommentRanges(sourceText, node.pos), addReferencesInCommentRange); - } - else { - forEach(node.getChildren(), addCommentReferencesInNode); - } - } - - function addReferencesInCommentRange(range: CommentRange) { - var commentText = sourceText.substring(range.pos, range.end); - - // Don't add matches in /// { @@ -3239,7 +3159,7 @@ module ts { if (lookUp(sourceFile.identifiers, symbolName)) { result = result || []; - getReferencesInNode(sourceFile, symbol, symbolName, node, searchMeaning, result); + getReferencesInNode(sourceFile, symbol, symbolName, node, searchMeaning, findInStrings, findInComments, result); } }); } @@ -3391,8 +3311,16 @@ module ts { * tuple of(searchSymbol, searchText, searchLocation, and searchMeaning). * searchLocation: a node where the search value */ - function getReferencesInNode(container: Node, searchSymbol: Symbol, searchText: string, searchLocation: Node, searchMeaning: SearchMeaning, result: ReferenceEntry[]): void { + function getReferencesInNode(container: Node, + searchSymbol: Symbol, + searchText: string, + searchLocation: Node, + searchMeaning: SearchMeaning, + findInStrings: boolean, + findInComments: boolean, + result: ReferenceEntry[]): void { var sourceFile = container.getSourceFile(); + var tripleSlashDirectivePrefixRegex = /^\/\/\/\s* token.getStart(); + } + + function isInComment(position: number) { + var token = getTokenAtPosition(sourceFile, position); + if (token && position < token.getStart()) { + // First, we have to see if this position actually landed in a comment. + var commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos); + + // Then we want to make sure that it wasn't in a "///<" directive comment + // We don't want to unintentionally update a file name. + return forEach(commentRanges, c => { + if (c.pos < position && position < c.end) { + var commentText = sourceFile.text.substring(c.pos, c.end); + if (!tripleSlashDirectivePrefixRegex.test(commentText)) { + return true; + } + } + }); + } + + return false; + } } function getReferencesForSuperKeyword(superKeyword: Node): ReferenceEntry[]{