From 95238ff84a1726648e0709e759a234e512847233 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Mon, 31 Dec 2018 16:37:46 -0700 Subject: [PATCH 01/17] Remove deprecated toObject methods Also update tests to correctly use the serializer - it wasn't tested before. --- src/lib/models/ReflectionCategory.ts | 21 ------- src/lib/models/ReflectionGroup.ts | 22 ------- src/lib/models/comments/comment.ts | 24 -------- src/lib/models/comments/tag.ts | 17 ------ src/lib/models/reflections/abstract.ts | 60 -------------------- src/lib/models/reflections/container.ts | 43 -------------- src/lib/models/reflections/declaration.ts | 46 --------------- src/lib/models/reflections/parameter.ts | 18 ------ src/lib/models/reflections/project.ts | 21 ------- src/lib/models/reflections/signature.ts | 26 --------- src/lib/models/reflections/type-parameter.ts | 14 ----- src/lib/models/types/abstract.ts | 11 ---- src/lib/models/types/array.ts | 11 ---- src/lib/models/types/intersection.ts | 14 ----- src/lib/models/types/intrinsic.ts | 10 ---- src/lib/models/types/reference.ts | 19 ------- src/lib/models/types/reflection.ts | 14 ----- src/lib/models/types/string-literal.ts | 10 ---- src/lib/models/types/tuple.ts | 14 ----- src/lib/models/types/type-operator.ts | 10 ---- src/lib/models/types/type-parameter.ts | 15 ----- src/lib/models/types/union.ts | 14 ----- src/lib/models/types/unknown.ts | 10 ---- src/test/converter.ts | 7 ++- 24 files changed, 4 insertions(+), 467 deletions(-) diff --git a/src/lib/models/ReflectionCategory.ts b/src/lib/models/ReflectionCategory.ts index 1b26bf182..9ceb9cc1f 100644 --- a/src/lib/models/ReflectionCategory.ts +++ b/src/lib/models/ReflectionCategory.ts @@ -48,25 +48,4 @@ export class ReflectionCategory { return onlyOwnDocuments; } - - /** - * Return a raw object representation of this reflection category. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = { - title: this.title - }; - - if (this.children) { - const children: any[] = []; - this.children.forEach((child) => { - children.push(child.id); - }); - - result['children'] = children; - } - - return result; - } } diff --git a/src/lib/models/ReflectionGroup.ts b/src/lib/models/ReflectionGroup.ts index 88dbc9d26..bdb70f052 100644 --- a/src/lib/models/ReflectionGroup.ts +++ b/src/lib/models/ReflectionGroup.ts @@ -86,26 +86,4 @@ export class ReflectionGroup { return onlyOwnDocuments; } - - /** - * Return a raw object representation of this reflection group. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = { - title: this.title, - kind: this.kind - }; - - if (this.children) { - const children: any[] = []; - this.children.forEach((child) => { - children.push(child.id); - }); - - result['children'] = children; - } - - return result; - } } diff --git a/src/lib/models/comments/comment.ts b/src/lib/models/comments/comment.ts index 6c0a78bd5..5685ca17f 100644 --- a/src/lib/models/comments/comment.ts +++ b/src/lib/models/comments/comment.ts @@ -89,28 +89,4 @@ export class Comment { this.returns = comment.returns; this.tags = comment.tags ? comment.tags.map((tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)) : undefined; } - - /** - * Return a raw object representation of this comment. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = {}; - if (this.shortText) { - result.shortText = this.shortText; - } - if (this.text) { - result.text = this.text; - } - if (this.returns) { - result.returns = this.returns; - } - - if (this.tags && this.tags.length) { - result.tags = []; - this.tags.forEach((tag) => result.tags.push(tag.toObject())); - } - - return result; - } } diff --git a/src/lib/models/comments/tag.ts b/src/lib/models/comments/tag.ts index 3995f3305..8d59a61de 100644 --- a/src/lib/models/comments/tag.ts +++ b/src/lib/models/comments/tag.ts @@ -27,21 +27,4 @@ export class CommentTag { this.paramName = paramName || ''; this.text = text || ''; } - - /** - * Return a raw object representation of this tag. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = { - tag: this.tagName, - text: this.text - }; - - if (this.paramName) { - result.param = this.paramName; - } - - return result; - } } diff --git a/src/lib/models/reflections/abstract.ts b/src/lib/models/reflections/abstract.ts index ebd331711..d3f58b438 100644 --- a/src/lib/models/reflections/abstract.ts +++ b/src/lib/models/reflections/abstract.ts @@ -532,66 +532,6 @@ export abstract class Reflection { */ traverse(callback: TraverseCallback) { } - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = { - id: this.id, - name: this.name, - kind: this.kind, - kindString: this.kindString, - flags: {} - }; - - if (this.originalName !== this.name) { - result.originalName = this.originalName; - } - - if (this.comment) { - result.comment = this.comment.toObject(); - } - - Object.getOwnPropertyNames(ReflectionFlags.prototype).forEach(name => { - const descriptor = Object.getOwnPropertyDescriptor(ReflectionFlags.prototype, name)!; - if (typeof descriptor.get === 'function' && this.flags[name] === true) { - result.flags[name] = true; - } - }); - - if (this.decorates) { - result.decorates = this.decorates.map((type) => type.toObject()); - } - - if (this.decorators) { - result.decorators = this.decorators.map((decorator) => { - const result: any = { name: decorator.name }; - if (decorator.type) { - result.type = decorator.type.toObject(); - } - if (decorator.arguments) { - result.arguments = decorator.arguments; - } - return result; - }); - } - - this.traverse((child, property) => { - if (property === TraverseProperty.TypeLiteral) { - return; - } - let name = TraverseProperty[property]; - name = name.substr(0, 1).toLowerCase() + name.substr(1); - if (!result[name]) { - result[name] = []; - } - result[name].push(child.toObject()); - }); - - return result; - } - /** * Return a string representation of this reflection. */ diff --git a/src/lib/models/reflections/container.ts b/src/lib/models/reflections/container.ts index 777291006..39cab3a67 100644 --- a/src/lib/models/reflections/container.ts +++ b/src/lib/models/reflections/container.ts @@ -44,47 +44,4 @@ export class ContainerReflection extends Reflection { }); } } - - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = super.toObject(); - - if (this.groups) { - const groups: any[] = []; - this.groups.forEach((group) => { - groups.push(group.toObject()); - }); - - result['groups'] = groups; - } - - if (this.categories) { - const categories: any[] = []; - this.categories.forEach((category) => { - categories.push(category.toObject()); - }); - - if (categories.length > 0) { - result['categories'] = categories; - } - } - - if (this.sources) { - const sources: any[] = []; - this.sources.forEach((source) => { - sources.push({ - fileName: source.fileName, - line: source.line, - character: source.character - }); - }); - - result['sources'] = sources; - } - - return result; - } } diff --git a/src/lib/models/reflections/declaration.ts b/src/lib/models/reflections/declaration.ts index 45290eeb9..15cf33715 100644 --- a/src/lib/models/reflections/declaration.ts +++ b/src/lib/models/reflections/declaration.ts @@ -179,52 +179,6 @@ export class DeclarationReflection extends ContainerReflection implements Defaul super.traverse(callback); } - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - let result = super.toObject(); - - if (this.type) { - result.type = this.type.toObject(); - } - - if (this.defaultValue) { - result.defaultValue = this.defaultValue; - } - - if (this.overwrites) { - result.overwrites = this.overwrites.toObject(); - } - - if (this.inheritedFrom) { - result.inheritedFrom = this.inheritedFrom.toObject(); - } - - if (this.extendedTypes) { - result.extendedTypes = this.extendedTypes.map((t) => t.toObject()); - } - - if (this.extendedBy) { - result.extendedBy = this.extendedBy.map((t) => t.toObject()); - } - - if (this.implementedTypes) { - result.implementedTypes = this.implementedTypes.map((t) => t.toObject()); - } - - if (this.implementedBy) { - result.implementedBy = this.implementedBy.map((t) => t.toObject()); - } - - if (this.implementationOf) { - result.implementationOf = this.implementationOf.toObject(); - } - - return result; - } - /** * Return a string representation of this reflection. */ diff --git a/src/lib/models/reflections/parameter.ts b/src/lib/models/reflections/parameter.ts index 5a493737c..a1d847e77 100644 --- a/src/lib/models/reflections/parameter.ts +++ b/src/lib/models/reflections/parameter.ts @@ -25,24 +25,6 @@ export class ParameterReflection extends Reflection implements DefaultValueConta super.traverse(callback); } - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = super.toObject(); - - if (this.type) { - result.type = this.type.toObject(); - } - - if (this.defaultValue) { - result.defaultValue = this.defaultValue; - } - - return result; - } - /** * Return a string representation of this reflection. */ diff --git a/src/lib/models/reflections/project.ts b/src/lib/models/reflections/project.ts index 17394207f..33e40bc0e 100644 --- a/src/lib/models/reflections/project.ts +++ b/src/lib/models/reflections/project.ts @@ -123,25 +123,4 @@ export class ProjectReflection extends ContainerReflection { return undefined; } - - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = super.toObject(); - - if (this.categories) { - const categories: any[] = []; - this.categories.forEach((category) => { - categories.push(category.toObject()); - }); - - if (categories.length > 0) { - result['categories'] = categories; - } - } - - return result; - } } diff --git a/src/lib/models/reflections/signature.ts b/src/lib/models/reflections/signature.ts index f1cb0420f..7f1cc0e0c 100644 --- a/src/lib/models/reflections/signature.ts +++ b/src/lib/models/reflections/signature.ts @@ -71,32 +71,6 @@ export class SignatureReflection extends Reflection implements TypeContainer, Ty super.traverse(callback); } - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = super.toObject(); - - if (this.type) { - result.type = this.type.toObject(); - } - - if (this.overwrites) { - result.overwrites = this.overwrites.toObject(); - } - - if (this.inheritedFrom) { - result.inheritedFrom = this.inheritedFrom.toObject(); - } - - if (this.implementationOf) { - result.implementationOf = this.implementationOf.toObject(); - } - - return result; - } - /** * Return a string representation of this reflection. */ diff --git a/src/lib/models/reflections/type-parameter.ts b/src/lib/models/reflections/type-parameter.ts index c7e55502f..e27b22340 100644 --- a/src/lib/models/reflections/type-parameter.ts +++ b/src/lib/models/reflections/type-parameter.ts @@ -14,18 +14,4 @@ export class TypeParameterReflection extends Reflection implements TypeContainer super(type.name, ReflectionKind.TypeParameter, parent); this.type = type.constraint; } - - /** - * Return a raw object representation of this reflection. - * @deprecated Use serializers instead - */ - toObject(): any { - const result = super.toObject(); - - if (this.type) { - result.type = this.type.toObject(); - } - - return result; - } } diff --git a/src/lib/models/types/abstract.ts b/src/lib/models/types/abstract.ts index 56ef22929..cdfb76eeb 100644 --- a/src/lib/models/types/abstract.ts +++ b/src/lib/models/types/abstract.ts @@ -27,17 +27,6 @@ export abstract class Type { return false; } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - let result: any = {}; - result.type = this.type; - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/array.ts b/src/lib/models/types/array.ts index 2bdc11ed9..46c042f5f 100644 --- a/src/lib/models/types/array.ts +++ b/src/lib/models/types/array.ts @@ -51,17 +51,6 @@ export class ArrayType extends Type { return type.elementType.equals(this.elementType); } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.elementType = this.elementType.toObject(); - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/intersection.ts b/src/lib/models/types/intersection.ts index da5f7962e..831954cec 100644 --- a/src/lib/models/types/intersection.ts +++ b/src/lib/models/types/intersection.ts @@ -50,20 +50,6 @@ export class IntersectionType extends Type { return Type.isTypeListSimilar(type.types, this.types); } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - - if (this.types && this.types.length) { - result.types = this.types.map((e) => e.toObject()); - } - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/intrinsic.ts b/src/lib/models/types/intrinsic.ts index 6e030d1bb..089ec1d11 100644 --- a/src/lib/models/types/intrinsic.ts +++ b/src/lib/models/types/intrinsic.ts @@ -48,16 +48,6 @@ export class IntrinsicType extends Type { type.name === this.name; } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.name = this.name; - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/reference.ts b/src/lib/models/types/reference.ts index 686fbea7e..820910fa3 100644 --- a/src/lib/models/types/reference.ts +++ b/src/lib/models/types/reference.ts @@ -88,25 +88,6 @@ export class ReferenceType extends Type { (type.symbolID === this.symbolID || type.reflection === this.reflection); } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.name = this.name; - - if (this.reflection) { - result.id = this.reflection.id; - } - - if (this.typeArguments && this.typeArguments.length) { - result.typeArguments = this.typeArguments.map((t) => t.toObject()); - } - - return result; - } - /** * Return a string representation of this type. * @example EventEmitter diff --git a/src/lib/models/types/reflection.ts b/src/lib/models/types/reflection.ts index 536a1481e..1b5645cc0 100644 --- a/src/lib/models/types/reflection.ts +++ b/src/lib/models/types/reflection.ts @@ -48,20 +48,6 @@ export class ReflectionType extends Type { return type === this; } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - - if (this.declaration) { - result.declaration = this.declaration.toObject(); - } - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/string-literal.ts b/src/lib/models/types/string-literal.ts index 1af96a338..efaed79b3 100644 --- a/src/lib/models/types/string-literal.ts +++ b/src/lib/models/types/string-literal.ts @@ -48,16 +48,6 @@ export class StringLiteralType extends Type { type.value === this.value; } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.value = this.value; - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/tuple.ts b/src/lib/models/types/tuple.ts index c761c8052..bc162f509 100644 --- a/src/lib/models/types/tuple.ts +++ b/src/lib/models/types/tuple.ts @@ -50,20 +50,6 @@ export class TupleType extends Type { return Type.isTypeListEqual(type.elements, this.elements); } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - - if (this.elements && this.elements.length) { - result.elements = this.elements.map((e) => e.toObject()); - } - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/type-operator.ts b/src/lib/models/types/type-operator.ts index 2e280ff7c..badb139e4 100644 --- a/src/lib/models/types/type-operator.ts +++ b/src/lib/models/types/type-operator.ts @@ -48,16 +48,6 @@ export class TypeOperatorType extends Type { return type.target.equals(this.target); } - /** - * Return a raw object representation of this type. - */ - toObject(): any { - const result: any = super.toObject(); - result.operator = this.operator; - result.target = this.target.toObject(); - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/type-parameter.ts b/src/lib/models/types/type-parameter.ts index f34d222ea..efc07f404 100644 --- a/src/lib/models/types/type-parameter.ts +++ b/src/lib/models/types/type-parameter.ts @@ -56,21 +56,6 @@ export class TypeParameterType extends Type { } } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.name = this.name; - - if (this.constraint) { - result.constraint = this.constraint.toObject(); - } - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/union.ts b/src/lib/models/types/union.ts index bb3ce3a36..ade4bf63b 100644 --- a/src/lib/models/types/union.ts +++ b/src/lib/models/types/union.ts @@ -50,20 +50,6 @@ export class UnionType extends Type { return Type.isTypeListSimilar(type.types, this.types); } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - - if (this.types && this.types.length) { - result.types = this.types.map((e) => e.toObject()); - } - - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/lib/models/types/unknown.ts b/src/lib/models/types/unknown.ts index fe93406c4..affeb1945 100644 --- a/src/lib/models/types/unknown.ts +++ b/src/lib/models/types/unknown.ts @@ -44,16 +44,6 @@ export class UnknownType extends Type { type.name === this.name; } - /** - * Return a raw object representation of this type. - * @deprecated Use serializers instead - */ - toObject(): any { - const result: any = super.toObject(); - result.name = this.name; - return result; - } - /** * Return a string representation of this type. */ diff --git a/src/test/converter.ts b/src/test/converter.ts index a9c2ead65..3fa65aef7 100644 --- a/src/test/converter.ts +++ b/src/test/converter.ts @@ -89,7 +89,8 @@ describe('Converter', function() { it('matches specs', function() { const specs = JSON.parse(FS.readFileSync(Path.join(path, 'specs.json')).toString()); - let data = JSON.stringify(result!.toObject(), null, ' '); + + let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); compareReflections(JSON.parse(data), specs); @@ -127,7 +128,7 @@ describe('Converter with excludeNotExported=true', function() { it('matches specs', function() { const specs = JSON.parse(FS.readFileSync(Path.join(exportWithLocalDir, 'specs-without-exported.json')).toString()); - let data = JSON.stringify(result!.toObject(), null, ' '); + let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); compareReflections(JSON.parse(data), specs); @@ -143,7 +144,7 @@ describe('Converter with excludeNotExported=true', function() { it('matches specs', function() { const specs = JSON.parse(FS.readFileSync(Path.join(classDir, 'specs-without-exported.json')).toString()); - let data = JSON.stringify(result!.toObject(), null, ' '); + let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); compareReflections(JSON.parse(data), specs); From 7fc3f5b5988497bda2041354037b81787450d17f Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Mon, 31 Dec 2018 17:22:01 -0700 Subject: [PATCH 02/17] 2 spaces -> 4 spaces --- src/lib/serialization/browser.ts | 534 +++++++++--------- src/lib/serialization/components.ts | 124 ++-- src/lib/serialization/index.ts | 58 +- src/lib/serialization/serializer.ts | 208 +++---- .../serializers/comments/comment-tag.ts | 41 +- .../serializers/comments/comment.ts | 61 +- .../serialization/serializers/decorator.ts | 56 +- .../serializers/models/decorator-wrapper.ts | 2 +- .../models/source-reference-wrapper.ts | 2 +- .../serializers/reflection-group.ts | 52 +- .../serializers/reflections/abstract.ts | 101 ++-- .../serializers/reflections/container.ts | 47 +- .../serializers/reflections/declaration.ts | 72 ++- .../serializers/reflections/parameter.ts | 28 +- .../serializers/reflections/project.ts | 18 +- .../serializers/reflections/signature.ts | 40 +- .../serializers/reflections/type-parameter.ts | 26 +- .../serializers/types/abstract.ts | 22 +- .../serialization/serializers/types/array.ts | 20 +- .../serializers/types/intersection-union.ts | 22 +- .../serializers/types/intrinsic.ts | 20 +- .../serializers/types/reference.ts | 30 +- .../serializers/types/reflection.ts | 54 +- .../serializers/types/string-literal.ts | 20 +- .../serialization/serializers/types/tuple.ts | 22 +- .../serializers/types/type-operator.ts | 22 +- .../serializers/types/type-parameter.ts | 24 +- .../serializers/types/unknown.ts | 20 +- src/lib/utils/__events.ts | 0 tslint.json | 153 ++--- 30 files changed, 947 insertions(+), 952 deletions(-) delete mode 100644 src/lib/utils/__events.ts diff --git a/src/lib/serialization/browser.ts b/src/lib/serialization/browser.ts index c7f345dae..33b24041b 100644 --- a/src/lib/serialization/browser.ts +++ b/src/lib/serialization/browser.ts @@ -20,320 +20,332 @@ */ export interface ReflectionContainer { - id: number; - name: string; - kind: number; - kindString: string; - flags: ReflectionFlagsObject; - originalName?: string; + id: number; + name: string; + kind: number; + kindString: string; + flags: ReflectionFlagsObject; + originalName?: string; } export interface DefaultValueContainer { - defaultValue: string; + defaultValue: string; } -export interface TypeContainer { - type: TypeObject; +export interface TypeContainer { + type: TypeObject; } export interface TypeParameterContainer { - typeParameters: TypeContainer[]; + typeParameters: TypeContainer[]; } export interface DecoratesContainer { - decorates: TypeObject[]; + decorates: TypeObject[]; } export interface DecoratorsContainer { - decorators: T[]; + decorators: T[]; } export interface SourceReferenceContainer { - sources: T[]; + sources: T[]; } export interface GroupsContainer { - groups: T[]; + groups: T[]; } export interface ContainerReflectionContainer { - children: TChildren[]; + children: TChildren[]; } export interface CommentContainer { - comment: TComment; + comment: TComment; } export interface SignatureReflectionContainer { - /** - * A type that points to the reflection that has been overwritten by this reflection. - * - * Applies to interface and class members. - */ - overwrites?: TypeObject; - - /** - * A type that points to the reflection this reflection has been inherited from. - * - * Applies to interface and class members. - */ - inheritedFrom?: TypeObject; - - /** - * A type that points to the reflection this reflection is the implementation of. - * - * Applies to class members. - */ - implementationOf?: TypeObject; - - parameters?: TParameters[]; + /** + * A type that points to the reflection that has been overwritten by this reflection. + * + * Applies to interface and class members. + */ + overwrites?: TypeObject; + + /** + * A type that points to the reflection this reflection has been inherited from. + * + * Applies to interface and class members. + */ + inheritedFrom?: TypeObject; + + /** + * A type that points to the reflection this reflection is the implementation of. + * + * Applies to class members. + */ + implementationOf?: TypeObject; + + parameters?: TParameters[]; } export interface DeclarationReflectionContainer { - /** - * A list of call signatures attached to this declaration. - * - * TypeDoc creates one declaration per function that may contain ore or more - * signature reflections. - */ - signatures?: T[]; - - /** - * The index signature of this declaration. - */ - indexSignature?: T[]; - - /** - * The get signature of this declaration. - */ - getSignature?: T[]; - - /** - * The set signature of this declaration. - */ - setSignature?: T[]; - - /** - * A list of all types this reflection extends (e.g. the parent classes). - */ - extendedTypes?: TypeObject[]; - - /** - * A list of all types that extend this reflection (e.g. the subclasses). - */ - extendedBy?: TypeObject[]; - - /** - * A list of all types this reflection implements. - */ - implementedTypes?: TypeObject[]; - - /** - * A list of all types that implement this reflection. - */ - implementedBy?: TypeObject[]; - + /** + * A list of call signatures attached to this declaration. + * + * TypeDoc creates one declaration per function that may contain ore or more + * signature reflections. + */ + signatures?: T[]; + + /** + * The index signature of this declaration. + */ + indexSignature?: T[]; + + /** + * The get signature of this declaration. + */ + getSignature?: T[]; + + /** + * The set signature of this declaration. + */ + setSignature?: T[]; + + /** + * A list of all types this reflection extends (e.g. the parent classes). + */ + extendedTypes?: TypeObject[]; + + /** + * A list of all types that extend this reflection (e.g. the subclasses). + */ + extendedBy?: TypeObject[]; + + /** + * A list of all types this reflection implements. + */ + implementedTypes?: TypeObject[]; + + /** + * A list of all types that implement this reflection. + */ + implementedBy?: TypeObject[]; } -export interface ReflectionObject extends ReflectionContainer, - Partial>, - Partial, - Partial> { } - -export interface ParameterReflectionObject extends ReflectionObject, - TypeContainer, - DefaultValueContainer {} - -export interface ContainerReflectionObject extends ReflectionObject, - Partial>, - Partial>, - ContainerReflectionContainer {} - -export interface DeclarationReflectionObject extends ContainerReflectionObject, - DefaultValueContainer, - Partial, - Partial, - Partial>, - DeclarationReflectionContainer {} - -export interface SignatureReflectionObject extends ReflectionObject, - Partial>, - Partial, - Partial { } +export interface ReflectionObject + extends ReflectionContainer, + Partial>, + Partial, + Partial> {} + +export interface ParameterReflectionObject extends ReflectionObject, TypeContainer, DefaultValueContainer {} + +export interface ContainerReflectionObject + extends ReflectionObject, + Partial>, + Partial>, + ContainerReflectionContainer {} + +export interface DeclarationReflectionObject + extends ContainerReflectionObject, + DefaultValueContainer, + Partial, + Partial, + Partial>, + DeclarationReflectionContainer {} + +export interface SignatureReflectionObject + extends ReflectionObject, + Partial>, + Partial, + Partial {} export interface CommentObject { - shortText?: string; - text?: string; - returns?: string; - tags?: CommentTagObject[]; + shortText?: string; + text?: string; + returns?: string; + tags?: CommentTagObject[]; } export interface CommentTagObject { - tag: string; - text: string; - param?: string; + tag: string; + text: string; + param?: string; } export interface DecoratorObject { - /** - * The name of the decorator being applied. - */ - name: string; - - /** - * The type declaring the decorator. - * Usually a ReferenceType instance pointing to the decorator function. - */ - type?: TypeObject; - - /** - * A named map of arguments the decorator is applied with. - */ - arguments?: any; + /** + * The name of the decorator being applied. + */ + name: string; + + /** + * The type declaring the decorator. + * Usually a ReferenceType instance pointing to the decorator function. + */ + type?: TypeObject; + + /** + * A named map of arguments the decorator is applied with. + */ + arguments?: any; } -export interface ProjectReflectionObject extends ContainerReflectionObject { } +export interface ProjectReflectionObject extends ContainerReflectionObject {} export interface SourceReferenceObject { - fileName: string; - line: number; - character: number; + fileName: string; + line: number; + character: number; } export interface TypeObject { - - /** - * The type name identifier. - */ - type: 'void' | 'array' | 'intersection' | 'intrinsic' | 'reference' | 'reflection' | 'stringLiteral' | 'tuple' | 'typeParameter' | 'union' | 'unknown' | string; - - // array - /** - * For Array type only, The type (T) of the array's elements. - */ - elementType?: TypeObject; - - // intersection - /** - * For intersection type only, the types the union consists of. - * For union type only, the types the union consists of. - */ - types?: TypeObject[]; - - // intrinsic, reference, typeParameter, unknown - /** - * For intrinsic type only, The name of the intrinsic type like `string` or `boolean`. - * - * For reference type only, The name of the referenced type. - * If the symbol cannot be found cause it's not part of the documentation this - * can be used to represent the type. - * - * For typeParameter type only, the name of the type. - * - * For unknown type only, the name of the type. - */ - name?: 'Object' | 'string' | 'number' | 'boolean' | 'this' | string; - - // reference - /** - * The reflection id for this type - */ - id?: number; - - /** - * For reference type only, The type arguments of this reference. - */ - typeArguments?: TypeObject[]; - - // reflection - /** - * For reflection type only, The reflection of the type. - */ - declaration?: ReflectionObject; - - // stringLiteral - /** - * For stringLiteral type only, The string literal value. - */ - value?: string; - - // tuple - /** - * For tuple type only, The ordered type elements of the tuple type. - */ - elements?: TypeObject[]; - - // typeParameter - /** - * For typeParameter type only, The constraint type for the generic type. - */ - constraint?: TypeObject; + /** + * The type name identifier. + */ + type: + | 'void' + | 'array' + | 'intersection' + | 'intrinsic' + | 'reference' + | 'reflection' + | 'stringLiteral' + | 'tuple' + | 'typeParameter' + | 'union' + | 'unknown' + | string; + + // array + /** + * For Array type only, The type (T) of the array's elements. + */ + elementType?: TypeObject; + + // intersection + /** + * For intersection type only, the types the union consists of. + * For union type only, the types the union consists of. + */ + types?: TypeObject[]; + + // intrinsic, reference, typeParameter, unknown + /** + * For intrinsic type only, The name of the intrinsic type like `string` or `boolean`. + * + * For reference type only, The name of the referenced type. + * If the symbol cannot be found cause it's not part of the documentation this + * can be used to represent the type. + * + * For typeParameter type only, the name of the type. + * + * For unknown type only, the name of the type. + */ + name?: 'Object' | 'string' | 'number' | 'boolean' | 'this' | string; + + // reference + /** + * The reflection id for this type + */ + id?: number; + + /** + * For reference type only, The type arguments of this reference. + */ + typeArguments?: TypeObject[]; + + // reflection + /** + * For reflection type only, The reflection of the type. + */ + declaration?: ReflectionObject; + + // stringLiteral + /** + * For stringLiteral type only, The string literal value. + */ + value?: string; + + // tuple + /** + * For tuple type only, The ordered type elements of the tuple type. + */ + elements?: TypeObject[]; + + // typeParameter + /** + * For typeParameter type only, The constraint type for the generic type. + */ + constraint?: TypeObject; } export interface ReflectionGroupObject { - /** - * The title, a string representation of the typescript kind, of this group. - */ - title: string; - - /** - * The original typescript kind of the children of this group. - */ - kind: number; - - /** - * A list of reflection id's for this group. - */ - children?: number[]; + /** + * The title, a string representation of the typescript kind, of this group. + */ + title: string; + + /** + * The original typescript kind of the children of this group. + */ + kind: number; + + /** + * A list of reflection id's for this group. + */ + children?: number[]; } export interface ReflectionFlagsObject { - /** - * Is this a private member? - */ - isPrivate?: boolean; - - /** - * Is this a protected member? - */ - isProtected?: boolean; - - /** - * Is this a public member? - */ - isPublic?: boolean; - - /** - * Is this a static member? - */ - isStatic?: boolean; - - /** - * Is this member exported? - */ - isExported?: boolean; - - /** - * Is this a declaration from an external document? - */ - isExternal?: boolean; - - /** - * Whether this reflection is an optional component or not. - * - * Applies to function parameters and object members. - */ - isOptional?: boolean; - - /** - * Whether it's a rest parameter, like `foo(...params);`. - */ - isRest?: boolean; - - /** - * - */ - hasExportAssignment?: boolean; - - isConstructorProperty?: boolean; + /** + * Is this a private member? + */ + isPrivate?: boolean; + + /** + * Is this a protected member? + */ + isProtected?: boolean; + + /** + * Is this a public member? + */ + isPublic?: boolean; + + /** + * Is this a static member? + */ + isStatic?: boolean; + + /** + * Is this member exported? + */ + isExported?: boolean; + + /** + * Is this a declaration from an external document? + */ + isExternal?: boolean; + + /** + * Whether this reflection is an optional component or not. + * + * Applies to function parameters and object members. + */ + isOptional?: boolean; + + /** + * Whether it's a rest parameter, like `foo(...params);`. + */ + isRest?: boolean; + + /** + * + */ + hasExportAssignment?: boolean; + + isConstructorProperty?: boolean; } diff --git a/src/lib/serialization/components.ts b/src/lib/serialization/components.ts index 8cf7b679b..aeecf636e 100644 --- a/src/lib/serialization/components.ts +++ b/src/lib/serialization/components.ts @@ -16,80 +16,76 @@ import { Serializer } from './serializer'; * serializers when we are looking for type (or any other) serializers. */ export abstract class SerializerComponent extends AbstractComponent { + /** + * The priority this serializer should be executed with. + * A higher priority means the [[Serializer]] will be applied earlier. + */ + static PRIORITY = 0; - /** - * The priority this serializer should be executed with. - * A higher priority means the [[Serializer]] will be applied earlier. - */ - static PRIORITY = 0; + /** + * A high-level predicate filtering which group this serializer belongs to. + * This is a high-level filter before the [[SerializerComponent.supports]] predicate filter. + * + * When the filter returns true the group identifier is taken from + * [[SerializerComponentType.serializeGroupSymbol]]. + * + * For example, use the [[Reflection]] class class to group all reflection based serializers: + * ```typescript + * class ReflectionSerializer { + * serializeGroup(instance) { return instance instanceof Reflection } + * serializeGroupSymbol = Reflection; + * } + * ``` + * + * Use the [[Type]] class to group all type based serializers: + * ```typescript + * class TypeSerializer { + * serializeGroup(instance) { return instance instanceof Type } + * serializeGroupSymbol = Type; + * } + * ``` + * + * > When a serializer component extends a parent serializer component the SERIALIZE_GROUP + * and SERIALIZE_GROUP_SYMBOL are also inherited so child serializers of the same group do not + * need to declare a predicate nor a group. + */ + abstract serializeGroup(instance: unknown): boolean; + /** + * The symbol representing the group this serializer belongs to. + */ + abstract serializeGroupSymbol: any; - /** - * A high-level predicate filtering which group this serializer belongs to. - * This is a high-level filter before the [[SerializerComponent.supports]] predicate filter. - * - * When the filter returns true the group identifier is taken from - * [[SerializerComponentType.serializeGroupSymbol]]. - * - * For example, use the [[Reflection]] class class to group all reflection based serializers: - * ```typescript - * class ReflectionSerializer { - * serializeGroup(instance) { return instance instanceof Reflection } - * serializeGroupSymbol = Reflection; - * } - * ``` - * - * Use the [[Type]] class to group all type based serializers: - * ```typescript - * class TypeSerializer { - * serializeGroup(instance) { return instance instanceof Type } - * serializeGroupSymbol = Type; - * } - * ``` - * - * > When a serializer component extends a parent serializer component the SERIALIZE_GROUP - * and SERIALIZE_GROUP_SYMBOL are also inherited so child serializers of the same group do not - * need to declare a predicate nor a group. - */ - abstract serializeGroup(instance: unknown): boolean; - /** - * The symbol representing the group this serializer belongs to. - */ - abstract serializeGroupSymbol: any; + /** + * The priority this serializer should be executed with. + * A higher priority means the [[Serializer]] will be applied earlier. + */ + get priority(): number { + return this.constructor['PRIORITY']; + } - /** - * The priority this serializer should be executed with. - * A higher priority means the [[Serializer]] will be applied earlier. - */ - get priority(): number { - return this.constructor['PRIORITY']; - } - - abstract supports(item: unknown): boolean; - - abstract toObject(item: T, obj?: any): any; + abstract supports(item: unknown): boolean; + abstract toObject(item: T, obj?: any): any; } export abstract class ReflectionSerializerComponent extends SerializerComponent { + /** + * Filter for instances of [[Reflection]] + */ + serializeGroup(instance: unknown): boolean { + return instance instanceof Reflection; + } - /** - * Filter for instances of [[Reflection]] - */ - serializeGroup(instance: unknown): boolean { - return instance instanceof Reflection; - } - - serializeGroupSymbol = Reflection; + serializeGroupSymbol = Reflection; } export abstract class TypeSerializerComponent extends SerializerComponent { + /** + * Filter for instances of [[Type]] + */ + serializeGroup(instance: unknown): boolean { + return instance instanceof Type; + } - /** - * Filter for instances of [[Type]] - */ - serializeGroup(instance: unknown): boolean { - return instance instanceof Type; - } - - serializeGroupSymbol = Type; + serializeGroupSymbol = Type; } diff --git a/src/lib/serialization/index.ts b/src/lib/serialization/index.ts index 37ec1c249..a39a0931f 100644 --- a/src/lib/serialization/index.ts +++ b/src/lib/serialization/index.ts @@ -1,38 +1,34 @@ -export { - ReflectionSerializerComponent, - SerializerComponent, - TypeSerializerComponent -} from './components'; +export { ReflectionSerializerComponent, SerializerComponent, TypeSerializerComponent } from './components'; export { Serializer } from './serializer'; export { - SourceReferenceContainerSerializer, - SourceReferenceWrapper, - DecoratorContainerSerializer, - DecoratorWrapper, - ContainerReflectionSerializer, - CommentTagSerializer, - CommentSerializer, - ArrayTypeSerializer, - DeclarationReflectionSerializer, - IntersectionTypeSerializer, - IntersectionUnion, - IntrinsicTypeSerializer, - ParameterReflectionSerializer, - ProjectReflectionSerializer, - ReferenceTypeSerializer, - ReflectionGroupSerializer, - ReflectionSerializer, - ReflectionTypeSerializer, - SignatureReflectionSerializer, - StringLiteralTypeSerializer, - TupleTypeSerializer, - TypeSerializer, - TypeParameterReflectionSerializer, - TypeOperatorTypeSerializer, - TypeParameterTypeSerializer, - UnknownTypeSerializer + SourceReferenceContainerSerializer, + SourceReferenceWrapper, + DecoratorContainerSerializer, + DecoratorWrapper, + ContainerReflectionSerializer, + CommentTagSerializer, + CommentSerializer, + ArrayTypeSerializer, + DeclarationReflectionSerializer, + IntersectionTypeSerializer, + IntersectionUnion, + IntrinsicTypeSerializer, + ParameterReflectionSerializer, + ProjectReflectionSerializer, + ReferenceTypeSerializer, + ReflectionGroupSerializer, + ReflectionSerializer, + ReflectionTypeSerializer, + SignatureReflectionSerializer, + StringLiteralTypeSerializer, + TupleTypeSerializer, + TypeSerializer, + TypeParameterReflectionSerializer, + TypeOperatorTypeSerializer, + TypeParameterTypeSerializer, + UnknownTypeSerializer } from './serializers'; export { SerializeEvent } from './events'; diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index c77ecdb6a..aae90ef37 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -6,119 +6,121 @@ import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; import { SerializeEvent } from './events'; -@Component({name: 'serializer', internal: true, childClass: SerializerComponent}) +@Component({ name: 'serializer', internal: true, childClass: SerializerComponent }) export class Serializer extends ChildableComponent> { + /** + * Triggered when the [[Serializer]] begins transforming a project. + * @event EVENT_BEGIN + */ + static EVENT_BEGIN = 'begin'; + + /** + * Triggered when the [[Serializer]] has finished transforming a project. + * @event EVENT_END + */ + static EVENT_END = 'end'; + + private router!: Map[] }>; + private routes!: any[]; + + initialize(): void { + this.router = new Map[] }>(); + this.routes = []; + } + + addComponent & Component>( + name: string, + componentClass: T | ComponentClass + ): T { + const component = super.addComponent(name, componentClass); + + if (component.serializeGroup && component.serializeGroupSymbol) { + let match = this.router.get(component.serializeGroup); + + if (!match) { + match = Array.from(this.router.values()).find( + v => v.symbol === component.serializeGroupSymbol + ) || { symbol: component.serializeGroupSymbol, group: [] }; + this.router.set(component.serializeGroup, match); + this.routes.push(component.serializeGroup); + } + match.group.push(component); + match.group.sort((a, b) => (b.priority || 0) - (a.priority || 0)); + } - /** - * Triggered when the [[Serializer]] begins transforming a project. - * @event EVENT_BEGIN - */ - static EVENT_BEGIN = 'begin'; - - /** - * Triggered when the [[Serializer]] has finished transforming a project. - * @event EVENT_END - */ - static EVENT_END = 'end'; - - private router!: Map[] }>; - private routes!: any[]; - - initialize(): void { - this.router = new Map[] }>(); - this.routes = []; - } - - addComponent & Component>(name: string, componentClass: T | ComponentClass): T { - const component = super.addComponent(name, componentClass); - - if (component.serializeGroup && component.serializeGroupSymbol) { - let match = this.router.get(component.serializeGroup); - - if (!match) { - match = Array.from(this.router.values()).find( v => v.symbol === component.serializeGroupSymbol) - || { symbol: component.serializeGroupSymbol , group: [] }; - this.router.set(component.serializeGroup, match); - this.routes.push(component.serializeGroup); - } - match.group.push(component); - match.group.sort((a, b) => (b.priority || 0) - (a.priority || 0)); + return component; } - return component; - } - - /** - * Remove a child component from the registry. - * @param name The name the component registered as - */ - removeComponent(name: string): SerializerComponent | undefined { - const component = super.removeComponent(name); - const symbol = component && component.serializeGroupSymbol; - if (symbol) { - const values = Array.from(this.router.values()); - for (let i = 0, len = values.length; i < len; i++) { - const idx = values[i].group.findIndex( o => o === symbol ); - if (idx > -1) { - values[i].group.splice(idx, 1); - break; + /** + * Remove a child component from the registry. + * @param name The name the component registered as + */ + removeComponent(name: string): SerializerComponent | undefined { + const component = super.removeComponent(name); + const symbol = component && component.serializeGroupSymbol; + if (symbol) { + const values = Array.from(this.router.values()); + for (let i = 0, len = values.length; i < len; i++) { + const idx = values[i].group.findIndex(o => o === symbol); + if (idx > -1) { + values[i].group.splice(idx, 1); + break; + } + } } - } + return component; + } + + removeAllComponents() { + super.removeAllComponents(); + + this.router = new Map[] }>(); + this.routes = []; } - return component; - } - - removeAllComponents() { - super.removeAllComponents(); - - this.router = new Map[] }>(); - this.routes = []; - } - - toObject(value: any, obj?: any): any { - return this.findRoutes(value) - .reduce( (result, curr) => curr.toObject(value, result), obj); - } - - /** - * Same as toObject but emits [[ Serializer#EVENT_BEGIN ]] and [[ Serializer#EVENT_END ]] events. - * @param value - * @param eventData Partial information to set in the event - * @return {any} - */ - projectToObject(value: ProjectReflection, eventData?: { begin?: any, end?: any }): any { - const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value); - - if (eventData && eventData.begin) { - Object.assign(eventBegin, eventData.begin); + + toObject(value: any, obj?: any): any { + return this.findRoutes(value).reduce((result, curr) => curr.toObject(value, result), obj); } - let project: any = eventBegin.output = {}; - this.trigger(eventBegin); - project = this.toObject(value, project); + /** + * Same as toObject but emits [[ Serializer#EVENT_BEGIN ]] and [[ Serializer#EVENT_END ]] events. + * @param value + * @param eventData Partial information to set in the event + * @return {any} + */ + projectToObject(value: ProjectReflection, eventData?: { begin?: any; end?: any }): any { + const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value); + + if (eventData && eventData.begin) { + Object.assign(eventBegin, eventData.begin); + } + let project: any = (eventBegin.output = {}); + + this.trigger(eventBegin); + project = this.toObject(value, project); - const eventEnd = new SerializeEvent(Serializer.EVENT_END, value); - if (eventData && eventData.end) { - Object.assign(eventEnd, eventData.end); + const eventEnd = new SerializeEvent(Serializer.EVENT_END, value); + if (eventData && eventData.end) { + Object.assign(eventEnd, eventData.end); + } + eventEnd.output = project; + this.trigger(eventEnd); + + return project; } - eventEnd.output = project; - this.trigger(eventEnd); - - return project; - } - - private findRoutes(value: any): SerializerComponent[] { - const routes: SerializerComponent[] = []; - for (let i = 0, len = this.routes.length; i < len; i++) { - if (this.routes[i](value)) { - const serializers = this.router.get(this.routes[i])!.group; - for (let serializer of serializers) { - if (serializer.supports(value)) { - routes.push(serializer); - } + + private findRoutes(value: any): SerializerComponent[] { + const routes: SerializerComponent[] = []; + for (let i = 0, len = this.routes.length; i < len; i++) { + if (this.routes[i](value)) { + const serializers = this.router.get(this.routes[i])!.group; + for (let serializer of serializers) { + if (serializer.supports(value)) { + routes.push(serializer); + } + } + } } - } + return routes; } - return routes; - } } diff --git a/src/lib/serialization/serializers/comments/comment-tag.ts b/src/lib/serialization/serializers/comments/comment-tag.ts index c4b101094..effbbc83c 100644 --- a/src/lib/serialization/serializers/comments/comment-tag.ts +++ b/src/lib/serialization/serializers/comments/comment-tag.ts @@ -3,34 +3,33 @@ import { CommentTag } from '../../../models'; import { SerializerComponent } from '../../components'; -@Component({name: 'serializer:comment-tag'}) +@Component({ name: 'serializer:comment-tag' }) export class CommentTagSerializer extends SerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; + /** + * Filter for instances of [[CommentTag]] + */ + serializeGroup(instance: unknown): boolean { + return instance instanceof CommentTag; + } - /** - * Filter for instances of [[CommentTag]] - */ - serializeGroup(instance: unknown): boolean { - return instance instanceof CommentTag; - } + serializeGroupSymbol = CommentTag; - serializeGroupSymbol = CommentTag; + supports(t: unknown) { + return true; + } - supports(t: unknown) { - return true; - } + toObject(tag: CommentTag, obj?: any): any { + obj = obj || {}; - toObject(tag: CommentTag, obj?: any): any { - obj = obj || {}; + obj.tag = tag.tagName; + obj.text = tag.text; - obj.tag = tag.tagName; - obj.text = tag.text; + if (tag.paramName) { + obj.param = tag.paramName; + } - if (tag.paramName) { - obj.param = tag.paramName; + return obj; } - - return obj; - } } diff --git a/src/lib/serialization/serializers/comments/comment.ts b/src/lib/serialization/serializers/comments/comment.ts index d05dd3914..7fffba672 100644 --- a/src/lib/serialization/serializers/comments/comment.ts +++ b/src/lib/serialization/serializers/comments/comment.ts @@ -3,41 +3,40 @@ import { Comment } from '../../../models'; import { SerializerComponent } from '../../components'; -@Component({name: 'serializer:comment'}) +@Component({ name: 'serializer:comment' }) export class CommentSerializer extends SerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; - - /** - * Filter for instances of [[Comment]] - */ - serializeGroup(instance: unknown): boolean { - return instance instanceof Comment; - } - - supports(t: unknown) { - return true; - } - - serializeGroupSymbol = Comment; - - toObject(comment: Comment, obj?: any): any { - obj = obj || {}; - - if (comment.shortText) { - obj.shortText = comment.shortText; - } - if (comment.text) { - obj.text = comment.text; + /** + * Filter for instances of [[Comment]] + */ + serializeGroup(instance: unknown): boolean { + return instance instanceof Comment; } - if (comment.returns) { - obj.returns = comment.returns; + + supports(t: unknown) { + return true; } - if (comment.tags && comment.tags.length) { - obj.tags = []; - comment.tags.forEach((tag) => obj.tags.push(this.owner.toObject(tag))); + serializeGroupSymbol = Comment; + + toObject(comment: Comment, obj?: any): any { + obj = obj || {}; + + if (comment.shortText) { + obj.shortText = comment.shortText; + } + if (comment.text) { + obj.text = comment.text; + } + if (comment.returns) { + obj.returns = comment.returns; + } + + if (comment.tags && comment.tags.length) { + obj.tags = []; + comment.tags.forEach(tag => obj.tags.push(this.owner.toObject(tag))); + } + return obj; } - return obj; - } } diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index eb66505b9..f26fb3405 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -3,43 +3,41 @@ import { Component } from '../../utils/component'; import { SerializerComponent } from '../components'; import { DecoratorWrapper } from './models/decorator-wrapper'; -@Component({name: 'serializer:decorator-container'}) +@Component({ name: 'serializer:decorator-container' }) export class DecoratorContainerSerializer extends SerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; + /** + * Filter for instances of [[DecoratorWrapper]] + */ + serializeGroup(instance: any): boolean { + return instance instanceof DecoratorWrapper; + } - /** - * Filter for instances of [[DecoratorWrapper]] - */ - serializeGroup(instance: any): boolean { - return instance instanceof DecoratorWrapper; - } + serializeGroupSymbol = DecoratorWrapper; - serializeGroupSymbol = DecoratorWrapper; + initialize(): void { + super.initialize(); + } - initialize(): void { - super.initialize(); - } + supports(s: unknown) { + return s instanceof DecoratorWrapper; + } - supports(s: unknown) { - return s instanceof DecoratorWrapper; - } + toObject(decoratorWrapper: DecoratorWrapper, obj?: any): any { + obj = obj || {}; - toObject(decoratorWrapper: DecoratorWrapper, obj?: any): any { - obj = obj || {}; + const decorator = decoratorWrapper.decorator; + obj.name = decorator.name; - const decorator = decoratorWrapper.decorator; - obj.name = decorator.name; + if (decorator.type) { + obj.type = this.owner.toObject(decorator.type); + } - if (decorator.type) { - obj.type = this.owner.toObject(decorator.type); - } - - if (decorator.arguments) { - obj.arguments = decorator.arguments; - } - - return obj; - } + if (decorator.arguments) { + obj.arguments = decorator.arguments; + } + return obj; + } } diff --git a/src/lib/serialization/serializers/models/decorator-wrapper.ts b/src/lib/serialization/serializers/models/decorator-wrapper.ts index 95e116ee2..522d73bb6 100644 --- a/src/lib/serialization/serializers/models/decorator-wrapper.ts +++ b/src/lib/serialization/serializers/models/decorator-wrapper.ts @@ -5,5 +5,5 @@ import { Decorator } from '../../../models'; * so it can be identified */ export class DecoratorWrapper { - constructor(public decorator: Decorator) { } + constructor(public decorator: Decorator) {} } diff --git a/src/lib/serialization/serializers/models/source-reference-wrapper.ts b/src/lib/serialization/serializers/models/source-reference-wrapper.ts index 1970d0fb6..02254d294 100644 --- a/src/lib/serialization/serializers/models/source-reference-wrapper.ts +++ b/src/lib/serialization/serializers/models/source-reference-wrapper.ts @@ -5,5 +5,5 @@ import { SourceReference } from '../../../models/sources/file'; * so it can be identified */ export class SourceReferenceWrapper { - constructor(public sourceReference: SourceReference) { } + constructor(public sourceReference: SourceReference) {} } diff --git a/src/lib/serialization/serializers/reflection-group.ts b/src/lib/serialization/serializers/reflection-group.ts index b9ab40a61..a168b80ca 100644 --- a/src/lib/serialization/serializers/reflection-group.ts +++ b/src/lib/serialization/serializers/reflection-group.ts @@ -3,41 +3,39 @@ import { ReflectionGroup } from '../../models/ReflectionGroup'; import { SerializerComponent } from '../components'; -@Component({name: 'serializer:reflection-group'}) +@Component({ name: 'serializer:reflection-group' }) export class ReflectionGroupSerializer extends SerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; + /** + * Filter for instances of [[ReflectionGroup]] + */ + serializeGroup(instance: any): boolean { + return instance instanceof ReflectionGroup; + } - /** - * Filter for instances of [[ReflectionGroup]] - */ - serializeGroup(instance: any): boolean { - return instance instanceof ReflectionGroup; - } + serializeGroupSymbol = ReflectionGroup; - serializeGroupSymbol = ReflectionGroup; + initialize(): void { + super.initialize(); + } - initialize(): void { - super.initialize(); - } + supports(r: unknown) { + return r instanceof ReflectionGroup; + } - supports(r: unknown) { - return r instanceof ReflectionGroup; - } + toObject(group: ReflectionGroup, obj?: any): any { + obj = obj || {}; - toObject(group: ReflectionGroup, obj?: any): any { - obj = obj || {}; + Object.assign(obj, { + title: group.title, + kind: group.kind + }); - Object.assign(obj, { - title: group.title, - kind: group.kind - }); + if (group.children && group.children.length > 0) { + obj.children = group.children.map(child => child.id); + } - if (group.children && group.children.length > 0) { - obj.children = group.children.map( child => child.id ); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/reflections/abstract.ts b/src/lib/serialization/serializers/reflections/abstract.ts index 3ec7069a2..d4e17736b 100644 --- a/src/lib/serialization/serializers/reflections/abstract.ts +++ b/src/lib/serialization/serializers/reflections/abstract.ts @@ -5,60 +5,59 @@ import { ReflectionSerializerComponent } from '../../components'; import { DecoratorWrapper } from '../models'; import { ReflectionFlags } from '../../../models/reflections/abstract'; -@Component({name: 'serializer:reflection'}) +@Component({ name: 'serializer:reflection' }) export class ReflectionSerializer extends ReflectionSerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; - - supports(t: unknown) { - return t instanceof Reflection; - } - - toObject(reflection: Reflection, obj?: any): any { - obj = obj || {}; - - Object.assign(obj, { - id: reflection.id, - name: reflection.name, - kind: reflection.kind, - kindString: reflection.kindString, - flags: {} // TODO: remove if no flags - }); - - if (reflection.originalName !== reflection.name) { - obj.originalName = reflection.originalName; - } - - if (reflection.comment) { - obj.comment = this.owner.toObject(reflection.comment); + supports(t: unknown) { + return t instanceof Reflection; } - for (const key of Object.getOwnPropertyNames(ReflectionFlags.prototype)) { - if (reflection.flags[key] === true) { - obj.flags[key] = true; - } + toObject(reflection: Reflection, obj?: any): any { + obj = obj || {}; + + Object.assign(obj, { + id: reflection.id, + name: reflection.name, + kind: reflection.kind, + kindString: reflection.kindString, + flags: {} // TODO: remove if no flags + }); + + if (reflection.originalName !== reflection.name) { + obj.originalName = reflection.originalName; + } + + if (reflection.comment) { + obj.comment = this.owner.toObject(reflection.comment); + } + + for (const key of Object.getOwnPropertyNames(ReflectionFlags.prototype)) { + if (reflection.flags[key] === true) { + obj.flags[key] = true; + } + } + + if (reflection.decorates && reflection.decorates.length > 0) { + obj.decorates = reflection.decorates.map(t => this.owner.toObject(t)); + } + + if (reflection.decorators && reflection.decorators.length > 0) { + obj.decorators = reflection.decorators.map(d => this.owner.toObject(new DecoratorWrapper(d))); + } + + reflection.traverse((child, property) => { + if (property === TraverseProperty.TypeLiteral) { + return; + } + let name = TraverseProperty[property]; + name = name.substr(0, 1).toLowerCase() + name.substr(1); + if (!obj[name]) { + obj[name] = []; + } + obj[name].push(this.owner.toObject(child)); + }); + + return obj; } - - if (reflection.decorates && reflection.decorates.length > 0) { - obj.decorates = reflection.decorates.map( t => this.owner.toObject(t) ); - } - - if (reflection.decorators && reflection.decorators.length > 0) { - obj.decorators = reflection.decorators.map( d => this.owner.toObject(new DecoratorWrapper(d)) ); - } - - reflection.traverse((child, property) => { - if (property === TraverseProperty.TypeLiteral) { - return; - } - let name = TraverseProperty[property]; - name = name.substr(0, 1).toLowerCase() + name.substr(1); - if (!obj[name]) { - obj[name] = []; - } - obj[name].push(this.owner.toObject(child)); - }); - - return obj; - } } diff --git a/src/lib/serialization/serializers/reflections/container.ts b/src/lib/serialization/serializers/reflections/container.ts index c7caec21a..6d8097104 100644 --- a/src/lib/serialization/serializers/reflections/container.ts +++ b/src/lib/serialization/serializers/reflections/container.ts @@ -4,32 +4,31 @@ import { ContainerReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { SourceReferenceWrapper } from '../models'; -@Component({name: 'serializer:container-reflection'}) +@Component({ name: 'serializer:container-reflection' }) export class ContainerReflectionSerializer extends ReflectionSerializerComponent { - - supports(t: unknown) { - return t instanceof ContainerReflection; - } - - toObject(container: ContainerReflection, obj?: any): any { - obj = obj || {}; - - if (container.groups && container.groups.length > 0) { - obj.groups = container.groups.map( group => this.owner.toObject(group) ); + supports(t: unknown) { + return t instanceof ContainerReflection; } - if (container.sources && container.sources.length > 0) { - obj.sources = container.sources - .map( source => this.owner - .toObject(new SourceReferenceWrapper({ - fileName: source.fileName, - line: source.line, - character: source.character - })) - ); + toObject(container: ContainerReflection, obj?: any): any { + obj = obj || {}; + + if (container.groups && container.groups.length > 0) { + obj.groups = container.groups.map(group => this.owner.toObject(group)); + } + + if (container.sources && container.sources.length > 0) { + obj.sources = container.sources.map(source => + this.owner.toObject( + new SourceReferenceWrapper({ + fileName: source.fileName, + line: source.line, + character: source.character + }) + ) + ); + } + + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/reflections/declaration.ts b/src/lib/serialization/serializers/reflections/declaration.ts index 439419a83..d13899012 100644 --- a/src/lib/serialization/serializers/reflections/declaration.ts +++ b/src/lib/serialization/serializers/reflections/declaration.ts @@ -4,55 +4,53 @@ import { DeclarationReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; -@Component({name: 'serializer:declaration-reflection'}) +@Component({ name: 'serializer:declaration-reflection' }) export class DeclarationReflectionSerializer extends ReflectionSerializerComponent { + static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent - static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent + supports(t: unknown) { + return t instanceof DeclarationReflection; + } - supports(t: unknown) { - return t instanceof DeclarationReflection; - } + toObject(declaration: DeclarationReflection, obj?: any): any { + obj = obj || {}; - toObject(declaration: DeclarationReflection, obj?: any): any { - obj = obj || {}; + if (declaration.type) { + obj.type = this.owner.toObject(declaration.type); + } - if (declaration.type) { - obj.type = this.owner.toObject(declaration.type); - } + if (declaration.defaultValue) { + obj.defaultValue = declaration.defaultValue; + } - if (declaration.defaultValue) { - obj.defaultValue = declaration.defaultValue; - } + if (declaration.overwrites) { + obj.overwrites = this.owner.toObject(declaration.overwrites); + } - if (declaration.overwrites) { - obj.overwrites = this.owner.toObject(declaration.overwrites); - } + if (declaration.inheritedFrom) { + obj.inheritedFrom = this.owner.toObject(declaration.inheritedFrom); + } - if (declaration.inheritedFrom) { - obj.inheritedFrom = this.owner.toObject(declaration.inheritedFrom); - } + if (declaration.extendedTypes) { + obj.extendedTypes = declaration.extendedTypes.map(t => this.owner.toObject(t)); + } - if (declaration.extendedTypes) { - obj.extendedTypes = declaration.extendedTypes.map((t) => this.owner.toObject(t) ); - } + if (declaration.extendedBy) { + obj.extendedBy = declaration.extendedBy.map(t => this.owner.toObject(t)); + } - if (declaration.extendedBy) { - obj.extendedBy = declaration.extendedBy.map((t) => this.owner.toObject(t) ); - } + if (declaration.implementedTypes) { + obj.implementedTypes = declaration.implementedTypes.map(t => this.owner.toObject(t)); + } - if (declaration.implementedTypes) { - obj.implementedTypes = declaration.implementedTypes.map((t) => this.owner.toObject(t) ); - } + if (declaration.implementedBy) { + obj.implementedBy = declaration.implementedBy.map(t => this.owner.toObject(t)); + } - if (declaration.implementedBy) { - obj.implementedBy = declaration.implementedBy.map((t) => this.owner.toObject(t) ); - } + if (declaration.implementationOf) { + obj.implementationOf = this.owner.toObject(declaration.implementationOf); + } - if (declaration.implementationOf) { - obj.implementationOf = this.owner.toObject(declaration.implementationOf); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/reflections/parameter.ts b/src/lib/serialization/serializers/reflections/parameter.ts index 3ef5e9e6b..1eaabc4c2 100644 --- a/src/lib/serialization/serializers/reflections/parameter.ts +++ b/src/lib/serialization/serializers/reflections/parameter.ts @@ -3,25 +3,23 @@ import { ParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; -@Component({name: 'serializer:parameter-reflection'}) +@Component({ name: 'serializer:parameter-reflection' }) export class ParameterReflectionSerializer extends ReflectionSerializerComponent { + supports(t: unknown) { + return t instanceof ParameterReflection; + } - supports(t: unknown) { - return t instanceof ParameterReflection; - } + toObject(parameter: ParameterReflection, obj?: any): any { + obj = obj || {}; - toObject(parameter: ParameterReflection, obj?: any): any { - obj = obj || {}; + if (parameter.type) { + obj.type = this.owner.toObject(parameter.type); + } - if (parameter.type) { - obj.type = this.owner.toObject(parameter.type); - } + if (parameter.defaultValue) { + obj.defaultValue = parameter.defaultValue; + } - if (parameter.defaultValue) { - obj.defaultValue = parameter.defaultValue; + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/reflections/project.ts b/src/lib/serialization/serializers/reflections/project.ts index 2db643a2d..648074b5d 100644 --- a/src/lib/serialization/serializers/reflections/project.ts +++ b/src/lib/serialization/serializers/reflections/project.ts @@ -4,17 +4,15 @@ import { ProjectReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; -@Component({name: 'serializer:project-reflection'}) +@Component({ name: 'serializer:project-reflection' }) export class ProjectReflectionSerializer extends ReflectionSerializerComponent { + static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent - static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent - - supports(t: unknown) { - return t instanceof ProjectReflection; - } - - toObject(container: ProjectReflection, obj?: any): any { - return obj; - } + supports(t: unknown) { + return t instanceof ProjectReflection; + } + toObject(container: ProjectReflection, obj?: any): any { + return obj; + } } diff --git a/src/lib/serialization/serializers/reflections/signature.ts b/src/lib/serialization/serializers/reflections/signature.ts index 1591f3588..0cd35facd 100644 --- a/src/lib/serialization/serializers/reflections/signature.ts +++ b/src/lib/serialization/serializers/reflections/signature.ts @@ -3,33 +3,31 @@ import { SignatureReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; -@Component({name: 'serializer:signature-reflection'}) +@Component({ name: 'serializer:signature-reflection' }) export class SignatureReflectionSerializer extends ReflectionSerializerComponent { + supports(t: unknown) { + return t instanceof SignatureReflection; + } - supports(t: unknown) { - return t instanceof SignatureReflection; - } + toObject(signature: SignatureReflection, obj?: any): any { + obj = obj || {}; - toObject(signature: SignatureReflection, obj?: any): any { - obj = obj || {}; + if (signature.type) { + obj.type = this.owner.toObject(signature.type); + } - if (signature.type) { - obj.type = this.owner.toObject(signature.type); - } + if (signature.overwrites) { + obj.overwrites = this.owner.toObject(signature.overwrites); + } - if (signature.overwrites) { - obj.overwrites = this.owner.toObject(signature.overwrites); - } + if (signature.inheritedFrom) { + obj.inheritedFrom = this.owner.toObject(signature.inheritedFrom); + } - if (signature.inheritedFrom) { - obj.inheritedFrom = this.owner.toObject(signature.inheritedFrom); - } + if (signature.implementationOf) { + obj.implementationOf = this.owner.toObject(signature.implementationOf); + } - if (signature.implementationOf) { - obj.implementationOf = this.owner.toObject(signature.implementationOf); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/reflections/type-parameter.ts b/src/lib/serialization/serializers/reflections/type-parameter.ts index e86a4aa76..3a26441f1 100644 --- a/src/lib/serialization/serializers/reflections/type-parameter.ts +++ b/src/lib/serialization/serializers/reflections/type-parameter.ts @@ -3,21 +3,21 @@ import { TypeParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; -@Component({name: 'serializer:type-parameter-reflection'}) -export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent { +@Component({ name: 'serializer:type-parameter-reflection' }) +export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent< + TypeParameterReflection +> { + supports(t: unknown) { + return t instanceof TypeParameterReflection; + } - supports(t: unknown) { - return t instanceof TypeParameterReflection; - } + toObject(typeParameter: TypeParameterReflection, obj?: any): any { + obj = obj || {}; - toObject(typeParameter: TypeParameterReflection, obj?: any): any { - obj = obj || {}; + if (typeParameter.type) { + obj.type = this.owner.toObject(typeParameter.type); + } - if (typeParameter.type) { - obj.type = this.owner.toObject(typeParameter.type); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/types/abstract.ts b/src/lib/serialization/serializers/types/abstract.ts index 03991000a..4bbd2d2bd 100644 --- a/src/lib/serialization/serializers/types/abstract.ts +++ b/src/lib/serialization/serializers/types/abstract.ts @@ -3,21 +3,19 @@ import { Type } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:type'}) +@Component({ name: 'serializer:type' }) export class TypeSerializer extends TypeSerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; + supports(t: unknown) { + return t instanceof Type; + } - supports(t: unknown) { - return t instanceof Type; - } + toObject(type: Type, obj?: any): any { + obj = obj || {}; - toObject(type: Type, obj?: any): any { - obj = obj || {}; - - obj.type = type.type; - - return obj; - } + obj.type = type.type; + return obj; + } } diff --git a/src/lib/serialization/serializers/types/array.ts b/src/lib/serialization/serializers/types/array.ts index f2ea7128d..c58e1b384 100644 --- a/src/lib/serialization/serializers/types/array.ts +++ b/src/lib/serialization/serializers/types/array.ts @@ -3,19 +3,17 @@ import { ArrayType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:array-type'}) +@Component({ name: 'serializer:array-type' }) export class ArrayTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof ArrayType; + } - supports(t: unknown) { - return t instanceof ArrayType; - } + toObject(arrayType: ArrayType, obj?: any): any { + obj = obj || {}; - toObject(arrayType: ArrayType, obj?: any): any { - obj = obj || {}; - - obj.elementType = this.owner.toObject(arrayType.elementType); - - return obj; - } + obj.elementType = this.owner.toObject(arrayType.elementType); + return obj; + } } diff --git a/src/lib/serialization/serializers/types/intersection-union.ts b/src/lib/serialization/serializers/types/intersection-union.ts index 65b23f5b1..bb0bb52a0 100644 --- a/src/lib/serialization/serializers/types/intersection-union.ts +++ b/src/lib/serialization/serializers/types/intersection-union.ts @@ -5,21 +5,19 @@ import { TypeSerializerComponent } from '../../components'; export type IntersectionUnion = IntersectionType | UnionType; -@Component({name: 'serializer:intersection-type'}) +@Component({ name: 'serializer:intersection-type' }) export class IntersectionTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof IntersectionType || t instanceof UnionType; + } - supports(t: unknown) { - return t instanceof IntersectionType || t instanceof UnionType; - } + toObject(intersectionUnion: IntersectionUnion, obj?: any): any { + obj = obj || {}; - toObject(intersectionUnion: IntersectionUnion, obj?: any): any { - obj = obj || {}; + if (intersectionUnion.types && intersectionUnion.types.length) { + obj.types = intersectionUnion.types.map(t => this.owner.toObject(t)); + } - if (intersectionUnion.types && intersectionUnion.types.length) { - obj.types = intersectionUnion.types.map( t => this.owner.toObject(t) ); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/types/intrinsic.ts b/src/lib/serialization/serializers/types/intrinsic.ts index fc4b5d8ca..16cbafeed 100644 --- a/src/lib/serialization/serializers/types/intrinsic.ts +++ b/src/lib/serialization/serializers/types/intrinsic.ts @@ -3,19 +3,17 @@ import { IntrinsicType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:intrinsic-type'}) +@Component({ name: 'serializer:intrinsic-type' }) export class IntrinsicTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof IntrinsicType; + } - supports(t: unknown) { - return t instanceof IntrinsicType; - } + toObject(intrinsic: IntrinsicType, obj?: any): any { + obj = obj || {}; - toObject(intrinsic: IntrinsicType, obj?: any): any { - obj = obj || {}; - - obj.name = intrinsic.name; - - return obj; - } + obj.name = intrinsic.name; + return obj; + } } diff --git a/src/lib/serialization/serializers/types/reference.ts b/src/lib/serialization/serializers/types/reference.ts index 559000750..a1c72de0d 100644 --- a/src/lib/serialization/serializers/types/reference.ts +++ b/src/lib/serialization/serializers/types/reference.ts @@ -3,27 +3,25 @@ import { ReferenceType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:reference-type'}) +@Component({ name: 'serializer:reference-type' }) export class ReferenceTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof ReferenceType; + } - supports(t: unknown) { - return t instanceof ReferenceType; - } + toObject(reference: ReferenceType, obj?: any): any { + obj = obj || {}; - toObject(reference: ReferenceType, obj?: any): any { - obj = obj || {}; + obj.name = reference.name; - obj.name = reference.name; + if (reference.reflection) { + obj.id = reference.reflection.id; + } - if (reference.reflection) { - obj.id = reference.reflection.id; - } + if (reference.typeArguments && reference.typeArguments.length > 0) { + obj.typeArguments = reference.typeArguments.map(t => this.owner.toObject(t)); + } - if (reference.typeArguments && reference.typeArguments.length > 0) { - obj.typeArguments = reference.typeArguments.map( t => this.owner.toObject(t) ); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/types/reflection.ts b/src/lib/serialization/serializers/types/reflection.ts index feddf59ce..fa9ff0814 100644 --- a/src/lib/serialization/serializers/types/reflection.ts +++ b/src/lib/serialization/serializers/types/reflection.ts @@ -3,41 +3,39 @@ import { DeclarationReflection, ReflectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:reflection-type'}) +@Component({ name: 'serializer:reflection-type' }) export class ReflectionTypeSerializer extends TypeSerializerComponent { + private declaration?: DeclarationReflection; - private declaration?: DeclarationReflection; - - supports(t: unknown) { - return t instanceof ReflectionType; - } + supports(t: unknown) { + return t instanceof ReflectionType; + } - toObject(reference: ReflectionType, obj?: any): any { - obj = obj || {}; + toObject(reference: ReflectionType, obj?: any): any { + obj = obj || {}; - if (reference.declaration) { + if (reference.declaration) { + // Because `DeclarationReflection` has reference to multiple types objectifying a declaration + // on a type might fall into a loop trap (cyclic dependency). + // The TypeDoc code does not apply logic that can create this scenario but a 3rd party plugin + // might do that unintentionally so a protection is in place. - // Because `DeclarationReflection` has reference to multiple types objectifying a declaration - // on a type might fall into a loop trap (cyclic dependency). - // The TypeDoc code does not apply logic that can create this scenario but a 3rd party plugin - // might do that unintentionally so a protection is in place. + if (this.declaration === reference.declaration) { + // if we're here it means that the reference type is rendered for the 2nd time + // by the declaration it is referencing, we will render a pointer-only declaration. + obj.declaration = { id: reference.declaration.id }; + } else { + // mark this declaration to trap a loop + this.declaration = reference.declaration; - if (this.declaration === reference.declaration) { - // if we're here it means that the reference type is rendered for the 2nd time - // by the declaration it is referencing, we will render a pointer-only declaration. - obj.declaration = { id: reference.declaration.id }; - } else { - // mark this declaration to trap a loop - this.declaration = reference.declaration; + // objectify the declaration + obj.declaration = this.owner.toObject(reference.declaration); + } - // objectify the declaration - obj.declaration = this.owner.toObject(reference.declaration); - } + // no more declaration rendering, remove marker. + this.declaration = undefined; + } - // no more declaration rendering, remove marker. - this.declaration = undefined; + return obj; } - - return obj; - } } diff --git a/src/lib/serialization/serializers/types/string-literal.ts b/src/lib/serialization/serializers/types/string-literal.ts index 0fb4906a5..6c0c93447 100644 --- a/src/lib/serialization/serializers/types/string-literal.ts +++ b/src/lib/serialization/serializers/types/string-literal.ts @@ -3,19 +3,17 @@ import { StringLiteralType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:string-literal-type'}) +@Component({ name: 'serializer:string-literal-type' }) export class StringLiteralTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof StringLiteralType; + } - supports(t: unknown) { - return t instanceof StringLiteralType; - } + toObject(stringLiteral: StringLiteralType, obj?: any): any { + obj = obj || {}; - toObject(stringLiteral: StringLiteralType, obj?: any): any { - obj = obj || {}; - - obj.value = stringLiteral.value; - - return obj; - } + obj.value = stringLiteral.value; + return obj; + } } diff --git a/src/lib/serialization/serializers/types/tuple.ts b/src/lib/serialization/serializers/types/tuple.ts index 617016cf3..3c130bdcf 100644 --- a/src/lib/serialization/serializers/types/tuple.ts +++ b/src/lib/serialization/serializers/types/tuple.ts @@ -3,21 +3,19 @@ import { TupleType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:tuple-type'}) +@Component({ name: 'serializer:tuple-type' }) export class TupleTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof TupleType; + } - supports(t: unknown) { - return t instanceof TupleType; - } + toObject(tuple: TupleType, obj?: any): any { + obj = obj || {}; - toObject(tuple: TupleType, obj?: any): any { - obj = obj || {}; + if (tuple.elements && tuple.elements.length > 0) { + obj.elements = tuple.elements.map(t => this.owner.toObject(t)); + } - if (tuple.elements && tuple.elements.length > 0) { - obj.elements = tuple.elements.map( t => this.owner.toObject(t) ); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/types/type-operator.ts b/src/lib/serialization/serializers/types/type-operator.ts index 5f2235913..9ea5565b5 100644 --- a/src/lib/serialization/serializers/types/type-operator.ts +++ b/src/lib/serialization/serializers/types/type-operator.ts @@ -3,20 +3,18 @@ import { TypeOperatorType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:type-operator-type'}) +@Component({ name: 'serializer:type-operator-type' }) export class TypeOperatorTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof TypeOperatorType; + } - supports(t: unknown) { - return t instanceof TypeOperatorType; - } + toObject(typeOperator: TypeOperatorType, obj?: any): any { + obj = obj || {}; - toObject(typeOperator: TypeOperatorType, obj?: any): any { - obj = obj || {}; - - obj.operator = typeOperator.operator; - obj.target = this.owner.toObject(typeOperator.target); - - return obj; - } + obj.operator = typeOperator.operator; + obj.target = this.owner.toObject(typeOperator.target); + return obj; + } } diff --git a/src/lib/serialization/serializers/types/type-parameter.ts b/src/lib/serialization/serializers/types/type-parameter.ts index 92027ecb8..9f241f4af 100644 --- a/src/lib/serialization/serializers/types/type-parameter.ts +++ b/src/lib/serialization/serializers/types/type-parameter.ts @@ -3,23 +3,21 @@ import { TypeParameterType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:type-parameter-type'}) +@Component({ name: 'serializer:type-parameter-type' }) export class TypeParameterTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof TypeParameterType; + } - supports(t: unknown) { - return t instanceof TypeParameterType; - } + toObject(typeParameter: TypeParameterType, obj?: any): any { + obj = obj || {}; - toObject(typeParameter: TypeParameterType, obj?: any): any { - obj = obj || {}; + obj.name = typeParameter.name; - obj.name = typeParameter.name; + if (typeParameter.constraint) { + obj.constraint = this.owner.toObject(typeParameter.constraint); + } - if (typeParameter.constraint) { - obj.constraint = this.owner.toObject(typeParameter.constraint); + return obj; } - - return obj; - } - } diff --git a/src/lib/serialization/serializers/types/unknown.ts b/src/lib/serialization/serializers/types/unknown.ts index 56784473a..84c7e4acf 100644 --- a/src/lib/serialization/serializers/types/unknown.ts +++ b/src/lib/serialization/serializers/types/unknown.ts @@ -3,19 +3,17 @@ import { UnknownType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -@Component({name: 'serializer:unknown-type'}) +@Component({ name: 'serializer:unknown-type' }) export class UnknownTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof UnknownType; + } - supports(t: unknown) { - return t instanceof UnknownType; - } + toObject(unknown: UnknownType, obj?: any): any { + obj = obj || {}; - toObject(unknown: UnknownType, obj?: any): any { - obj = obj || {}; - - obj.name = unknown.name; - - return obj; - } + obj.name = unknown.name; + return obj; + } } diff --git a/src/lib/utils/__events.ts b/src/lib/utils/__events.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/tslint.json b/tslint.json index 1718dddfd..10ad5d89b 100644 --- a/tslint.json +++ b/tslint.json @@ -1,65 +1,92 @@ { - "rules": { - "align": false, - "class-name": true, - "comment-format": [ true, "check-space" ], - "curly": true, - "eofline": true, - "forin": false, - "indent": [ true, "spaces", 4 ], - "interface-name": [ true, "never-prefix" ], - "jsdoc-format": true, - "label-position": true, - "max-line-length": [true, 170], - "member-access": false, - "member-ordering": false, - "no-any": false, - "no-arg": true, - "no-bitwise": false, - "no-consecutive-blank-lines": true, - "no-console": false, - "no-construct": false, - "no-debugger": true, - "no-duplicate-variable": true, - "no-empty": false, - "no-eval": true, - "no-for-in-array": true, - "no-inferrable-types": [ true, "ignore-params" ], - "no-shadowed-variable": false, - "no-string-literal": false, - "no-switch-case-fall-through": false, - "no-trailing-whitespace": true, - "no-unnecessary-type-assertion": true, - "no-unused-expression": false, - "no-use-before-declare": false, - "no-var-keyword": true, - "no-var-requires": false, - "strict-type-predicates": true, - "object-literal-sort-keys": false, - "one-line": [ true, "check-open-brace", "check-whitespace", "check-else", "check-catch" ], - "quotemark": [ true, "single", "avoid-escape" ], - "radix": true, - "semicolon": [ true, "always" ], - "trailing-comma": [ true, { - "multiline": "never", - "singleline": "never" - } ], - "triple-equals": [ true, "allow-null-check" ], - "typedef": false, - "typedef-whitespace": [ true, { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - }, { - "call-signature": "onespace", - "index-signature": "onespace", - "parameter": "onespace", - "property-declaration": "onespace", - "variable-declaration": "onespace" - } ], - "variable-name": [ true, "check-format", "allow-leading-underscore", "ban-keywords" ], - "whitespace": [ true, "check-branch", "check-decl", "check-operator", "check-module", "check-separator", "check-type", "check-typecast" ] - } + "rules": { + "align": false, + "class-name": true, + "comment-format": [true, "check-space"], + "curly": true, + "eofline": true, + "forin": false, + "indent": [true, "spaces", 4], + "interface-name": [true, "never-prefix"], + "jsdoc-format": true, + "label-position": true, + "max-line-length": [true, 170], + "member-access": false, + "member-ordering": false, + "no-any": false, + "no-arg": true, + "no-bitwise": false, + "no-consecutive-blank-lines": true, + "no-console": false, + "no-construct": false, + "no-debugger": true, + "no-duplicate-variable": true, + "no-empty": false, + "no-eval": true, + "no-for-in-array": true, + "no-inferrable-types": [true, "ignore-params"], + "no-shadowed-variable": false, + "no-string-literal": false, + "no-switch-case-fall-through": false, + "no-trailing-whitespace": true, + "no-unnecessary-type-assertion": true, + "no-unused-expression": false, + "no-use-before-declare": false, + "no-var-keyword": true, + "no-var-requires": false, + "strict-type-predicates": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-whitespace", + "check-else", + "check-catch" + ], + "quotemark": [true, "single", "avoid-escape"], + "radix": true, + "semicolon": [true, "always"], + "trailing-comma": [ + true, + { + "multiline": "never", + "singleline": "never" + } + ], + "triple-equals": [true, "allow-null-check"], + "typedef": false, + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + }, + { + "call-signature": "onespace", + "index-signature": "onespace", + "parameter": "onespace", + "property-declaration": "onespace", + "variable-declaration": "onespace" + } + ], + "variable-name": [ + true, + "check-format", + "allow-leading-underscore", + "ban-keywords" + ], + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-separator", + "check-type", + "check-typecast" + ] + } } From ac206491e70db248fba47e11fc237cb00917da2e Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Mon, 31 Dec 2018 22:34:41 -0700 Subject: [PATCH 03/17] Remove the unnecessary serializeGroupSymbol --- src/lib/serialization/components.ts | 24 +----- src/lib/serialization/serializer.ts | 78 ++++++++++--------- .../serializers/comments/comment-tag.ts | 2 - .../serializers/comments/comment.ts | 2 - .../serialization/serializers/decorator.ts | 2 - .../serializers/reflection-group.ts | 2 - .../serializers/sources/source-reference.ts | 1 - 7 files changed, 46 insertions(+), 65 deletions(-) diff --git a/src/lib/serialization/components.ts b/src/lib/serialization/components.ts index aeecf636e..b757d48f5 100644 --- a/src/lib/serialization/components.ts +++ b/src/lib/serialization/components.ts @@ -9,11 +9,12 @@ import { Serializer } from './serializer'; * Like [[Converter]] plugins each [[Serializer]] plugin defines a predicate that instructs if an * object can be serialized by it, this is done dynamically at runtime via a `supports` method. * - * Additionally, each [[Serializer]] plugin must defines a predicate that instructs the group + * Additionally, each [[Serializer]] plugin must define a predicate that instructs the group * it belongs to. * - * Grouping serializers is required due to performance, we don't need to check all the reflection - * serializers when we are looking for type (or any other) serializers. + * Serializers are grouped to improve performance when finding serializers that apply to a node, + * this makes it possible to skip the `supports` calls for `Type`s when searching for a + * `Reflection` and vise versa. */ export abstract class SerializerComponent extends AbstractComponent { /** @@ -26,14 +27,10 @@ export abstract class SerializerComponent extends AbstractComponent extends AbstractComponent When a serializer component extends a parent serializer component the SERIALIZE_GROUP - * and SERIALIZE_GROUP_SYMBOL are also inherited so child serializers of the same group do not - * need to declare a predicate nor a group. */ abstract serializeGroup(instance: unknown): boolean; - /** - * The symbol representing the group this serializer belongs to. - */ - abstract serializeGroupSymbol: any; /** * The priority this serializer should be executed with. @@ -75,8 +63,6 @@ export abstract class ReflectionSerializerComponent extend serializeGroup(instance: unknown): boolean { return instance instanceof Reflection; } - - serializeGroupSymbol = Reflection; } export abstract class TypeSerializerComponent extends SerializerComponent { @@ -86,6 +72,4 @@ export abstract class TypeSerializerComponent extends Serializer serializeGroup(instance: unknown): boolean { return instance instanceof Type; } - - serializeGroupSymbol = Type; } diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index aae90ef37..c253ad66c 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -20,12 +20,17 @@ export class Serializer extends ChildableComponent[] }>; - private routes!: any[]; + /** + * Serializers, sorted by their `serializeGroup` function to enable higher performance. + * + * TODO: This implementation results in a more complicated implementation, potentially + * without actual performance benefits due to the Map overhead. Does this actually help? + */ + private serializers!: Map<(instance: unknown) => boolean, SerializerComponent[]>; - initialize(): void { - this.router = new Map[] }>(); - this.routes = []; + initialize() { + super.initialize(); + this.serializers = new Map(); } addComponent & Component>( @@ -34,18 +39,15 @@ export class Serializer extends ChildableComponent v.symbol === component.serializeGroupSymbol - ) || { symbol: component.serializeGroupSymbol, group: [] }; - this.router.set(component.serializeGroup, match); - this.routes.push(component.serializeGroup); + if (!group) { + this.serializers.set(component.serializeGroup, group = []); } - match.group.push(component); - match.group.sort((a, b) => (b.priority || 0) - (a.priority || 0)); + + group.push(component); + group.sort((a, b) => (b.priority || 0) - (a.priority || 0)); } return component; @@ -57,36 +59,39 @@ export class Serializer extends ChildableComponent | undefined { const component = super.removeComponent(name); - const symbol = component && component.serializeGroupSymbol; - if (symbol) { - const values = Array.from(this.router.values()); - for (let i = 0, len = values.length; i < len; i++) { - const idx = values[i].group.findIndex(o => o === symbol); - if (idx > -1) { - values[i].group.splice(idx, 1); - break; - } + + if (component && component.serializeGroup) { + // Remove from the router + const group = this.serializers + .get(component.serializeGroup)! + .filter(comp => comp !== component); + + if (group.length) { + this.serializers.set(component.serializeGroup, group); + } else { + this.serializers.delete(component.serializeGroup); } } + return component; } removeAllComponents() { super.removeAllComponents(); - - this.router = new Map[] }>(); - this.routes = []; + this.serializers = new Map(); } toObject(value: any, obj?: any): any { - return this.findRoutes(value).reduce((result, curr) => curr.toObject(value, result), obj); + return this.findSerializers(value).reduce( + (result, curr) => curr.toObject(value, result), + obj + ); } /** * Same as toObject but emits [[ Serializer#EVENT_BEGIN ]] and [[ Serializer#EVENT_END ]] events. * @param value * @param eventData Partial information to set in the event - * @return {any} */ projectToObject(value: ProjectReflection, eventData?: { begin?: any; end?: any }): any { const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value); @@ -109,18 +114,19 @@ export class Serializer extends ChildableComponent[] { + private findSerializers(value: any): SerializerComponent[] { const routes: SerializerComponent[] = []; - for (let i = 0, len = this.routes.length; i < len; i++) { - if (this.routes[i](value)) { - const serializers = this.router.get(this.routes[i])!.group; - for (let serializer of serializers) { - if (serializer.supports(value)) { - routes.push(serializer); + + for (const [ groupSupports, components ] of this.serializers.entries()) { + if (groupSupports(value)) { + for (const component of components) { + if (component.supports(value)) { + routes.push(component); } } } } + return routes; } } diff --git a/src/lib/serialization/serializers/comments/comment-tag.ts b/src/lib/serialization/serializers/comments/comment-tag.ts index effbbc83c..679475b02 100644 --- a/src/lib/serialization/serializers/comments/comment-tag.ts +++ b/src/lib/serialization/serializers/comments/comment-tag.ts @@ -14,8 +14,6 @@ export class CommentTagSerializer extends SerializerComponent { return instance instanceof CommentTag; } - serializeGroupSymbol = CommentTag; - supports(t: unknown) { return true; } diff --git a/src/lib/serialization/serializers/comments/comment.ts b/src/lib/serialization/serializers/comments/comment.ts index 7fffba672..06620f69e 100644 --- a/src/lib/serialization/serializers/comments/comment.ts +++ b/src/lib/serialization/serializers/comments/comment.ts @@ -18,8 +18,6 @@ export class CommentSerializer extends SerializerComponent { return true; } - serializeGroupSymbol = Comment; - toObject(comment: Comment, obj?: any): any { obj = obj || {}; diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index f26fb3405..9baeec2ff 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -14,8 +14,6 @@ export class DecoratorContainerSerializer extends SerializerComponent Date: Tue, 1 Jan 2019 22:13:49 -0700 Subject: [PATCH 04/17] Strongly type the JSON output --- src/lib/models/index.ts | 2 + src/lib/models/sources/index.ts | 2 +- src/lib/models/types/array.ts | 2 +- src/lib/models/types/intrinsic.ts | 2 +- src/lib/models/types/reference.ts | 2 +- src/lib/models/types/reflection.ts | 2 +- src/lib/models/types/string-literal.ts | 2 +- src/lib/models/types/tuple.ts | 2 +- src/lib/models/types/type-operator.ts | 2 +- src/lib/models/types/unknown.ts | 2 +- src/lib/serialization/browser.ts | 351 ------------------ src/lib/serialization/components.ts | 5 +- src/lib/serialization/index.ts | 1 - src/lib/serialization/schema.ts | 247 ++++++++++++ src/lib/serialization/serializer.ts | 30 +- .../serializers/comments/comment-tag.ts | 15 +- .../serializers/comments/comment.ts | 10 +- .../serialization/serializers/decorator.ts | 25 +- .../models/source-reference-wrapper.ts | 2 +- .../serializers/reflection-group.ts | 20 +- .../serializers/reflections/abstract.ts | 32 +- .../serializers/reflections/container.ts | 18 +- .../serializers/reflections/declaration.ts | 27 +- .../serializers/reflections/parameter.ts | 13 +- .../serializers/reflections/project.ts | 3 +- .../serializers/reflections/signature.ts | 15 +- .../serializers/reflections/type-parameter.ts | 9 +- .../serializers/sources/source-reference.ts | 44 ++- .../serializers/types/abstract.ts | 12 +- .../serialization/serializers/types/array.ts | 17 +- .../serialization/serializers/types/index.ts | 3 +- .../serializers/types/intersection-union.ts | 23 -- .../serializers/types/intersection.ts | 24 ++ .../serializers/types/intrinsic.ts | 17 +- .../serializers/types/reference.ts | 23 +- .../serializers/types/reflection.ts | 54 +-- .../serializers/types/string-literal.ts | 12 +- .../serialization/serializers/types/tuple.ts | 9 +- .../serializers/types/type-operator.ts | 14 +- .../serializers/types/type-parameter.ts | 16 +- .../serialization/serializers/types/union.ts | 24 ++ .../serializers/types/unknown.ts | 17 +- 42 files changed, 552 insertions(+), 600 deletions(-) delete mode 100644 src/lib/serialization/browser.ts create mode 100644 src/lib/serialization/schema.ts delete mode 100644 src/lib/serialization/serializers/types/intersection-union.ts create mode 100644 src/lib/serialization/serializers/types/intersection.ts create mode 100644 src/lib/serialization/serializers/types/union.ts diff --git a/src/lib/models/index.ts b/src/lib/models/index.ts index da48d254d..cc2592725 100644 --- a/src/lib/models/index.ts +++ b/src/lib/models/index.ts @@ -2,3 +2,5 @@ export * from './reflections/index'; export * from './types/index'; export * from './comments/index'; export * from './sources/index'; +export * from './ReflectionGroup'; +export * from './ReflectionCategory'; diff --git a/src/lib/models/sources/index.ts b/src/lib/models/sources/index.ts index b65653d4a..189fe5ccc 100644 --- a/src/lib/models/sources/index.ts +++ b/src/lib/models/sources/index.ts @@ -1,2 +1,2 @@ export { SourceDirectory } from './directory'; -export { SourceFile } from './file'; +export { SourceFile, SourceReference } from './file'; diff --git a/src/lib/models/types/array.ts b/src/lib/models/types/array.ts index 46c042f5f..0440d885b 100644 --- a/src/lib/models/types/array.ts +++ b/src/lib/models/types/array.ts @@ -17,7 +17,7 @@ export class ArrayType extends Type { /** * The type name identifier. */ - readonly type: string = 'array'; + readonly type = 'array'; /** * Create a new TupleType instance. diff --git a/src/lib/models/types/intrinsic.ts b/src/lib/models/types/intrinsic.ts index 089ec1d11..2947f1871 100644 --- a/src/lib/models/types/intrinsic.ts +++ b/src/lib/models/types/intrinsic.ts @@ -16,7 +16,7 @@ export class IntrinsicType extends Type { /** * The type name identifier. */ - readonly type: string = 'intrinsic'; + readonly type = 'intrinsic'; /** * Create a new instance of IntrinsicType. diff --git a/src/lib/models/types/reference.ts b/src/lib/models/types/reference.ts index 820910fa3..ccc9c613d 100644 --- a/src/lib/models/types/reference.ts +++ b/src/lib/models/types/reference.ts @@ -12,7 +12,7 @@ export class ReferenceType extends Type { /** * The type name identifier. */ - readonly type: string = 'reference'; + readonly type = 'reference'; /** * The name of the referenced type. diff --git a/src/lib/models/types/reflection.ts b/src/lib/models/types/reflection.ts index 1b5645cc0..492c03d35 100644 --- a/src/lib/models/types/reflection.ts +++ b/src/lib/models/types/reflection.ts @@ -17,7 +17,7 @@ export class ReflectionType extends Type { /** * The type name identifier. */ - readonly type: string = 'reflection'; + readonly type = 'reflection'; /** * Create a new instance of ReflectionType. diff --git a/src/lib/models/types/string-literal.ts b/src/lib/models/types/string-literal.ts index efaed79b3..6f8e29587 100644 --- a/src/lib/models/types/string-literal.ts +++ b/src/lib/models/types/string-literal.ts @@ -16,7 +16,7 @@ export class StringLiteralType extends Type { /** * The type name identifier. */ - readonly type: string = 'stringLiteral'; + readonly type = 'stringLiteral'; /** * Create a new instance of StringLiteralType. diff --git a/src/lib/models/types/tuple.ts b/src/lib/models/types/tuple.ts index bc162f509..8778c71e7 100644 --- a/src/lib/models/types/tuple.ts +++ b/src/lib/models/types/tuple.ts @@ -16,7 +16,7 @@ export class TupleType extends Type { /** * The type name identifier. */ - readonly type: string = 'tuple'; + readonly type = 'tuple'; /** * Create a new TupleType instance. diff --git a/src/lib/models/types/type-operator.ts b/src/lib/models/types/type-operator.ts index badb139e4..75623f4ce 100644 --- a/src/lib/models/types/type-operator.ts +++ b/src/lib/models/types/type-operator.ts @@ -12,7 +12,7 @@ export class TypeOperatorType extends Type { /** * The type name identifier. */ - readonly type: string = 'typeOperator'; + readonly type = 'typeOperator'; target: Type; diff --git a/src/lib/models/types/unknown.ts b/src/lib/models/types/unknown.ts index affeb1945..5818636da 100644 --- a/src/lib/models/types/unknown.ts +++ b/src/lib/models/types/unknown.ts @@ -12,7 +12,7 @@ export class UnknownType extends Type { /** * The type name identifier. */ - readonly type: string = 'unknown'; + readonly type = 'unknown'; /** * Create a new instance of UnknownType. diff --git a/src/lib/serialization/browser.ts b/src/lib/serialization/browser.ts deleted file mode 100644 index 33b24041b..000000000 --- a/src/lib/serialization/browser.ts +++ /dev/null @@ -1,351 +0,0 @@ -/** - * Shared serialized symbols - * The shared symbols can be used in node or in a browser web application. - * - * There are 2 types of symbols: - * - Object - * - Container - * - * ## Object - * Object symbols (XXXObject) represents the final structure of a JSON object after it was - * serialized by the native typedoc serializers. It is a type composition of Container symbols. - * - * ## Container - * Container symbols (XXXContainer) are partial symbols used to compose an Object symbol. - * - * ## Object vs Container symbols - * While Container symbols might look redundant they are not, when an external serialization plugin - * is used it will, most likely, alter the structure of the output, the plugin can then use the - * Container symbols to expose custom Object symbols with minimal effort. - */ - -export interface ReflectionContainer { - id: number; - name: string; - kind: number; - kindString: string; - flags: ReflectionFlagsObject; - originalName?: string; -} - -export interface DefaultValueContainer { - defaultValue: string; -} - -export interface TypeContainer { - type: TypeObject; -} - -export interface TypeParameterContainer { - typeParameters: TypeContainer[]; -} - -export interface DecoratesContainer { - decorates: TypeObject[]; -} - -export interface DecoratorsContainer { - decorators: T[]; -} - -export interface SourceReferenceContainer { - sources: T[]; -} - -export interface GroupsContainer { - groups: T[]; -} - -export interface ContainerReflectionContainer { - children: TChildren[]; -} - -export interface CommentContainer { - comment: TComment; -} - -export interface SignatureReflectionContainer { - /** - * A type that points to the reflection that has been overwritten by this reflection. - * - * Applies to interface and class members. - */ - overwrites?: TypeObject; - - /** - * A type that points to the reflection this reflection has been inherited from. - * - * Applies to interface and class members. - */ - inheritedFrom?: TypeObject; - - /** - * A type that points to the reflection this reflection is the implementation of. - * - * Applies to class members. - */ - implementationOf?: TypeObject; - - parameters?: TParameters[]; -} - -export interface DeclarationReflectionContainer { - /** - * A list of call signatures attached to this declaration. - * - * TypeDoc creates one declaration per function that may contain ore or more - * signature reflections. - */ - signatures?: T[]; - - /** - * The index signature of this declaration. - */ - indexSignature?: T[]; - - /** - * The get signature of this declaration. - */ - getSignature?: T[]; - - /** - * The set signature of this declaration. - */ - setSignature?: T[]; - - /** - * A list of all types this reflection extends (e.g. the parent classes). - */ - extendedTypes?: TypeObject[]; - - /** - * A list of all types that extend this reflection (e.g. the subclasses). - */ - extendedBy?: TypeObject[]; - - /** - * A list of all types this reflection implements. - */ - implementedTypes?: TypeObject[]; - - /** - * A list of all types that implement this reflection. - */ - implementedBy?: TypeObject[]; -} - -export interface ReflectionObject - extends ReflectionContainer, - Partial>, - Partial, - Partial> {} - -export interface ParameterReflectionObject extends ReflectionObject, TypeContainer, DefaultValueContainer {} - -export interface ContainerReflectionObject - extends ReflectionObject, - Partial>, - Partial>, - ContainerReflectionContainer {} - -export interface DeclarationReflectionObject - extends ContainerReflectionObject, - DefaultValueContainer, - Partial, - Partial, - Partial>, - DeclarationReflectionContainer {} - -export interface SignatureReflectionObject - extends ReflectionObject, - Partial>, - Partial, - Partial {} - -export interface CommentObject { - shortText?: string; - text?: string; - returns?: string; - tags?: CommentTagObject[]; -} - -export interface CommentTagObject { - tag: string; - text: string; - param?: string; -} - -export interface DecoratorObject { - /** - * The name of the decorator being applied. - */ - name: string; - - /** - * The type declaring the decorator. - * Usually a ReferenceType instance pointing to the decorator function. - */ - type?: TypeObject; - - /** - * A named map of arguments the decorator is applied with. - */ - arguments?: any; -} - -export interface ProjectReflectionObject extends ContainerReflectionObject {} - -export interface SourceReferenceObject { - fileName: string; - line: number; - character: number; -} - -export interface TypeObject { - /** - * The type name identifier. - */ - type: - | 'void' - | 'array' - | 'intersection' - | 'intrinsic' - | 'reference' - | 'reflection' - | 'stringLiteral' - | 'tuple' - | 'typeParameter' - | 'union' - | 'unknown' - | string; - - // array - /** - * For Array type only, The type (T) of the array's elements. - */ - elementType?: TypeObject; - - // intersection - /** - * For intersection type only, the types the union consists of. - * For union type only, the types the union consists of. - */ - types?: TypeObject[]; - - // intrinsic, reference, typeParameter, unknown - /** - * For intrinsic type only, The name of the intrinsic type like `string` or `boolean`. - * - * For reference type only, The name of the referenced type. - * If the symbol cannot be found cause it's not part of the documentation this - * can be used to represent the type. - * - * For typeParameter type only, the name of the type. - * - * For unknown type only, the name of the type. - */ - name?: 'Object' | 'string' | 'number' | 'boolean' | 'this' | string; - - // reference - /** - * The reflection id for this type - */ - id?: number; - - /** - * For reference type only, The type arguments of this reference. - */ - typeArguments?: TypeObject[]; - - // reflection - /** - * For reflection type only, The reflection of the type. - */ - declaration?: ReflectionObject; - - // stringLiteral - /** - * For stringLiteral type only, The string literal value. - */ - value?: string; - - // tuple - /** - * For tuple type only, The ordered type elements of the tuple type. - */ - elements?: TypeObject[]; - - // typeParameter - /** - * For typeParameter type only, The constraint type for the generic type. - */ - constraint?: TypeObject; -} - -export interface ReflectionGroupObject { - /** - * The title, a string representation of the typescript kind, of this group. - */ - title: string; - - /** - * The original typescript kind of the children of this group. - */ - kind: number; - - /** - * A list of reflection id's for this group. - */ - children?: number[]; -} - -export interface ReflectionFlagsObject { - /** - * Is this a private member? - */ - isPrivate?: boolean; - - /** - * Is this a protected member? - */ - isProtected?: boolean; - - /** - * Is this a public member? - */ - isPublic?: boolean; - - /** - * Is this a static member? - */ - isStatic?: boolean; - - /** - * Is this member exported? - */ - isExported?: boolean; - - /** - * Is this a declaration from an external document? - */ - isExternal?: boolean; - - /** - * Whether this reflection is an optional component or not. - * - * Applies to function parameters and object members. - */ - isOptional?: boolean; - - /** - * Whether it's a rest parameter, like `foo(...params);`. - */ - isRest?: boolean; - - /** - * - */ - hasExportAssignment?: boolean; - - isConstructorProperty?: boolean; -} diff --git a/src/lib/serialization/components.ts b/src/lib/serialization/components.ts index b757d48f5..16cedb95c 100644 --- a/src/lib/serialization/components.ts +++ b/src/lib/serialization/components.ts @@ -2,6 +2,7 @@ import { Reflection, Type } from '../models'; import { AbstractComponent } from '../utils'; import { Serializer } from './serializer'; +import { ModelToObject } from './schema'; /** * Represents Serializer plugin component. @@ -48,12 +49,12 @@ export abstract class SerializerComponent extends AbstractComponent>): Partial>; } export abstract class ReflectionSerializerComponent extends SerializerComponent { diff --git a/src/lib/serialization/index.ts b/src/lib/serialization/index.ts index a39a0931f..7a3e2ecfa 100644 --- a/src/lib/serialization/index.ts +++ b/src/lib/serialization/index.ts @@ -13,7 +13,6 @@ export { ArrayTypeSerializer, DeclarationReflectionSerializer, IntersectionTypeSerializer, - IntersectionUnion, IntrinsicTypeSerializer, ParameterReflectionSerializer, ProjectReflectionSerializer, diff --git a/src/lib/serialization/schema.ts b/src/lib/serialization/schema.ts new file mode 100644 index 000000000..a6de333d4 --- /dev/null +++ b/src/lib/serialization/schema.ts @@ -0,0 +1,247 @@ +/** + * Documents the exported JSON schema. The root object is a [[JSONOutput.ProjectReflection]]. + */ + +/** */ +import * as M from '../models'; +import { SourceReferenceWrapper, DecoratorWrapper } from './serializers'; + +/** + * Describes the mapping from Model types to the corresponding JSON output type. + */ +export type ModelToObject = T extends Array ? _ModelToObject[] : _ModelToObject; + +// Order matters here. Some types are subtypes of other types. +type _ModelToObject = + // Reflections + T extends M.ReflectionGroup ? JSONOutput.ReflectionGroup : + T extends M.SignatureReflection ? JSONOutput.SignatureReflection : + T extends M.ParameterReflection ? JSONOutput.ParameterReflection : + T extends M.DeclarationReflection ? JSONOutput.DeclarationReflection | JSONOutput.ReflectionPointer : + T extends M.TypeParameterReflection ? JSONOutput.TypeParameterReflection : + T extends M.ProjectReflection ? JSONOutput.ProjectReflection : + T extends M.ContainerReflection ? JSONOutput.ContainerReflection : + T extends M.Reflection ? JSONOutput.Reflection : + // Types + T extends M.ArrayType ? JSONOutput.ArrayType : + T extends M.IntersectionType ? JSONOutput.IntersectionType : + T extends M.IntrinsicType ? JSONOutput.IntrinsicType : + T extends M.ReferenceType ? JSONOutput.ReferenceType : + T extends M.ReflectionType ? JSONOutput.ReflectionType : + T extends M.StringLiteralType ? JSONOutput.StringLiteralType : + T extends M.TupleType ? JSONOutput.TupleType : + T extends M.UnknownType ? JSONOutput.UnknownType : + T extends M.Type ? JSONOutput.SomeType : // Technically AbstractType, but the union is more useful + // Miscellaneous + T extends M.Comment ? JSONOutput.Comment : + T extends M.CommentTag ? JSONOutput.CommentTag : + T extends DecoratorWrapper ? JSONOutput.Decorator : + T extends SourceReferenceWrapper ? JSONOutput.SourceReference : + never; + +/** + * Contains interfaces which describe the JSON output. Each interface is related to a specific type of serializer. + * + * ## Plugins + * Plugins which modify the serialization process can use declaration merging + * to add custom properties to the exported interfaces. + * For example, if your custom serializer adds a property to all [[Reflection]] objects: + * ```ts + * declare module 'typedoc' { + * export namespace JSONOutput { + * export interface AbstractReflection { + * myCustomProp: boolean + * } + * } + * } + * ``` + * + * If a plugin defines a new Model type, [[ModelToObject]] will not pick up the serializer type. + * To fix this, use declaration merging to augment the [[Serializer]] class. + * ```ts + * declare module 'typedoc' { + * export interface Serializer { + * toObject(value: CustomModel, obj?: Partial): CustomOutput + * } + * } + * ``` + * + * For documentation on the JSON output properties, view the corresponding model. + */ +export namespace JSONOutput { + + // Reflections + + export interface ReflectionGroup { + title: M.ReflectionGroup['title']; + kind: M.ReflectionGroup['kind']; + children?: M.ReflectionGroup['children'][number]['id'][]; + } + + export interface SignatureReflection extends Reflection { + type?: ModelToObject; + overwrites?: ModelToObject; + inheritedFrom?: ModelToObject; + implementationOf?: ModelToObject; + } + + export interface ParameterReflection extends Reflection { + type?: ModelToObject; + defaultValue?: M.ParameterReflection['defaultValue']; + } + + export interface DeclarationReflection extends ContainerReflection { + type?: ModelToObject; + defaultValue?: M.DeclarationReflection['defaultValue']; + overwrites?: ModelToObject; + inheritedFrom?: ModelToObject; + extendedTypes?: ModelToObject; + extendedBy?: ModelToObject; + implementedTypes?: ModelToObject; + implementedBy?: ModelToObject; + implementationOf?: ModelToObject; + } + + export interface TypeParameterReflection extends Reflection { + type?: ModelToObject; + } + + // Nothing extra yet. + export interface ProjectReflection extends ContainerReflection { } + + export interface ContainerReflection extends Reflection { + groups?: ModelToObject; + sources?: ModelToObject; + } + + /** + * If a 3rd party serializer creates a loop when serializing, a pointer will be created + * instead of re-serializing the [[DeclarationReflection]] + */ + export interface ReflectionPointer { + id: M.Reflection['id']; + } + + export interface Reflection { + id: M.Reflection['id']; + name: M.Reflection['name']; + originalName?: M.Reflection['originalName']; + kind: M.Reflection['kind']; + kindString: M.Reflection['kindString']; + flags: ReflectionFlags; + comment?: ModelToObject; + decorates?: ModelToObject; + decorators?: ModelToObject; + } + + // Types + + export type SomeType = + | ArrayType + | IntersectionType + | UnionType + | IntrinsicType + | ReferenceType + | ReflectionType + | StringLiteralType + | TupleType + | TypeOperatorType + | TypeParameterType + | UnionType + | UnknownType; + + export interface ArrayType extends Type { + elementType: ModelToObject; + } + + export interface IntersectionType extends Type { + types: ModelToObject; + } + + export interface UnionType extends Type { + types: ModelToObject; + } + + export interface IntrinsicType extends Type { + name: M.IntrinsicType['name']; + } + + export interface ReferenceType extends Type { + name: M.ReferenceType['name']; + id?: number; + typeArguments?: ModelToObject; + } + + export interface ReflectionType extends Type { + declaration?: ModelToObject; + } + + export interface StringLiteralType extends Type { + value: M.StringLiteralType['value']; + } + + export interface TupleType extends Type { + elements?: ModelToObject; + } + + export interface TypeOperatorType extends Type { + operator: M.TypeOperatorType['operator']; + target: ModelToObject; + } + + export interface TypeParameterType extends Type { + name: M.TypeParameterType['name']; + constraint?: ModelToObject; + } + + export interface UnknownType extends Type { + name: M.UnknownType['name']; + } + + export interface Type { + type: T['type']; + } + + // Miscellaneous + + export interface ReflectionFlags { + isPrivate?: boolean; + isProtected?: boolean; + isPublic?: boolean; + isStatic?: boolean; + isExported?: boolean; + isExternal?: boolean; + isOptional?: boolean; + isRest?: boolean; + hasExportAssignment?: boolean; + isConstructorProperty?: boolean; + isAbstract?: boolean; + isConst?: boolean; + isLet?: boolean; + } + + export interface Comment { + shortText?: M.Comment['shortText']; + text?: M.Comment['text']; + returns?: M.Comment['returns']; + tags?: ModelToObject; + } + + export interface CommentTag { + tag: M.CommentTag['tagName']; + text: M.CommentTag['text']; + param?: M.CommentTag['paramName']; + } + + export interface SourceReference { + fileName: M.SourceReference['fileName']; + line: M.SourceReference['line']; + character: M.SourceReference['character']; + } + + export interface Decorator { + name: M.Decorator['name']; + type?: ModelToObject; + arguments?: M.Decorator['arguments']; + } +} diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index c253ad66c..a5acf80a3 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -5,6 +5,7 @@ import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; import { SerializeEvent } from './events'; +import { ModelToObject } from './schema'; @Component({ name: 'serializer', internal: true, childClass: SerializerComponent }) export class Serializer extends ChildableComponent> { @@ -22,9 +23,6 @@ export class Serializer extends ChildableComponent boolean, SerializerComponent[]>; @@ -43,11 +41,11 @@ export class Serializer extends ChildableComponent (b.priority || 0) - (a.priority || 0)); + group.sort((a, b) => b.priority - a.priority); } return component; @@ -62,9 +60,7 @@ export class Serializer extends ChildableComponent comp !== component); + const group = this.serializers.get(component.serializeGroup)!.filter(comp => comp !== component); if (group.length) { this.serializers.set(component.serializeGroup, group); @@ -81,11 +77,11 @@ export class Serializer extends ChildableComponent curr.toObject(value, result), - obj - ); + toObject(value: T, obj?: Partial>): ModelToObject { + // Note: This type *could* potentially lie, if a serializer declares a partial type but fails to provide + // the defined property, but the benefit of being mostly typed is probably worth it. + return this.findSerializers(value) + .reduce((result, curr) => curr.toObject(value, result), obj) as ModelToObject; } /** @@ -93,7 +89,7 @@ export class Serializer extends ChildableComponent { const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value); if (eventData && eventData.begin) { @@ -114,10 +110,10 @@ export class Serializer extends ChildableComponent[] { - const routes: SerializerComponent[] = []; + private findSerializers(value: T): SerializerComponent[] { + const routes: SerializerComponent[] = []; - for (const [ groupSupports, components ] of this.serializers.entries()) { + for (const [groupSupports, components] of this.serializers.entries()) { if (groupSupports(value)) { for (const component of components) { if (component.supports(value)) { diff --git a/src/lib/serialization/serializers/comments/comment-tag.ts b/src/lib/serialization/serializers/comments/comment-tag.ts index 679475b02..7c6ce5bfd 100644 --- a/src/lib/serialization/serializers/comments/comment-tag.ts +++ b/src/lib/serialization/serializers/comments/comment-tag.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { CommentTag } from '../../../models'; import { SerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:comment-tag' }) export class CommentTagSerializer extends SerializerComponent { @@ -18,16 +19,16 @@ export class CommentTagSerializer extends SerializerComponent { return true; } - toObject(tag: CommentTag, obj?: any): any { - obj = obj || {}; - - obj.tag = tag.tagName; - obj.text = tag.text; + toObject(tag: CommentTag, obj: Partial = {}): JSONOutput.CommentTag { + const result: JSONOutput.CommentTag = { + tag: tag.tagName, + text: tag.text + }; if (tag.paramName) { - obj.param = tag.paramName; + result.param = tag.paramName; } - return obj; + return { ...obj, ...result }; } } diff --git a/src/lib/serialization/serializers/comments/comment.ts b/src/lib/serialization/serializers/comments/comment.ts index 06620f69e..33f382336 100644 --- a/src/lib/serialization/serializers/comments/comment.ts +++ b/src/lib/serialization/serializers/comments/comment.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { Comment } from '../../../models'; import { SerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:comment' }) export class CommentSerializer extends SerializerComponent { @@ -18,9 +19,7 @@ export class CommentSerializer extends SerializerComponent { return true; } - toObject(comment: Comment, obj?: any): any { - obj = obj || {}; - + toObject(comment: Comment, obj: Partial = {}): JSONOutput.Comment { if (comment.shortText) { obj.shortText = comment.shortText; } @@ -30,11 +29,10 @@ export class CommentSerializer extends SerializerComponent { if (comment.returns) { obj.returns = comment.returns; } - if (comment.tags && comment.tags.length) { - obj.tags = []; - comment.tags.forEach(tag => obj.tags.push(this.owner.toObject(tag))); + obj.tags = comment.tags.map(tag => this.owner.toObject(tag)); } + return obj; } } diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index 9baeec2ff..a28242821 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -2,6 +2,7 @@ import { Component } from '../../utils/component'; import { SerializerComponent } from '../components'; import { DecoratorWrapper } from './models/decorator-wrapper'; +import { JSONOutput } from '../schema'; @Component({ name: 'serializer:decorator-container' }) export class DecoratorContainerSerializer extends SerializerComponent { @@ -14,28 +15,24 @@ export class DecoratorContainerSerializer extends SerializerComponent): JSONOutput.Decorator { + const result: JSONOutput.Decorator = { + ...obj, + name: decorator.name + }; if (decorator.type) { - obj.type = this.owner.toObject(decorator.type); + result.type = this.owner.toObject(decorator.type); } if (decorator.arguments) { - obj.arguments = decorator.arguments; + result.arguments = decorator.arguments; } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/models/source-reference-wrapper.ts b/src/lib/serialization/serializers/models/source-reference-wrapper.ts index 02254d294..b62b41082 100644 --- a/src/lib/serialization/serializers/models/source-reference-wrapper.ts +++ b/src/lib/serialization/serializers/models/source-reference-wrapper.ts @@ -1,4 +1,4 @@ -import { SourceReference } from '../../../models/sources/file'; +import { SourceReference } from '../../../models'; /** * An internal concrete implementation for the [[ SourceReference ]] interface diff --git a/src/lib/serialization/serializers/reflection-group.ts b/src/lib/serialization/serializers/reflection-group.ts index 06d08b95d..c34a82fbc 100644 --- a/src/lib/serialization/serializers/reflection-group.ts +++ b/src/lib/serialization/serializers/reflection-group.ts @@ -2,6 +2,7 @@ import { Component } from '../../utils/component'; import { ReflectionGroup } from '../../models/ReflectionGroup'; import { SerializerComponent } from '../components'; +import { JSONOutput } from '../schema'; @Component({ name: 'serializer:reflection-group' }) export class ReflectionGroupSerializer extends SerializerComponent { @@ -14,26 +15,21 @@ export class ReflectionGroupSerializer extends SerializerComponent): JSONOutput.ReflectionGroup { + const result: JSONOutput.ReflectionGroup = { + ...obj, title: group.title, kind: group.kind - }); + }; if (group.children && group.children.length > 0) { - obj.children = group.children.map(child => child.id); + result.children = group.children.map(child => child.id); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/reflections/abstract.ts b/src/lib/serialization/serializers/reflections/abstract.ts index d4e17736b..b335a3029 100644 --- a/src/lib/serialization/serializers/reflections/abstract.ts +++ b/src/lib/serialization/serializers/reflections/abstract.ts @@ -4,6 +4,7 @@ import { Reflection, TraverseProperty } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { DecoratorWrapper } from '../models'; import { ReflectionFlags } from '../../../models/reflections/abstract'; +import { JSONOutput, ModelToObject } from '../../schema'; @Component({ name: 'serializer:reflection' }) export class ReflectionSerializer extends ReflectionSerializerComponent { @@ -13,37 +14,36 @@ export class ReflectionSerializer extends ReflectionSerializerComponent>): JSONOutput.Reflection { + const result: JSONOutput.Reflection = { + ...obj, id: reflection.id, name: reflection.name, kind: reflection.kind, kindString: reflection.kindString, - flags: {} // TODO: remove if no flags - }); + flags: {} + }; if (reflection.originalName !== reflection.name) { - obj.originalName = reflection.originalName; + result.originalName = reflection.originalName; } if (reflection.comment) { - obj.comment = this.owner.toObject(reflection.comment); + result.comment = this.owner.toObject(reflection.comment); } for (const key of Object.getOwnPropertyNames(ReflectionFlags.prototype)) { if (reflection.flags[key] === true) { - obj.flags[key] = true; + result.flags[key] = true; } } if (reflection.decorates && reflection.decorates.length > 0) { - obj.decorates = reflection.decorates.map(t => this.owner.toObject(t)); + result.decorates = reflection.decorates.map(t => this.owner.toObject(t)); } if (reflection.decorators && reflection.decorators.length > 0) { - obj.decorators = reflection.decorators.map(d => this.owner.toObject(new DecoratorWrapper(d))); + result.decorators = reflection.decorators.map(d => this.owner.toObject(new DecoratorWrapper(d))); } reflection.traverse((child, property) => { @@ -51,13 +51,13 @@ export class ReflectionSerializer extends ReflectionSerializerComponent { @@ -10,15 +11,22 @@ export class ContainerReflectionSerializer extends ReflectionSerializerComponent return t instanceof ContainerReflection; } - toObject(container: ContainerReflection, obj?: any): any { - obj = obj || {}; + /** + * Will be run after [[ReflectionSerializer]] so will be passed the result of that serialization. + * @param container + * @param obj + */ + toObject(container: ContainerReflection, obj: JSONOutput.Reflection): JSONOutput.ContainerReflection { + const result: JSONOutput.ContainerReflection = { + ...obj + }; if (container.groups && container.groups.length > 0) { - obj.groups = container.groups.map(group => this.owner.toObject(group)); + result.groups = container.groups.map(group => this.owner.toObject(group)); } if (container.sources && container.sources.length > 0) { - obj.sources = container.sources.map(source => + result.sources = container.sources.map(source => this.owner.toObject( new SourceReferenceWrapper({ fileName: source.fileName, @@ -29,6 +37,6 @@ export class ContainerReflectionSerializer extends ReflectionSerializerComponent ); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/reflections/declaration.ts b/src/lib/serialization/serializers/reflections/declaration.ts index d13899012..7ae4bc4a5 100644 --- a/src/lib/serialization/serializers/reflections/declaration.ts +++ b/src/lib/serialization/serializers/reflections/declaration.ts @@ -3,6 +3,7 @@ import { DeclarationReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:declaration-reflection' }) export class DeclarationReflectionSerializer extends ReflectionSerializerComponent { @@ -12,45 +13,47 @@ export class DeclarationReflectionSerializer extends ReflectionSerializerCompone return t instanceof DeclarationReflection; } - toObject(declaration: DeclarationReflection, obj?: any): any { - obj = obj || {}; + toObject(declaration: DeclarationReflection, obj: JSONOutput.ContainerReflection): JSONOutput.DeclarationReflection { + const result: JSONOutput.DeclarationReflection = { + ...obj + }; if (declaration.type) { - obj.type = this.owner.toObject(declaration.type); + result.type = this.owner.toObject(declaration.type); } if (declaration.defaultValue) { - obj.defaultValue = declaration.defaultValue; + result.defaultValue = declaration.defaultValue; } if (declaration.overwrites) { - obj.overwrites = this.owner.toObject(declaration.overwrites); + result.overwrites = this.owner.toObject(declaration.overwrites); } if (declaration.inheritedFrom) { - obj.inheritedFrom = this.owner.toObject(declaration.inheritedFrom); + result.inheritedFrom = this.owner.toObject(declaration.inheritedFrom); } if (declaration.extendedTypes) { - obj.extendedTypes = declaration.extendedTypes.map(t => this.owner.toObject(t)); + result.extendedTypes = declaration.extendedTypes.map(t => this.owner.toObject(t)); } if (declaration.extendedBy) { - obj.extendedBy = declaration.extendedBy.map(t => this.owner.toObject(t)); + result.extendedBy = declaration.extendedBy.map(t => this.owner.toObject(t)); } if (declaration.implementedTypes) { - obj.implementedTypes = declaration.implementedTypes.map(t => this.owner.toObject(t)); + result.implementedTypes = declaration.implementedTypes.map(t => this.owner.toObject(t)); } if (declaration.implementedBy) { - obj.implementedBy = declaration.implementedBy.map(t => this.owner.toObject(t)); + result.implementedBy = declaration.implementedBy.map(t => this.owner.toObject(t)); } if (declaration.implementationOf) { - obj.implementationOf = this.owner.toObject(declaration.implementationOf); + result.implementationOf = this.owner.toObject(declaration.implementationOf); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/reflections/parameter.ts b/src/lib/serialization/serializers/reflections/parameter.ts index 1eaabc4c2..fcdc9ac61 100644 --- a/src/lib/serialization/serializers/reflections/parameter.ts +++ b/src/lib/serialization/serializers/reflections/parameter.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { ParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:parameter-reflection' }) export class ParameterReflectionSerializer extends ReflectionSerializerComponent { @@ -9,17 +10,19 @@ export class ParameterReflectionSerializer extends ReflectionSerializerComponent return t instanceof ParameterReflection; } - toObject(parameter: ParameterReflection, obj?: any): any { - obj = obj || {}; + toObject(parameter: ParameterReflection, obj: JSONOutput.Reflection): JSONOutput.ParameterReflection { + const result: JSONOutput.ParameterReflection = { + ...obj + }; if (parameter.type) { - obj.type = this.owner.toObject(parameter.type); + result.type = this.owner.toObject(parameter.type); } if (parameter.defaultValue) { - obj.defaultValue = parameter.defaultValue; + result.defaultValue = parameter.defaultValue; } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/reflections/project.ts b/src/lib/serialization/serializers/reflections/project.ts index 648074b5d..1c9f9ea60 100644 --- a/src/lib/serialization/serializers/reflections/project.ts +++ b/src/lib/serialization/serializers/reflections/project.ts @@ -3,6 +3,7 @@ import { ProjectReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:project-reflection' }) export class ProjectReflectionSerializer extends ReflectionSerializerComponent { @@ -12,7 +13,7 @@ export class ProjectReflectionSerializer extends ReflectionSerializerComponent

{ @@ -9,25 +10,25 @@ export class SignatureReflectionSerializer extends ReflectionSerializerComponent return t instanceof SignatureReflection; } - toObject(signature: SignatureReflection, obj?: any): any { - obj = obj || {}; + toObject(signature: SignatureReflection, obj: JSONOutput.Reflection): JSONOutput.SignatureReflection { + const result: JSONOutput.SignatureReflection = { ...obj }; if (signature.type) { - obj.type = this.owner.toObject(signature.type); + result.type = this.owner.toObject(signature.type); } if (signature.overwrites) { - obj.overwrites = this.owner.toObject(signature.overwrites); + result.overwrites = this.owner.toObject(signature.overwrites); } if (signature.inheritedFrom) { - obj.inheritedFrom = this.owner.toObject(signature.inheritedFrom); + result.inheritedFrom = this.owner.toObject(signature.inheritedFrom); } if (signature.implementationOf) { - obj.implementationOf = this.owner.toObject(signature.implementationOf); + result.implementationOf = this.owner.toObject(signature.implementationOf); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/reflections/type-parameter.ts b/src/lib/serialization/serializers/reflections/type-parameter.ts index 3a26441f1..17b290f8b 100644 --- a/src/lib/serialization/serializers/reflections/type-parameter.ts +++ b/src/lib/serialization/serializers/reflections/type-parameter.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { TypeParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:type-parameter-reflection' }) export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent< @@ -11,13 +12,13 @@ export class TypeParameterReflectionSerializer extends ReflectionSerializerCompo return t instanceof TypeParameterReflection; } - toObject(typeParameter: TypeParameterReflection, obj?: any): any { - obj = obj || {}; + toObject(typeParameter: TypeParameterReflection, obj: JSONOutput.Reflection): JSONOutput.TypeParameterReflection { + const result: JSONOutput.TypeParameterReflection = { ...obj }; if (typeParameter.type) { - obj.type = this.owner.toObject(typeParameter.type); + result.type = this.owner.toObject(typeParameter.type); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/sources/source-reference.ts b/src/lib/serialization/serializers/sources/source-reference.ts index 1e82388b8..a079d74e4 100644 --- a/src/lib/serialization/serializers/sources/source-reference.ts +++ b/src/lib/serialization/serializers/sources/source-reference.ts @@ -1,29 +1,27 @@ import { Component } from '../../../utils/component'; import { SerializerComponent } from '../../components'; -import { SourceReferenceWrapper } from '../models/source-reference-wrapper'; +import { SourceReferenceWrapper } from '../models'; +import { JSONOutput } from '../../schema'; -@Component({name: 'serializer:source-reference-container'}) +@Component({ name: 'serializer:source-reference-container' }) export class SourceReferenceContainerSerializer extends SerializerComponent { - - static PRIORITY = 1000; - - serializeGroup(instance: unknown) { - return instance instanceof SourceReferenceWrapper; - } - - supports(t: unknown) { - return t instanceof SourceReferenceWrapper; - } - - toObject(sourceReferenceContainer: SourceReferenceWrapper, obj?: any): any { - obj = obj || {}; - - const sourceReference = sourceReferenceContainer.sourceReference; - obj.fileName = sourceReference.fileName; - obj.line = sourceReference.line; - obj.character = sourceReference.character; - - return obj; - } + static PRIORITY = 1000; + + serializeGroup(instance: unknown) { + return instance instanceof SourceReferenceWrapper; + } + + supports(_: unknown) { + return true; + } + + toObject({ sourceReference: ref }: SourceReferenceWrapper, obj?: Partial): JSONOutput.SourceReference { + return { + ...obj, + fileName: ref.fileName, + line: ref.line, + character: ref.character + }; + } } diff --git a/src/lib/serialization/serializers/types/abstract.ts b/src/lib/serialization/serializers/types/abstract.ts index 4bbd2d2bd..19ef91e3f 100644 --- a/src/lib/serialization/serializers/types/abstract.ts +++ b/src/lib/serialization/serializers/types/abstract.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { Type } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:type' }) export class TypeSerializer extends TypeSerializerComponent { @@ -11,11 +12,10 @@ export class TypeSerializer extends TypeSerializerComponent { return t instanceof Type; } - toObject(type: Type, obj?: any): any { - obj = obj || {}; - - obj.type = type.type; - - return obj; + toObject(type: Type, obj?: Partial>): JSONOutput.Type { + return { + ...obj, + type: type.type + }; } } diff --git a/src/lib/serialization/serializers/types/array.ts b/src/lib/serialization/serializers/types/array.ts index c58e1b384..2a0c2b636 100644 --- a/src/lib/serialization/serializers/types/array.ts +++ b/src/lib/serialization/serializers/types/array.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { ArrayType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:array-type' }) export class ArrayTypeSerializer extends TypeSerializerComponent { @@ -9,11 +10,15 @@ export class ArrayTypeSerializer extends TypeSerializerComponent { return t instanceof ArrayType; } - toObject(arrayType: ArrayType, obj?: any): any { - obj = obj || {}; - - obj.elementType = this.owner.toObject(arrayType.elementType); - - return obj; + /** + * Will be run after [[TypeSerializer]] so `type` will already be set. + * @param type + * @param obj + */ + toObject(type: ArrayType, obj: Pick): JSONOutput.ArrayType { + return { + ...obj, + elementType: this.owner.toObject(type.elementType) + }; } } diff --git a/src/lib/serialization/serializers/types/index.ts b/src/lib/serialization/serializers/types/index.ts index 6131bdf3e..3e5418b2c 100644 --- a/src/lib/serialization/serializers/types/index.ts +++ b/src/lib/serialization/serializers/types/index.ts @@ -1,6 +1,6 @@ export * from './abstract'; export * from './array'; -export * from './intersection-union'; +export * from './intersection'; export * from './intrinsic'; export * from './reference'; export * from './reflection'; @@ -8,4 +8,5 @@ export * from './string-literal'; export * from './tuple'; export * from './type-operator'; export * from './type-parameter'; +export * from './union'; export * from './unknown'; diff --git a/src/lib/serialization/serializers/types/intersection-union.ts b/src/lib/serialization/serializers/types/intersection-union.ts deleted file mode 100644 index bb0bb52a0..000000000 --- a/src/lib/serialization/serializers/types/intersection-union.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Component } from '../../../utils/component'; -import { IntersectionType, UnionType } from '../../../models'; - -import { TypeSerializerComponent } from '../../components'; - -export type IntersectionUnion = IntersectionType | UnionType; - -@Component({ name: 'serializer:intersection-type' }) -export class IntersectionTypeSerializer extends TypeSerializerComponent { - supports(t: unknown) { - return t instanceof IntersectionType || t instanceof UnionType; - } - - toObject(intersectionUnion: IntersectionUnion, obj?: any): any { - obj = obj || {}; - - if (intersectionUnion.types && intersectionUnion.types.length) { - obj.types = intersectionUnion.types.map(t => this.owner.toObject(t)); - } - - return obj; - } -} diff --git a/src/lib/serialization/serializers/types/intersection.ts b/src/lib/serialization/serializers/types/intersection.ts new file mode 100644 index 000000000..6ddf2467d --- /dev/null +++ b/src/lib/serialization/serializers/types/intersection.ts @@ -0,0 +1,24 @@ +import { Component } from '../../../utils/component'; +import { IntersectionType } from '../../../models'; + +import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; + +@Component({ name: 'serializer:intersection-type' }) +export class IntersectionTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof IntersectionType; + } + + /** + * Will be run after [[TypeSerializer]] so `type` will already be set. + * @param type + * @param obj + */ + toObject(type: IntersectionType, obj: Pick): JSONOutput.IntersectionType { + return { + ...obj, + types: type.types.map(t => this.owner.toObject(t)) + }; + } +} diff --git a/src/lib/serialization/serializers/types/intrinsic.ts b/src/lib/serialization/serializers/types/intrinsic.ts index 16cbafeed..e46e96675 100644 --- a/src/lib/serialization/serializers/types/intrinsic.ts +++ b/src/lib/serialization/serializers/types/intrinsic.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { IntrinsicType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:intrinsic-type' }) export class IntrinsicTypeSerializer extends TypeSerializerComponent { @@ -9,11 +10,15 @@ export class IntrinsicTypeSerializer extends TypeSerializerComponent): JSONOutput.IntrinsicType { + return { + ...obj, + name: type.name + }; } } diff --git a/src/lib/serialization/serializers/types/reference.ts b/src/lib/serialization/serializers/types/reference.ts index a1c72de0d..674ff8562 100644 --- a/src/lib/serialization/serializers/types/reference.ts +++ b/src/lib/serialization/serializers/types/reference.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { ReferenceType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:reference-type' }) export class ReferenceTypeSerializer extends TypeSerializerComponent { @@ -9,19 +10,21 @@ export class ReferenceTypeSerializer extends TypeSerializerComponent & Partial + ): JSONOutput.ReferenceType { + if (type.reflection) { + obj.id = type.reflection.id; } - if (reference.typeArguments && reference.typeArguments.length > 0) { - obj.typeArguments = reference.typeArguments.map(t => this.owner.toObject(t)); + if (type.typeArguments && type.typeArguments.length > 0) { + obj.typeArguments = type.typeArguments.map(t => this.owner.toObject(t)); } - return obj; + return { + ...obj, + name: type.name + }; } } diff --git a/src/lib/serialization/serializers/types/reflection.ts b/src/lib/serialization/serializers/types/reflection.ts index fa9ff0814..9d5bf9aba 100644 --- a/src/lib/serialization/serializers/types/reflection.ts +++ b/src/lib/serialization/serializers/types/reflection.ts @@ -2,40 +2,44 @@ import { Component } from '../../../utils/component'; import { DeclarationReflection, ReflectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:reflection-type' }) export class ReflectionTypeSerializer extends TypeSerializerComponent { - private declaration?: DeclarationReflection; + private visited = new Set(); supports(t: unknown) { return t instanceof ReflectionType; } - toObject(reference: ReflectionType, obj?: any): any { - obj = obj || {}; - - if (reference.declaration) { - // Because `DeclarationReflection` has reference to multiple types objectifying a declaration - // on a type might fall into a loop trap (cyclic dependency). - // The TypeDoc code does not apply logic that can create this scenario but a 3rd party plugin - // might do that unintentionally so a protection is in place. - - if (this.declaration === reference.declaration) { - // if we're here it means that the reference type is rendered for the 2nd time - // by the declaration it is referencing, we will render a pointer-only declaration. - obj.declaration = { id: reference.declaration.id }; - } else { - // mark this declaration to trap a loop - this.declaration = reference.declaration; - - // objectify the declaration - obj.declaration = this.owner.toObject(reference.declaration); - } - - // no more declaration rendering, remove marker. - this.declaration = undefined; + toObject(reference: ReflectionType, obj: Pick): JSONOutput.ReflectionType { + const result: JSONOutput.ReflectionType = { + ...obj + }; + + // Because `DeclarationReflection` has reference to multiple types objectifying a declaration + // on a type might fall into a loop trap (cyclic dependency). + // The TypeDoc code does not apply logic that can create this scenario but a 3rd party plugin + // might do that unintentionally so a protection is in place. + // TODO: Should this protection really be here? It seems like it might make more sense to + // do this check in the DeclarationReflection serializer... if it should even be checked. In the + // old `.toObject` code, it wasn't checked. + + if (this.visited.has(reference.declaration)) { + // if we're here it means that the reference type is rendered for the 2nd time + // by the declaration it is referencing, we will render a pointer-only declaration. + result.declaration = { id: reference.declaration.id }; + } else { + // mark this declaration to trap a loop + this.visited.add(reference.declaration); + + // objectify the declaration + result.declaration = this.owner.toObject(reference.declaration); } - return obj; + // no more declaration rendering, remove marker. + this.visited.delete(reference.declaration); + + return result; } } diff --git a/src/lib/serialization/serializers/types/string-literal.ts b/src/lib/serialization/serializers/types/string-literal.ts index 6c0c93447..f4590ac4e 100644 --- a/src/lib/serialization/serializers/types/string-literal.ts +++ b/src/lib/serialization/serializers/types/string-literal.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { StringLiteralType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:string-literal-type' }) export class StringLiteralTypeSerializer extends TypeSerializerComponent { @@ -9,11 +10,10 @@ export class StringLiteralTypeSerializer extends TypeSerializerComponent): JSONOutput.StringLiteralType { + return { + ...obj, + value: type.value + }; } } diff --git a/src/lib/serialization/serializers/types/tuple.ts b/src/lib/serialization/serializers/types/tuple.ts index 3c130bdcf..d42f10e4e 100644 --- a/src/lib/serialization/serializers/types/tuple.ts +++ b/src/lib/serialization/serializers/types/tuple.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { TupleType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:tuple-type' }) export class TupleTypeSerializer extends TypeSerializerComponent { @@ -9,13 +10,13 @@ export class TupleTypeSerializer extends TypeSerializerComponent { return t instanceof TupleType; } - toObject(tuple: TupleType, obj?: any): any { - obj = obj || {}; + toObject(tuple: TupleType, obj: Pick): JSONOutput.TupleType { + const result: JSONOutput.TupleType = { ...obj }; if (tuple.elements && tuple.elements.length > 0) { - obj.elements = tuple.elements.map(t => this.owner.toObject(t)); + result.elements = tuple.elements.map(t => this.owner.toObject(t)); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/types/type-operator.ts b/src/lib/serialization/serializers/types/type-operator.ts index 9ea5565b5..ee53063b6 100644 --- a/src/lib/serialization/serializers/types/type-operator.ts +++ b/src/lib/serialization/serializers/types/type-operator.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { TypeOperatorType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:type-operator-type' }) export class TypeOperatorTypeSerializer extends TypeSerializerComponent { @@ -9,12 +10,11 @@ export class TypeOperatorTypeSerializer extends TypeSerializerComponent): JSONOutput.TypeOperatorType { + return { + ...obj, + operator: type.operator, + target: this.owner.toObject(type.target) + }; } } diff --git a/src/lib/serialization/serializers/types/type-parameter.ts b/src/lib/serialization/serializers/types/type-parameter.ts index 9f241f4af..a18068cd4 100644 --- a/src/lib/serialization/serializers/types/type-parameter.ts +++ b/src/lib/serialization/serializers/types/type-parameter.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { TypeParameterType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:type-parameter-type' }) export class TypeParameterTypeSerializer extends TypeSerializerComponent { @@ -9,15 +10,16 @@ export class TypeParameterTypeSerializer extends TypeSerializerComponent): JSONOutput.TypeParameterType { + const result: JSONOutput.TypeParameterType = { + ...obj, + name: type.name + }; - obj.name = typeParameter.name; - - if (typeParameter.constraint) { - obj.constraint = this.owner.toObject(typeParameter.constraint); + if (type.constraint) { + result.constraint = this.owner.toObject(type.constraint); } - return obj; + return result; } } diff --git a/src/lib/serialization/serializers/types/union.ts b/src/lib/serialization/serializers/types/union.ts new file mode 100644 index 000000000..4648b9c03 --- /dev/null +++ b/src/lib/serialization/serializers/types/union.ts @@ -0,0 +1,24 @@ +import { Component } from '../../../utils/component'; +import { UnionType } from '../../../models'; + +import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; + +@Component({ name: 'serializer:union-type' }) +export class UnionTypeSerializer extends TypeSerializerComponent { + supports(t: unknown) { + return t instanceof UnionType; + } + + /** + * Will be run after [[TypeSerializer]] so `type` will already be set. + * @param type + * @param obj + */ + toObject(type: UnionType, obj: Pick): JSONOutput.UnionType { + return { + ...obj, + types: type.types.map(t => this.owner.toObject(t)) + }; + } +} diff --git a/src/lib/serialization/serializers/types/unknown.ts b/src/lib/serialization/serializers/types/unknown.ts index 84c7e4acf..a9435468e 100644 --- a/src/lib/serialization/serializers/types/unknown.ts +++ b/src/lib/serialization/serializers/types/unknown.ts @@ -2,6 +2,7 @@ import { Component } from '../../../utils/component'; import { UnknownType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { JSONOutput } from '../../schema'; @Component({ name: 'serializer:unknown-type' }) export class UnknownTypeSerializer extends TypeSerializerComponent { @@ -9,11 +10,15 @@ export class UnknownTypeSerializer extends TypeSerializerComponent return t instanceof UnknownType; } - toObject(unknown: UnknownType, obj?: any): any { - obj = obj || {}; - - obj.name = unknown.name; - - return obj; + /** + * Will be run after [[TypeSerializer]] so `type` will already be set. + * @param type + * @param obj + */ + toObject(type: UnknownType, obj: Pick): JSONOutput.UnknownType { + return { + ...obj, + name: type.name + }; } } From 461315b277151e12b2316d4f8b8b457baeb57075 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Tue, 1 Jan 2019 22:30:48 -0700 Subject: [PATCH 05/17] Improve serialization types --- src/lib/serialization/events.ts | 14 ++++++-- src/lib/serialization/serializer.ts | 35 +++++++++++-------- .../serialization/serializers/decorator.ts | 2 +- .../serializers/reflection-group.ts | 2 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/lib/serialization/events.ts b/src/lib/serialization/events.ts index a8918eb60..13c3d9a90 100644 --- a/src/lib/serialization/events.ts +++ b/src/lib/serialization/events.ts @@ -1,5 +1,14 @@ import { Event } from '../utils/events'; import { ProjectReflection } from '../models'; +import { JSONOutput } from './schema'; + +/** + * Optional data associated with the [[SerializeEvent]]. + */ +export interface SerializeEventData { + outputDirectory?: string; + outputFile?: string; +} /** * An event emitted by the [[Serializer]] class at the very beginning and @@ -24,10 +33,11 @@ export class SerializeEvent extends Event { */ outputFile?: string; - output: any; + output: Partial; - constructor(name: string, project: ProjectReflection) { + constructor(name: string, project: ProjectReflection, output: Partial) { super(name); this.project = project; + this.output = output; } } diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index a5acf80a3..1291a2eda 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -4,7 +4,7 @@ import { Application } from '../application'; import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; -import { SerializeEvent } from './events'; +import { SerializeEvent, SerializeEventData } from './events'; import { ModelToObject } from './schema'; @Component({ name: 'serializer', internal: true, childClass: SerializerComponent }) @@ -80,8 +80,10 @@ export class Serializer extends ChildableComponent(value: T, obj?: Partial>): ModelToObject { // Note: This type *could* potentially lie, if a serializer declares a partial type but fails to provide // the defined property, but the benefit of being mostly typed is probably worth it. - return this.findSerializers(value) - .reduce((result, curr) => curr.toObject(value, result), obj) as ModelToObject; + return this.findSerializers(value).reduce( + (result, curr) => curr.toObject(value, result), + obj + ) as ModelToObject; } /** @@ -89,22 +91,25 @@ export class Serializer extends ChildableComponent { - const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value); - - if (eventData && eventData.begin) { - Object.assign(eventBegin, eventData.begin); + projectToObject( + value: ProjectReflection, + eventData: { begin?: SerializeEventData; end?: SerializeEventData } = {} + ): ModelToObject { + + const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value, {}); + if (eventData.begin) { + eventBegin.outputDirectory = eventData.begin.outputDirectory; + eventBegin.outputFile = eventData.begin.outputFile; } - let project: any = (eventBegin.output = {}); - this.trigger(eventBegin); - project = this.toObject(value, project); - const eventEnd = new SerializeEvent(Serializer.EVENT_END, value); - if (eventData && eventData.end) { - Object.assign(eventEnd, eventData.end); + const project = this.toObject(value, eventBegin.output); + + const eventEnd = new SerializeEvent(Serializer.EVENT_END, value, project); + if (eventData.end) { + eventBegin.outputDirectory = eventData.end.outputDirectory; + eventBegin.outputFile = eventData.end.outputFile; } - eventEnd.output = project; this.trigger(eventEnd); return project; diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index a28242821..d3527ae10 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -11,7 +11,7 @@ export class DecoratorContainerSerializer extends SerializerComponent Date: Tue, 1 Jan 2019 22:51:12 -0700 Subject: [PATCH 06/17] Export JSON schema --- src/index.ts | 1 + src/lib/serialization/index.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 36c8fb168..a3e3b99a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,3 +11,4 @@ export { Renderer } from './lib/output/renderer'; export { DefaultTheme } from './lib/output/themes/DefaultTheme'; export { NavigationItem } from './lib/output/models/NavigationItem'; export { UrlMapping } from './lib/output/models/UrlMapping'; +export { JSONOutput } from './lib/serialization'; diff --git a/src/lib/serialization/index.ts b/src/lib/serialization/index.ts index 7a3e2ecfa..b36d8e446 100644 --- a/src/lib/serialization/index.ts +++ b/src/lib/serialization/index.ts @@ -32,4 +32,4 @@ export { export { SerializeEvent } from './events'; -export * from './browser'; +export { JSONOutput } from './schema'; From 1146c3ff87720eb553c03a4a3d52ceb852629e90 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 2 Jan 2019 15:08:54 -0700 Subject: [PATCH 07/17] Update declaration merging instructions See https://github.com/Microsoft/TypeScript/issues/18877 --- src/lib/serialization/schema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/serialization/schema.ts b/src/lib/serialization/schema.ts index a6de333d4..9e249fb76 100644 --- a/src/lib/serialization/schema.ts +++ b/src/lib/serialization/schema.ts @@ -47,7 +47,7 @@ type _ModelToObject = * to add custom properties to the exported interfaces. * For example, if your custom serializer adds a property to all [[Reflection]] objects: * ```ts - * declare module 'typedoc' { + * declare module 'typedoc/dist/lib/serialization/schema' { * export namespace JSONOutput { * export interface AbstractReflection { * myCustomProp: boolean @@ -59,7 +59,7 @@ type _ModelToObject = * If a plugin defines a new Model type, [[ModelToObject]] will not pick up the serializer type. * To fix this, use declaration merging to augment the [[Serializer]] class. * ```ts - * declare module 'typedoc' { + * declare module 'typedoc/dist/lib/serialization/serializer' { * export interface Serializer { * toObject(value: CustomModel, obj?: Partial): CustomOutput * } From b7f570ea63fed197e93e9b4149fbd1eff8a934f4 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 2 Jan 2019 15:16:00 -0700 Subject: [PATCH 08/17] Correct spec rebuild script --- gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gruntfile.js b/gruntfile.js index edb6528e8..fef6892d0 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -140,7 +140,7 @@ module.exports = function(grunt) var src = app.expandInputFiles([path]); var out = Path.join(base, directory, 'specs.json'); var result = app.convert(src); - var data = JSON.stringify(result.toObject(), null, ' '); + var data = JSON.stringify(app.serializer.projectToObject(result), null, ' '); data = data.split(TypeDoc.normalizePath(base)).join('%BASE%'); FS.writeFileSync(out, data); }); From fb3eef43cafa4acad1555473f9e9819b12fa4d09 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 17:04:09 -0600 Subject: [PATCH 09/17] Update serialization types to with category changes --- src/lib/serialization/index.ts | 1 - src/lib/serialization/schema.ts | 8 +++ .../serializers/reflection-category.ts | 50 ++++++++----------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/lib/serialization/index.ts b/src/lib/serialization/index.ts index 4f3358c06..2cb3b8861 100644 --- a/src/lib/serialization/index.ts +++ b/src/lib/serialization/index.ts @@ -13,7 +13,6 @@ export { ArrayTypeSerializer, DeclarationReflectionSerializer, IntersectionTypeSerializer, - IntersectionUnion, IntrinsicTypeSerializer, ParameterReflectionSerializer, ProjectReflectionSerializer, diff --git a/src/lib/serialization/schema.ts b/src/lib/serialization/schema.ts index 9e249fb76..3ee1e0fc2 100644 --- a/src/lib/serialization/schema.ts +++ b/src/lib/serialization/schema.ts @@ -15,6 +15,7 @@ export type ModelToObject = T extends Array ? _ModelToObject[] : type _ModelToObject = // Reflections T extends M.ReflectionGroup ? JSONOutput.ReflectionGroup : + T extends M.ReflectionCategory ? JSONOutput.ReflectionCategory : T extends M.SignatureReflection ? JSONOutput.SignatureReflection : T extends M.ParameterReflection ? JSONOutput.ParameterReflection : T extends M.DeclarationReflection ? JSONOutput.DeclarationReflection | JSONOutput.ReflectionPointer : @@ -76,6 +77,12 @@ export namespace JSONOutput { title: M.ReflectionGroup['title']; kind: M.ReflectionGroup['kind']; children?: M.ReflectionGroup['children'][number]['id'][]; + categories?: ModelToObject; + } + + export interface ReflectionCategory { + title: M.ReflectionCategory['title']; + children?: M.ReflectionCategory['children'][number]['id'][]; } export interface SignatureReflection extends Reflection { @@ -111,6 +118,7 @@ export namespace JSONOutput { export interface ContainerReflection extends Reflection { groups?: ModelToObject; + categories?: ModelToObject; sources?: ModelToObject; } diff --git a/src/lib/serialization/serializers/reflection-category.ts b/src/lib/serialization/serializers/reflection-category.ts index 5cc9a59e3..4337f5ebc 100644 --- a/src/lib/serialization/serializers/reflection-category.ts +++ b/src/lib/serialization/serializers/reflection-category.ts @@ -2,41 +2,33 @@ import { Component } from '../../utils/component'; import { ReflectionCategory } from '../../models/ReflectionCategory'; import { SerializerComponent } from '../components'; +import { JSONOutput } from '../schema'; -@Component({name: 'serializer:reflection-category'}) +@Component({ name: 'serializer:reflection-category' }) export class ReflectionCategorySerializer extends SerializerComponent { + static PRIORITY = 1000; - static PRIORITY = 1000; - - /** - * Filter for instances of [[ReflectionCategory]] - */ - serializeGroup(instance: any): boolean { - return instance instanceof ReflectionCategory; - } - - serializeGroupSymbol = ReflectionCategory; - - initialize(): void { - super.initialize(); - } + /** + * Filter for instances of [[ReflectionCategory]] + */ + serializeGroup(instance: any): boolean { + return instance instanceof ReflectionCategory; + } - supports(r: unknown) { - return r instanceof ReflectionCategory; - } + supports(r: unknown) { + return r instanceof ReflectionCategory; + } - toObject(category: ReflectionCategory, obj?: any): any { - obj = obj || {}; + toObject(category: ReflectionCategory, obj?: Partial): JSONOutput.ReflectionCategory { + const result: JSONOutput.ReflectionCategory = { + ...obj, + title: category.title + }; - Object.assign(obj, { - title: category.title - }); + if (category.children && category.children.length > 0) { + result.children = category.children.map(child => child.id); + } - if (category.children && category.children.length > 0) { - obj.children = category.children.map( child => child.id ); + return result; } - - return obj; - } - } From 39ad773467fad7424ac03604ecdebb42de2c8756 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 19:43:45 -0600 Subject: [PATCH 10/17] Remove @Component from serializers --- src/lib/application.ts | 2 +- src/lib/serialization/components.ts | 10 ++- src/lib/serialization/serializer.ts | 75 ++++--------------- .../serializers/comments/comment-tag.ts | 2 - .../serializers/comments/comment.ts | 2 - .../serialization/serializers/decorator.ts | 3 - .../serializers/reflection-category.ts | 2 - .../serializers/reflection-group.ts | 2 - .../serializers/reflections/abstract.ts | 2 - .../serializers/reflections/container.ts | 2 - .../serializers/reflections/declaration.ts | 2 - .../serializers/reflections/parameter.ts | 2 - .../serializers/reflections/project.ts | 2 - .../serializers/reflections/signature.ts | 2 - .../serializers/reflections/type-parameter.ts | 2 - .../serializers/sources/source-reference.ts | 3 - .../serializers/types/abstract.ts | 2 - .../serialization/serializers/types/array.ts | 2 - .../serializers/types/intersection.ts | 2 - .../serializers/types/intrinsic.ts | 2 - .../serializers/types/reference.ts | 2 - .../serializers/types/reflection.ts | 2 - .../serializers/types/string-literal.ts | 2 - .../serialization/serializers/types/tuple.ts | 2 - .../serializers/types/type-operator.ts | 2 - .../serializers/types/type-parameter.ts | 2 - .../serialization/serializers/types/union.ts | 2 - .../serializers/types/unknown.ts | 2 - 28 files changed, 24 insertions(+), 115 deletions(-) diff --git a/src/lib/application.ts b/src/lib/application.ts index f72a13f5f..96bfe868c 100644 --- a/src/lib/application.ts +++ b/src/lib/application.ts @@ -97,8 +97,8 @@ export class Application extends ChildableComponent('converter', Converter); - this.serializer = this.addComponent('serializer', Serializer); this.renderer = this.addComponent('renderer', Renderer); this.plugins = this.addComponent('plugins', PluginHost); this.options = this.addComponent('options', Options); diff --git a/src/lib/serialization/components.ts b/src/lib/serialization/components.ts index 16cedb95c..a376e6753 100644 --- a/src/lib/serialization/components.ts +++ b/src/lib/serialization/components.ts @@ -1,5 +1,4 @@ import { Reflection, Type } from '../models'; -import { AbstractComponent } from '../utils'; import { Serializer } from './serializer'; import { ModelToObject } from './schema'; @@ -17,7 +16,12 @@ import { ModelToObject } from './schema'; * this makes it possible to skip the `supports` calls for `Type`s when searching for a * `Reflection` and vise versa. */ -export abstract class SerializerComponent extends AbstractComponent { +export abstract class SerializerComponent { + /** + * Set when the SerializerComponent is added to the serializer. + */ + protected owner!: Serializer; + /** * The priority this serializer should be executed with. * A higher priority means the [[Serializer]] will be applied earlier. @@ -54,7 +58,7 @@ export abstract class SerializerComponent extends AbstractComponent>): Partial>; + abstract toObject(item: T, obj?: object): Partial>; } export abstract class ReflectionSerializerComponent extends SerializerComponent { diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index 1291a2eda..567073cde 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -1,14 +1,11 @@ -import { ChildableComponent } from '../utils'; -import { Component, ComponentClass } from '../utils/component'; -import { Application } from '../application'; +import { EventDispatcher } from '../utils'; import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; import { SerializeEvent, SerializeEventData } from './events'; import { ModelToObject } from './schema'; -@Component({ name: 'serializer', internal: true, childClass: SerializerComponent }) -export class Serializer extends ChildableComponent> { +export class Serializer extends EventDispatcher { /** * Triggered when the [[Serializer]] begins transforming a project. * @event EVENT_BEGIN @@ -24,66 +21,27 @@ export class Serializer extends ChildableComponent boolean, SerializerComponent[]>; + private serializers = new Map<(instance: unknown) => boolean, SerializerComponent[]>(); - initialize() { - super.initialize(); - this.serializers = new Map(); - } - - addComponent & Component>( - name: string, - componentClass: T | ComponentClass - ): T { - const component = super.addComponent(name, componentClass); - - if (component.serializeGroup) { - let group = this.serializers.get(component.serializeGroup); - - if (!group) { - this.serializers.set(component.serializeGroup, (group = [])); - } + addSerializer(serializer: SerializerComponent): void { + let group = this.serializers.get(serializer.serializeGroup); - group.push(component); - group.sort((a, b) => b.priority - a.priority); + if (!group) { + this.serializers.set(serializer.serializeGroup, (group = [])); } - return component; + serializer['owner'] = this; + group.push(serializer); + group.sort((a, b) => b.priority - a.priority); } - /** - * Remove a child component from the registry. - * @param name The name the component registered as - */ - removeComponent(name: string): SerializerComponent | undefined { - const component = super.removeComponent(name); - - if (component && component.serializeGroup) { - // Remove from the router - const group = this.serializers.get(component.serializeGroup)!.filter(comp => comp !== component); - - if (group.length) { - this.serializers.set(component.serializeGroup, group); - } else { - this.serializers.delete(component.serializeGroup); - } - } - - return component; - } - - removeAllComponents() { - super.removeAllComponents(); - this.serializers = new Map(); - } - - toObject(value: T, obj?: Partial>): ModelToObject { + toObject(value: T, init: object = {}): ModelToObject { // Note: This type *could* potentially lie, if a serializer declares a partial type but fails to provide // the defined property, but the benefit of being mostly typed is probably worth it. - return this.findSerializers(value).reduce( + return this.findSerializers(value).reduce( (result, curr) => curr.toObject(value, result), - obj - ) as ModelToObject; + init + ); } /** @@ -95,7 +53,6 @@ export class Serializer extends ChildableComponent { - const eventBegin = new SerializeEvent(Serializer.EVENT_BEGIN, value, {}); if (eventData.begin) { eventBegin.outputDirectory = eventData.begin.outputDirectory; @@ -116,7 +73,7 @@ export class Serializer extends ChildableComponent(value: T): SerializerComponent[] { - const routes: SerializerComponent[] = []; + const routes: SerializerComponent[] = []; for (const [groupSupports, components] of this.serializers.entries()) { if (groupSupports(value)) { @@ -128,6 +85,6 @@ export class Serializer extends ChildableComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/comments/comment.ts b/src/lib/serialization/serializers/comments/comment.ts index 33f382336..9bd5890e4 100644 --- a/src/lib/serialization/serializers/comments/comment.ts +++ b/src/lib/serialization/serializers/comments/comment.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { Comment } from '../../../models'; import { SerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:comment' }) export class CommentSerializer extends SerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index d3527ae10..815861ecb 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -1,10 +1,7 @@ -import { Component } from '../../utils/component'; - import { SerializerComponent } from '../components'; import { DecoratorWrapper } from './models/decorator-wrapper'; import { JSONOutput } from '../schema'; -@Component({ name: 'serializer:decorator-container' }) export class DecoratorContainerSerializer extends SerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/reflection-category.ts b/src/lib/serialization/serializers/reflection-category.ts index 4337f5ebc..a1b2fc45d 100644 --- a/src/lib/serialization/serializers/reflection-category.ts +++ b/src/lib/serialization/serializers/reflection-category.ts @@ -1,10 +1,8 @@ -import { Component } from '../../utils/component'; import { ReflectionCategory } from '../../models/ReflectionCategory'; import { SerializerComponent } from '../components'; import { JSONOutput } from '../schema'; -@Component({ name: 'serializer:reflection-category' }) export class ReflectionCategorySerializer extends SerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/reflection-group.ts b/src/lib/serialization/serializers/reflection-group.ts index f4cb74d46..13ae33995 100644 --- a/src/lib/serialization/serializers/reflection-group.ts +++ b/src/lib/serialization/serializers/reflection-group.ts @@ -1,10 +1,8 @@ -import { Component } from '../../utils/component'; import { ReflectionGroup } from '../../models/ReflectionGroup'; import { SerializerComponent } from '../components'; import { JSONOutput } from '../schema'; -@Component({ name: 'serializer:reflection-group' }) export class ReflectionGroupSerializer extends SerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/reflections/abstract.ts b/src/lib/serialization/serializers/reflections/abstract.ts index b335a3029..ffeec1a51 100644 --- a/src/lib/serialization/serializers/reflections/abstract.ts +++ b/src/lib/serialization/serializers/reflections/abstract.ts @@ -1,4 +1,3 @@ -import { Component } from '../../../utils/component'; import { Reflection, TraverseProperty } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; @@ -6,7 +5,6 @@ import { DecoratorWrapper } from '../models'; import { ReflectionFlags } from '../../../models/reflections/abstract'; import { JSONOutput, ModelToObject } from '../../schema'; -@Component({ name: 'serializer:reflection' }) export class ReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/reflections/container.ts b/src/lib/serialization/serializers/reflections/container.ts index 95d195436..49110b91e 100644 --- a/src/lib/serialization/serializers/reflections/container.ts +++ b/src/lib/serialization/serializers/reflections/container.ts @@ -1,11 +1,9 @@ -import { Component } from '../../../utils/component'; import { ContainerReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { SourceReferenceWrapper } from '../models'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:container-reflection' }) export class ContainerReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { return t instanceof ContainerReflection; diff --git a/src/lib/serialization/serializers/reflections/declaration.ts b/src/lib/serialization/serializers/reflections/declaration.ts index 7ae4bc4a5..199f649fa 100644 --- a/src/lib/serialization/serializers/reflections/declaration.ts +++ b/src/lib/serialization/serializers/reflections/declaration.ts @@ -1,11 +1,9 @@ -import { Component } from '../../../utils/component'; import { DeclarationReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:declaration-reflection' }) export class DeclarationReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent diff --git a/src/lib/serialization/serializers/reflections/parameter.ts b/src/lib/serialization/serializers/reflections/parameter.ts index fcdc9ac61..e3fe4ee1d 100644 --- a/src/lib/serialization/serializers/reflections/parameter.ts +++ b/src/lib/serialization/serializers/reflections/parameter.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { ParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:parameter-reflection' }) export class ParameterReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { return t instanceof ParameterReflection; diff --git a/src/lib/serialization/serializers/reflections/project.ts b/src/lib/serialization/serializers/reflections/project.ts index 1c9f9ea60..5452295a0 100644 --- a/src/lib/serialization/serializers/reflections/project.ts +++ b/src/lib/serialization/serializers/reflections/project.ts @@ -1,11 +1,9 @@ -import { Component } from '../../../utils/component'; import { ProjectReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:project-reflection' }) export class ProjectReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent diff --git a/src/lib/serialization/serializers/reflections/signature.ts b/src/lib/serialization/serializers/reflections/signature.ts index 8513bc9f9..fb462de1b 100644 --- a/src/lib/serialization/serializers/reflections/signature.ts +++ b/src/lib/serialization/serializers/reflections/signature.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { SignatureReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:signature-reflection' }) export class SignatureReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { return t instanceof SignatureReflection; diff --git a/src/lib/serialization/serializers/reflections/type-parameter.ts b/src/lib/serialization/serializers/reflections/type-parameter.ts index 17b290f8b..7bce9d0ad 100644 --- a/src/lib/serialization/serializers/reflections/type-parameter.ts +++ b/src/lib/serialization/serializers/reflections/type-parameter.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { TypeParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:type-parameter-reflection' }) export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent< TypeParameterReflection > { diff --git a/src/lib/serialization/serializers/sources/source-reference.ts b/src/lib/serialization/serializers/sources/source-reference.ts index a079d74e4..7221eecad 100644 --- a/src/lib/serialization/serializers/sources/source-reference.ts +++ b/src/lib/serialization/serializers/sources/source-reference.ts @@ -1,10 +1,7 @@ -import { Component } from '../../../utils/component'; - import { SerializerComponent } from '../../components'; import { SourceReferenceWrapper } from '../models'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:source-reference-container' }) export class SourceReferenceContainerSerializer extends SerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/types/abstract.ts b/src/lib/serialization/serializers/types/abstract.ts index 19ef91e3f..6ef147451 100644 --- a/src/lib/serialization/serializers/types/abstract.ts +++ b/src/lib/serialization/serializers/types/abstract.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { Type } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:type' }) export class TypeSerializer extends TypeSerializerComponent { static PRIORITY = 1000; diff --git a/src/lib/serialization/serializers/types/array.ts b/src/lib/serialization/serializers/types/array.ts index 2a0c2b636..4d3231cac 100644 --- a/src/lib/serialization/serializers/types/array.ts +++ b/src/lib/serialization/serializers/types/array.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { ArrayType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:array-type' }) export class ArrayTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof ArrayType; diff --git a/src/lib/serialization/serializers/types/intersection.ts b/src/lib/serialization/serializers/types/intersection.ts index 6ddf2467d..a8c8781a8 100644 --- a/src/lib/serialization/serializers/types/intersection.ts +++ b/src/lib/serialization/serializers/types/intersection.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { IntersectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:intersection-type' }) export class IntersectionTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof IntersectionType; diff --git a/src/lib/serialization/serializers/types/intrinsic.ts b/src/lib/serialization/serializers/types/intrinsic.ts index e46e96675..956656b16 100644 --- a/src/lib/serialization/serializers/types/intrinsic.ts +++ b/src/lib/serialization/serializers/types/intrinsic.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { IntrinsicType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:intrinsic-type' }) export class IntrinsicTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof IntrinsicType; diff --git a/src/lib/serialization/serializers/types/reference.ts b/src/lib/serialization/serializers/types/reference.ts index 674ff8562..6e304e551 100644 --- a/src/lib/serialization/serializers/types/reference.ts +++ b/src/lib/serialization/serializers/types/reference.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { ReferenceType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:reference-type' }) export class ReferenceTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof ReferenceType; diff --git a/src/lib/serialization/serializers/types/reflection.ts b/src/lib/serialization/serializers/types/reflection.ts index 9d5bf9aba..3c869a6b5 100644 --- a/src/lib/serialization/serializers/types/reflection.ts +++ b/src/lib/serialization/serializers/types/reflection.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { DeclarationReflection, ReflectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:reflection-type' }) export class ReflectionTypeSerializer extends TypeSerializerComponent { private visited = new Set(); diff --git a/src/lib/serialization/serializers/types/string-literal.ts b/src/lib/serialization/serializers/types/string-literal.ts index f4590ac4e..0ce588340 100644 --- a/src/lib/serialization/serializers/types/string-literal.ts +++ b/src/lib/serialization/serializers/types/string-literal.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { StringLiteralType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:string-literal-type' }) export class StringLiteralTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof StringLiteralType; diff --git a/src/lib/serialization/serializers/types/tuple.ts b/src/lib/serialization/serializers/types/tuple.ts index d42f10e4e..0e6592d69 100644 --- a/src/lib/serialization/serializers/types/tuple.ts +++ b/src/lib/serialization/serializers/types/tuple.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { TupleType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:tuple-type' }) export class TupleTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TupleType; diff --git a/src/lib/serialization/serializers/types/type-operator.ts b/src/lib/serialization/serializers/types/type-operator.ts index ee53063b6..696772039 100644 --- a/src/lib/serialization/serializers/types/type-operator.ts +++ b/src/lib/serialization/serializers/types/type-operator.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { TypeOperatorType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:type-operator-type' }) export class TypeOperatorTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TypeOperatorType; diff --git a/src/lib/serialization/serializers/types/type-parameter.ts b/src/lib/serialization/serializers/types/type-parameter.ts index a18068cd4..e3e99788f 100644 --- a/src/lib/serialization/serializers/types/type-parameter.ts +++ b/src/lib/serialization/serializers/types/type-parameter.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { TypeParameterType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:type-parameter-type' }) export class TypeParameterTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TypeParameterType; diff --git a/src/lib/serialization/serializers/types/union.ts b/src/lib/serialization/serializers/types/union.ts index 4648b9c03..57d8cf770 100644 --- a/src/lib/serialization/serializers/types/union.ts +++ b/src/lib/serialization/serializers/types/union.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { UnionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:union-type' }) export class UnionTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof UnionType; diff --git a/src/lib/serialization/serializers/types/unknown.ts b/src/lib/serialization/serializers/types/unknown.ts index a9435468e..cf606daea 100644 --- a/src/lib/serialization/serializers/types/unknown.ts +++ b/src/lib/serialization/serializers/types/unknown.ts @@ -1,10 +1,8 @@ -import { Component } from '../../../utils/component'; import { UnknownType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; import { JSONOutput } from '../../schema'; -@Component({ name: 'serializer:unknown-type' }) export class UnknownTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof UnknownType; From b353c4d87d9aa2ef78c07f403b5becdaa2ed3967 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 20:11:02 -0600 Subject: [PATCH 11/17] Fix compiler errors --- src/lib/serialization/components.ts | 14 ++++-- src/lib/serialization/serializer.ts | 73 +++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/lib/serialization/components.ts b/src/lib/serialization/components.ts index a376e6753..661b25f51 100644 --- a/src/lib/serialization/components.ts +++ b/src/lib/serialization/components.ts @@ -17,17 +17,21 @@ import { ModelToObject } from './schema'; * `Reflection` and vise versa. */ export abstract class SerializerComponent { - /** - * Set when the SerializerComponent is added to the serializer. - */ - protected owner!: Serializer; - /** * The priority this serializer should be executed with. * A higher priority means the [[Serializer]] will be applied earlier. */ static PRIORITY = 0; + constructor(owner: Serializer) { + this.owner = owner; + } + + /** + * Set when the SerializerComponent is added to the serializer. + */ + protected owner: Serializer; + /** * A high-level predicate filtering which group this serializer belongs to. * This is a high-level filter before the [[SerializerComponent.supports]] predicate filter. diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index 567073cde..88ac9b0ea 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -4,6 +4,31 @@ import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; import { SerializeEvent, SerializeEventData } from './events'; import { ModelToObject } from './schema'; +import { + CommentTagSerializer, + CommentSerializer, + ContainerReflectionSerializer, + DeclarationReflectionSerializer, + ParameterReflectionSerializer, + SignatureReflectionSerializer, + TypeParameterReflectionSerializer, + ProjectReflectionSerializer, + SourceReferenceContainerSerializer, + ArrayTypeSerializer, + IntersectionTypeSerializer, + IntrinsicTypeSerializer, + ReferenceTypeSerializer, + ReflectionTypeSerializer, + StringLiteralTypeSerializer, + TupleTypeSerializer, + TypeOperatorTypeSerializer, + TypeParameterTypeSerializer, + UnionTypeSerializer, + UnknownTypeSerializer, + DecoratorContainerSerializer, + ReflectionCategorySerializer, + ReflectionGroupSerializer +} from './serializers'; export class Serializer extends EventDispatcher { /** @@ -23,6 +48,11 @@ export class Serializer extends EventDispatcher { */ private serializers = new Map<(instance: unknown) => boolean, SerializerComponent[]>(); + constructor() { + super(); + addSerializers(this); + } + addSerializer(serializer: SerializerComponent): void { let group = this.serializers.get(serializer.serializeGroup); @@ -30,7 +60,6 @@ export class Serializer extends EventDispatcher { this.serializers.set(serializer.serializeGroup, (group = [])); } - serializer['owner'] = this; group.push(serializer); group.sort((a, b) => b.priority - a.priority); } @@ -38,10 +67,8 @@ export class Serializer extends EventDispatcher { toObject(value: T, init: object = {}): ModelToObject { // Note: This type *could* potentially lie, if a serializer declares a partial type but fails to provide // the defined property, but the benefit of being mostly typed is probably worth it. - return this.findSerializers(value).reduce( - (result, curr) => curr.toObject(value, result), - init - ); + // TypeScript errors out if init is correctly typed as `Partial>` + return this.findSerializers(value).reduce((result, curr) => curr.toObject(value, result), init); } /** @@ -88,3 +115,39 @@ export class Serializer extends EventDispatcher { return routes as any; } } + +const serializerComponents: (new (owner: Serializer) => SerializerComponent)[] = [ + CommentTagSerializer, + CommentSerializer, + + ContainerReflectionSerializer, + DeclarationReflectionSerializer, + ParameterReflectionSerializer, + ProjectReflectionSerializer, + SignatureReflectionSerializer, + TypeParameterReflectionSerializer, + + SourceReferenceContainerSerializer, + + ArrayTypeSerializer, + IntersectionTypeSerializer, + IntrinsicTypeSerializer, + ReferenceTypeSerializer, + ReflectionTypeSerializer, + StringLiteralTypeSerializer, + TupleTypeSerializer, + TypeOperatorTypeSerializer, + TypeParameterTypeSerializer, + UnionTypeSerializer, + UnknownTypeSerializer, + + DecoratorContainerSerializer, + ReflectionCategorySerializer, + ReflectionGroupSerializer +]; + +function addSerializers(owner: Serializer) { + for (const component of serializerComponents) { + owner.addSerializer(new component(owner)); + } +} From 555c66d11a27b67ce9b19ffec6f970468920a885 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 20:57:46 -0600 Subject: [PATCH 12/17] Clean up imports --- src/lib/serialization/serializer.ts | 80 ++++++++++------------------- 1 file changed, 28 insertions(+), 52 deletions(-) diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index 88ac9b0ea..eedf37858 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -4,31 +4,7 @@ import { ProjectReflection } from '../models'; import { SerializerComponent } from './components'; import { SerializeEvent, SerializeEventData } from './events'; import { ModelToObject } from './schema'; -import { - CommentTagSerializer, - CommentSerializer, - ContainerReflectionSerializer, - DeclarationReflectionSerializer, - ParameterReflectionSerializer, - SignatureReflectionSerializer, - TypeParameterReflectionSerializer, - ProjectReflectionSerializer, - SourceReferenceContainerSerializer, - ArrayTypeSerializer, - IntersectionTypeSerializer, - IntrinsicTypeSerializer, - ReferenceTypeSerializer, - ReflectionTypeSerializer, - StringLiteralTypeSerializer, - TupleTypeSerializer, - TypeOperatorTypeSerializer, - TypeParameterTypeSerializer, - UnionTypeSerializer, - UnknownTypeSerializer, - DecoratorContainerSerializer, - ReflectionCategorySerializer, - ReflectionGroupSerializer -} from './serializers'; +import * as S from './serializers'; export class Serializer extends EventDispatcher { /** @@ -117,33 +93,33 @@ export class Serializer extends EventDispatcher { } const serializerComponents: (new (owner: Serializer) => SerializerComponent)[] = [ - CommentTagSerializer, - CommentSerializer, - - ContainerReflectionSerializer, - DeclarationReflectionSerializer, - ParameterReflectionSerializer, - ProjectReflectionSerializer, - SignatureReflectionSerializer, - TypeParameterReflectionSerializer, - - SourceReferenceContainerSerializer, - - ArrayTypeSerializer, - IntersectionTypeSerializer, - IntrinsicTypeSerializer, - ReferenceTypeSerializer, - ReflectionTypeSerializer, - StringLiteralTypeSerializer, - TupleTypeSerializer, - TypeOperatorTypeSerializer, - TypeParameterTypeSerializer, - UnionTypeSerializer, - UnknownTypeSerializer, - - DecoratorContainerSerializer, - ReflectionCategorySerializer, - ReflectionGroupSerializer + S.CommentTagSerializer, + S.CommentSerializer, + + S.ContainerReflectionSerializer, + S.DeclarationReflectionSerializer, + S.ParameterReflectionSerializer, + S.ProjectReflectionSerializer, + S.SignatureReflectionSerializer, + S.TypeParameterReflectionSerializer, + + S.SourceReferenceContainerSerializer, + + S.ArrayTypeSerializer, + S.IntersectionTypeSerializer, + S.IntrinsicTypeSerializer, + S.ReferenceTypeSerializer, + S.ReflectionTypeSerializer, + S.StringLiteralTypeSerializer, + S.TupleTypeSerializer, + S.TypeOperatorTypeSerializer, + S.TypeParameterTypeSerializer, + S.UnionTypeSerializer, + S.UnknownTypeSerializer, + + S.DecoratorContainerSerializer, + S.ReflectionCategorySerializer, + S.ReflectionGroupSerializer ]; function addSerializers(owner: Serializer) { From 4e50da99d4da796eb577905fe898057445e42b32 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 21:08:43 -0600 Subject: [PATCH 13/17] Fix tests --- src/lib/serialization/serializer.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/serialization/serializer.ts b/src/lib/serialization/serializer.ts index eedf37858..a47a292e4 100644 --- a/src/lib/serialization/serializer.ts +++ b/src/lib/serialization/serializer.ts @@ -96,6 +96,7 @@ const serializerComponents: (new (owner: Serializer) => SerializerComponent S.CommentTagSerializer, S.CommentSerializer, + S.ReflectionSerializer, S.ContainerReflectionSerializer, S.DeclarationReflectionSerializer, S.ParameterReflectionSerializer, @@ -105,6 +106,7 @@ const serializerComponents: (new (owner: Serializer) => SerializerComponent S.SourceReferenceContainerSerializer, + S.TypeSerializer, S.ArrayTypeSerializer, S.IntersectionTypeSerializer, S.IntrinsicTypeSerializer, From 1dad162cfd060ddfca2ceef6e6abf0bdaf574129 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 8 May 2019 21:24:56 -0600 Subject: [PATCH 14/17] [ci skip] Fix rebuild spec script --- scripts/rebuild_specs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/rebuild_specs.js b/scripts/rebuild_specs.js index 2a56cd3e2..727c15ed1 100644 --- a/scripts/rebuild_specs.js +++ b/scripts/rebuild_specs.js @@ -2,7 +2,7 @@ const fs = require('fs-extra'); const path = require('path'); -const TypeDoc = require(path.join(__dirname, '..')); +const TypeDoc = require('..'); const app = new TypeDoc.Application({ mode: 'Modules', @@ -38,7 +38,7 @@ fs.remove(path.join(__dirname, '../src/test/renderer/specs')) const src = app.expandInputFiles([ fullPath ]); const out = path.join(fullPath, 'specs.json'); const result = app.convert(src); - const data = JSON.stringify(result.toObject(), null, ' ') + const data = JSON.stringify(app.serializer.toObject(result), null, ' ') .split(TypeDoc.normalizePath(base)) .join('%BASE%'); From 66ddd001eca824f168bf7dbb6a56dda63e393311 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 13 Oct 2019 13:42:23 -0600 Subject: [PATCH 15/17] Fix build errors --- src/test/converter.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/converter.test.ts b/src/test/converter.test.ts index f7d57f50a..73ca1e2e0 100644 --- a/src/test/converter.test.ts +++ b/src/test/converter.test.ts @@ -128,7 +128,7 @@ describe('Converter with categorizeByGroup=false', function() { it('matches specs', function() { const specs = JSON.parse(FS.readFileSync(Path.join(categoryDir, 'specs-with-lump-categories.json')).toString()); - let data = JSON.stringify(result!.toObject(), null, ' '); + let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); compareReflections(JSON.parse(data), specs); @@ -145,7 +145,7 @@ describe('Converter with categorizeByGroup=false', function() { it('matches specs', function() { const specs = JSON.parse(FS.readFileSync(Path.join(classDir, 'specs.json')).toString()); - let data = JSON.stringify(result!.toObject(), null, ' '); + let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); compareReflections(JSON.parse(data), specs); From f5b5837ace26c0139781a904f137c349f4dbfe77 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 13 Oct 2019 13:51:30 -0600 Subject: [PATCH 16/17] Remove unnecessary compareReflections It did nothing more than Assert.deepStrictEqual, since if the objects weren't exactly the same deepEqual would throw. --- src/test/converter.test.ts | 64 +++----------------------------------- 1 file changed, 5 insertions(+), 59 deletions(-) diff --git a/src/test/converter.test.ts b/src/test/converter.test.ts index 73ca1e2e0..d3a6c78a2 100644 --- a/src/test/converter.test.ts +++ b/src/test/converter.test.ts @@ -3,60 +3,6 @@ import * as FS from 'fs'; import * as Path from 'path'; import Assert = require('assert'); -function compareReflections(fixture, spec, path?: string) { - path = (path ? path + '/' : '') + spec.name; - Assert.deepEqual(fixture, spec); - - for (let key in spec) { - if (!spec.hasOwnProperty(key)) { - continue; - } - Assert(fixture.hasOwnProperty(key), path + ': Missing property "' + key + '"'); - } - - for (let key in fixture) { - if (!fixture.hasOwnProperty(key) || typeof fixture[key] === 'undefined') { - continue; - } - Assert(spec.hasOwnProperty(key), path + ': Unknown property "' + key + '"'); - - const a = fixture[key]; - const b = spec[key]; - Assert(a instanceof Object === b instanceof Object, path + ': Property "' + key + '" type mismatch'); - - if (a instanceof Object) { - switch (key) { - case 'signatures': - case 'typeParameters': - case 'children': - compareChildren(a, b, path); - break; - case 'indexSignature': - case 'getSignature': - case 'setSignature': - compareReflections(a, b, path); - break; - default: - Assert.deepEqual(a, b, path + ': Property "' + key + '" value mismatch'); - } - } else { - Assert(a === b, path + ': Property "' + key + '" value mismatch'); - } - } -} - -function compareChildren(fixture, spec, path) { - const a = fixture.map(function(child) { return child.id; }); - const b = spec.map(function(child) { return child.id; }); - - Assert(a.length === b.length, path + ': Number of children differs'); - Assert(a.every(function(u, i) { return u === b[i]; }), path + ': Children are different'); - - fixture.forEach(function(a, index) { - compareReflections(a, spec[index], path); - }); -} - describe('Converter', function() { const base = Path.join(__dirname, 'converter'); let app: Application; @@ -93,7 +39,7 @@ describe('Converter', function() { let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); - compareReflections(JSON.parse(data), specs); + Assert.deepStrictEqual(JSON.parse(data), specs); }); }); }); @@ -131,7 +77,7 @@ describe('Converter with categorizeByGroup=false', function() { let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); - compareReflections(JSON.parse(data), specs); + Assert.deepStrictEqual(JSON.parse(data), specs); }); }); @@ -148,7 +94,7 @@ describe('Converter with categorizeByGroup=false', function() { let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); - compareReflections(JSON.parse(data), specs); + Assert.deepStrictEqual(JSON.parse(data), specs); }); }); }); @@ -185,7 +131,7 @@ describe('Converter with excludeNotExported=true', function() { let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); - compareReflections(JSON.parse(data), specs); + Assert.deepStrictEqual(JSON.parse(data), specs); }); }); @@ -201,7 +147,7 @@ describe('Converter with excludeNotExported=true', function() { let data = JSON.stringify(app.serializer.projectToObject(result!), null, ' '); data = data.split(normalizePath(base)).join('%BASE%'); - compareReflections(JSON.parse(data), specs); + Assert.deepStrictEqual(JSON.parse(data), specs); }); }); From 5a2fac6e07ea49be10b7fa13ea4f1855b94204db Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 13 Oct 2019 20:47:46 -0600 Subject: [PATCH 17/17] Address PR comments - Move JSON types out of the namespace - Introduce a helper to semi-automatically conver types to their JSON equivalent - Address all the build errors caused by removing the namespace --- src/lib/models/reflections/index.ts | 2 +- src/lib/serialization/events.ts | 6 +- src/lib/serialization/index.ts | 3 +- src/lib/serialization/schema.ts | 421 ++++++++---------- .../serializers/comments/comment-tag.ts | 6 +- .../serializers/comments/comment.ts | 4 +- .../serialization/serializers/decorator.ts | 6 +- .../serializers/reflection-category.ts | 6 +- .../serializers/reflection-group.ts | 6 +- .../serializers/reflections/abstract.ts | 6 +- .../serializers/reflections/container.ts | 6 +- .../serializers/reflections/declaration.ts | 9 +- .../serializers/reflections/parameter.ts | 6 +- .../serializers/reflections/project.ts | 7 +- .../serializers/reflections/signature.ts | 6 +- .../serializers/reflections/type-parameter.ts | 13 +- .../serializers/sources/source-reference.ts | 7 +- .../serializers/types/abstract.ts | 4 +- .../serialization/serializers/types/array.ts | 4 +- .../serializers/types/intersection.ts | 4 +- .../serializers/types/intrinsic.ts | 4 +- .../serializers/types/reference.ts | 6 +- .../serializers/types/reflection.ts | 6 +- .../serializers/types/string-literal.ts | 4 +- .../serialization/serializers/types/tuple.ts | 6 +- .../serializers/types/type-operator.ts | 4 +- .../serializers/types/type-parameter.ts | 6 +- .../serialization/serializers/types/union.ts | 4 +- .../serializers/types/unknown.ts | 4 +- 29 files changed, 281 insertions(+), 295 deletions(-) diff --git a/src/lib/models/reflections/index.ts b/src/lib/models/reflections/index.ts index 856577dd2..3b4b2b5c9 100644 --- a/src/lib/models/reflections/index.ts +++ b/src/lib/models/reflections/index.ts @@ -1,4 +1,4 @@ -export { Reflection, ReflectionKind, ReflectionFlag, TypeParameterContainer, Decorator, TraverseProperty } from './abstract'; +export { Reflection, ReflectionKind, ReflectionFlag, TypeParameterContainer, Decorator, TraverseProperty, ReflectionFlags } from './abstract'; export { ContainerReflection } from './container'; export { DeclarationReflection, DeclarationHierarchy } from './declaration'; export { ParameterReflection } from './parameter'; diff --git a/src/lib/serialization/events.ts b/src/lib/serialization/events.ts index 13c3d9a90..804c8db9d 100644 --- a/src/lib/serialization/events.ts +++ b/src/lib/serialization/events.ts @@ -1,6 +1,6 @@ import { Event } from '../utils/events'; import { ProjectReflection } from '../models'; -import { JSONOutput } from './schema'; +import { ProjectReflection as JSONProjectReflection } from './schema'; /** * Optional data associated with the [[SerializeEvent]]. @@ -33,9 +33,9 @@ export class SerializeEvent extends Event { */ outputFile?: string; - output: Partial; + output: Partial; - constructor(name: string, project: ProjectReflection, output: Partial) { + constructor(name: string, project: ProjectReflection, output: Partial) { super(name); this.project = project; this.output = output; diff --git a/src/lib/serialization/index.ts b/src/lib/serialization/index.ts index 2cb3b8861..963e15449 100644 --- a/src/lib/serialization/index.ts +++ b/src/lib/serialization/index.ts @@ -33,4 +33,5 @@ export { export { SerializeEvent } from './events'; -export { JSONOutput } from './schema'; +import * as JSONOutput from './schema'; +export { JSONOutput }; diff --git a/src/lib/serialization/schema.ts b/src/lib/serialization/schema.ts index 3ee1e0fc2..d42562c58 100644 --- a/src/lib/serialization/schema.ts +++ b/src/lib/serialization/schema.ts @@ -1,45 +1,3 @@ -/** - * Documents the exported JSON schema. The root object is a [[JSONOutput.ProjectReflection]]. - */ - -/** */ -import * as M from '../models'; -import { SourceReferenceWrapper, DecoratorWrapper } from './serializers'; - -/** - * Describes the mapping from Model types to the corresponding JSON output type. - */ -export type ModelToObject = T extends Array ? _ModelToObject[] : _ModelToObject; - -// Order matters here. Some types are subtypes of other types. -type _ModelToObject = - // Reflections - T extends M.ReflectionGroup ? JSONOutput.ReflectionGroup : - T extends M.ReflectionCategory ? JSONOutput.ReflectionCategory : - T extends M.SignatureReflection ? JSONOutput.SignatureReflection : - T extends M.ParameterReflection ? JSONOutput.ParameterReflection : - T extends M.DeclarationReflection ? JSONOutput.DeclarationReflection | JSONOutput.ReflectionPointer : - T extends M.TypeParameterReflection ? JSONOutput.TypeParameterReflection : - T extends M.ProjectReflection ? JSONOutput.ProjectReflection : - T extends M.ContainerReflection ? JSONOutput.ContainerReflection : - T extends M.Reflection ? JSONOutput.Reflection : - // Types - T extends M.ArrayType ? JSONOutput.ArrayType : - T extends M.IntersectionType ? JSONOutput.IntersectionType : - T extends M.IntrinsicType ? JSONOutput.IntrinsicType : - T extends M.ReferenceType ? JSONOutput.ReferenceType : - T extends M.ReflectionType ? JSONOutput.ReflectionType : - T extends M.StringLiteralType ? JSONOutput.StringLiteralType : - T extends M.TupleType ? JSONOutput.TupleType : - T extends M.UnknownType ? JSONOutput.UnknownType : - T extends M.Type ? JSONOutput.SomeType : // Technically AbstractType, but the union is more useful - // Miscellaneous - T extends M.Comment ? JSONOutput.Comment : - T extends M.CommentTag ? JSONOutput.CommentTag : - T extends DecoratorWrapper ? JSONOutput.Decorator : - T extends SourceReferenceWrapper ? JSONOutput.SourceReference : - never; - /** * Contains interfaces which describe the JSON output. Each interface is related to a specific type of serializer. * @@ -49,15 +7,14 @@ type _ModelToObject = * For example, if your custom serializer adds a property to all [[Reflection]] objects: * ```ts * declare module 'typedoc/dist/lib/serialization/schema' { - * export namespace JSONOutput { - * export interface AbstractReflection { - * myCustomProp: boolean - * } + * export interface AbstractReflection { + * myCustomProp: boolean * } * } * ``` * - * If a plugin defines a new Model type, [[ModelToObject]] will not pick up the serializer type. + * If a plugin defines a new Model type, [[ModelToObject]] will not pick up the serializer type and + * the resulting type will not be included in the return type of {@link Serializer.toObject}. * To fix this, use declaration merging to augment the [[Serializer]] class. * ```ts * declare module 'typedoc/dist/lib/serialization/serializer' { @@ -69,187 +26,205 @@ type _ModelToObject = * * For documentation on the JSON output properties, view the corresponding model. */ -export namespace JSONOutput { - // Reflections +/** */ +import * as M from '../models'; +import { SourceReferenceWrapper, DecoratorWrapper } from './serializers'; - export interface ReflectionGroup { - title: M.ReflectionGroup['title']; - kind: M.ReflectionGroup['kind']; - children?: M.ReflectionGroup['children'][number]['id'][]; - categories?: ModelToObject; - } - - export interface ReflectionCategory { - title: M.ReflectionCategory['title']; - children?: M.ReflectionCategory['children'][number]['id'][]; - } - - export interface SignatureReflection extends Reflection { - type?: ModelToObject; - overwrites?: ModelToObject; - inheritedFrom?: ModelToObject; - implementationOf?: ModelToObject; - } - - export interface ParameterReflection extends Reflection { - type?: ModelToObject; - defaultValue?: M.ParameterReflection['defaultValue']; - } - - export interface DeclarationReflection extends ContainerReflection { - type?: ModelToObject; - defaultValue?: M.DeclarationReflection['defaultValue']; - overwrites?: ModelToObject; - inheritedFrom?: ModelToObject; - extendedTypes?: ModelToObject; - extendedBy?: ModelToObject; - implementedTypes?: ModelToObject; - implementedBy?: ModelToObject; - implementationOf?: ModelToObject; - } - - export interface TypeParameterReflection extends Reflection { - type?: ModelToObject; - } - - // Nothing extra yet. - export interface ProjectReflection extends ContainerReflection { } - - export interface ContainerReflection extends Reflection { - groups?: ModelToObject; - categories?: ModelToObject; - sources?: ModelToObject; - } - - /** - * If a 3rd party serializer creates a loop when serializing, a pointer will be created - * instead of re-serializing the [[DeclarationReflection]] - */ - export interface ReflectionPointer { - id: M.Reflection['id']; - } - - export interface Reflection { - id: M.Reflection['id']; - name: M.Reflection['name']; - originalName?: M.Reflection['originalName']; - kind: M.Reflection['kind']; - kindString: M.Reflection['kindString']; - flags: ReflectionFlags; - comment?: ModelToObject; - decorates?: ModelToObject; - decorators?: ModelToObject; - } +/** + * Describes the mapping from Model types to the corresponding JSON output type. + */ +export type ModelToObject = T extends Array ? _ModelToObject[] : _ModelToObject; +// Order matters here. Some types are subtypes of other types. +type _ModelToObject = + // Reflections + T extends M.ReflectionGroup ? ReflectionGroup : + T extends M.ReflectionCategory ? ReflectionCategory : + T extends M.SignatureReflection ? SignatureReflection : + T extends M.ParameterReflection ? ParameterReflection : + T extends M.DeclarationReflection ? DeclarationReflection | ReflectionPointer : + T extends M.TypeParameterReflection ? TypeParameterReflection : + T extends M.ProjectReflection ? ProjectReflection : + T extends M.ContainerReflection ? ContainerReflection : + T extends M.Reflection ? Reflection : // Types + T extends M.ArrayType ? ArrayType : + T extends M.IntersectionType ? IntersectionType : + T extends M.IntrinsicType ? IntrinsicType : + T extends M.ReferenceType ? ReferenceType : + T extends M.ReflectionType ? ReflectionType : + T extends M.StringLiteralType ? StringLiteralType : + T extends M.TupleType ? TupleType : + T extends M.UnknownType ? UnknownType : + T extends M.Type ? SomeType : // Technically AbstractType, but the union is more useful + // Miscellaneous + T extends M.Comment ? Comment : + T extends M.CommentTag ? CommentTag : + T extends DecoratorWrapper ? Decorator : + T extends SourceReferenceWrapper ? SourceReference : + never; - export type SomeType = - | ArrayType - | IntersectionType - | UnionType - | IntrinsicType - | ReferenceType - | ReflectionType - | StringLiteralType - | TupleType - | TypeOperatorType - | TypeParameterType - | UnionType - | UnknownType; - - export interface ArrayType extends Type { - elementType: ModelToObject; - } - - export interface IntersectionType extends Type { - types: ModelToObject; - } - - export interface UnionType extends Type { - types: ModelToObject; - } - - export interface IntrinsicType extends Type { - name: M.IntrinsicType['name']; - } - - export interface ReferenceType extends Type { - name: M.ReferenceType['name']; - id?: number; - typeArguments?: ModelToObject; - } - - export interface ReflectionType extends Type { - declaration?: ModelToObject; - } - - export interface StringLiteralType extends Type { - value: M.StringLiteralType['value']; - } - - export interface TupleType extends Type { - elements?: ModelToObject; - } - - export interface TypeOperatorType extends Type { - operator: M.TypeOperatorType['operator']; - target: ModelToObject; - } - - export interface TypeParameterType extends Type { - name: M.TypeParameterType['name']; - constraint?: ModelToObject; - } - - export interface UnknownType extends Type { - name: M.UnknownType['name']; - } - - export interface Type { - type: T['type']; - } +type Primitive = string | number | undefined | null | boolean; - // Miscellaneous +/** + * Helper to describe a set of serialized properties. Primitive types are returned + * directly, while other models are first passed through ModelToObject. + * This helper removes the readonly modifier from properties since the result of serialization + * is a plain object that consumers may modify as they choose, TypeDoc doesn't care. + */ +type S = { + -readonly [K2 in K]: T[K2] extends Primitive ? T[K2] : ModelToObject +}; + +// Reflections + +export interface ReflectionGroup extends S { + children?: M.ReflectionGroup['children'][number]['id'][]; +} + +export interface ReflectionCategory extends S { + children?: M.ReflectionCategory['children'][number]['id'][]; +} + +export interface SignatureReflection extends Reflection, S { +} + +export interface ParameterReflection extends Reflection, S { +} + +export interface DeclarationReflection extends ContainerReflection, S { +} + +export interface TypeParameterReflection extends Reflection, S { +} + +// Nothing extra yet. +export interface ProjectReflection extends ContainerReflection { } + +export interface ContainerReflection extends Reflection, S { + sources?: ModelToObject; +} + +/** + * If a 3rd party serializer creates a loop when serializing, a pointer will be created + * instead of re-serializing the [[DeclarationReflection]] + */ +export interface ReflectionPointer extends S { +} + +export interface Reflection extends S { + originalName?: M.Reflection['originalName']; + flags: ReflectionFlags; + decorators?: ModelToObject; +} + +// Types + +export type SomeType = + | ArrayType + | IntersectionType + | UnionType + | IntrinsicType + | ReferenceType + | ReflectionType + | StringLiteralType + | TupleType + | TypeOperatorType + | TypeParameterType + | UnionType + | UnknownType; + +export interface ArrayType extends Type, S { +} + +export interface IntersectionType extends Type, S { +} + +export interface UnionType extends Type, S { +} + +export interface IntrinsicType extends Type, S { +} + +export interface ReferenceType extends Type, S { + id?: number; +} + +export interface ReflectionType extends Type, S { + declaration?: ModelToObject; +} + +export interface StringLiteralType extends Type, S { +} + +export interface TupleType extends Type, S { + elements?: ModelToObject; +} + +export interface TypeOperatorType extends Type, S { +} + +export interface TypeParameterType extends Type, S { +} + +export interface UnknownType extends Type, S { +} + +/** + * Technically not correct, the `type` property will be set by the abstract serializer. + * But to allow tagged literals, the `type` property is instead defined by each child type. + */ +export interface Type { +} + +// Miscellaneous + +export interface ReflectionFlags extends Partial> { +} + +export interface Comment extends Partial> { +} + +export interface CommentTag extends S { + tag: M.CommentTag['tagName']; + param?: M.CommentTag['paramName']; +} + +export interface SourceReference extends S { +} - export interface ReflectionFlags { - isPrivate?: boolean; - isProtected?: boolean; - isPublic?: boolean; - isStatic?: boolean; - isExported?: boolean; - isExternal?: boolean; - isOptional?: boolean; - isRest?: boolean; - hasExportAssignment?: boolean; - isConstructorProperty?: boolean; - isAbstract?: boolean; - isConst?: boolean; - isLet?: boolean; - } - - export interface Comment { - shortText?: M.Comment['shortText']; - text?: M.Comment['text']; - returns?: M.Comment['returns']; - tags?: ModelToObject; - } - - export interface CommentTag { - tag: M.CommentTag['tagName']; - text: M.CommentTag['text']; - param?: M.CommentTag['paramName']; - } - - export interface SourceReference { - fileName: M.SourceReference['fileName']; - line: M.SourceReference['line']; - character: M.SourceReference['character']; - } - - export interface Decorator { - name: M.Decorator['name']; - type?: ModelToObject; - arguments?: M.Decorator['arguments']; - } +export interface Decorator extends S { } diff --git a/src/lib/serialization/serializers/comments/comment-tag.ts b/src/lib/serialization/serializers/comments/comment-tag.ts index 821e6ecf9..8df3f3439 100644 --- a/src/lib/serialization/serializers/comments/comment-tag.ts +++ b/src/lib/serialization/serializers/comments/comment-tag.ts @@ -1,7 +1,7 @@ import { CommentTag } from '../../../models'; import { SerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { CommentTag as JSONCommentTag } from '../../schema'; export class CommentTagSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -17,8 +17,8 @@ export class CommentTagSerializer extends SerializerComponent { return true; } - toObject(tag: CommentTag, obj: Partial = {}): JSONOutput.CommentTag { - const result: JSONOutput.CommentTag = { + toObject(tag: CommentTag, obj: Partial = {}): JSONCommentTag { + const result: JSONCommentTag = { tag: tag.tagName, text: tag.text }; diff --git a/src/lib/serialization/serializers/comments/comment.ts b/src/lib/serialization/serializers/comments/comment.ts index 9bd5890e4..a64d26397 100644 --- a/src/lib/serialization/serializers/comments/comment.ts +++ b/src/lib/serialization/serializers/comments/comment.ts @@ -1,7 +1,7 @@ import { Comment } from '../../../models'; import { SerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { Comment as JSONComment } from '../../schema'; export class CommentSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -17,7 +17,7 @@ export class CommentSerializer extends SerializerComponent { return true; } - toObject(comment: Comment, obj: Partial = {}): JSONOutput.Comment { + toObject(comment: Comment, obj: Partial = {}): JSONComment { if (comment.shortText) { obj.shortText = comment.shortText; } diff --git a/src/lib/serialization/serializers/decorator.ts b/src/lib/serialization/serializers/decorator.ts index 815861ecb..4a7c0d88d 100644 --- a/src/lib/serialization/serializers/decorator.ts +++ b/src/lib/serialization/serializers/decorator.ts @@ -1,6 +1,6 @@ import { SerializerComponent } from '../components'; import { DecoratorWrapper } from './models/decorator-wrapper'; -import { JSONOutput } from '../schema'; +import { Decorator } from '../schema'; export class DecoratorContainerSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -16,8 +16,8 @@ export class DecoratorContainerSerializer extends SerializerComponent): JSONOutput.Decorator { - const result: JSONOutput.Decorator = { + toObject({ decorator }: DecoratorWrapper, obj?: Partial): Decorator { + const result: Decorator = { ...obj, name: decorator.name }; diff --git a/src/lib/serialization/serializers/reflection-category.ts b/src/lib/serialization/serializers/reflection-category.ts index a1b2fc45d..01fabc667 100644 --- a/src/lib/serialization/serializers/reflection-category.ts +++ b/src/lib/serialization/serializers/reflection-category.ts @@ -1,7 +1,7 @@ import { ReflectionCategory } from '../../models/ReflectionCategory'; import { SerializerComponent } from '../components'; -import { JSONOutput } from '../schema'; +import { ReflectionCategory as JSONReflectionCategory } from '../schema'; export class ReflectionCategorySerializer extends SerializerComponent { static PRIORITY = 1000; @@ -17,8 +17,8 @@ export class ReflectionCategorySerializer extends SerializerComponent): JSONOutput.ReflectionCategory { - const result: JSONOutput.ReflectionCategory = { + toObject(category: ReflectionCategory, obj?: Partial): JSONReflectionCategory { + const result: JSONReflectionCategory = { ...obj, title: category.title }; diff --git a/src/lib/serialization/serializers/reflection-group.ts b/src/lib/serialization/serializers/reflection-group.ts index 13ae33995..27dcb6265 100644 --- a/src/lib/serialization/serializers/reflection-group.ts +++ b/src/lib/serialization/serializers/reflection-group.ts @@ -1,7 +1,7 @@ import { ReflectionGroup } from '../../models/ReflectionGroup'; import { SerializerComponent } from '../components'; -import { JSONOutput } from '../schema'; +import { ReflectionGroup as JSONReflectionGroup } from '../schema'; export class ReflectionGroupSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -17,8 +17,8 @@ export class ReflectionGroupSerializer extends SerializerComponent): JSONOutput.ReflectionGroup { - const result: JSONOutput.ReflectionGroup = { + toObject(group: ReflectionGroup, obj?: Partial): JSONReflectionGroup { + const result: JSONReflectionGroup = { ...obj, title: group.title, kind: group.kind diff --git a/src/lib/serialization/serializers/reflections/abstract.ts b/src/lib/serialization/serializers/reflections/abstract.ts index ffeec1a51..148b313c0 100644 --- a/src/lib/serialization/serializers/reflections/abstract.ts +++ b/src/lib/serialization/serializers/reflections/abstract.ts @@ -3,7 +3,7 @@ import { Reflection, TraverseProperty } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { DecoratorWrapper } from '../models'; import { ReflectionFlags } from '../../../models/reflections/abstract'; -import { JSONOutput, ModelToObject } from '../../schema'; +import { Reflection as JSONReflection } from '../../schema'; export class ReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = 1000; @@ -12,8 +12,8 @@ export class ReflectionSerializer extends ReflectionSerializerComponent>): JSONOutput.Reflection { - const result: JSONOutput.Reflection = { + toObject(reflection: Reflection, obj?: Partial): JSONReflection { + const result: JSONReflection = { ...obj, id: reflection.id, name: reflection.name, diff --git a/src/lib/serialization/serializers/reflections/container.ts b/src/lib/serialization/serializers/reflections/container.ts index 49110b91e..7290f5ccc 100644 --- a/src/lib/serialization/serializers/reflections/container.ts +++ b/src/lib/serialization/serializers/reflections/container.ts @@ -2,7 +2,7 @@ import { ContainerReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { SourceReferenceWrapper } from '../models'; -import { JSONOutput } from '../../schema'; +import { ContainerReflection as JSONContainerReflection, Reflection as JSONReflection } from '../../schema'; export class ContainerReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { @@ -14,8 +14,8 @@ export class ContainerReflectionSerializer extends ReflectionSerializerComponent * @param container * @param obj */ - toObject(container: ContainerReflection, obj: JSONOutput.Reflection): JSONOutput.ContainerReflection { - const result: JSONOutput.ContainerReflection = { + toObject(container: ContainerReflection, obj: JSONReflection): JSONContainerReflection { + const result: JSONContainerReflection = { ...obj }; diff --git a/src/lib/serialization/serializers/reflections/declaration.ts b/src/lib/serialization/serializers/reflections/declaration.ts index 199f649fa..0f88d6d77 100644 --- a/src/lib/serialization/serializers/reflections/declaration.ts +++ b/src/lib/serialization/serializers/reflections/declaration.ts @@ -2,7 +2,10 @@ import { DeclarationReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; -import { JSONOutput } from '../../schema'; +import { + DeclarationReflection as JSONDeclarationReflection, + ContainerReflection as JSONContainerReflection +} from '../../schema'; export class DeclarationReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent @@ -11,8 +14,8 @@ export class DeclarationReflectionSerializer extends ReflectionSerializerCompone return t instanceof DeclarationReflection; } - toObject(declaration: DeclarationReflection, obj: JSONOutput.ContainerReflection): JSONOutput.DeclarationReflection { - const result: JSONOutput.DeclarationReflection = { + toObject(declaration: DeclarationReflection, obj: JSONContainerReflection): JSONDeclarationReflection { + const result: JSONDeclarationReflection = { ...obj }; diff --git a/src/lib/serialization/serializers/reflections/parameter.ts b/src/lib/serialization/serializers/reflections/parameter.ts index e3fe4ee1d..2be25af1b 100644 --- a/src/lib/serialization/serializers/reflections/parameter.ts +++ b/src/lib/serialization/serializers/reflections/parameter.ts @@ -1,15 +1,15 @@ import { ParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { ParameterReflection as JSONParameterReflection, Reflection as JSONReflection } from '../../schema'; export class ParameterReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { return t instanceof ParameterReflection; } - toObject(parameter: ParameterReflection, obj: JSONOutput.Reflection): JSONOutput.ParameterReflection { - const result: JSONOutput.ParameterReflection = { + toObject(parameter: ParameterReflection, obj: JSONReflection): JSONParameterReflection { + const result: JSONParameterReflection = { ...obj }; diff --git a/src/lib/serialization/serializers/reflections/project.ts b/src/lib/serialization/serializers/reflections/project.ts index 5452295a0..c15ca5559 100644 --- a/src/lib/serialization/serializers/reflections/project.ts +++ b/src/lib/serialization/serializers/reflections/project.ts @@ -2,7 +2,10 @@ import { ProjectReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; import { ContainerReflectionSerializer } from './container'; -import { JSONOutput } from '../../schema'; +import { + ProjectReflection as JSONProjectReflection, + ContainerReflection as JSONContainerReflection +} from '../../schema'; export class ProjectReflectionSerializer extends ReflectionSerializerComponent { static PRIORITY = ContainerReflectionSerializer.PRIORITY - 1; // mimic inheritance, run after parent @@ -11,7 +14,7 @@ export class ProjectReflectionSerializer extends ReflectionSerializerComponent

{ supports(t: unknown) { return t instanceof SignatureReflection; } - toObject(signature: SignatureReflection, obj: JSONOutput.Reflection): JSONOutput.SignatureReflection { - const result: JSONOutput.SignatureReflection = { ...obj }; + toObject(signature: SignatureReflection, obj: JSONReflection): JSONSignatureReflection { + const result: JSONSignatureReflection = { ...obj }; if (signature.type) { result.type = this.owner.toObject(signature.type); diff --git a/src/lib/serialization/serializers/reflections/type-parameter.ts b/src/lib/serialization/serializers/reflections/type-parameter.ts index 7bce9d0ad..39766ea69 100644 --- a/src/lib/serialization/serializers/reflections/type-parameter.ts +++ b/src/lib/serialization/serializers/reflections/type-parameter.ts @@ -1,17 +1,18 @@ import { TypeParameterReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { + TypeParameterReflection as JSONTypeParameterReflection, + Reflection as JSONReflection +} from '../../schema'; -export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent< - TypeParameterReflection -> { +export class TypeParameterReflectionSerializer extends ReflectionSerializerComponent { supports(t: unknown) { return t instanceof TypeParameterReflection; } - toObject(typeParameter: TypeParameterReflection, obj: JSONOutput.Reflection): JSONOutput.TypeParameterReflection { - const result: JSONOutput.TypeParameterReflection = { ...obj }; + toObject(typeParameter: TypeParameterReflection, obj: JSONReflection): JSONTypeParameterReflection { + const result: JSONTypeParameterReflection = { ...obj }; if (typeParameter.type) { result.type = this.owner.toObject(typeParameter.type); diff --git a/src/lib/serialization/serializers/sources/source-reference.ts b/src/lib/serialization/serializers/sources/source-reference.ts index 7221eecad..fb22ec693 100644 --- a/src/lib/serialization/serializers/sources/source-reference.ts +++ b/src/lib/serialization/serializers/sources/source-reference.ts @@ -1,6 +1,6 @@ import { SerializerComponent } from '../../components'; import { SourceReferenceWrapper } from '../models'; -import { JSONOutput } from '../../schema'; +import { SourceReference as JSONSourceReference } from '../../schema'; export class SourceReferenceContainerSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -13,7 +13,10 @@ export class SourceReferenceContainerSerializer extends SerializerComponent): JSONOutput.SourceReference { + toObject( + { sourceReference: ref }: SourceReferenceWrapper, + obj?: Partial + ): JSONSourceReference { return { ...obj, fileName: ref.fileName, diff --git a/src/lib/serialization/serializers/types/abstract.ts b/src/lib/serialization/serializers/types/abstract.ts index 6ef147451..59cb2966d 100644 --- a/src/lib/serialization/serializers/types/abstract.ts +++ b/src/lib/serialization/serializers/types/abstract.ts @@ -1,7 +1,7 @@ import { Type } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { Type as JSONType } from '../../schema'; export class TypeSerializer extends TypeSerializerComponent { static PRIORITY = 1000; @@ -10,7 +10,7 @@ export class TypeSerializer extends TypeSerializerComponent { return t instanceof Type; } - toObject(type: Type, obj?: Partial>): JSONOutput.Type { + toObject(type: Type, obj?: Partial): JSONType { return { ...obj, type: type.type diff --git a/src/lib/serialization/serializers/types/array.ts b/src/lib/serialization/serializers/types/array.ts index 4d3231cac..1f534bcf7 100644 --- a/src/lib/serialization/serializers/types/array.ts +++ b/src/lib/serialization/serializers/types/array.ts @@ -1,7 +1,7 @@ import { ArrayType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { ArrayType as JSONArrayType } from '../../schema'; export class ArrayTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -13,7 +13,7 @@ export class ArrayTypeSerializer extends TypeSerializerComponent { * @param type * @param obj */ - toObject(type: ArrayType, obj: Pick): JSONOutput.ArrayType { + toObject(type: ArrayType, obj: Pick): JSONArrayType { return { ...obj, elementType: this.owner.toObject(type.elementType) diff --git a/src/lib/serialization/serializers/types/intersection.ts b/src/lib/serialization/serializers/types/intersection.ts index a8c8781a8..40eecd572 100644 --- a/src/lib/serialization/serializers/types/intersection.ts +++ b/src/lib/serialization/serializers/types/intersection.ts @@ -1,7 +1,7 @@ import { IntersectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { IntersectionType as JSONIntersectionType } from '../../schema'; export class IntersectionTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -13,7 +13,7 @@ export class IntersectionTypeSerializer extends TypeSerializerComponent): JSONOutput.IntersectionType { + toObject(type: IntersectionType, obj: Pick): JSONIntersectionType { return { ...obj, types: type.types.map(t => this.owner.toObject(t)) diff --git a/src/lib/serialization/serializers/types/intrinsic.ts b/src/lib/serialization/serializers/types/intrinsic.ts index 956656b16..676ea9318 100644 --- a/src/lib/serialization/serializers/types/intrinsic.ts +++ b/src/lib/serialization/serializers/types/intrinsic.ts @@ -1,7 +1,7 @@ import { IntrinsicType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { IntrinsicType as JSONIntrinsicType } from '../../schema'; export class IntrinsicTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -13,7 +13,7 @@ export class IntrinsicTypeSerializer extends TypeSerializerComponent): JSONOutput.IntrinsicType { + toObject(type: IntrinsicType, obj: Pick): JSONIntrinsicType { return { ...obj, name: type.name diff --git a/src/lib/serialization/serializers/types/reference.ts b/src/lib/serialization/serializers/types/reference.ts index 6e304e551..34346fd1a 100644 --- a/src/lib/serialization/serializers/types/reference.ts +++ b/src/lib/serialization/serializers/types/reference.ts @@ -1,7 +1,7 @@ import { ReferenceType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { ReferenceType as JSONReferenceType } from '../../schema'; export class ReferenceTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -10,8 +10,8 @@ export class ReferenceTypeSerializer extends TypeSerializerComponent & Partial - ): JSONOutput.ReferenceType { + obj: Pick & Partial + ): JSONReferenceType { if (type.reflection) { obj.id = type.reflection.id; } diff --git a/src/lib/serialization/serializers/types/reflection.ts b/src/lib/serialization/serializers/types/reflection.ts index 3c869a6b5..a4285958c 100644 --- a/src/lib/serialization/serializers/types/reflection.ts +++ b/src/lib/serialization/serializers/types/reflection.ts @@ -1,7 +1,7 @@ import { DeclarationReflection, ReflectionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { ReflectionType as JSONReflectionType } from '../../schema'; export class ReflectionTypeSerializer extends TypeSerializerComponent { private visited = new Set(); @@ -10,8 +10,8 @@ export class ReflectionTypeSerializer extends TypeSerializerComponent): JSONOutput.ReflectionType { - const result: JSONOutput.ReflectionType = { + toObject(reference: ReflectionType, obj: Pick): JSONReflectionType { + const result: JSONReflectionType = { ...obj }; diff --git a/src/lib/serialization/serializers/types/string-literal.ts b/src/lib/serialization/serializers/types/string-literal.ts index 0ce588340..cc15a1415 100644 --- a/src/lib/serialization/serializers/types/string-literal.ts +++ b/src/lib/serialization/serializers/types/string-literal.ts @@ -1,14 +1,14 @@ import { StringLiteralType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { StringLiteralType as JSONStringLiteralType } from '../../schema'; export class StringLiteralTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof StringLiteralType; } - toObject(type: StringLiteralType, obj: Pick): JSONOutput.StringLiteralType { + toObject(type: StringLiteralType, obj: Pick): JSONStringLiteralType { return { ...obj, value: type.value diff --git a/src/lib/serialization/serializers/types/tuple.ts b/src/lib/serialization/serializers/types/tuple.ts index 0e6592d69..0aebed210 100644 --- a/src/lib/serialization/serializers/types/tuple.ts +++ b/src/lib/serialization/serializers/types/tuple.ts @@ -1,15 +1,15 @@ import { TupleType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { TupleType as JSONTupleType } from '../../schema'; export class TupleTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TupleType; } - toObject(tuple: TupleType, obj: Pick): JSONOutput.TupleType { - const result: JSONOutput.TupleType = { ...obj }; + toObject(tuple: TupleType, obj: Pick): JSONTupleType { + const result: JSONTupleType = { ...obj }; if (tuple.elements && tuple.elements.length > 0) { result.elements = tuple.elements.map(t => this.owner.toObject(t)); diff --git a/src/lib/serialization/serializers/types/type-operator.ts b/src/lib/serialization/serializers/types/type-operator.ts index 696772039..200c024df 100644 --- a/src/lib/serialization/serializers/types/type-operator.ts +++ b/src/lib/serialization/serializers/types/type-operator.ts @@ -1,14 +1,14 @@ import { TypeOperatorType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { TypeOperatorType as JSONTypeOperatorType } from '../../schema'; export class TypeOperatorTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TypeOperatorType; } - toObject(type: TypeOperatorType, obj: Pick): JSONOutput.TypeOperatorType { + toObject(type: TypeOperatorType, obj: Pick): JSONTypeOperatorType { return { ...obj, operator: type.operator, diff --git a/src/lib/serialization/serializers/types/type-parameter.ts b/src/lib/serialization/serializers/types/type-parameter.ts index e3e99788f..ab2925700 100644 --- a/src/lib/serialization/serializers/types/type-parameter.ts +++ b/src/lib/serialization/serializers/types/type-parameter.ts @@ -1,15 +1,15 @@ import { TypeParameterType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { TypeParameterType as JSONTypeParameterType } from '../../schema'; export class TypeParameterTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { return t instanceof TypeParameterType; } - toObject(type: TypeParameterType, obj: Pick): JSONOutput.TypeParameterType { - const result: JSONOutput.TypeParameterType = { + toObject(type: TypeParameterType, obj: Pick): JSONTypeParameterType { + const result: JSONTypeParameterType = { ...obj, name: type.name }; diff --git a/src/lib/serialization/serializers/types/union.ts b/src/lib/serialization/serializers/types/union.ts index 57d8cf770..75df55e34 100644 --- a/src/lib/serialization/serializers/types/union.ts +++ b/src/lib/serialization/serializers/types/union.ts @@ -1,7 +1,7 @@ import { UnionType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { UnionType as JSONUnionType } from '../../schema'; export class UnionTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -13,7 +13,7 @@ export class UnionTypeSerializer extends TypeSerializerComponent { * @param type * @param obj */ - toObject(type: UnionType, obj: Pick): JSONOutput.UnionType { + toObject(type: UnionType, obj: Pick): JSONUnionType { return { ...obj, types: type.types.map(t => this.owner.toObject(t)) diff --git a/src/lib/serialization/serializers/types/unknown.ts b/src/lib/serialization/serializers/types/unknown.ts index cf606daea..01caa451f 100644 --- a/src/lib/serialization/serializers/types/unknown.ts +++ b/src/lib/serialization/serializers/types/unknown.ts @@ -1,7 +1,7 @@ import { UnknownType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; -import { JSONOutput } from '../../schema'; +import { UnknownType as JSONUnknownType } from '../../schema'; export class UnknownTypeSerializer extends TypeSerializerComponent { supports(t: unknown) { @@ -13,7 +13,7 @@ export class UnknownTypeSerializer extends TypeSerializerComponent * @param type * @param obj */ - toObject(type: UnknownType, obj: Pick): JSONOutput.UnknownType { + toObject(type: UnknownType, obj: Pick): JSONUnknownType { return { ...obj, name: type.name