Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Reintroduce support for index signatures
- Loading branch information
Showing
5 changed files
with
425 additions
and
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as assert from "assert"; | ||
import * as ts from "typescript"; | ||
import { | ||
DeclarationReflection, | ||
ParameterReflection, | ||
ReflectionKind, | ||
SignatureReflection, | ||
} from "../../models"; | ||
import { Context } from "../context"; | ||
import { ConverterEvents } from "../converter-events"; | ||
|
||
export function convertIndexSignature(context: Context, symbol: ts.Symbol) { | ||
assert(context.scope instanceof DeclarationReflection); | ||
|
||
const indexSymbol = symbol.members?.get("__index" as ts.__String); | ||
if (indexSymbol) { | ||
// Right now TypeDoc models don't have a way to distinguish between string | ||
// and number index signatures... { [x: string]: 1 | 2; [x: number]: 2 } | ||
// will be misrepresented. | ||
const indexDeclaration = indexSymbol.getDeclarations()?.[0]; | ||
assert( | ||
indexDeclaration && ts.isIndexSignatureDeclaration(indexDeclaration) | ||
); | ||
const param = indexDeclaration.parameters[0]; | ||
assert(param && ts.isParameter(param)); | ||
const index = new SignatureReflection( | ||
"__index", | ||
ReflectionKind.IndexSignature, | ||
context.scope | ||
); | ||
index.parameters = [ | ||
new ParameterReflection( | ||
param.name.getText(), | ||
ReflectionKind.Parameter, | ||
index | ||
), | ||
]; | ||
index.parameters[0].type = context.converter.convertType( | ||
context.withScope(index.parameters[0]), | ||
param.type | ||
); | ||
index.type = context.converter.convertType( | ||
context.withScope(index), | ||
indexDeclaration.type | ||
); | ||
context.registerReflection(index, indexSymbol); | ||
context.trigger( | ||
ConverterEvents.CREATE_SIGNATURE, | ||
index, | ||
indexDeclaration | ||
); | ||
|
||
context.scope.indexSignature = index; | ||
} | ||
} |
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
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,15 @@ | ||
export interface StrIndex { | ||
[x: string]: 1; | ||
} | ||
|
||
export interface NumIndex { | ||
[x: number]: 1; | ||
} | ||
|
||
// This is broken... but here's a test for the broken behavior so we know when it is fixed. | ||
export interface BothIndex { | ||
[x: number]: 1; | ||
[x: string]: 1 | 2; | ||
} | ||
|
||
export type TypeIndex = { [x: string]: 1 }; |
Oops, something went wrong.