-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support TypeScript 5.7, drop TS <5.0
Also fix readonly index signatures.
- Loading branch information
Showing
15 changed files
with
572 additions
and
226 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1,97 @@ | ||
import assert from "assert"; | ||
import ts from "typescript"; | ||
import type ts from "typescript"; | ||
import { | ||
DeclarationReflection, | ||
ParameterReflection, | ||
ReflectionFlag, | ||
ReflectionKind, | ||
SignatureReflection, | ||
UnionType, | ||
} from "../../models/index.js"; | ||
import type { Context } from "../context.js"; | ||
import { ConverterEvents } from "../converter-events.js"; | ||
|
||
export function convertIndexSignatures(context: Context, symbol: ts.Symbol) { | ||
export function convertIndexSignatures(context: Context, type: ts.Type) { | ||
assert(context.scope instanceof DeclarationReflection); | ||
|
||
const indexSymbol = symbol.members?.get("__index" as ts.__String); | ||
if (!indexSymbol) return; | ||
// Each declaration should have one SignatureReflection | ||
// If we don't have a declaration, the index signature was inferred by TS | ||
// and we should make a separate SignatureReflection for each index signature. | ||
const seenByDeclaration = new Map<ts.Declaration, SignatureReflection>(); | ||
const createdSignatures: [ | ||
ts.IndexSignatureDeclaration | undefined, | ||
SignatureReflection, | ||
][] = []; | ||
|
||
for (const indexDeclaration of indexSymbol.getDeclarations() || []) { | ||
assert(ts.isIndexSignatureDeclaration(indexDeclaration)); | ||
const param = indexDeclaration.parameters[0] as | ||
| ts.ParameterDeclaration | ||
| undefined; | ||
assert(param && ts.isParameter(param)); | ||
for (const indexInfo of context.checker.getIndexInfosOfType(type)) { | ||
// Check if we've already created an index signature for this type | ||
if ( | ||
indexInfo.declaration && | ||
seenByDeclaration.has(indexInfo.declaration) | ||
) { | ||
const createdSig = seenByDeclaration.get(indexInfo.declaration)!; | ||
if (createdSig.parameters![0].type?.type !== "union") { | ||
createdSig.parameters![0].type = new UnionType([ | ||
createdSig.parameters![0].type!, | ||
]); | ||
} | ||
createdSig.parameters![0].type.types.push( | ||
context.converter.convertType( | ||
context.withScope(createdSig), | ||
indexInfo.keyType, | ||
), | ||
); | ||
// No need to update the return type as it will be the same for all members. | ||
continue; | ||
} | ||
|
||
// Otherwise create a new one | ||
const index = new SignatureReflection( | ||
"__index", | ||
ReflectionKind.IndexSignature, | ||
context.scope, | ||
); | ||
index.comment = context.getNodeComment(indexDeclaration, false); | ||
if (indexInfo.isReadonly) { | ||
index.setFlag(ReflectionFlag.Readonly); | ||
} | ||
|
||
createdSignatures.push([indexInfo.declaration, index]); | ||
if (indexInfo.declaration) { | ||
seenByDeclaration.set(indexInfo.declaration, index); | ||
index.comment = context.getNodeComment( | ||
indexInfo.declaration, | ||
/* moduleComment */ false, | ||
); | ||
} | ||
|
||
index.parameters = [ | ||
new ParameterReflection( | ||
param.name.getText(), | ||
indexInfo.declaration?.parameters[0].name.getText() ?? "key", | ||
ReflectionKind.Parameter, | ||
index, | ||
), | ||
]; | ||
index.parameters[0].type = context.converter.convertType( | ||
context.withScope(index.parameters[0]), | ||
param.type, | ||
indexInfo.keyType, | ||
); | ||
index.type = context.converter.convertType( | ||
context.withScope(index), | ||
indexDeclaration.type, | ||
indexInfo.type, | ||
); | ||
context.registerReflection(index, indexSymbol); | ||
|
||
context.registerReflection(index, indexInfo.declaration?.symbol); | ||
context.scope.indexSignatures ||= []; | ||
context.scope.indexSignatures.push(index); | ||
} | ||
|
||
// Now that we've created everything, trigger events for them. | ||
for (const [declaration, index] of createdSignatures) { | ||
context.converter.trigger( | ||
ConverterEvents.CREATE_SIGNATURE, | ||
context, | ||
index, | ||
indexDeclaration, | ||
declaration, | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.