Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1998,13 +1998,13 @@ namespace ts.FindAllReferences {
}

function getReferencesForStringLiteral(node: StringLiteralLike, sourceFiles: readonly SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): SymbolAndEntries[] {
const type = getContextualTypeOrAncestorTypeNodeType(node, checker);
const type = getContextualTypeFromParentOrAncestorTypeNode(node, checker);
const references = flatMap(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, node.text), ref => {
if (isStringLiteralLike(ref) && ref.text === node.text) {
if (type) {
const refType = getContextualTypeOrAncestorTypeNodeType(ref, checker);
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker);
if (type !== checker.getStringType() && type === refType) {
return nodeEntry(ref, EntryKind.StringLiteral);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ts.Rename {
const symbol = typeChecker.getSymbolAtLocation(node);
if (!symbol) {
if (isStringLiteralLike(node)) {
const type = getContextualTypeOrAncestorTypeNodeType(node, typeChecker);
const type = getContextualTypeFromParentOrAncestorTypeNode(node, typeChecker);
if (type && ((type.flags & TypeFlags.StringLiteral) || (
(type.flags & TypeFlags.Union) && every((type as UnionType).types, type => !!(type.flags & TypeFlags.StringLiteral))
))) {
Expand Down
13 changes: 3 additions & 10 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,16 +796,9 @@ namespace ts {
return lastTypeNode;
}

export function getContextualTypeOrAncestorTypeNodeType(node: Expression, checker: TypeChecker) {
const contextualType = checker.getContextualType(node);
if (contextualType) {
return contextualType;
}

const parent = node.parent;
if (parent && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind)) {
return checker.getTypeAtLocation(node === parent.left ? parent.right : parent.left);
}
export function getContextualTypeFromParentOrAncestorTypeNode(node: Expression, checker: TypeChecker): Type | undefined {
const contextualType = getContextualTypeFromParent(node, checker);
if (contextualType) return contextualType;

const ancestorTypeNode = getAncestorTypeNode(node);
return ancestorTypeNode && checker.getTypeAtLocation(ancestorTypeNode);
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/renameStringLiteralTypes3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

////type Foo = "[|a|]" | "b";
////
////class C {
//// p: Foo = "[|a|]";
//// m() {
//// switch (this.p) {
//// case "[|a|]":
//// return 1;
//// case "b":
//// return 2;
//// }
//// }
////}

verify.rangesWithSameTextAreRenameLocations("a");