diff --git a/.gitignore b/.gitignore index 3c63f408..61d54757 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ venv .venv coverage.xml test-results.xml +node_modules +tsconfig.tsbuildinfo diff --git a/sphinx_js/js/convertTopLevel.ts b/sphinx_js/js/convertTopLevel.ts index 3d48de4e..8dfe0921 100644 --- a/sphinx_js/js/convertTopLevel.ts +++ b/sphinx_js/js/convertTopLevel.ts @@ -12,7 +12,7 @@ import { TypeContext, TypeParameterReflection, } from "typedoc"; -import { referenceToXRef, renderType } from "./renderType.ts"; +import { referenceToXRef, convertType } from "./convertType.ts"; import { NO_DEFAULT, Attribute, @@ -78,7 +78,7 @@ export function parseFilePath(path: string, base_dir: string): string[] { function isAnonymousTypeLiteral( refl: DeclarationReflection | SignatureReflection, ): boolean { - return refl.kind === ReflectionKind.TypeLiteral && refl.name === "__type"; + return refl.kindOf(ReflectionKind.TypeLiteral) && refl.name === "__type"; } /** @@ -151,14 +151,14 @@ class PathComputer implements ReflectionVisitor { ); // Skip some redundant names const suppressReflName = - [ + refl.kindOf( // Module names are redundant with the file path - ReflectionKind.Module, - // Signature names are redundant with the callable. TODO: do we want to - // handle callables with multiple signatures? - ReflectionKind.ConstructorSignature, - ReflectionKind.CallSignature, - ].includes(refl.kind) || isAnonymousTypeLiteral(refl); + ReflectionKind.Module | + // Signature names are redundant with the callable. TODO: do we want to + // handle callables with multiple signatures? + ReflectionKind.ConstructorSignature | + ReflectionKind.CallSignature, + ) || isAnonymousTypeLiteral(refl); if (suppressReflName) { return segments; } @@ -359,7 +359,7 @@ export class Converter { } renderType(type: SomeType, context: TypeContext = TypeContext.none): Type { - return renderType( + return convertType( this.basePath, this.pathMap, this.symbolToType, @@ -418,18 +418,18 @@ export class Converter { toIr(object: DeclarationReflection | SignatureReflection): ConvertResult { // ReflectionKinds that we give no conversion. if ( - [ - ReflectionKind.Module, - ReflectionKind.Namespace, - // TODO: document TypeAliases - ReflectionKind.TypeAlias, - // TODO: document enums - ReflectionKind.Enum, - ReflectionKind.EnumMember, - // A ReferenceReflection is when we reexport something. - // TODO: should we handle this somehow? - ReflectionKind.Reference, - ].includes(object.kind) + object.kindOf( + ReflectionKind.Module | + ReflectionKind.Namespace | + // TODO: document TypeAliases + ReflectionKind.TypeAlias | + // TODO: document enums + ReflectionKind.Enum | + ReflectionKind.EnumMember | + // A ReferenceReflection is when we reexport something. + // TODO: should we handle this somehow? + ReflectionKind.Reference, + ) ) { // TODO: The children of these have no rendered parent in the docs. If // "object" is marked as a documentation_root, maybe the children should @@ -528,7 +528,7 @@ export class Converter { convertProperty(prop: DeclarationReflection): ConvertResult { if ( prop.type?.type === "reflection" && - prop.type.declaration.kind == ReflectionKind.TypeLiteral && + prop.type.declaration.kindOf(ReflectionKind.TypeLiteral) && prop.type.declaration.signatures?.length ) { // Render {f: () => void} like {f(): void} @@ -596,12 +596,12 @@ export class Converter { convertClassChild(child: DeclarationReflection): IRFunction | Attribute { if ( - ![ - ReflectionKind.Accessor, - ReflectionKind.Constructor, - ReflectionKind.Method, - ReflectionKind.Property, - ].includes(child.kind) + !child.kindOf( + ReflectionKind.Accessor | + ReflectionKind.Constructor | + ReflectionKind.Method | + ReflectionKind.Property, + ) ) { throw new TypeError( "Expected an Accessor, Constructor, Method, or Property", @@ -632,7 +632,7 @@ export class Converter { if (child.inheritedFrom) { continue; } - if (child.kind === ReflectionKind.Constructor) { + if (child.kindOf(ReflectionKind.Constructor)) { // This really, really should happen exactly once per class. constructor = this.functionToIR(child); constructor.returns = []; @@ -824,11 +824,11 @@ export class Converter { // correct that it returns a class instance but it looks weird). // Also hide explicit void return type. const voidReturnType = - func.kind === ReflectionKind.Constructor || + func.kindOf(ReflectionKind.Constructor) || !first_sig.type || (first_sig.type.type === "intrinsic" && first_sig.type.name === "void"); let type_params = this.typeParamsToIR(first_sig.typeParameters); - if (func.kind === ReflectionKind.Constructor) { + if (func.kindOf(ReflectionKind.Constructor)) { // I think this is wrong // TODO: remove it type_params = this.typeParamsToIR( diff --git a/sphinx_js/js/renderType.ts b/sphinx_js/js/convertType.ts similarity index 83% rename from sphinx_js/js/renderType.ts rename to sphinx_js/js/convertType.ts index 607c70b5..733f0929 100644 --- a/sphinx_js/js/renderType.ts +++ b/sphinx_js/js/convertType.ts @@ -16,7 +16,6 @@ import { ReflectionKind, ReflectionType, RestType, - Serializer, SignatureReflection, SomeType, TemplateLiteralType, @@ -32,19 +31,19 @@ import { TypeXRefExternal, TypeXRefInternal, intrinsicType, -} from "./ir.ts"; -import { parseFilePath } from "./convertTopLevel.ts"; -import { ReadonlySymbolToType } from "./redirectPrivateAliases.ts"; +} from "./ir.js"; +import { parseFilePath } from "./convertTopLevel.js"; +import { ReadonlySymbolToType } from "./redirectPrivateAliases.js"; /** - * Render types into a list of strings and XRefs. + * Convert types into a list of strings and XRefs. * * Most visitor nodes should be similar to the implementation of getTypeString * on the same type. * * TODO: implement the remaining not implemented cases and add test coverage. */ -class TypeRenderer implements TypeVisitor { +class TypeConverter implements TypeVisitor { private readonly basePath: string; // For resolving XRefs. private readonly reflToPath: ReadonlyMap< @@ -87,9 +86,9 @@ class TypeRenderer implements TypeVisitor { } /** - * Render the type, maybe add parentheses + * Convert the type, maybe add parentheses */ - render(type: SomeType, context: TypeContext): Type { + convert(type: SomeType, context: TypeContext): Type { const result = type.visit(this); if (type.needsParenthesis(context)) { result.unshift("("); @@ -105,9 +104,9 @@ class TypeRenderer implements TypeVisitor { // TODO: switch to correct impl return [""]; return [ - ...this.render(type.objectType, TypeContext.indexedObject), + ...this.convert(type.objectType, TypeContext.indexedObject), "[", - ...this.render(type.indexType, TypeContext.indexedIndex), + ...this.convert(type.indexType, TypeContext.indexedIndex), "]", ]; } @@ -117,7 +116,7 @@ class TypeRenderer implements TypeVisitor { intersection(type: IntersectionType): Type { const result: Type = []; for (const elt of type.types) { - result.push(...this.render(elt, TypeContext.intersectionElement)); + result.push(...this.convert(elt, TypeContext.intersectionElement)); result.push(" & "); } result.pop(); @@ -151,7 +150,7 @@ class TypeRenderer implements TypeVisitor { query(type: QueryType): Type { return [ "typeof ", - ...this.render(type.queryType, TypeContext.queryTypeTarget), + ...this.convert(type.queryType, TypeContext.queryTypeTarget), ]; } /** @@ -267,21 +266,17 @@ class TypeRenderer implements TypeVisitor { } } reflection(type: ReflectionType): Type { - if (type.declaration.kind === ReflectionKind.TypeLiteral) { - return this.renderTypeLiteral(type.declaration); + if (type.declaration.kindOf(ReflectionKind.TypeLiteral)) { + return this.convertTypeLiteral(type.declaration); } - if (type.declaration.kind === ReflectionKind.Constructor) { - const result = this.renderSignature(type.declaration.signatures![0]); + if (type.declaration.kindOf(ReflectionKind.Constructor)) { + const result = this.convertSignature(type.declaration.signatures![0]); result.unshift("{new "); result.push("}"); return result; } - if ( - [ReflectionKind.Function, ReflectionKind.Method].includes( - type.declaration.kind, - ) - ) { - return this.renderSignature(type.declaration.signatures![0]); + if (type.declaration.kindOf(ReflectionKind.FunctionOrMethod)) { + return this.convertSignature(type.declaration.signatures![0]); } throw new Error("Not implemented"); } @@ -294,7 +289,7 @@ class TypeRenderer implements TypeVisitor { tuple(type: TupleType): Type { const result: Type = []; for (const elt of type.elements) { - result.push(...this.render(elt, TypeContext.tupleElement)); + result.push(...this.convert(elt, TypeContext.tupleElement)); result.push(", "); } result.pop(); @@ -304,20 +299,20 @@ class TypeRenderer implements TypeVisitor { } namedTupleMember(type: NamedTupleMember): Type { const result: Type = [`${type.name}${type.isOptional ? "?" : ""}: `]; - result.push(...this.render(type.element, TypeContext.tupleElement)); + result.push(...this.convert(type.element, TypeContext.tupleElement)); return result; } typeOperator(type: TypeOperatorType): Type { return [ type.operator, " ", - ...this.render(type.target, TypeContext.typeOperatorTarget), + ...this.convert(type.target, TypeContext.typeOperatorTarget), ]; } union(type: UnionType): Type { const result: Type = []; for (const elt of type.types) { - result.push(...this.render(elt, TypeContext.unionElement)); + result.push(...this.convert(elt, TypeContext.unionElement)); result.push(" | "); } result.pop(); @@ -329,12 +324,12 @@ class TypeRenderer implements TypeVisitor { return [type.name]; } array(t: ArrayType): Type { - const res = this.render(t.elementType, TypeContext.arrayElement); + const res = this.convert(t.elementType, TypeContext.arrayElement); res.push("[]"); return res; } - renderSignature(sig: SignatureReflection): Type { + convertSignature(sig: SignatureReflection): Type { const result: Type = ["("]; for (const param of sig.parameters || []) { result.push(param.name + ": "); @@ -353,9 +348,9 @@ class TypeRenderer implements TypeVisitor { return result; } - renderTypeLiteral(lit: DeclarationReflection): Type { + convertTypeLiteral(lit: DeclarationReflection): Type { if (lit.signatures) { - return this.renderSignature(lit.signatures[0]); + return this.convertSignature(lit.signatures[0]); } const result: Type = ["{ "]; const index_sig = lit.indexSignature; @@ -370,8 +365,8 @@ class TypeRenderer implements TypeVisitor { // [k in mappedParam]: mappedTemplate // vs // [k: keyType]: valueType - const keyType = this.render(key.type!, TypeContext.mappedParameter); - const valueType = this.render( + const keyType = this.convert(key.type!, TypeContext.mappedParameter); + const valueType = this.convert( index_sig.type!, TypeContext.mappedTemplate, ); @@ -396,7 +391,7 @@ class TypeRenderer implements TypeVisitor { } } -export function renderType( +export function convertType( basePath: string, reflToPath: ReadonlyMap< DeclarationReflection | SignatureReflection, @@ -406,8 +401,8 @@ export function renderType( type: SomeType, context: TypeContext = TypeContext.none, ): Type { - const renderer = new TypeRenderer(basePath, reflToPath, symbolToType); - return renderer.render(type, context); + const typeConverter = new TypeConverter(basePath, reflToPath, symbolToType); + return typeConverter.convert(type, context); } export function referenceToXRef( @@ -419,6 +414,6 @@ export function referenceToXRef( symbolToType: ReadonlySymbolToType, type: ReferenceType, ): Type { - const renderer = new TypeRenderer(basePath, reflToPath, symbolToType); - return renderer.convertReferenceToXRef(type); + const converter = new TypeConverter(basePath, reflToPath, symbolToType); + return converter.convertReferenceToXRef(type); }