mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
feat(44720): allow renaming string literal in switch/case (#45084)
This commit is contained in:
parent
693c2d08c1
commit
e00722f262
@ -2003,13 +2003,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);
|
||||
}
|
||||
|
||||
@ -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))
|
||||
))) {
|
||||
|
||||
@ -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);
|
||||
|
||||
17
tests/cases/fourslash/renameStringLiteralTypes3.ts
Normal file
17
tests/cases/fourslash/renameStringLiteralTypes3.ts
Normal file
@ -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");
|
||||
Loading…
x
Reference in New Issue
Block a user