Skip to content

Commit b9ae36c

Browse files
author
Arthur Ozga
committed
Simplify index signature generation
1 parent 1338b94 commit b9ae36c

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ namespace ts {
20372037
return result;
20382038
}
20392039

2040-
function indexSignatureToString(info: IndexInfo, kind: SyntaxKind, enclosingDeclaration?: Node): string {
2040+
function indexSignatureToString(info: IndexInfo, kind: IndexKind, enclosingDeclaration?: Node): string {
20412041
const writer = getSingleLineStringWriter();
20422042
getSymbolDisplayBuilder().buildIndexSignatureDisplay(info, writer, kind, enclosingDeclaration);
20432043
const result = writer.string();
@@ -2557,8 +2557,8 @@ namespace ts {
25572557
writePunctuation(writer, SyntaxKind.SemicolonToken);
25582558
writer.writeLine();
25592559
}
2560-
buildIndexSignatureDisplay(resolved.stringIndexInfo, writer, SyntaxKind.StringKeyword, enclosingDeclaration, globalFlags, symbolStack);
2561-
buildIndexSignatureDisplay(resolved.numberIndexInfo, writer, SyntaxKind.NumberKeyword, enclosingDeclaration, globalFlags, symbolStack);
2560+
buildIndexSignatureDisplay(resolved.stringIndexInfo, writer, IndexKind.String, enclosingDeclaration, globalFlags, symbolStack);
2561+
buildIndexSignatureDisplay(resolved.numberIndexInfo, writer, IndexKind.Number, enclosingDeclaration, globalFlags, symbolStack);
25622562
for (const p of resolved.properties) {
25632563
const t = getTypeOfSymbol(p);
25642564
if (p.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(t).length) {
@@ -2791,10 +2791,7 @@ namespace ts {
27912791
buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack);
27922792
}
27932793

2794-
/**
2795-
* @param keyword The keyword for the type of IndexSignature. Must be one of SyntaxKind.NumberKeyword or SyntaxKind.StringKeyword.
2796-
*/
2797-
function buildIndexSignatureDisplay(info: IndexInfo, writer: SymbolWriter, keyword: SyntaxKind, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]) {
2794+
function buildIndexSignatureDisplay(info: IndexInfo, writer: SymbolWriter, kind: IndexKind, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]) {
27982795
if (info) {
27992796
if (info.isReadonly) {
28002797
writeKeyword(writer, SyntaxKind.ReadonlyKeyword);
@@ -2804,7 +2801,15 @@ namespace ts {
28042801
writer.writeParameter(info.declaration ? declarationNameToString(info.declaration.parameters[0].name) : "x");
28052802
writePunctuation(writer, SyntaxKind.ColonToken);
28062803
writeSpace(writer);
2807-
writeKeyword(writer, keyword);
2804+
switch (kind) {
2805+
case IndexKind.Number:
2806+
writeKeyword(writer, SyntaxKind.NumberKeyword);
2807+
break;
2808+
case IndexKind.String:
2809+
writeKeyword(writer, SyntaxKind.StringKeyword);
2810+
break;
2811+
}
2812+
28082813
writePunctuation(writer, SyntaxKind.CloseBracketToken);
28092814
writePunctuation(writer, SyntaxKind.ColonToken);
28102815
writeSpace(writer);

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,7 @@ namespace ts {
23532353
getTypeAtLocation(node: Node): Type;
23542354
getTypeFromTypeReference(node: TypeReferenceNode | ExpressionWithTypeArguments | JSDocTypeReference): Type;
23552355
signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): string;
2356-
indexSignatureToString(info: IndexInfo, kind: SyntaxKind, enclosingDeclaration?: Node): string;
2356+
indexSignatureToString(info: IndexInfo, kind: IndexKind, enclosingDeclaration?: Node): string;
23572357
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
23582358
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;
23592359
createSymbol(flags: SymbolFlags, name: string): Symbol;
@@ -2398,7 +2398,7 @@ namespace ts {
23982398
buildTypeDisplay(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
23992399
buildSymbolDisplay(symbol: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, meaning?: SymbolFlags, flags?: SymbolFormatFlags): void;
24002400
buildSignatureDisplay(signatures: Signature, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): void;
2401-
buildIndexSignatureDisplay(info: IndexInfo, writer: SymbolWriter, keyword: SyntaxKind, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]): void;
2401+
buildIndexSignatureDisplay(info: IndexInfo, writer: SymbolWriter, kind: IndexKind, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]): void;
24022402
buildParameterDisplay(parameter: Symbol, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
24032403
buildTypeParameterDisplay(tp: TypeParameter, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;
24042404
buildTypePredicateDisplay(predicate: TypePredicate, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags): void;

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,16 @@ namespace ts.codefix {
2222
const hasStringIndexSignature = !!checker.getIndexTypeOfType(classType, IndexKind.String);
2323

2424
for (const implementedTypeNode of implementedTypeNodes) {
25-
const implementedType = checker.getTypeFromTypeReference(implementedTypeNode) as InterfaceTypeWithDeclaredMembers;
25+
const implementedType = checker.getTypeFromTypeReference(implementedTypeNode) as InterfaceType;
2626
// Note that this is ultimately derived from a map indexed by symbol names,
2727
// so duplicates cannot occur.
2828
const implementedTypeSymbols = checker.getPropertiesOfType(implementedType);
2929
const nonPrivateMembers = implementedTypeSymbols.filter(symbolRefersToNonPrivateMember);
3030

31-
let insertion = "";
32-
33-
if (!hasNumericIndexSignature) {
34-
const typeNumericIndexInfo = checker.getIndexInfoOfType(implementedType, IndexKind.Number);
35-
if (typeNumericIndexInfo) {
36-
insertion = checker.indexSignatureToString(typeNumericIndexInfo, SyntaxKind.NumberKeyword, classDecl);
37-
}
38-
}
39-
40-
if (!hasStringIndexSignature) {
41-
const typeStringIndexInfo = checker.getIndexInfoOfType(implementedType, IndexKind.String);
42-
if (typeStringIndexInfo) {
43-
insertion += checker.indexSignatureToString(typeStringIndexInfo, SyntaxKind.StringKeyword, classDecl);
44-
}
45-
}
46-
31+
let insertion = getMissingIndexSignatureInsertion(implementedType, IndexKind.Number, classDecl, hasNumericIndexSignature);
32+
insertion += getMissingIndexSignatureInsertion(implementedType, IndexKind.String, classDecl, hasStringIndexSignature);
4733
insertion += getMissingMembersInsertion(classDecl, nonPrivateMembers, checker, context.newLineCharacter);
34+
4835
const message = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Implement_interface_0), [implementedTypeNode.getText()]);
4936
if (insertion) {
5037
pushAction(result, insertion, message);
@@ -53,6 +40,16 @@ namespace ts.codefix {
5340

5441
return result;
5542

43+
function getMissingIndexSignatureInsertion(type: InterfaceType, kind: IndexKind, enclosingDeclaration: ClassLikeDeclaration, hasIndexSigOfKind: boolean) {
44+
if (!hasIndexSigOfKind) {
45+
const IndexInfoOfKind = checker.getIndexInfoOfType(type, kind);
46+
if (IndexInfoOfKind) {
47+
return checker.indexSignatureToString(IndexInfoOfKind, kind, enclosingDeclaration);
48+
}
49+
}
50+
return "";
51+
}
52+
5653
function symbolRefersToNonPrivateMember(symbol: Symbol): boolean {
5754
const decls = symbol.getDeclarations();
5855
Debug.assert(!!(decls && decls.length > 0));

0 commit comments

Comments
 (0)