Skip to content

Commit e00722f

Browse files
authored
feat(44720): allow renaming string literal in switch/case (microsoft#45084)
1 parent 693c2d0 commit e00722f

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/services/findAllReferences.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2003,13 +2003,13 @@ namespace ts.FindAllReferences {
20032003
}
20042004

20052005
function getReferencesForStringLiteral(node: StringLiteralLike, sourceFiles: readonly SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken): SymbolAndEntries[] {
2006-
const type = getContextualTypeOrAncestorTypeNodeType(node, checker);
2006+
const type = getContextualTypeFromParentOrAncestorTypeNode(node, checker);
20072007
const references = flatMap(sourceFiles, sourceFile => {
20082008
cancellationToken.throwIfCancellationRequested();
20092009
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, node.text), ref => {
20102010
if (isStringLiteralLike(ref) && ref.text === node.text) {
20112011
if (type) {
2012-
const refType = getContextualTypeOrAncestorTypeNodeType(ref, checker);
2012+
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker);
20132013
if (type !== checker.getStringType() && type === refType) {
20142014
return nodeEntry(ref, EntryKind.StringLiteral);
20152015
}

src/services/rename.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace ts.Rename {
1515
const symbol = typeChecker.getSymbolAtLocation(node);
1616
if (!symbol) {
1717
if (isStringLiteralLike(node)) {
18-
const type = getContextualTypeOrAncestorTypeNodeType(node, typeChecker);
18+
const type = getContextualTypeFromParentOrAncestorTypeNode(node, typeChecker);
1919
if (type && ((type.flags & TypeFlags.StringLiteral) || (
2020
(type.flags & TypeFlags.Union) && every((type as UnionType).types, type => !!(type.flags & TypeFlags.StringLiteral))
2121
))) {

src/services/utilities.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -796,16 +796,9 @@ namespace ts {
796796
return lastTypeNode;
797797
}
798798

799-
export function getContextualTypeOrAncestorTypeNodeType(node: Expression, checker: TypeChecker) {
800-
const contextualType = checker.getContextualType(node);
801-
if (contextualType) {
802-
return contextualType;
803-
}
804-
805-
const parent = node.parent;
806-
if (parent && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind)) {
807-
return checker.getTypeAtLocation(node === parent.left ? parent.right : parent.left);
808-
}
799+
export function getContextualTypeFromParentOrAncestorTypeNode(node: Expression, checker: TypeChecker): Type | undefined {
800+
const contextualType = getContextualTypeFromParent(node, checker);
801+
if (contextualType) return contextualType;
809802

810803
const ancestorTypeNode = getAncestorTypeNode(node);
811804
return ancestorTypeNode && checker.getTypeAtLocation(ancestorTypeNode);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////type Foo = "[|a|]" | "b";
4+
////
5+
////class C {
6+
//// p: Foo = "[|a|]";
7+
//// m() {
8+
//// switch (this.p) {
9+
//// case "[|a|]":
10+
//// return 1;
11+
//// case "b":
12+
//// return 2;
13+
//// }
14+
//// }
15+
////}
16+
17+
verify.rangesWithSameTextAreRenameLocations("a");

0 commit comments

Comments
 (0)