Skip to content

Commit 5bcb8fa

Browse files
author
Andy Hanson
committed
Don't need types to handle string literals
1 parent 4c71862 commit 5bcb8fa

File tree

3 files changed

+12
-37
lines changed

3 files changed

+12
-37
lines changed

src/services/findAllReferences.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ namespace ts.FindAllReferences.Core {
279279
if (!symbol) {
280280
// String literal might be a property (and thus have a symbol), so do this here rather than in getReferencedSymbolsSpecial.
281281
if (!options.implementations && node.kind === SyntaxKind.StringLiteral) {
282-
return getReferencesForStringLiteral(<StringLiteral>node, sourceFiles, checker, cancellationToken);
282+
return getReferencesForStringLiteral(<StringLiteral>node, sourceFiles, cancellationToken);
283283
}
284284
// Can't have references to something that we have no symbol for.
285285
return undefined;
@@ -1328,37 +1328,24 @@ namespace ts.FindAllReferences.Core {
13281328
}
13291329
}
13301330

1331-
function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): SymbolAndEntries[] {
1332-
const type = getStringLiteralTypeForNode(node, checker);
1333-
1334-
if (!type) {
1335-
// nothing to do here. moving on
1336-
return undefined;
1337-
}
1338-
1331+
function getReferencesForStringLiteral(node: StringLiteral, sourceFiles: SourceFile[], cancellationToken: CancellationToken): SymbolAndEntries[] {
13391332
const references: NodeEntry[] = [];
13401333

13411334
for (const sourceFile of sourceFiles) {
1342-
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, type.text, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken);
1343-
getReferencesForStringLiteralInFile(sourceFile, type, possiblePositions, references);
1335+
cancellationToken.throwIfCancellationRequested();
1336+
const possiblePositions = getPossibleSymbolReferencePositions(sourceFile, node.text, sourceFile.getStart(), sourceFile.getEnd(), cancellationToken);
1337+
getReferencesForStringLiteralInFile(sourceFile, node.text, possiblePositions, references);
13441338
}
13451339

13461340
return [{
13471341
definition: { type: "string", node },
13481342
references
13491343
}];
13501344

1351-
function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchType: Type, possiblePositions: number[], references: Push<NodeEntry>): void {
1345+
function getReferencesForStringLiteralInFile(sourceFile: SourceFile, searchText: string, possiblePositions: number[], references: Push<NodeEntry>): void {
13521346
for (const position of possiblePositions) {
1353-
cancellationToken.throwIfCancellationRequested();
1354-
13551347
const node = getTouchingWord(sourceFile, position);
1356-
if (!node || node.kind !== SyntaxKind.StringLiteral) {
1357-
return;
1358-
}
1359-
1360-
const type = getStringLiteralTypeForNode(<StringLiteral>node, checker);
1361-
if (type === searchType) {
1348+
if (node && node.kind === SyntaxKind.StringLiteral && (node as StringLiteral).text === searchText) {
13621349
references.push(nodeEntry(node, /*isInString*/true));
13631350
}
13641351
}

src/services/rename.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@ namespace ts.Rename {
3737
}
3838
}
3939
else if (node.kind === SyntaxKind.StringLiteral) {
40-
const type = getStringLiteralTypeForNode(<StringLiteral>node, typeChecker);
41-
if (type) {
42-
if (isDefinedInLibraryFile(node)) {
43-
return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library);
44-
}
45-
46-
const displayName = stripQuotes(type.text);
47-
return getRenameInfoSuccess(displayName, displayName, ScriptElementKind.variableElement, ScriptElementKindModifier.none, node, sourceFile);
40+
if (isDefinedInLibraryFile(node)) {
41+
return getRenameInfoError(Diagnostics.You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library);
4842
}
43+
44+
const displayName = stripQuotes((node as StringLiteral).text);
45+
return getRenameInfoSuccess(displayName, displayName, ScriptElementKind.variableElement, ScriptElementKindModifier.none, node, sourceFile);
4946
}
5047
}
5148

src/services/utilities.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,6 @@ namespace ts {
364364
}
365365
}
366366

367-
export function getStringLiteralTypeForNode(node: StringLiteral | LiteralTypeNode, typeChecker: TypeChecker): LiteralType {
368-
const searchNode = node.parent.kind === SyntaxKind.LiteralType ? <LiteralTypeNode>node.parent : node;
369-
const type = typeChecker.getTypeAtLocation(searchNode);
370-
if (type && type.flags & TypeFlags.StringLiteral) {
371-
return <LiteralType>type;
372-
}
373-
return undefined;
374-
}
375-
376367
export function isThis(node: Node): boolean {
377368
switch (node.kind) {
378369
case SyntaxKind.ThisKeyword:

0 commit comments

Comments
 (0)