|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixIdAddMissingTypeof = "fixConvertToMappedObjectType"; |
| 4 | + const fixId = fixIdAddMissingTypeof; |
| 5 | + const errorCodes = [Diagnostics.An_index_signature_parameter_type_cannot_be_a_union_type_Consider_using_a_mapped_object_type_instead.code]; |
| 6 | + |
| 7 | + type FixableDeclaration = InterfaceDeclaration | TypeAliasDeclaration; |
| 8 | + |
| 9 | + interface Info { |
| 10 | + indexSignature: IndexSignatureDeclaration; |
| 11 | + container: FixableDeclaration; |
| 12 | + otherMembers: ReadonlyArray<TypeElement>; |
| 13 | + parameterName: Identifier; |
| 14 | + parameterType: TypeNode; |
| 15 | + } |
| 16 | + |
| 17 | + registerCodeFix({ |
| 18 | + errorCodes, |
| 19 | + getCodeActions: context => { |
| 20 | + const { sourceFile, span } = context; |
| 21 | + const info = getFixableSignatureAtPosition(sourceFile, span.start); |
| 22 | + if (!info) return; |
| 23 | + const { indexSignature, container, otherMembers, parameterName, parameterType } = info; |
| 24 | + |
| 25 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, indexSignature, container, otherMembers, parameterName, parameterType)); |
| 26 | + return [createCodeFixAction(fixId, changes, [Diagnostics.Convert_0_to_mapped_object_type, idText(container.name)], fixId, [Diagnostics.Convert_0_to_mapped_object_type, idText(container.name)])]; |
| 27 | + }, |
| 28 | + fixIds: [fixId], |
| 29 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
| 30 | + const info = getFixableSignatureAtPosition(diag.file, diag.start); |
| 31 | + if (!info) return; |
| 32 | + const { indexSignature, container, otherMembers, parameterName, parameterType } = info; |
| 33 | + |
| 34 | + doChange(changes, context.sourceFile, indexSignature, container, otherMembers, parameterName, parameterType); |
| 35 | + }) |
| 36 | + }); |
| 37 | + |
| 38 | + function isFixableParameterName(node: Node): boolean { |
| 39 | + return node && node.parent && node.parent.parent && node.parent.parent.parent && !isClassDeclaration(node.parent.parent.parent); |
| 40 | + } |
| 41 | + |
| 42 | + function getFixableSignatureAtPosition(sourceFile: SourceFile, pos: number): Info | undefined { |
| 43 | + const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); |
| 44 | + if (!isFixableParameterName(token)) return undefined; |
| 45 | + |
| 46 | + const indexSignature = <IndexSignatureDeclaration>token.parent.parent; |
| 47 | + const container = isInterfaceDeclaration(indexSignature.parent) ? indexSignature.parent : <TypeAliasDeclaration>indexSignature.parent.parent; |
| 48 | + const members = isInterfaceDeclaration(container) ? container.members : (<TypeLiteralNode>container.type).members; |
| 49 | + const otherMembers = filter(members, member => !isIndexSignatureDeclaration(member)); |
| 50 | + const parameter = first(indexSignature.parameters); |
| 51 | + |
| 52 | + return { |
| 53 | + indexSignature, |
| 54 | + container, |
| 55 | + otherMembers, |
| 56 | + parameterName: <Identifier>parameter.name, |
| 57 | + parameterType: parameter.type! |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + function getInterfaceHeritageClauses(declaration: FixableDeclaration): NodeArray<ExpressionWithTypeArguments> | undefined { |
| 62 | + if (!isInterfaceDeclaration(declaration)) return undefined; |
| 63 | + |
| 64 | + const heritageClause = getHeritageClause(declaration.heritageClauses, SyntaxKind.ExtendsKeyword); |
| 65 | + return heritageClause && heritageClause.types; |
| 66 | + } |
| 67 | + |
| 68 | + function createTypeAliasFromInterface(indexSignature: IndexSignatureDeclaration, declaration: FixableDeclaration, otherMembers: ReadonlyArray<TypeElement>, parameterName: Identifier, parameterType: TypeNode) { |
| 69 | + const heritageClauses = getInterfaceHeritageClauses(declaration); |
| 70 | + const mappedTypeParameter = createTypeParameterDeclaration(parameterName, parameterType); |
| 71 | + const mappedIntersectionType = createMappedTypeNode( |
| 72 | + hasReadonlyModifier(indexSignature) ? createModifier(SyntaxKind.ReadonlyKeyword) : undefined, |
| 73 | + mappedTypeParameter, |
| 74 | + indexSignature.questionToken, |
| 75 | + indexSignature.type); |
| 76 | + |
| 77 | + return createTypeAliasDeclaration( |
| 78 | + declaration.decorators, |
| 79 | + declaration.modifiers, |
| 80 | + declaration.name, |
| 81 | + declaration.typeParameters, |
| 82 | + createIntersectionTypeNode( |
| 83 | + concatenate( |
| 84 | + heritageClauses, |
| 85 | + append<TypeNode>([mappedIntersectionType], otherMembers.length ? createTypeLiteralNode(otherMembers) : undefined) |
| 86 | + ) |
| 87 | + ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, indexSignature: IndexSignatureDeclaration, declaration: FixableDeclaration, otherMembers: ReadonlyArray<TypeElement>, parameterName: Identifier, parameterType: TypeNode) { |
| 92 | + changes.replaceNode(sourceFile, declaration, createTypeAliasFromInterface(indexSignature, declaration, otherMembers, parameterName, parameterType)); |
| 93 | + } |
| 94 | +} |
0 commit comments