Skip to content

Commit

Permalink
fix: Add missing types for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jan 11, 2020
1 parent 7dee2c3 commit 930aac1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 22 deletions.
40 changes: 35 additions & 5 deletions src/lib/serialization/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ type _ModelToObject<T> =
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 :
Expand Down Expand Up @@ -79,14 +84,22 @@ type S<T, K extends keyof T> = {

// Reflections

export interface ReflectionGroup extends S<M.ReflectionGroup, 'title' | 'kind' | 'categories'> {
export interface ReflectionGroup extends Reflection, S<M.ReflectionGroup, 'title' | 'kind' | 'categories'> {
children?: M.ReflectionGroup['children'][number]['id'][];
}

export interface ReflectionCategory extends S<M.ReflectionCategory, 'title'> {
export interface ReflectionCategory extends Reflection, S<M.ReflectionCategory, 'title'> {
children?: M.ReflectionCategory['children'][number]['id'][];
}

export interface ReferenceReflection extends DeclarationReflection, S<M.ReferenceReflection, never> {
/**
* -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<M.SignatureReflection,
'type'
| 'overwrites'
Expand Down Expand Up @@ -142,9 +155,12 @@ export interface Reflection extends S<M.Reflection,

export type SomeType =
| ArrayType
| ConditionalType
| IndexedAccessType
| InferredType
| IntersectionType
| UnionType
| IntrinsicType
| PredicateType
| ReferenceType
| ReflectionType
| StringLiteralType
Expand All @@ -157,15 +173,26 @@ export type SomeType =
export interface ArrayType extends Type, S<M.ArrayType, 'type' | 'elementType'> {
}

export interface IntersectionType extends Type, S<M.IntersectionType, 'type' | 'types'> {
export interface ConditionalType extends Type, S<M.ConditionalType,
'type' | 'checkType' | 'extendsType' | 'trueType' | 'falseType'> {

}

export interface UnionType extends Type, S<M.UnionType, 'type' | 'types'> {
export interface IndexedAccessType extends Type, S<M.IndexedAccessType, 'type' | 'indexType' | 'objectType'> {
}

export interface InferredType extends Type, S<M.InferredType, 'type' | 'name'> {
}

export interface IntersectionType extends Type, S<M.IntersectionType, 'type' | 'types'> {
}

export interface IntrinsicType extends Type, S<M.IntrinsicType, 'type' | 'name'> {
}

export interface PredicateType extends Type, S<M.PredicateType, 'type' | 'name' | 'asserts' | 'targetType'> {
}

export interface ReferenceType extends Type, S<M.ReferenceType, 'type' | 'name' | 'typeArguments'> {
id?: number;
}
Expand All @@ -187,6 +214,9 @@ export interface TypeOperatorType extends Type, S<M.TypeOperatorType, 'type' | '
export interface TypeParameterType extends Type, S<M.TypeParameterType, 'type' | 'name' | 'constraint'> {
}

export interface UnionType extends Type, S<M.UnionType, 'type' | 'types'> {
}

export interface UnknownType extends Type, S<M.UnknownType, 'type' | 'name'> {
}

Expand Down
9 changes: 6 additions & 3 deletions src/lib/serialization/serializers/reflection-category.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
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<ReflectionCategory> {
static PRIORITY = 1000;

/**
* Filter for instances of [[ReflectionCategory]]
*/
serializeGroup(instance: any): boolean {
serializeGroup(instance: unknown): boolean {
return instance instanceof ReflectionCategory;
}

supports(r: unknown) {
return r instanceof ReflectionCategory;
}

toObject(category: ReflectionCategory, obj?: Partial<JSONReflectionCategory>): JSONReflectionCategory {
toObject(category: ReflectionCategory, obj: JSONReflection): JSONReflectionCategory {
const result: JSONReflectionCategory = {
...obj,
title: category.title
Expand Down
7 changes: 5 additions & 2 deletions src/lib/serialization/serializers/reflection-group.ts
Original file line number Diff line number Diff line change
@@ -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<ReflectionGroup> {
static PRIORITY = 1000;
Expand All @@ -17,7 +20,7 @@ export class ReflectionGroupSerializer extends SerializerComponent<ReflectionGro
return true;
}

toObject(group: ReflectionGroup, obj?: Partial<JSONReflectionGroup>): JSONReflectionGroup {
toObject(group: ReflectionGroup, obj: JSONReflection): JSONReflectionGroup {
const result: JSONReflectionGroup = {
...obj,
title: group.title,
Expand Down
9 changes: 8 additions & 1 deletion src/lib/serialization/serializers/reflections/reference.ts
Original file line number Diff line number Diff line change
@@ -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<ReferenceReflection> {
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
Expand Down
21 changes: 12 additions & 9 deletions src/lib/serialization/serializers/types/conditional.ts
Original file line number Diff line number Diff line change
@@ -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<ConditionalType> {
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<JSONConditionalType, 'type'> & 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)
};
}
}
6 changes: 5 additions & 1 deletion src/lib/serialization/serializers/types/indexed-access.ts
Original file line number Diff line number Diff line change
@@ -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<IndexedAccessType> {
supports(item: unknown): boolean {
return item instanceof IndexedAccessType;
}

toObject(type: IndexedAccessType, obj?: any): any {
toObject(type: IndexedAccessType, obj: Pick<JSONIndexedAccessType, 'type'> & JSONType): JSONIndexedAccessType {
return {
...obj,
indexType: this.owner.toObject(type.indexType),
Expand Down
7 changes: 6 additions & 1 deletion src/lib/serialization/serializers/types/inferred.ts
Original file line number Diff line number Diff line change
@@ -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<InferredType> {
supports(item: unknown): boolean {
return item instanceof InferredType;
}

toObject(inferred: InferredType, obj?: any): any {
toObject(inferred: InferredType, obj: JSONType & Pick<JSONInferredType, 'type'>): JSONInferredType {
return {
...obj,
name: inferred.name
Expand Down

0 comments on commit 930aac1

Please sign in to comment.