-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(25770): add Quick Fix to convert LiteralType to MappedType (#40226)
- Loading branch information
1 parent
6101fbc
commit 6aec7f4
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "convertLiteralTypeToMappedType"; | ||
const errorCodes = [Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Did_you_mean_to_use_1_in_0.code]; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions: context => { | ||
const { sourceFile, span } = context; | ||
const info = getInfo(sourceFile, span.start); | ||
if (!info) { | ||
return undefined; | ||
} | ||
const { name, constraint } = info; | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info)); | ||
return [createCodeFixAction(fixId, changes, [Diagnostics.Convert_0_to_1_in_0, constraint, name], fixId, Diagnostics.Convert_all_type_literals_to_mapped_type)]; | ||
}, | ||
fixIds: [fixId], | ||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const info = getInfo(diag.file, diag.start); | ||
if (info) { | ||
doChange(changes, diag.file, info); | ||
} | ||
}) | ||
}); | ||
|
||
interface Info { | ||
container: TypeLiteralNode, | ||
typeNode: TypeNode | undefined; | ||
constraint: string; | ||
name: string; | ||
} | ||
|
||
function getInfo(sourceFile: SourceFile, pos: number): Info | undefined { | ||
const token = getTokenAtPosition(sourceFile, pos); | ||
if (isIdentifier(token)) { | ||
const propertySignature = cast(token.parent.parent, isPropertySignature); | ||
const propertyName = token.getText(sourceFile); | ||
return { | ||
container: cast(propertySignature.parent, isTypeLiteralNode), | ||
typeNode: propertySignature.type, | ||
constraint: propertyName, | ||
name: propertyName === "K" ? "P" : "K", | ||
}; | ||
} | ||
return undefined; | ||
} | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { container, typeNode, constraint, name }: Info): void { | ||
changes.replaceNode(sourceFile, container, factory.createMappedTypeNode(/*readonlyToken*/ undefined, | ||
factory.createTypeParameterDeclaration(name, factory.createTypeReferenceNode(constraint)), /*questionToken*/ undefined, typeNode)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////type K = number | string; | ||
////type T = { | ||
//// [K]: number; | ||
////} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Convert_0_to_1_in_0.message, "K", "P"], | ||
index: 0, | ||
newFileContent: | ||
`type K = number | string; | ||
type T = { | ||
[P in K]: number; | ||
}` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////type Keys = number | string; | ||
////type T = { | ||
//// [Keys]: number; | ||
////} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Convert_0_to_1_in_0.message, "Keys", "K"], | ||
index: 0, | ||
newFileContent: | ||
`type Keys = number | string; | ||
type T = { | ||
[K in Keys]: number; | ||
}` | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
////type K1 = number | string; | ||
////type T1 = { | ||
//// [K1]: number; | ||
////} | ||
////type K2 = number | string; | ||
////type T2 = { | ||
//// [K2]: number; | ||
////} | ||
|
||
verify.codeFixAll({ | ||
fixAllDescription: ts.Diagnostics.Convert_all_type_literals_to_mapped_type.message, | ||
fixId: 'convertLiteralTypeToMappedType', | ||
newFileContent: | ||
`type K1 = number | string; | ||
type T1 = { | ||
[K in K1]: number; | ||
} | ||
type K2 = number | string; | ||
type T2 = { | ||
[K in K2]: number; | ||
}` | ||
}); |