Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFC tidy up #140

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ venv
.venv
coverage.xml
test-results.xml
node_modules
tsconfig.tsbuildinfo
64 changes: 32 additions & 32 deletions sphinx_js/js/convertTopLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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(
Expand Down
69 changes: 32 additions & 37 deletions sphinx_js/js/renderType.ts → sphinx_js/js/convertType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ReflectionKind,
ReflectionType,
RestType,
Serializer,
SignatureReflection,
SomeType,
TemplateLiteralType,
Expand All @@ -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<Type> {
class TypeConverter implements TypeVisitor<Type> {
private readonly basePath: string;
// For resolving XRefs.
private readonly reflToPath: ReadonlyMap<
Expand Down Expand Up @@ -87,9 +86,9 @@ class TypeRenderer implements TypeVisitor<Type> {
}

/**
* 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("(");
Expand All @@ -105,9 +104,9 @@ class TypeRenderer implements TypeVisitor<Type> {
// TODO: switch to correct impl
return ["<TODO: not implemented indexedAccess>"];
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),
"]",
];
}
Expand All @@ -117,7 +116,7 @@ class TypeRenderer implements TypeVisitor<Type> {
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();
Expand Down Expand Up @@ -151,7 +150,7 @@ class TypeRenderer implements TypeVisitor<Type> {
query(type: QueryType): Type {
return [
"typeof ",
...this.render(type.queryType, TypeContext.queryTypeTarget),
...this.convert(type.queryType, TypeContext.queryTypeTarget),
];
}
/**
Expand Down Expand Up @@ -267,21 +266,17 @@ class TypeRenderer implements TypeVisitor<Type> {
}
}
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");
}
Expand All @@ -294,7 +289,7 @@ class TypeRenderer implements TypeVisitor<Type> {
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();
Expand All @@ -304,20 +299,20 @@ class TypeRenderer implements TypeVisitor<Type> {
}
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();
Expand All @@ -329,12 +324,12 @@ class TypeRenderer implements TypeVisitor<Type> {
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 + ": ");
Expand All @@ -353,9 +348,9 @@ class TypeRenderer implements TypeVisitor<Type> {
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;
Expand All @@ -370,8 +365,8 @@ class TypeRenderer implements TypeVisitor<Type> {
// [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,
);
Expand All @@ -396,7 +391,7 @@ class TypeRenderer implements TypeVisitor<Type> {
}
}

export function renderType(
export function convertType(
basePath: string,
reflToPath: ReadonlyMap<
DeclarationReflection | SignatureReflection,
Expand All @@ -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(
Expand All @@ -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);
}
Loading