From ad532eb4dd1c6adad5faf60cc5ca7ca0871ffb9f Mon Sep 17 00:00:00 2001 From: ZEROM22 Date: Tue, 17 Dec 2024 16:23:08 +0300 Subject: [PATCH] temp --- packages/typedoc-signature/lib/console.ts | 11 + .../typedoc-signature/lib/internal/concise.ts | 182 +++++++--- .../typedoc-signature/lib/internal/context.ts | 26 ++ .../lib/internal/formatter.ts | 73 ++-- .../typedoc-signature/lib/internal/tokens.ts | 14 + .../typedoc-signature/lib/internal/verbose.ts | 329 ++++++++++-------- packages/typedoc-signature/lib/main.ts | 35 +- 7 files changed, 430 insertions(+), 240 deletions(-) create mode 100644 packages/typedoc-signature/lib/console.ts create mode 100644 packages/typedoc-signature/lib/internal/context.ts create mode 100644 packages/typedoc-signature/lib/internal/tokens.ts diff --git a/packages/typedoc-signature/lib/console.ts b/packages/typedoc-signature/lib/console.ts new file mode 100644 index 000000000..f1139e8d2 --- /dev/null +++ b/packages/typedoc-signature/lib/console.ts @@ -0,0 +1,11 @@ +import process from "node:process" +import {Console as NodeConsole} from "@onlyoffice/console" +import pack from "../package.json" with {type: "json"} + +export class Console extends NodeConsole { + static shared = new Console() + + constructor() { + super(pack.name, process.stdout, process.stderr) + } +} diff --git a/packages/typedoc-signature/lib/internal/concise.ts b/packages/typedoc-signature/lib/internal/concise.ts index 63d3aca73..4deb09306 100644 --- a/packages/typedoc-signature/lib/internal/concise.ts +++ b/packages/typedoc-signature/lib/internal/concise.ts @@ -1,5 +1,8 @@ /* eslint-disable unicorn/no-array-push-push */ -import {type Fragment} from "@onlyoffice/library-declaration/next.ts" +import { + type Declaration, + type Fragment, +} from "@onlyoffice/library-declaration/next.ts" import { ParameterToken, Reference, @@ -21,19 +24,31 @@ import { isTypeAliasReflection, isVariableReflection, } from "@onlyoffice/typedoc-util-is-reflection" -import {type JSONOutput as J} from "typedoc" import { isArrayType, isIntrinsicType, isLiteralType, isReferenceType, isReflectionType, + isTemplateLiteralType, isTupleType, isUnionType, -} from "../../../typedoc-util-is-type/lib/main.ts" -import {type ComputeFormat} from "../main.ts" +} from "@onlyoffice/typedoc-util-is-type" +import {type JSONOutput as J} from "typedoc" +import {type Context} from "./context.ts" +import {type Formatter} from "./formatter.ts" + +export interface ContextFormat { + context: Context + formatter: Formatter + trailOf(t: Declaration | Fragment): FlatTrail | undefined + reflectionOf(t: FlatTrail): J.Reflection | undefined + idOf(id: number): number | undefined +} -export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export type FlatTrail = number[] + +export function classDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isClassReflection(r)) { @@ -44,7 +59,7 @@ export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature t.text = " " s.push(t) - s.push(reference(r.id, r.name, ct)) + s.push(reference(r.id, r.name, cf)) return s } @@ -62,7 +77,7 @@ export function constructorDeclaration(r: J.Reflection): Signature { return s } -export function enumMemberReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function enumMemberReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isEnumMemberReflection(r)) { @@ -74,16 +89,16 @@ export function enumMemberReflection(r: J.Reflection, ct: ComputeFormat): Signat s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } else { - s.push(reference(r.id, "0", ct)) + s.push(reference(r.id, "0", cf)) } return s } -export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function enumReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isEnumReflection(r)) { @@ -94,7 +109,7 @@ export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { t.text = " " s.push(t) - s.push(reference(r.id, r.name, ct)) + s.push(reference(r.id, r.name, cf)) return s } @@ -122,7 +137,7 @@ export function functionsDeclaration(f: J.Reflection): Signature { return s } -export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function interfaceReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isInterfaceReflection(r)) { @@ -133,7 +148,7 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu t.text = " " s.push(t) - s.push(reference(r.id, r.name, ct)) + s.push(reference(r.id, r.name, cf)) return s } @@ -155,7 +170,7 @@ export function methodDeclaration(m: J.Reflection): Signature { return s } -export function propertyReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function propertyReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isPropertyReflection(r)) { @@ -170,14 +185,14 @@ export function propertyReflection(r: J.Reflection, ct: ComputeFormat): Signatur s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } return s } -export function typeAliasReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function typeAliasReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isTypeAliasReflection(r)) { @@ -188,12 +203,12 @@ export function typeAliasReflection(r: J.Reflection, ct: ComputeFormat): Signatu t.text = " " s.push(t) - s.push(reference(r.id, r.name, ct)) + s.push(reference(r.id, r.name, cf)) return s } -export function variableDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export function variableDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isVariableReflection(r)) { @@ -205,7 +220,7 @@ export function variableDeclaration(r: J.Reflection, ct: ComputeFormat): Signatu s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } @@ -252,9 +267,9 @@ export function value(p: J.Reflection): Signature { return s } -export function type(t: J.SomeType, ct: ComputeFormat): Signature { +export function type(t: J.SomeType, cf: ContextFormat): Signature { if (isArrayType(t)) { - return arrayType(t, ct) + return arrayType(t, cf) } if (isIntrinsicType(t)) { return intrinsicType(t) @@ -263,34 +278,37 @@ export function type(t: J.SomeType, ct: ComputeFormat): Signature { return literalType(t) } if (isReferenceType(t)) { - return referenceType(t, ct) + return referenceType(t, cf) } if (isReflectionType(t)) { - return reflectionType(t, ct) + return reflectionType(t, cf) + } + if (isTemplateLiteralType(t)) { + return templateLiteralType(t, cf) } if (isTupleType(t)) { - return tupleType(t, ct) + return tupleType(t, cf) } if (isUnionType(t)) { - return unionType(t, ct) + return unionType(t, cf) } return [] } -export function arrayType(a: J.ArrayType, ct: ComputeFormat): Signature { +export function arrayType(a: J.ArrayType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token - if (a.elementType.type === "union") { + if (isUnionType(a.elementType.type)) { t = new TextToken() t.text = "(" s.push(t) } - const b = type(a.elementType, ct) + const b = type(a.elementType, cf) s.push(...b) - if (a.elementType.type === "union") { + if (isUnionType(a.elementType.type)) { const t = new TextToken() t.text = ")" s.push(t) @@ -329,30 +347,27 @@ export function literalType(l: J.LiteralType): Signature { return s } -export function referenceType(r: J.ReferenceType, ct: ComputeFormat): Signature { +export function referenceType(r: J.ReferenceType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token if (typeof r.target === "number") { - s.push(reference(r.target, r.name, ct)) - } else { - t = new TypeToken() - t.text = r.name - s.push(t) + s.push(reference(r.target, r.name, cf)) } - if (r.name === "Promise") { + if (r.typeArguments) { t = new TextToken() t.text = "<" s.push(t) if (r.typeArguments) { for (const a of r.typeArguments) { - const b = type(a, ct) + const b = type(a, cf) + cf.formatter.preprocess(b) s.push(...b) t = new TextToken() - t.text = " | " + t.text = ", " s.push(t) } s.pop() @@ -367,10 +382,24 @@ export function referenceType(r: J.ReferenceType, ct: ComputeFormat): Signature s.push(t) } + if (s.length === 0) { + t = new TextToken() + t.text = "<" + s.push(t) + + t = new TypeToken() + t.text = r.name + s.push(t) + + t = new TextToken() + t.text = ">" + s.push(t) + } + return s } -export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signature { +export function reflectionType(r: J.ReflectionType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -384,7 +413,7 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur t.text = " => " s.push(t) - const b = type(c.type, ct) + const b = type(c.type, cf) s.push(...b) } } @@ -406,7 +435,7 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur } s.push(t) - const b = type(c.type, ct) + const b = type(c.type, cf) s.push(...b) t = new TextToken() t.text = "; " @@ -431,7 +460,49 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur return s } -export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { +export function templateLiteralType(tt: J.TemplateLiteralType, cf: ContextFormat): Signature { + const s: Signature = [] + let t: Token + + t = new StringToken() + t.text = "`" + s.push(t) + + if (tt.head) { + t = new StringToken() + t.text = tt.head + s.push(t) + } + + for (const tl of tt.tail) { + for (const e of tl) { + if (typeof e !== "string") { + t = new TextToken() + t.text = "${" + s.push(t) + + const b = type(e, cf) + cf.formatter.preprocess(b) + s.push(...b) + + t = new TextToken() + t.text = "}" + s.push(t) + } else { + t = new StringToken() + t.text = String(e) + s.push(t) + } + } + } + t = new StringToken() + t.text = "`" + s.push(t) + + return s +} + +export function tupleType(tt: J.TupleType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -444,7 +515,7 @@ export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { s.push(t) for (const e of tt.elements) { - const b = type(e, ct) + const b = type(e, cf) s.push(...b) t = new TextToken() @@ -460,7 +531,7 @@ export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { return s } -export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { +export function unionType(u: J.UnionType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -469,16 +540,16 @@ export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { } for (const ts of u.types) { - if (ts.type === "reflection") { + if (isReferenceType(ts.type)) { t = new TextToken() t.text = "(" s.push(t) } - const b = type(ts, ct) + const b = type(ts, cf) s.push(...b) - if (ts.type === "reflection") { + if (isReferenceType(ts.type)) { t = new TextToken() t.text = ")" s.push(t) @@ -493,27 +564,27 @@ export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { return s } -export function fragment(f: Fragment[], ct: ComputeFormat): void { +export function fragment(f: Fragment[], cf: ContextFormat): void { for (const e of f) { - const ft = ct.trailOf(e) + const ft = cf.trailOf(e) if (ft) { - const t = ct.reflectionOf(ft) + const t = cf.reflectionOf(ft) if (!isParameterReflection(t)) { return } if (t.type) { - const b = type(t.type, ct) + const b = type(t.type, cf) e.signature.concise.push(...b) } } } } -export function returns(r: (J.SomeType | undefined), ct: ComputeFormat): Signature { +export function returns(r: (J.SomeType | undefined), cf: ContextFormat): Signature { const s: Signature = [] if (r && r.type) { - const b = type(r, ct) + const b = type(r, cf) s.push(...b) } else { const t = new TypeToken() @@ -524,10 +595,9 @@ export function returns(r: (J.SomeType | undefined), ct: ComputeFormat): Signatu return s } -function reference(t: number, n: string, ct: ComputeFormat): Token | Reference { +function reference(t: number, n: string, cf: ContextFormat): Token | Reference { let r: Token | Reference - - const id = ct.idOf(t) + const id = cf.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..e05484721 --- /dev/null +++ b/packages/typedoc-signature/lib/internal/context.ts @@ -0,0 +1,26 @@ +import {IndentToken, NewlineToken} from "./tokens.ts" + +export class Context { + m = 2 + i = 0 + + constructor(m = 2) { + this.m = m + } + + in(): void { + this.i += this.m + } + + out(): void { + this.i -= this.m + } + + nt(): NewlineToken { + return new NewlineToken() + } + + it(i = this.i): IndentToken { + return new IndentToken(i) + } +} diff --git a/packages/typedoc-signature/lib/internal/formatter.ts b/packages/typedoc-signature/lib/internal/formatter.ts index 3b944892c..43569176e 100644 --- a/packages/typedoc-signature/lib/internal/formatter.ts +++ b/packages/typedoc-signature/lib/internal/formatter.ts @@ -1,48 +1,75 @@ -import {NoopToken, Reference, type Signature, TextToken} from "@onlyoffice/signature" +import {NoopToken, Reference, type Signature, TextToken, type Token} from "@onlyoffice/signature" +import {IndentToken, NewlineToken} from "./tokens.ts" export class Formatter { - i: number l: number n: string - constructor(i = 0, l = 100, n = "\n") { - this.i = i + constructor(l = 100, n = "\n") { this.l = l this.n = n } - format(s: Signature): void { - let l = this.i - let f = false + preprocess(s: Signature): void { + let l = 0 + let f = false // has a formatted signature in the child elements + for (const t of s) { if (!(t instanceof Reference || t instanceof NoopToken)) { - if (t.text !== "_NEWLINE_" && !/^_INDENT-\d+_/.test(t.text)) { + if (t instanceof NewlineToken || t instanceof IndentToken) { + if (t.processed) { + f = true + break + } + t.processed = true + } else { l += t.text.length } - if (t.text === " ".repeat(this.i + 4)) { - f = true - break + } + } + + // if the length is less than 100 and there are no newlines in the signature of the child elements + // remove indents and newlines + if (l < 100 && !f) { + const ts: Signature = [] + + for (const t of s) { + if (!(t instanceof NewlineToken || t instanceof IndentToken)) { + ts.push(t) } } + + s.length = 0 + s.push(...ts) } + } - for (const t of s) { - if (t instanceof TextToken && t.text === "_NEWLINE_") { - if (l > this.l || f) { + format(s: Signature): void { + const ts: Signature = [] + let t: Token + + for (const e of s) { + if (e instanceof NewlineToken || e instanceof IndentToken) { + t = new TextToken() + if (e instanceof NewlineToken) { t.text = this.n - } else { - t.text = "" } - } - if (t instanceof TextToken && /^_INDENT-\d+_$/.test(t.text)) { - const m = /_INDENT-(\d+)_/.exec(t.text) - let ci = 0 - if ((l > this.l || f) && m) { - ci = Number.parseInt(m[1]) + if (e instanceof IndentToken) { + let ci = 0 + const i = /_INDENT-(\d+)_/.exec(e.text) + if (i) { + ci = Number.parseInt(i[1]) + } + t.text = " ".repeat(ci) } - t.text = " ".repeat(ci) + ts.push(t) + } else { + ts.push(e) } } + + s.length = 0 + s.push(...ts) } } diff --git a/packages/typedoc-signature/lib/internal/tokens.ts b/packages/typedoc-signature/lib/internal/tokens.ts new file mode 100644 index 000000000..5fef4d73c --- /dev/null +++ b/packages/typedoc-signature/lib/internal/tokens.ts @@ -0,0 +1,14 @@ +import {TextToken} from "@onlyoffice/signature" + +export class NewlineToken extends TextToken { + processed = false + text = "_NEWLINE_" +} + +export class IndentToken extends TextToken { + processed = false + constructor(i = 0) { + super() + this.text = `_INDENT-${i}_` + } +} diff --git a/packages/typedoc-signature/lib/internal/verbose.ts b/packages/typedoc-signature/lib/internal/verbose.ts index 31e9c7ce8..72fdbf3f4 100644 --- a/packages/typedoc-signature/lib/internal/verbose.ts +++ b/packages/typedoc-signature/lib/internal/verbose.ts @@ -1,3 +1,8 @@ +/* eslint-disable unicorn/no-array-push-push */ +import { + type Declaration, + type Fragment, +} from "@onlyoffice/library-declaration/next.ts" import { EntityToken, KeywordToken, @@ -24,7 +29,6 @@ import { isTypeAliasReflection, isVariableReflection, } from "@onlyoffice/typedoc-util-is-reflection" -import {type JSONOutput as J} from "typedoc" import { isArrayType, isIntrinsicType, @@ -34,10 +38,22 @@ import { isTemplateLiteralType, isTupleType, isUnionType, -} from "../../../typedoc-util-is-type/lib/main.ts" -import {type ComputeFormat} from "../main.ts" +} from "@onlyoffice/typedoc-util-is-type" +import {type JSONOutput as J} from "typedoc" +import {type Context} from "./context.ts" +import {type Formatter} from "./formatter.ts" + +export interface ContextFormat { + context: Context + formatter: Formatter + trailOf(t: Declaration | Fragment): FlatTrail | undefined + reflectionOf(t: FlatTrail): J.Reflection | undefined + idOf(id: number): number | undefined +} -export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export type FlatTrail = number[] + +export function classDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isClassReflection(r)) { @@ -82,35 +98,38 @@ export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature t.text = " " s.push(t) - const b = type(e, ct) - ct.formatter.format(b) + const b = type(e, cf) + cf.formatter.preprocess(b) s.push(...b) } } - const childrenSignatures: Signature = [] + const ss: Signature = [] if (r.children) { - ct.formatter.i += 2 + cf.context.in() t = new TextToken() t.text = " {" - childrenSignatures.push(t, ...newline(ct)) + ss.push(t) + + ss.push(...newline(cf)) for (const c of r.children) { if (isConstructorReflection(c) && c.signatures) { for (const cs of c.signatures) { if (isSignatureReflection(cs)) { - const cd = constructorDeclaration(cs, ct) - childrenSignatures.push(...cd, ...newline(ct)) + const cd = constructorDeclaration(cs, cf) + ss.push(...cd) + ss.push(...newline(cf)) } } } if (isPropertyReflection(c)) { - const pr = propertyReflection(c, ct) + const pr = propertyReflection(c, cf) const ts: Signature = [] for (const e of pr) { - if ("type" in e && e.type === "entity") { + if (e instanceof EntityToken) { t = new ParameterToken() t.text = e.text ts.push(t) @@ -118,16 +137,18 @@ export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature ts.push(e) } } - childrenSignatures.push(...ts, ...newline(ct)) + ss.push(...ts) + + ss.push(...newline(cf)) } if (isMethodReflection(c) && c.signatures) { for (const cs of c.signatures) { if (isSignatureReflection(cs)) { - const md = methodDeclaration(cs, c, ct) + const md = methodDeclaration(cs, c, cf) const ts: Signature = [] for (const e of md) { - if ("type" in e && e.type === "entity") { + if (e instanceof EntityToken) { t = new ParameterToken() t.text = e.text ts.push(t) @@ -135,26 +156,27 @@ export function classDeclaration(r: J.Reflection, ct: ComputeFormat): Signature ts.push(e) } } - childrenSignatures.push(...ts, ...newline(ct)) + ss.push(...ts) + ss.push(...newline(cf)) } } } } + cf.context.out() - ct.formatter.i -= 2 - childrenSignatures.pop() + ss.pop() t = new TextToken() t.text = "}" - childrenSignatures.push(t) + ss.push(t) - s.push(...childrenSignatures) + s.push(...ss) } return s } -export function constructorDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export function constructorDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isSignatureReflection(r)) { @@ -165,15 +187,15 @@ export function constructorDeclaration(r: J.Reflection, ct: ComputeFormat): Sign t.text = "constructor" s.push(t) - const b = parameters(r, ct) + const b = parameters(r, cf) s.push(...b) - ct.formatter.format(b) + cf.formatter.preprocess(s) return s } -export function enumMemberReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function enumMemberReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isEnumMemberReflection(r)) { @@ -191,8 +213,8 @@ export function enumMemberReflection(r: J.Reflection, ct: ComputeFormat): Signat s.push(t) if (r.type) { - const b = type(r.type, ct) - ct.formatter.format(b) + const b = type(r.type, cf) + cf.formatter.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -203,7 +225,7 @@ export function enumMemberReflection(r: J.Reflection, ct: ComputeFormat): Signat return s } -export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function enumReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isEnumReflection(r)) { @@ -233,12 +255,12 @@ export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { s.push(t) if (r.children) { - ct.formatter.i += 2 + cf.context.in() for (const c of r.children) { if (c.type) { - s.push(...newline(ct)) + s.push(...newline(cf)) - const b = value(c, ct) + const b = value(c, cf) s.push(...b) t = new TextToken() @@ -246,10 +268,10 @@ export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { s.push(t) } } - ct.formatter.i -= 2 + cf.context.out() } - s.push(...newline(ct)) + s.push(...newline(cf)) t = new TextToken() t.text = "}" @@ -258,7 +280,7 @@ export function enumReflection(r: J.Reflection, ct: ComputeFormat): Signature { return s } -export function functionsDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export function functionsDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isCallSignatureReflection(r)) { @@ -279,8 +301,8 @@ export function functionsDeclaration(r: J.Reflection, ct: ComputeFormat): Signat t.text = r.name s.push(t) - const b = parameters(r, ct) - ct.formatter.format(b) + const b = parameters(r, cf) + cf.formatter.preprocess(b) s.push(...b) t = new TextToken() @@ -288,8 +310,8 @@ export function functionsDeclaration(r: J.Reflection, ct: ComputeFormat): Signat s.push(t) if (r.type) { - const b = type(r.type, ct) - ct.formatter.format(b) + const b = type(r.type, cf) + cf.formatter.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -300,7 +322,7 @@ export function functionsDeclaration(r: J.Reflection, ct: ComputeFormat): Signat return s } -export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function interfaceReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isInterfaceReflection(r)) { @@ -330,12 +352,12 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu s.push(t) if (r.children) { - ct.formatter.i += 2 + cf.context.in() for (const c of r.children) { - s.push(...newline(ct)) + s.push(...newline(cf)) if (c.type) { - const b = value(c, ct) + const b = value(c, cf) s.push(...b) t = new TextToken() @@ -349,7 +371,7 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu t.text = v.name ts.push(t) - const b = parameters(v, ct) + const b = parameters(v, cf) ts.push(...b) if (v.type) { @@ -357,11 +379,11 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu t.text = ": " ts.push(t) - const b = type(v.type, ct) + const b = type(v.type, cf) ts.push(...b) } - ct.formatter.format(ts) + cf.formatter.preprocess(ts) s.push(...ts) t = new TextToken() @@ -370,11 +392,11 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu } } } - ct.formatter.i -= 2 + cf.context.out() s.pop() } - s.push(...newline(ct)) + s.push(...newline(cf)) t = new TextToken() t.text = "}" @@ -386,7 +408,7 @@ export function interfaceReflection(r: J.Reflection, ct: ComputeFormat): Signatu export function methodDeclaration( r: J.Reflection, p: J.Reflection, - ct: ComputeFormat, + cf: ContextFormat, ): Signature { const s: Signature = [] @@ -409,7 +431,7 @@ export function methodDeclaration( t.text = r.name s.push(t) - const b = parameters(r, ct) + const b = parameters(r, cf) s.push(...b) t = new TextToken() @@ -417,8 +439,8 @@ export function methodDeclaration( s.push(t) if (r.type) { - const b = type(r.type, ct) - ct.formatter.format(b) + const b = type(r.type, cf) + cf.formatter.preprocess(b) s.push(...b) } else { t = new TypeToken() @@ -426,11 +448,11 @@ export function methodDeclaration( s.push(t) } - ct.formatter.format(s) + cf.formatter.preprocess(s) return s } -export function propertyReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function propertyReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isPropertyReflection(r)) { @@ -460,15 +482,15 @@ export function propertyReflection(r: J.Reflection, ct: ComputeFormat): Signatur s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } - ct.formatter.format(s) + cf.formatter.preprocess(s) return s } -export function typeAliasReflection(r: J.Reflection, ct: ComputeFormat): Signature { +export function typeAliasReflection(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isTypeAliasReflection(r)) { @@ -494,15 +516,15 @@ export function typeAliasReflection(r: J.Reflection, ct: ComputeFormat): Signatu s.push(t) if (r.type) { - const b = type(r.type, ct) - ct.formatter.format(b) + const b = type(r.type, cf) + cf.formatter.preprocess(b) s.push(...b) } return s } -export function variableDeclaration(r: J.Reflection, ct: ComputeFormat): Signature { +export function variableDeclaration(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] if (!isVariableReflection(r)) { @@ -528,15 +550,15 @@ export function variableDeclaration(r: J.Reflection, ct: ComputeFormat): Signatu s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } - ct.formatter.format(s) + cf.formatter.preprocess(s) return s } -export function parameters(r: J.SignatureReflection, ct: ComputeFormat): Signature { +export function parameters(r: J.SignatureReflection, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -545,18 +567,16 @@ export function parameters(r: J.SignatureReflection, ct: ComputeFormat): Signatu s.push(t) if (r.parameters && r.parameters.length !== 0) { - ct.formatter.i += 2 + cf.context.in() for (const f of r.parameters) { if (f.type) { - t = new TextToken() - t.text = "_NEWLINE_" + t = cf.context.nt() s.push(t) - t = new TextToken() - t.text = `_INDENT-${ct.formatter.i}_` + t = cf.context.it() s.push(t) - const b = value(f, ct) + const b = value(f, cf) s.push(...b) t = new TextToken() @@ -564,15 +584,13 @@ export function parameters(r: J.SignatureReflection, ct: ComputeFormat): Signatu s.push(t) } } - ct.formatter.i -= 2 + cf.context.out() s.pop() - t = new TextToken() - t.text = "_NEWLINE_" + t = cf.context.nt() s.push(t) - t = new TextToken() - t.text = `_INDENT-${ct.formatter.i}_` + t = cf.context.it() s.push(t) } @@ -583,7 +601,7 @@ export function parameters(r: J.SignatureReflection, ct: ComputeFormat): Signatu return s } -export function value(r: J.Reflection, ct: ComputeFormat): Signature { +export function value(r: J.Reflection, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -604,7 +622,7 @@ export function value(r: J.Reflection, ct: ComputeFormat): Signature { s.push(t) if (r.type) { - const b = type(r.type, ct) + const b = type(r.type, cf) s.push(...b) } @@ -622,7 +640,7 @@ export function value(r: J.Reflection, ct: ComputeFormat): Signature { s.push(t) } - ct.formatter.format(s) + cf.formatter.preprocess(s) return s } @@ -663,8 +681,8 @@ export function flags(f: J.ReflectionFlags): Signature | undefined { if (s.length !== 0) { const r: Signature = [] - for (const char of s) { - r.push(char) + for (const e of s) { + r.push(e) const t = new TextToken() t.text = " " r.push(t) @@ -676,9 +694,9 @@ export function flags(f: J.ReflectionFlags): Signature | undefined { return undefined } -export function type(t: J.SomeType, ct: ComputeFormat): Signature { +export function type(t: J.SomeType, cf: ContextFormat): Signature { if (isArrayType(t)) { - return arrayType(t, ct) + return arrayType(t, cf) } if (isIntrinsicType(t)) { return intrinsicType(t) @@ -687,43 +705,41 @@ export function type(t: J.SomeType, ct: ComputeFormat): Signature { return literalType(t) } if (isReferenceType(t)) { - return referenceType(t, ct) + return referenceType(t, cf) } if (isReflectionType(t)) { - return reflectionType(t, ct) + return reflectionType(t, cf) } if (isTemplateLiteralType(t)) { - return templateLiteralType(t, ct) + return templateLiteralType(t, cf) } if (isTupleType(t)) { - return tupleType(t, ct) + return tupleType(t, cf) } if (isUnionType(t)) { - return unionType(t, ct) + return unionType(t, cf) } return [] } -export function arrayType(a: J.ArrayType, ct: ComputeFormat): Signature { +export function arrayType(a: J.ArrayType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token - if (a.elementType.type === "union") { + if (isUnionType(a.elementType.type)) { t = new TextToken() t.text = "(" s.push(t) } - const b = type(a.elementType, ct) + const b = type(a.elementType, cf) s.push(...b) - if (a.elementType.type === "union") { - t = new TextToken() - t.text = "_NEWLINE_" + if (isUnionType(a.elementType.type)) { + t = cf.context.nt() s.push(t) - t = new TextToken() - t.text = `_INDENT-${ct.formatter.i}_` + t = cf.context.it() s.push(t) t = new TextToken() @@ -735,7 +751,7 @@ export function arrayType(a: J.ArrayType, ct: ComputeFormat): Signature { t.text = "[]" s.push(t) - ct.formatter.format(s) + cf.formatter.preprocess(s) return s } @@ -765,12 +781,12 @@ export function literalType(l: J.LiteralType): Signature { return s } -export function referenceType(r: J.ReferenceType, ct: ComputeFormat): Signature { +export function referenceType(r: J.ReferenceType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token if (typeof r.target === "number") { - const id = ct.idOf(r.target) + const id = cf.idOf(r.target) if (id) { const rt = new Reference() rt.id = String(id) @@ -779,31 +795,20 @@ export function referenceType(r: J.ReferenceType, ct: ComputeFormat): Signature s.push(rt) } } - if (s.length === 0) { - t = new TypeToken() - t.text = r.name - s.push(t) - } - if (r.name === "Promise" || r.name === "Record") { + if (r.typeArguments) { t = new TextToken() t.text = "<" s.push(t) if (r.typeArguments) { for (const a of r.typeArguments) { - const b = type(a, ct) - ct.formatter.format(b) + const b = type(a, cf) + cf.formatter.preprocess(b) s.push(...b) t = new TextToken() - if (r.name === "Promise") { - t.text = " | " - } - if (r.name === "Record") { - t.text = ", " - } - + t.text = ", " s.push(t) } s.pop() @@ -818,17 +823,31 @@ export function referenceType(r: J.ReferenceType, ct: ComputeFormat): Signature s.push(t) } + if (s.length === 0) { + t = new TextToken() + t.text = "<" + s.push(t) + + t = new TypeToken() + t.text = r.name + s.push(t) + + t = new TextToken() + t.text = ">" + s.push(t) + } + return s } -export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signature { +export function reflectionType(r: J.ReflectionType, cf: ContextFormat): 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, ct) + const b = parameters(c, cf) ts.push(...b) if (c.type) { @@ -836,11 +855,11 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur t.text = " => " ts.push(t) - const b = type(c.type, ct) + const b = type(c.type, cf) ts.push(...b) } - ct.formatter.format(ts) + cf.formatter.preprocess(ts) s.push(...ts) } } else if (r.declaration.children) { @@ -848,10 +867,10 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur t.text = "{" s.push(t) - ct.formatter.i += 2 + cf.context.in() for (const c of r.declaration.children) { if (c.type) { - s.push(...newline(ct)) + s.push(...newline(cf)) const ts: Signature = [] t = new ParameterToken() @@ -865,16 +884,16 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur } ts.push(t) - const b = type(c.type, ct) + const b = type(c.type, cf) ts.push(...b) - ct.formatter.format(ts) + cf.formatter.preprocess(ts) s.push(...ts) } } - ct.formatter.i -= 2 + cf.context.out() - s.push(...newline(ct)) + s.push(...newline(cf)) t = new TextToken() t.text = "}" @@ -892,31 +911,40 @@ export function reflectionType(r: J.ReflectionType, ct: ComputeFormat): Signatur return s } -export function templateLiteralType(tt: J.TemplateLiteralType, ct: ComputeFormat): Signature { +export function templateLiteralType(tt: J.TemplateLiteralType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token t = new StringToken() t.text = "`" s.push(t) - for (const tl of tt.tail) { - t = new TextToken() - t.text = "${" + + if (tt.head) { + t = new StringToken() + t.text = tt.head s.push(t) + } + + for (const tl of tt.tail) { for (const e of tl) { if (typeof e !== "string") { - const b = type(e, ct) - ct.formatter.format(b) + t = new TextToken() + t.text = "${" + s.push(t) + + const b = type(e, cf) + cf.formatter.preprocess(b) s.push(...b) + + t = new TextToken() + t.text = "}" + s.push(t) } else { - const t = new TypeToken() + t = new StringToken() t.text = String(e) s.push(t) } } - t = new TextToken() - t.text = "}" - s.push(t) } t = new StringToken() t.text = "`" @@ -925,7 +953,7 @@ export function templateLiteralType(tt: J.TemplateLiteralType, ct: ComputeFormat return s } -export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { +export function tupleType(tt: J.TupleType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -938,8 +966,8 @@ export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { s.push(t) for (const e of tt.elements) { - const b = type(e, ct) - ct.formatter.format(b) + const b = type(e, cf) + cf.formatter.preprocess(b) s.push(...b) t = new TextToken() @@ -955,7 +983,7 @@ export function tupleType(tt: J.TupleType, ct: ComputeFormat): Signature { return s } -export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { +export function unionType(u: J.UnionType, cf: ContextFormat): Signature { const s: Signature = [] let t: Token @@ -963,24 +991,22 @@ export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { return s } - ct.formatter.i += 2 + cf.context.in() for (const ts of u.types) { - t = new TextToken() - t.text = "_NEWLINE_" + t = cf.context.nt() s.push(t) - t = new TextToken() - t.text = `_INDENT-${ct.formatter.i}_` + t = cf.context.it() s.push(t) - if (ts.type === "reflection") { + if (isReflectionType(ts.type)) { t = new TextToken() t.text = "(" s.push(t) } - const b = type(ts, ct) - ct.formatter.format(b) + const b = type(ts, cf) + cf.formatter.preprocess(b) s.push(...b) if (ts.type === "reflection") { @@ -993,23 +1019,22 @@ export function unionType(u: J.UnionType, ct: ComputeFormat): Signature { t.text = " | " s.push(t) } - ct.formatter.i -= 2 + cf.context.out() s.pop() return s } -function newline(ct: ComputeFormat): Signature { +function newline(cf: ContextFormat): Signature { const s: Signature = [] - let t: Token - t = new TextToken() - t.text = ct.formatter.n - s.push(t) + const nt = cf.context.nt() + nt.processed = true + s.push(nt) - t = new TextToken() - t.text = " ".repeat(ct.formatter.i) - s.push(t) + const it = cf.context.it() + it.processed = true + s.push(it) return s } diff --git a/packages/typedoc-signature/lib/main.ts b/packages/typedoc-signature/lib/main.ts index 6bc70786f..8cb66ca64 100644 --- a/packages/typedoc-signature/lib/main.ts +++ b/packages/typedoc-signature/lib/main.ts @@ -14,13 +14,16 @@ import { 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 {Context} from "./internal/context.ts" import {Formatter} from "./internal/formatter.ts" import * as V from "./internal/verbose.ts" -export interface ComputeFormat extends Format, Transport {} +const console = Console.shared -export interface Format { +export interface ContextFormat extends Transport { + context: Context formatter: Formatter } @@ -34,7 +37,7 @@ export interface Transport { idOf(id: number): number | undefined } -type FlatTrail = number[] +export type FlatTrail = number[] export function compute(ct: ComputeTransport): void { const tr: Transport = { @@ -48,19 +51,29 @@ export function compute(ct: ComputeTransport): void { continue } - let ft = tr.trailOf(e.declaration) + console.log(`Start computing the signature for ${e.declaration.name} id = ${e.id}`) + + let ft = ct.trailOf(e.declaration) if (!ft) { - return + console.log(`Trail for ${e.declaration.name} id = ${e.id} not found!`) + continue } - const t = tr.reflectionOf(ft) + const t = ct.reflectionOf(ft) ft = ft.slice(0, -1) - const p = tr.reflectionOf(ft) + const p = ct.reflectionOf(ft) - if (!t || !p) { + if (!t) { + console.log(`Target for ${e.declaration.name} id = ${e.id} not found!`) continue } - const cf: ComputeFormat = { + if (!t) { + console.log(`Parent for ${e.declaration.name} id = ${e.id} not found!`) + continue + } + + const cf: ContextFormat = { + context: new Context(), formatter: new Formatter(), ...tr, } @@ -107,5 +120,9 @@ export function compute(ct: ComputeTransport): void { v.push(...V.variableDeclaration(t, cf)) c.push(...C.variableDeclaration(t, cf)) } + + cf.formatter.format(e.declaration.signature.verbose) + + console.log(`Finish computing the signature for ${e.declaration.name} id = ${e.id}`) } }