From 5bcb8fa9fa33f73bd62bc09b2e561c84e42905ee Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 21 Mar 2017 13:24:39 -0700 Subject: [PATCH] Don't need types to handle string literals --- src/services/findAllReferences.ts | 27 +++++++-------------------- src/services/rename.ts | 13 +++++-------- src/services/utilities.ts | 9 --------- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index ba7a6917631..a40af45a25c 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -279,7 +279,7 @@ namespace ts.FindAllReferences.Core { if (!symbol) { // String literal might be a property (and thus have a symbol), so do this here rather than in getReferencedSymbolsSpecial. if (!options.implementations && node.kind === SyntaxKind.StringLiteral) { - return getReferencesForStringLiteral(node, sourceFiles, checker, cancellationToken); + return getReferencesForStringLiteral(node, sourceFiles, cancellationToken); } // Can't have references to something that we have no symbol for. return undefined; @@ -1328,19 +1328,13 @@ namespace ts.FindAllReferences.Core { } } - function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): SymbolAndEntries[] { - const type = getStringLiteralTypeForNode(node, checker); - - if (!type) { - // nothing to do here. moving on - return undefined; - } - + function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: SourceFile[], cancellationToken: CancellationToken): SymbolAndEntries[] { const references: NodeEntry[] = []; for (const sourceFile of sourceFiles) { - const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, type.text, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken); - getReferencesForStringLiteralInFile(sourceFile, type, possiblePositions, references); + cancellationToken.throwIfCancellationRequested(); + const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken); + getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references); } return [{ @@ -1348,17 +1342,10 @@ namespace ts.FindAllReferences.Core { references }]; - function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchType: Type, possiblePositions: number[], references: Push): void { + function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchText: string, possiblePositions: number[], references: Push): void { for (const position of possiblePositions) { - cancellationToken.throwIfCancellationRequested(); - const node = getTouchingWord(sourceFile, position); - if (!node || node.kind !== SyntaxKind.StringLiteral) { - return; - } - - const type = getStringLiteralTypeForNode(node, checker); - if (type === searchType) { + if (node && node.kind === SyntaxKind.StringLiteral && (node as StringLiteral).text === searchText) { references.push(nodeEntry(node, /*isInString*/true)); } } diff --git a/src/services/rename.ts b/src/services/rename.ts index 556283a44f4..bee530340bd 100644 --- a/src/services/rename.ts +++ b/src/services/rename.ts @@ -37,15 +37,12 @@ namespace ts.Rename { } } else if (node.kind === SyntaxKind.StringLiteral) { - const type = getStringLiteralTypeForNode(node, typeChecker); - if (type) { - if (isDefinedInLibraryFile(node)) { - return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library); - } - - const displayName = stripQuotes(type.text); - return getRenameInfoSuccess(displayName, displayName, ScriptElementKind.variableElement, ScriptElementKindModifier.none, node, sourceFile); + if (isDefinedInLibraryFile(node)) { + return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library); } + + const displayName = stripQuotes((node as StringLiteral).text); + return getRenameInfoSuccess(displayName, displayName, ScriptElementKind.variableElement, ScriptElementKindModifier.none, node, sourceFile); } } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 95de9d3a503..9996c3cb160 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -364,15 +364,6 @@ namespace ts { } } - export function getStringLiteralTypeForNode(node: StringLiteral | LiteralTypeNode, typeChecker: TypeChecker): LiteralType { - const searchNode = node.parent.kind === SyntaxKind.LiteralType ? node.parent : node; - const type = typeChecker.getTypeAtLocation(searchNode); - if (type && type.flags & TypeFlags.StringLiteral) { - return type; - } - return undefined; - } - export function isThis(node: Node): boolean { switch (node.kind) { case SyntaxKind.ThisKeyword: