From 930aac1d0db169462cf9f5ed345ac9bd2d41ebfe Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Fri, 10 Jan 2020 21:12:48 -0700 Subject: [PATCH] fix: Add missing types for serialization --- src/lib/serialization/schema.ts | 40 ++++++++++++++++--- .../serializers/reflection-category.ts | 9 +++-- .../serializers/reflection-group.ts | 7 +++- .../serializers/reflections/reference.ts | 9 ++++- .../serializers/types/conditional.ts | 21 +++++----- .../serializers/types/indexed-access.ts | 6 ++- .../serializers/types/inferred.ts | 7 +++- 7 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/lib/serialization/schema.ts b/src/lib/serialization/schema.ts index d42562c58..58316d6e1 100644 --- a/src/lib/serialization/schema.ts +++ b/src/lib/serialization/schema.ts @@ -47,11 +47,16 @@ type _ModelToObject = T extends M.TypeParameterReflection ? TypeParameterReflection : T extends M.ProjectReflection ? ProjectReflection : T extends M.ContainerReflection ? ContainerReflection : + T extends M.ReferenceReflection ? ReferenceReflection : T extends M.Reflection ? Reflection : // Types T extends M.ArrayType ? ArrayType : + T extends M.ConditionalType ? ConditionalType : + T extends M.IndexedAccessType ? IndexedAccessType : + T extends M.InferredType ? InferredType : T extends M.IntersectionType ? IntersectionType : T extends M.IntrinsicType ? IntrinsicType : + T extends M.PredicateType ? PredicateType : T extends M.ReferenceType ? ReferenceType : T extends M.ReflectionType ? ReflectionType : T extends M.StringLiteralType ? StringLiteralType : @@ -79,14 +84,22 @@ type S = { // Reflections -export interface ReflectionGroup extends S { +export interface ReflectionGroup extends Reflection, S { children?: M.ReflectionGroup['children'][number]['id'][]; } -export interface ReflectionCategory extends S { +export interface ReflectionCategory extends Reflection, S { children?: M.ReflectionCategory['children'][number]['id'][]; } +export interface ReferenceReflection extends DeclarationReflection, S { + /** + * -1 if the reference refers to a symbol that does not exist in the documentation. + * Otherwise, the reflection ID. + */ + target: number; +} + export interface SignatureReflection extends Reflection, S { } -export interface IntersectionType extends Type, S { +export interface ConditionalType extends Type, S { + } -export interface UnionType extends Type, S { +export interface IndexedAccessType extends Type, S { +} + +export interface InferredType extends Type, S { +} + +export interface IntersectionType extends Type, S { } export interface IntrinsicType extends Type, S { } +export interface PredicateType extends Type, S { +} + export interface ReferenceType extends Type, S { id?: number; } @@ -187,6 +214,9 @@ export interface TypeOperatorType extends Type, S { } +export interface UnionType extends Type, S { +} + export interface UnknownType extends Type, S { } diff --git a/src/lib/serialization/serializers/reflection-category.ts b/src/lib/serialization/serializers/reflection-category.ts index 01fabc667..9bc6c584a 100644 --- a/src/lib/serialization/serializers/reflection-category.ts +++ b/src/lib/serialization/serializers/reflection-category.ts @@ -1,7 +1,10 @@ import { ReflectionCategory } from '../../models/ReflectionCategory'; import { SerializerComponent } from '../components'; -import { ReflectionCategory as JSONReflectionCategory } from '../schema'; +import { + Reflection as JSONReflection, + ReflectionCategory as JSONReflectionCategory +} from '../schema'; export class ReflectionCategorySerializer extends SerializerComponent { static PRIORITY = 1000; @@ -9,7 +12,7 @@ export class ReflectionCategorySerializer extends SerializerComponent): JSONReflectionCategory { + toObject(category: ReflectionCategory, obj: JSONReflection): 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 27dcb6265..de890a0a6 100644 --- a/src/lib/serialization/serializers/reflection-group.ts +++ b/src/lib/serialization/serializers/reflection-group.ts @@ -1,7 +1,10 @@ import { ReflectionGroup } from '../../models/ReflectionGroup'; import { SerializerComponent } from '../components'; -import { ReflectionGroup as JSONReflectionGroup } from '../schema'; +import { + Reflection as JSONReflection, + ReflectionGroup as JSONReflectionGroup +} from '../schema'; export class ReflectionGroupSerializer extends SerializerComponent { static PRIORITY = 1000; @@ -17,7 +20,7 @@ export class ReflectionGroupSerializer extends SerializerComponent): JSONReflectionGroup { + toObject(group: ReflectionGroup, obj: JSONReflection): JSONReflectionGroup { const result: JSONReflectionGroup = { ...obj, title: group.title, diff --git a/src/lib/serialization/serializers/reflections/reference.ts b/src/lib/serialization/serializers/reflections/reference.ts index 4c20fdbfd..f9995a080 100644 --- a/src/lib/serialization/serializers/reflections/reference.ts +++ b/src/lib/serialization/serializers/reflections/reference.ts @@ -1,12 +1,19 @@ import { ReferenceReflection } from '../../../models'; import { ReflectionSerializerComponent } from '../../components'; +import { + DeclarationReflection as JSONDeclarationReflection, + ReferenceReflection as JSONReferenceReflection +} from '../../schema'; +import { DeclarationReflectionSerializer } from './declaration'; export class ReferenceReflectionSerializer extends ReflectionSerializerComponent { + static PRIORITY = DeclarationReflectionSerializer.PRIORITY - 1; + supports(t: unknown) { return t instanceof ReferenceReflection; } - toObject(ref: ReferenceReflection, obj?: any): any { + toObject(ref: ReferenceReflection, obj: JSONDeclarationReflection): JSONReferenceReflection { return { ...obj, target: ref.tryGetTargetReflection()?.id ?? -1 diff --git a/src/lib/serialization/serializers/types/conditional.ts b/src/lib/serialization/serializers/types/conditional.ts index 33100058b..532b5591c 100644 --- a/src/lib/serialization/serializers/types/conditional.ts +++ b/src/lib/serialization/serializers/types/conditional.ts @@ -1,19 +1,22 @@ import { ConditionalType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { + Type as JSONType, + ConditionalType as JSONConditionalType +} from '../../schema'; export class ConditionalTypeSerializer extends TypeSerializerComponent { supports(item: unknown): boolean { return item instanceof ConditionalType; } - toObject(conditional: ConditionalType, obj?: any): any { - obj = obj || {}; - - obj.checkType = this.owner.toObject(conditional.checkType); - obj.extendsType = this.owner.toObject(conditional.extendsType); - obj.trueType = this.owner.toObject(conditional.trueType); - obj.falseType = this.owner.toObject(conditional.falseType); - - return obj; + toObject(conditional: ConditionalType, obj: Pick & JSONType): JSONConditionalType { + return { + ...obj, + checkType: this.owner.toObject(conditional.checkType), + extendsType: this.owner.toObject(conditional.extendsType), + trueType: this.owner.toObject(conditional.trueType), + falseType: this.owner.toObject(conditional.falseType) + }; } } diff --git a/src/lib/serialization/serializers/types/indexed-access.ts b/src/lib/serialization/serializers/types/indexed-access.ts index 04688cdf7..ffd868ae2 100644 --- a/src/lib/serialization/serializers/types/indexed-access.ts +++ b/src/lib/serialization/serializers/types/indexed-access.ts @@ -1,12 +1,16 @@ import { IndexedAccessType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { + Type as JSONType, + IndexedAccessType as JSONIndexedAccessType +} from '../../schema'; export class IndexedAccessTypeSerializer extends TypeSerializerComponent { supports(item: unknown): boolean { return item instanceof IndexedAccessType; } - toObject(type: IndexedAccessType, obj?: any): any { + toObject(type: IndexedAccessType, obj: Pick & JSONType): JSONIndexedAccessType { return { ...obj, indexType: this.owner.toObject(type.indexType), diff --git a/src/lib/serialization/serializers/types/inferred.ts b/src/lib/serialization/serializers/types/inferred.ts index 0cfcb7b33..88655c50e 100644 --- a/src/lib/serialization/serializers/types/inferred.ts +++ b/src/lib/serialization/serializers/types/inferred.ts @@ -1,12 +1,17 @@ import { InferredType } from '../../../models'; import { TypeSerializerComponent } from '../../components'; +import { + Type as JSONType, + InferredType as JSONInferredType +} from '../../schema'; + export class InferredTypeSerializer extends TypeSerializerComponent { supports(item: unknown): boolean { return item instanceof InferredType; } - toObject(inferred: InferredType, obj?: any): any { + toObject(inferred: InferredType, obj: JSONType & Pick): JSONInferredType { return { ...obj, name: inferred.name