mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 21:06:50 -05:00
feat(44720): allow renaming string literal in switch/case (#45084)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user