mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Merge branch 'master' into add-braces
This commit is contained in:
commit
d470f32d8a
62 files changed
+967
-167
No files matched your search
@@ -0,0 +1,56 @@
|
||||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
const fixIdAddMissingTypeof = "fixConvertToMappedObjectType";
|
||||
const fixId = fixIdAddMissingTypeof;
|
||||
const errorCodes = [Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead.code];
|
||||
|
||||
type FixableDeclaration = InterfaceDeclaration | TypeAliasDeclaration;
|
||||
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
getCodeActions: context => {
|
||||
const { sourceFile, span } = context;
|
||||
const info = getInfo(sourceFile, span.start);
|
||||
if (!info) return undefined;
|
||||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info));
|
||||
const name = idText(info.container.name);
|
||||
return [createCodeFixAction(fixId, changes, [Diagnostics.Convert_0_to_mapped_object_type, name], fixId, [Diagnostics.Convert_0_to_mapped_object_type, name])];
|
||||
},
|
||||
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 { readonly indexSignature: IndexSignatureDeclaration; readonly container: FixableDeclaration; }
|
||||
function getInfo(sourceFile: SourceFile, pos: number): Info | undefined {
|
||||
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
|
||||
const indexSignature = cast(token.parent.parent, isIndexSignatureDeclaration);
|
||||
if (isClassDeclaration(indexSignature.parent)) return undefined;
|
||||
const container = isInterfaceDeclaration(indexSignature.parent) ? indexSignature.parent : cast(indexSignature.parent.parent, isTypeAliasDeclaration);
|
||||
return { indexSignature, container };
|
||||
}
|
||||
|
||||
function createTypeAliasFromInterface(declaration: FixableDeclaration, type: TypeNode): TypeAliasDeclaration {
|
||||
return createTypeAliasDeclaration(declaration.decorators, declaration.modifiers, declaration.name, declaration.typeParameters, type);
|
||||
}
|
||||
|
||||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { indexSignature, container }: Info): void {
|
||||
const members = isInterfaceDeclaration(container) ? container.members : (<TypeLiteralNode>container.type).members;
|
||||
const otherMembers = members.filter(member => !isIndexSignatureDeclaration(member));
|
||||
const parameter = first(indexSignature.parameters);
|
||||
const mappedTypeParameter = createTypeParameterDeclaration(cast(parameter.name, isIdentifier), parameter.type);
|
||||
const mappedIntersectionType = createMappedTypeNode(
|
||||
hasReadonlyModifier(indexSignature) ? createModifier(SyntaxKind.ReadonlyKeyword) : undefined,
|
||||
mappedTypeParameter,
|
||||
indexSignature.questionToken,
|
||||
indexSignature.type);
|
||||
const intersectionType = createIntersectionTypeNode([
|
||||
...getAllSuperTypeNodes(container),
|
||||
mappedIntersectionType,
|
||||
...(otherMembers.length ? [createTypeLiteralNode(otherMembers)] : emptyArray),
|
||||
]);
|
||||
changes.replaceNode(sourceFile, container, createTypeAliasFromInterface(container, intersectionType));
|
||||
}
|
||||
}
|
||||
@@ -211,7 +211,7 @@ namespace ts.codefix {
|
||||
|
||||
// if this file doesn't have any import statements, insert an import statement and then insert a new line
|
||||
// between the only import statement and user code. Otherwise just insert the statement because chances
|
||||
// are there are already a new line seperating code and import statements.
|
||||
// are there are already a new line separating code and import statements.
|
||||
return createCodeAction(Diagnostics.Import_0_from_module_1, [symbolName, moduleSpecifierWithoutQuotes], changes);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user