diff --git a/packages/typedoc-signature/lib/internal/concise.ts b/packages/typedoc-signature/lib/internal/concise.ts index 2709fee59..849961518 100644 --- a/packages/typedoc-signature/lib/internal/concise.ts +++ b/packages/typedoc-signature/lib/internal/concise.ts @@ -1,8 +1,5 @@ /* eslint-disable unicorn/no-array-push-push */ -import { - type Declaration, - type Fragment, -} from "@onlyoffice/library-declaration/next.ts" +import {type Fragment} from "@onlyoffice/library-declaration/next.ts" import { ParameterToken, Reference, @@ -35,18 +32,9 @@ import { isUnionType, } from "@onlyoffice/typedoc-util-is-type" import {type JSONOutput as J} from "typedoc" -import {type Formatter} from "./formatter.ts" - -export interface Context { - f: Formatter - trailOf(t: Declaration | Fragment): FlatTrail | undefined - reflectionOf(t: FlatTrail): J.Reflection | undefined - idOf(id: number): number | undefined -} - -export type FlatTrail = number[] +import {type Context} from "./context.ts" -export function classDeclaration(cf: Context, r: J.Reflection): Signature { +export function classDeclaration(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isClassReflection(r)) { @@ -57,7 +45,7 @@ export function classDeclaration(cf: Context, r: J.Reflection): Signature { t.text = " " s.push(t) - s.push(reference(r.id, r.name, cf)) + s.push(reference(ctx, r.id, r.name)) return s } @@ -75,7 +63,7 @@ export function constructorDeclaration(r: J.Reflection): Signature { return s } -export function enumMemberReflection(cf: Context, r: J.Reflection): Signature { +export function enumMemberReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isEnumMemberReflection(r)) { @@ -87,16 +75,16 @@ export function enumMemberReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } else { - s.push(reference(r.id, "0", cf)) + s.push(reference(ctx, r.id, "0")) } return s } -export function enumReflection(cf: Context, r: J.Reflection): Signature { +export function enumReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isEnumReflection(r)) { @@ -107,7 +95,7 @@ export function enumReflection(cf: Context, r: J.Reflection): Signature { t.text = " " s.push(t) - s.push(reference(r.id, r.name, cf)) + s.push(reference(ctx, r.id, r.name)) return s } @@ -135,7 +123,7 @@ export function functionsDeclaration(f: J.Reflection): Signature { return s } -export function interfaceReflection(cf: Context, r: J.Reflection): Signature { +export function interfaceReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isInterfaceReflection(r)) { @@ -146,7 +134,7 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { t.text = " " s.push(t) - s.push(reference(r.id, r.name, cf)) + s.push(reference(ctx, r.id, r.name)) return s } @@ -168,7 +156,7 @@ export function methodDeclaration(m: J.Reflection): Signature { return s } -export function propertyReflection(cf: Context, r: J.Reflection): Signature { +export function propertyReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isPropertyReflection(r)) { @@ -183,14 +171,14 @@ export function propertyReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } return s } -export function typeAliasReflection(cf: Context, r: J.Reflection): Signature { +export function typeAliasReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isTypeAliasReflection(r)) { @@ -201,12 +189,12 @@ export function typeAliasReflection(cf: Context, r: J.Reflection): Signature { t.text = " " s.push(t) - s.push(reference(r.id, r.name, cf)) + s.push(reference(ctx, r.id, r.name)) return s } -export function variableDeclaration(cf: Context, r: J.Reflection): Signature { +export function variableDeclaration(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isVariableReflection(r)) { @@ -218,7 +206,7 @@ export function variableDeclaration(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } @@ -265,9 +253,9 @@ export function value(p: J.Reflection): Signature { return s } -export function type(t: J.SomeType, cf: Context): Signature { +export function type(ctx: Context, t: J.SomeType): Signature { if (isArrayType(t)) { - return arrayType(t, cf) + return arrayType(ctx, t) } if (isIntrinsicType(t)) { return intrinsicType(t) @@ -276,24 +264,24 @@ export function type(t: J.SomeType, cf: Context): Signature { return literalType(t) } if (isReferenceType(t)) { - return referenceType(t, cf) + return referenceType(ctx, t) } if (isReflectionType(t)) { - return reflectionType(t, cf) + return reflectionType(ctx, t) } if (isTemplateLiteralType(t)) { - return templateLiteralType(t, cf) + return templateLiteralType(ctx, t) } if (isTupleType(t)) { - return tupleType(t, cf) + return tupleType(ctx, t) } if (isUnionType(t)) { - return unionType(t, cf) + return unionType(ctx, t) } return [] } -export function arrayType(a: J.ArrayType, cf: Context): Signature { +export function arrayType(ctx: Context, a: J.ArrayType): Signature { const s: Signature = [] let t: Token @@ -303,7 +291,7 @@ export function arrayType(a: J.ArrayType, cf: Context): Signature { s.push(t) } - const b = type(a.elementType, cf) + const b = type(ctx, a.elementType) s.push(...b) if (isUnionType(a.elementType.type)) { @@ -345,12 +333,12 @@ export function literalType(l: J.LiteralType): Signature { return s } -export function referenceType(r: J.ReferenceType, cf: Context): Signature { +export function referenceType(ctx: Context, r: J.ReferenceType): Signature { const s: Signature = [] let t: Token if (typeof r.target === "number") { - s.push(reference(r.target, r.name, cf)) + s.push(reference(ctx, r.target, r.name)) } if (r.typeArguments) { @@ -365,8 +353,8 @@ export function referenceType(r: J.ReferenceType, cf: Context): Signature { s.push(t) for (const a of r.typeArguments) { - const b = type(a, cf) - cf.f.preprocess(b) + let b = type(ctx, a) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -397,7 +385,7 @@ export function referenceType(r: J.ReferenceType, cf: Context): Signature { return s } -export function reflectionType(r: J.ReflectionType, cf: Context): Signature { +export function reflectionType(ctx: Context, r: J.ReflectionType): Signature { const s: Signature = [] let t: Token @@ -411,7 +399,7 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { t.text = " => " s.push(t) - const b = type(c.type, cf) + const b = type(ctx, c.type) s.push(...b) } } @@ -433,7 +421,7 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { } s.push(t) - const b = type(c.type, cf) + const b = type(ctx, c.type) s.push(...b) t = new TextToken() t.text = "; " @@ -458,7 +446,7 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { return s } -export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Signature { +export function templateLiteralType(ctx: Context, tt: J.TemplateLiteralType): Signature { const s: Signature = [] let t: Token @@ -479,8 +467,8 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Sig t.text = "${" s.push(t) - const b = type(e, cf) - cf.f.preprocess(b) + let b = type(ctx, e) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -500,7 +488,7 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Sig return s } -export function tupleType(tt: J.TupleType, cf: Context): Signature { +export function tupleType(ctx: Context, tt: J.TupleType): Signature { const s: Signature = [] let t: Token @@ -513,7 +501,7 @@ export function tupleType(tt: J.TupleType, cf: Context): Signature { s.push(t) for (const e of tt.elements) { - const b = type(e, cf) + const b = type(ctx, e) s.push(...b) t = new TextToken() @@ -529,7 +517,7 @@ export function tupleType(tt: J.TupleType, cf: Context): Signature { return s } -export function unionType(u: J.UnionType, cf: Context): Signature { +export function unionType(ctx: Context, u: J.UnionType): Signature { const s: Signature = [] let t: Token @@ -544,7 +532,7 @@ export function unionType(u: J.UnionType, cf: Context): Signature { s.push(t) } - const b = type(ts, cf) + const b = type(ctx, ts) s.push(...b) if (isReferenceType(ts.type)) { @@ -562,11 +550,11 @@ export function unionType(u: J.UnionType, cf: Context): Signature { return s } -export function fragment(f: Fragment[], cf: Context): void { +export function fragments(ctx: Context, f: Fragment[]): void { for (const e of f) { - const ft = cf.trailOf(e) + const ft = ctx.t.trailOf(e) if (ft) { - const t = cf.reflectionOf(ft) + const t = ctx.t.reflectionOf(ft) if (!t) { console.log(`Reflection for fragment ${e.name} not found`) continue @@ -575,7 +563,7 @@ export function fragment(f: Fragment[], cf: Context): void { return } if (t.type) { - const b = type(t.type, cf) + const b = type(ctx, t.type) e.signature.concise.push(...b) } } else { @@ -584,11 +572,11 @@ export function fragment(f: Fragment[], cf: Context): void { } } -export function returns(cf: Context, r: (J.SomeType | undefined)): Signature { +export function returns(ctx: Context, r?: J.SomeType): Signature { const s: Signature = [] if (r && r.type) { - const b = type(r, cf) + const b = type(ctx, r) s.push(...b) } else { const t = new TypeToken() @@ -599,9 +587,9 @@ export function returns(cf: Context, r: (J.SomeType | undefined)): Signature { return s } -function reference(t: number, n: string, cf: Context): Token | Reference { +function reference(ctx: Context, t: number, n: string): Token | Reference { let r: Token | Reference - const id = cf.idOf(t) + const id = ctx.t.idOf(t) if (id) { r = new Reference() diff --git a/packages/typedoc-signature/lib/internal/context.ts b/packages/typedoc-signature/lib/internal/context.ts new file mode 100644 index 000000000..c92b37d2d --- /dev/null +++ b/packages/typedoc-signature/lib/internal/context.ts @@ -0,0 +1,9 @@ +import {type Formatter} from "./formatter.ts" +import {type State} from "./state.ts" +import {type Transport} from "./transport.ts" + +export interface Context { + s: State + f: Formatter + t: Transport +} diff --git a/packages/typedoc-signature/lib/internal/formatter.ts b/packages/typedoc-signature/lib/internal/formatter.ts index 55aadb3b3..8340ed8b2 100644 --- a/packages/typedoc-signature/lib/internal/formatter.ts +++ b/packages/typedoc-signature/lib/internal/formatter.ts @@ -6,7 +6,7 @@ export class Formatter { l = 100 n = "\n" - preprocess(s: Signature): void { + preprocess(s: Signature): Signature { let l = 0 let f = false // has a formatted signature in the child elements @@ -35,12 +35,12 @@ export class Formatter { } } - s.length = 0 - s.push(...ts) + return ts } + return s } - format(s: Signature): void { + format(s: Signature): Signature { const ts: Signature = [] let t: Token @@ -59,8 +59,6 @@ export class Formatter { } } - s.length = 0 - s.push(...ts) + return ts } } - diff --git a/packages/typedoc-signature/lib/internal/transport.ts b/packages/typedoc-signature/lib/internal/transport.ts new file mode 100644 index 000000000..bf683aa4e --- /dev/null +++ b/packages/typedoc-signature/lib/internal/transport.ts @@ -0,0 +1,15 @@ +import { + type Declaration, + type Entity, + type Fragment, +} from "@onlyoffice/library-declaration/next.ts" +import {type JSONOutput as J} from "typedoc" + +export interface Transport { + entities: Entity[] + trailOf(t: Declaration | Fragment): FlatTrail | undefined + reflectionOf(t: FlatTrail): J.Reflection | undefined + idOf(id: number): number | undefined +} + +export type FlatTrail = number[] diff --git a/packages/typedoc-signature/lib/internal/verbose.ts b/packages/typedoc-signature/lib/internal/verbose.ts index dc4b2a6bd..84147cea3 100644 --- a/packages/typedoc-signature/lib/internal/verbose.ts +++ b/packages/typedoc-signature/lib/internal/verbose.ts @@ -37,20 +37,11 @@ import { } from "@onlyoffice/typedoc-util-is-type" import {type JSONOutput as J} from "typedoc" import {Console} from "../console.ts" -import {type Formatter} from "./formatter.ts" -import {type State} from "./state.ts" +import {type Context} from "./context.ts" const console = Console.shared -export interface Context { - s: State - f: Formatter - idOf(id: number): number | undefined -} - -export type FlatTrail = number[] - -export function classDeclaration(cf: Context, r: J.Reflection): Signature { +export function classDeclaration(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isClassReflection(r)) { @@ -95,35 +86,35 @@ export function classDeclaration(cf: Context, r: J.Reflection): Signature { t.text = " " s.push(t) - const b = type(e, cf) - cf.f.preprocess(b) + let b = type(ctx, e) + b = ctx.f.preprocess(b) s.push(...b) } } const ss: Signature = [] if (r.children) { - cf.s.in() + ctx.s.in() t = new TextToken() t.text = " {" ss.push(t) - ss.push(...newline(cf)) + ss.push(...newline(ctx)) for (const c of r.children) { if (isConstructorReflection(c) && c.signatures) { for (const cs of c.signatures) { if (isSignatureReflection(cs)) { - const cd = constructorDeclaration(cf, cs) + const cd = constructorDeclaration(ctx, cs) ss.push(...cd) - ss.push(...newline(cf)) + ss.push(...newline(ctx)) } } } if (isPropertyReflection(c)) { - const pr = propertyReflection(cf, c) + const pr = propertyReflection(ctx, c) const ts: Signature = [] for (const e of pr) { if (e instanceof EntityToken) { @@ -136,13 +127,13 @@ export function classDeclaration(cf: Context, r: J.Reflection): Signature { } ss.push(...ts) - ss.push(...newline(cf)) + ss.push(...newline(ctx)) } if (isMethodReflection(c) && c.signatures) { for (const cs of c.signatures) { if (isSignatureReflection(cs)) { - const md = methodDeclaration(cs, c, cf) + const md = methodDeclaration(cs, c, ctx) const ts: Signature = [] for (const e of md) { if (e instanceof EntityToken) { @@ -154,12 +145,12 @@ export function classDeclaration(cf: Context, r: J.Reflection): Signature { } } ss.push(...ts) - ss.push(...newline(cf)) + ss.push(...newline(ctx)) } } } } - cf.s.out() + ctx.s.out() ss.pop() @@ -173,8 +164,8 @@ export function classDeclaration(cf: Context, r: J.Reflection): Signature { return s } -export function constructorDeclaration(cf: Context, r: J.Reflection): Signature { - const s: Signature = [] +export function constructorDeclaration(ctx: Context, r: J.Reflection): Signature { + let s: Signature = [] if (!isSignatureReflection(r)) { return s @@ -184,15 +175,15 @@ export function constructorDeclaration(cf: Context, r: J.Reflection): Signature t.text = "constructor" s.push(t) - const b = parameters(r, cf) + const b = parameters(r, ctx) s.push(...b) - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } -export function enumMemberReflection(cf: Context, r: J.Reflection): Signature { +export function enumMemberReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isEnumMemberReflection(r)) { @@ -210,8 +201,8 @@ export function enumMemberReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) - cf.f.preprocess(b) + let b = type(ctx, r.type) + b = ctx.f.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -222,7 +213,7 @@ export function enumMemberReflection(cf: Context, r: J.Reflection): Signature { return s } -export function enumReflection(cf: Context, r: J.Reflection): Signature { +export function enumReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isEnumReflection(r)) { @@ -252,12 +243,12 @@ export function enumReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.children) { - cf.s.in() + ctx.s.in() for (const c of r.children) { if (c.type) { - s.push(...newline(cf)) + s.push(...newline(ctx)) - const b = value(cf, c) + const b = value(ctx, c) s.push(...b) t = new TextToken() @@ -265,10 +256,10 @@ export function enumReflection(cf: Context, r: J.Reflection): Signature { s.push(t) } } - cf.s.out() + ctx.s.out() } - s.push(...newline(cf)) + s.push(...newline(ctx)) t = new TextToken() t.text = "}" @@ -277,7 +268,7 @@ export function enumReflection(cf: Context, r: J.Reflection): Signature { return s } -export function functionsDeclaration(cf: Context, r: J.Reflection): Signature { +export function functionsDeclaration(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isCallSignatureReflection(r)) { @@ -298,8 +289,8 @@ export function functionsDeclaration(cf: Context, r: J.Reflection): Signature { t.text = r.name s.push(t) - const b = parameters(r, cf) - cf.f.preprocess(b) + let b = parameters(r, ctx) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -307,8 +298,8 @@ export function functionsDeclaration(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) - cf.f.preprocess(b) + let b = type(ctx, r.type) + b = ctx.f.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -319,7 +310,7 @@ export function functionsDeclaration(cf: Context, r: J.Reflection): Signature { return s } -export function interfaceReflection(cf: Context, r: J.Reflection): Signature { +export function interfaceReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isInterfaceReflection(r)) { @@ -349,12 +340,12 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.children) { - cf.s.in() + ctx.s.in() for (const c of r.children) { - s.push(...newline(cf)) + s.push(...newline(ctx)) if (c.type) { - const b = value(cf, c) + const b = value(ctx, c) s.push(...b) t = new TextToken() @@ -363,12 +354,12 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { } if (c.signatures) { for (const v of c.signatures) { - const ts: Signature = [] + let ts: Signature = [] t = new ParameterToken() t.text = v.name ts.push(t) - const b = parameters(v, cf) + const b = parameters(v, ctx) ts.push(...b) if (v.type) { @@ -376,11 +367,11 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { t.text = ": " ts.push(t) - const b = type(v.type, cf) + const b = type(ctx, v.type) ts.push(...b) } - cf.f.preprocess(ts) + ts = ctx.f.preprocess(ts) s.push(...ts) t = new TextToken() @@ -389,11 +380,11 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { } } } - cf.s.out() + ctx.s.out() s.pop() } - s.push(...newline(cf)) + s.push(...newline(ctx)) t = new TextToken() t.text = "}" @@ -405,9 +396,9 @@ export function interfaceReflection(cf: Context, r: J.Reflection): Signature { export function methodDeclaration( r: J.Reflection, p: J.Reflection, - cf: Context, + ctx: Context, ): Signature { - const s: Signature = [] + let s: Signature = [] if (!isSignatureReflection(r)) { return s @@ -428,7 +419,7 @@ export function methodDeclaration( t.text = r.name s.push(t) - const b = parameters(r, cf) + const b = parameters(r, ctx) s.push(...b) t = new TextToken() @@ -436,8 +427,8 @@ export function methodDeclaration( s.push(t) if (r.type) { - const b = type(r.type, cf) - cf.f.preprocess(b) + let b = type(ctx, r.type) + b = ctx.f.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -445,12 +436,12 @@ export function methodDeclaration( s.push(t) } - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } -export function propertyReflection(cf: Context, r: J.Reflection): Signature { - const s: Signature = [] +export function propertyReflection(ctx: Context, r: J.Reflection): Signature { + let s: Signature = [] if (!isPropertyReflection(r)) { return s @@ -479,15 +470,15 @@ export function propertyReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } -export function typeAliasReflection(cf: Context, r: J.Reflection): Signature { +export function typeAliasReflection(ctx: Context, r: J.Reflection): Signature { const s: Signature = [] if (!isTypeAliasReflection(r)) { @@ -513,16 +504,16 @@ export function typeAliasReflection(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) - cf.f.preprocess(b) + let b = type(ctx, r.type) + b = ctx.f.preprocess(b) s.push(...b) } return s } -export function variableDeclaration(cf: Context, r: J.Reflection): Signature { - const s: Signature = [] +export function variableDeclaration(ctx: Context, r: J.Reflection): Signature { + let s: Signature = [] if (!isVariableReflection(r)) { return s @@ -547,15 +538,15 @@ export function variableDeclaration(cf: Context, r: J.Reflection): Signature { s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } -export function parameters(r: J.SignatureReflection, cf: Context): Signature { +export function parameters(r: J.SignatureReflection, ctx: Context): Signature { const s: Signature = [] let t: Token @@ -564,16 +555,16 @@ export function parameters(r: J.SignatureReflection, cf: Context): Signature { s.push(t) if (r.parameters && r.parameters.length !== 0) { - cf.s.in() + ctx.s.in() for (const f of r.parameters) { if (f.type) { - t = cf.s.nt() + t = ctx.s.nt() s.push(t) - t = cf.s.it() + t = ctx.s.it() s.push(t) - const b = value(cf, f) + const b = value(ctx, f) s.push(...b) t = new TextToken() @@ -581,13 +572,13 @@ export function parameters(r: J.SignatureReflection, cf: Context): Signature { s.push(t) } } - cf.s.out() + ctx.s.out() s.pop() - t = cf.s.nt() + t = ctx.s.nt() s.push(t) - t = cf.s.it() + t = ctx.s.it() s.push(t) } @@ -598,8 +589,8 @@ export function parameters(r: J.SignatureReflection, cf: Context): Signature { return s } -export function value(cf: Context, r: J.Reflection): Signature { - const s: Signature = [] +export function value(ctx: Context, r: J.Reflection): Signature { + let s: Signature = [] let t: Token if ( @@ -616,10 +607,13 @@ export function value(cf: Context, r: J.Reflection): Signature { t = new TextToken() t.text = ": " + if (r.flags.isOptional) { + t.text = `?${t.text}` + } s.push(t) if (r.type) { - const b = type(r.type, cf) + const b = type(ctx, r.type) s.push(...b) } @@ -637,7 +631,7 @@ export function value(cf: Context, r: J.Reflection): Signature { s.push(t) } - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } @@ -691,9 +685,9 @@ export function flags(f: J.ReflectionFlags): Signature | undefined { return undefined } -export function type(t: J.SomeType, cf: Context): Signature { +export function type(ctx: Context, t: J.SomeType): Signature { if (isArrayType(t)) { - return arrayType(t, cf) + return arrayType(ctx, t) } if (isIntrinsicType(t)) { return intrinsicType(t) @@ -702,25 +696,25 @@ export function type(t: J.SomeType, cf: Context): Signature { return literalType(t) } if (isReferenceType(t)) { - return referenceType(t, cf) + return referenceType(ctx, t) } if (isReflectionType(t)) { - return reflectionType(t, cf) + return reflectionType(ctx, t) } if (isTemplateLiteralType(t)) { - return templateLiteralType(t, cf) + return templateLiteralType(ctx, t) } if (isTupleType(t)) { - return tupleType(t, cf) + return tupleType(ctx, t) } if (isUnionType(t)) { - return unionType(t, cf) + return unionType(ctx, t) } return [] } -export function arrayType(a: J.ArrayType, cf: Context): Signature { - const s: Signature = [] +export function arrayType(ctx: Context, a: J.ArrayType): Signature { + let s: Signature = [] let t: Token if (isUnionType(a.elementType.type)) { @@ -729,14 +723,14 @@ export function arrayType(a: J.ArrayType, cf: Context): Signature { s.push(t) } - const b = type(a.elementType, cf) + const b = type(ctx, a.elementType) s.push(...b) if (isUnionType(a.elementType.type)) { - t = cf.s.nt() + t = ctx.s.nt() s.push(t) - t = cf.s.it() + t = ctx.s.it() s.push(t) t = new TextToken() @@ -748,7 +742,7 @@ export function arrayType(a: J.ArrayType, cf: Context): Signature { t.text = "[]" s.push(t) - cf.f.preprocess(s) + s = ctx.f.preprocess(s) return s } @@ -778,12 +772,12 @@ export function literalType(l: J.LiteralType): Signature { return s } -export function referenceType(r: J.ReferenceType, cf: Context): Signature { +export function referenceType(ctx: Context, r: J.ReferenceType): Signature { const s: Signature = [] let t: Token if (typeof r.target === "number") { - const id = cf.idOf(r.target) + const id = ctx.t.idOf(r.target) if (id) { const rt = new Reference() rt.id = String(id) @@ -807,8 +801,8 @@ export function referenceType(r: J.ReferenceType, cf: Context): Signature { s.push(t) for (const a of r.typeArguments) { - const b = type(a, cf) - cf.f.preprocess(b) + let b = type(ctx, a) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -839,14 +833,14 @@ export function referenceType(r: J.ReferenceType, cf: Context): Signature { return s } -export function reflectionType(r: J.ReflectionType, cf: Context): Signature { +export function reflectionType(ctx: Context, r: J.ReflectionType): Signature { const s: Signature = [] let t: Token if (r.declaration.signatures) { for (const c of r.declaration.signatures) { - const ts: Signature = [] - const b = parameters(c, cf) + let ts: Signature = [] + const b = parameters(c, ctx) ts.push(...b) if (c.type) { @@ -854,11 +848,11 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { t.text = " => " ts.push(t) - const b = type(c.type, cf) + const b = type(ctx, c.type) ts.push(...b) } - cf.f.preprocess(ts) + ts = ctx.f.preprocess(ts) s.push(...ts) } } else if (r.declaration.children) { @@ -866,12 +860,12 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { t.text = "{" s.push(t) - cf.s.in() + ctx.s.in() for (const c of r.declaration.children) { if (c.type) { - s.push(...newline(cf)) + s.push(...newline(ctx)) - const ts: Signature = [] + let ts: Signature = [] t = new ParameterToken() t.text = c.name ts.push(t) @@ -883,16 +877,16 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { } ts.push(t) - const b = type(c.type, cf) + const b = type(ctx, c.type) ts.push(...b) - cf.f.preprocess(ts) + ts = ctx.f.preprocess(ts) s.push(...ts) } } - cf.s.out() + ctx.s.out() - s.push(...newline(cf)) + s.push(...newline(ctx)) t = new TextToken() t.text = "}" @@ -910,7 +904,7 @@ export function reflectionType(r: J.ReflectionType, cf: Context): Signature { return s } -export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Signature { +export function templateLiteralType(ctx: Context, tt: J.TemplateLiteralType): Signature { const s: Signature = [] let t: Token @@ -931,8 +925,8 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Sig t.text = "${" s.push(t) - const b = type(e, cf) - cf.f.preprocess(b) + let b = type(ctx, e) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -952,7 +946,7 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Sig return s } -export function tupleType(tt: J.TupleType, cf: Context): Signature { +export function tupleType(ctx: Context, tt: J.TupleType): Signature { const s: Signature = [] let t: Token @@ -965,8 +959,8 @@ export function tupleType(tt: J.TupleType, cf: Context): Signature { s.push(t) for (const e of tt.elements) { - const b = type(e, cf) - cf.f.preprocess(b) + let b = type(ctx, e) + b = ctx.f.preprocess(b) s.push(...b) t = new TextToken() @@ -982,7 +976,7 @@ export function tupleType(tt: J.TupleType, cf: Context): Signature { return s } -export function unionType(u: J.UnionType, cf: Context): Signature { +export function unionType(ctx: Context, u: J.UnionType): Signature { const s: Signature = [] let t: Token @@ -990,12 +984,12 @@ export function unionType(u: J.UnionType, cf: Context): Signature { return s } - cf.s.in() + ctx.s.in() for (const ts of u.types) { - t = cf.s.nt() + t = ctx.s.nt() s.push(t) - t = cf.s.it() + t = ctx.s.it() s.push(t) if (isReflectionType(ts.type)) { @@ -1004,8 +998,8 @@ export function unionType(u: J.UnionType, cf: Context): Signature { s.push(t) } - const b = type(ts, cf) - cf.f.preprocess(b) + let b = type(ctx, ts) + b = ctx.f.preprocess(b) s.push(...b) if (ts.type === "reflection") { @@ -1018,20 +1012,20 @@ export function unionType(u: J.UnionType, cf: Context): Signature { t.text = " | " s.push(t) } - cf.s.out() + ctx.s.out() s.pop() return s } -function newline(cf: Context): Signature { +function newline(ctx: Context): Signature { const s: Signature = [] - const nt = cf.s.nt() + const nt = ctx.s.nt() nt.processed = true s.push(nt) - const it = cf.s.it() + const it = ctx.s.it() it.processed = true s.push(it) diff --git a/packages/typedoc-signature/lib/main.test.ts b/packages/typedoc-signature/lib/main.test.ts index c4d1e9a1a..8afff409c 100644 --- a/packages/typedoc-signature/lib/main.test.ts +++ b/packages/typedoc-signature/lib/main.test.ts @@ -2,23 +2,26 @@ import {readdir} from "node:fs/promises" import path from "node:path" import {pipeline} from "node:stream" import {promisify} from "node:util" -import {type Entity, GroupEntity} from "@onlyoffice/library-declaration/next.ts" +import {DeclarationEntity, type Entity, GroupEntity} from "@onlyoffice/library-declaration/next.ts" import {Transformer} from "@onlyoffice/typedoc-transformer" +import {Console as C1} from "@onlyoffice/typedoc-transformer/console.ts" import {Transport} from "@onlyoffice/typedoc-transport" import {Application, type JSONOutput as J, type TypeDocOptions} from "typedoc" import {test} from "uvu" import {equal as eq} from "uvu/assert" -import {Console} from "./console.ts" +import {Console as C0} from "./console.ts" import {compute} from "./main.ts" const pipe = promisify(pipeline) test.before(() => { - Console.shared.mute() + C0.shared.mute() + C1.shared.mute() }) test.after(() => { - Console.shared.unmute() + C1.shared.unmute() + C0.shared.unmute() }) for (const f of await readdir("fixtures")) { @@ -30,16 +33,29 @@ for (const f of await readdir("fixtures")) { const a = t.collection const e = await process(o) - // TODO: handling cases where the equality test has not been met for (const x of a) { + if (x instanceof GroupEntity) { + continue + } + + if (x.id === 0) { + continue + } + + let c = new DeclarationEntity() + for (const y of e) { - if (x instanceof GroupEntity || y instanceof GroupEntity) { - continue // TODO - } - if (y.id === x.id) { - eq(x.declaration.signature.verbose, y.declaration.signature.verbose) + if (x.id === y.id && !(y instanceof GroupEntity)) { + c = y + break } } + + eq(c.id, x.id) + eq(c.declaration.parameters, x.declaration.parameters) + eq(c.declaration.signature.verbose, x.declaration.signature.verbose) + eq(c.declaration.signature.concise, x.declaration.signature.concise) + eq(c.declaration.returns.signature.concise, x.declaration.returns.signature.concise) } }) } @@ -49,7 +65,7 @@ test.run() interface Test { name: string options: Partial - collection: Entity[] + collection: DeclarationEntity[] } async function setupTest(n: string): Promise { diff --git a/packages/typedoc-signature/lib/main.ts b/packages/typedoc-signature/lib/main.ts index 84c73ac93..fc588eb36 100644 --- a/packages/typedoc-signature/lib/main.ts +++ b/packages/typedoc-signature/lib/main.ts @@ -1,4 +1,4 @@ -import {type Declaration, type Entity, type Fragment, GroupEntity} from "@onlyoffice/library-declaration/next.ts" +import {GroupEntity} from "@onlyoffice/library-declaration/next.ts" import { isCallSignatureReflection, isClassReflection, @@ -13,32 +13,16 @@ import { isTypeAliasReflection, isVariableReflection, } from "@onlyoffice/typedoc-util-is-reflection" -import {type JSONOutput as J} from "typedoc" import {Console} from "./console.ts" import * as C from "./internal/concise.ts" +import {type Context} from "./internal/context.ts" import {Formatter} from "./internal/formatter.ts" import {State} from "./internal/state.ts" +import {type Transport} from "./internal/transport.ts" import * as V from "./internal/verbose.ts" const console = Console.shared -export interface Context { - s: State - f: Formatter - trailOf(t: Declaration | Fragment): FlatTrail | undefined - reflectionOf(t: FlatTrail): J.Reflection | undefined - idOf(id: number): number | undefined -} - -export interface Transport { - entities: Entity[] - trailOf(t: Declaration | Fragment): FlatTrail | undefined - reflectionOf(t: FlatTrail): J.Reflection | undefined - idOf(id: number): number | undefined -} - -export type FlatTrail = number[] - export function compute(ct: Transport): void { for (const e of ct.entities) { if (e instanceof GroupEntity) { @@ -69,9 +53,7 @@ export function compute(ct: Transport): void { const cf: Context = { s: new State(), f: new Formatter(), - trailOf: ct.trailOf.bind(ct), - reflectionOf: ct.reflectionOf.bind(ct), - idOf: ct.idOf.bind(ct), + t: ct, } const f = e.declaration.parameters @@ -80,7 +62,7 @@ export function compute(ct: Transport): void { const r = e.declaration.returns.signature.concise if (f.length !== 0) { - C.fragment(f, cf) + C.fragments(cf, f) } if (isClassReflection(t)) { @@ -117,7 +99,7 @@ export function compute(ct: Transport): void { c.push(...C.variableDeclaration(cf, t)) } - cf.f.format(e.declaration.signature.verbose) + e.declaration.signature.verbose = cf.f.format(e.declaration.signature.verbose) console.log(`Finish computing the signature for ${e.declaration.name} id = ${e.id}`) }