Skip to content

Commit

Permalink
Refactor JSON generation process to support a plugin architecture (#597)
Browse files Browse the repository at this point in the history
* refactor JSON serialization from toObject() to serialization master plugin and child plugins, mimics 1:1 output of toObject() logic

* add es6 core types (for Object.assign, arrays etc..)

* add @depracated tag to all toObject() methods.

It is essential that they are removed, if not the code will contain duplicate logic which is a risk over time.

* flip the switch on serialization plugins

* add some event data

* fix typo in null check

* support `TypeOperator` sertialization

* fix wrong serialization of get/set/index signature

this is a legacy bug that was taken from the old toObject implementation
  • Loading branch information
shlomiassaf authored and aciccarello committed Nov 10, 2017
1 parent a24e61e commit 34377dc
Show file tree
Hide file tree
Showing 60 changed files with 1,493 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib/application.ts
Expand Up @@ -13,6 +13,7 @@ import { Minimatch, IMinimatch } from 'minimatch';

import { Converter } from './converter/index';
import { Renderer } from './output/renderer';
import { Serializer } from './serialization';
import { ProjectReflection } from './models/index';
import { Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile } from './utils/index';

Expand Down Expand Up @@ -48,6 +49,11 @@ export class Application extends ChildableComponent<Application, AbstractCompone
*/
renderer: Renderer;

/**
* The serializer used to generate JSON output.
*/
serializer: Serializer;

/**
* The logger that should be used to output messages.
*/
Expand Down Expand Up @@ -92,6 +98,7 @@ export class Application extends ChildableComponent<Application, AbstractCompone

this.logger = new ConsoleLogger();
this.converter = this.addComponent<Converter>('converter', Converter);
this.serializer = this.addComponent<Serializer>('serializer', Serializer);
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);
Expand Down Expand Up @@ -221,7 +228,9 @@ export class Application extends ChildableComponent<Application, AbstractCompone
}

out = Path.resolve(out);
writeFile(out, JSON.stringify(project.toObject(), null, '\t'), false);
const eventData = { outputDirectory: Path.dirname(out), outputFile: Path.basename(out) };
const ser = this.serializer.projectToObject(project, { begin: eventData, end: eventData });
writeFile(out, JSON.stringify(ser, null, '\t'), false);
this.logger.success('JSON written to %s', out);

return true;
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/ReflectionCategory.ts
Expand Up @@ -51,6 +51,7 @@ export class ReflectionCategory {

/**
* Return a raw object representation of this reflection category.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = {
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/ReflectionGroup.ts
Expand Up @@ -89,6 +89,7 @@ export class ReflectionGroup {

/**
* Return a raw object representation of this reflection group.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = {
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/comments/comment.ts
Expand Up @@ -99,6 +99,7 @@ export class Comment {

/**
* Return a raw object representation of this comment.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {};
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/comments/tag.ts
Expand Up @@ -30,6 +30,7 @@ export class CommentTag {

/**
* Return a raw object representation of this tag.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/abstract.ts
Expand Up @@ -551,6 +551,7 @@ export abstract class Reflection {

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/container.ts
Expand Up @@ -54,6 +54,7 @@ export class ContainerReflection extends Reflection {

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/declaration.ts
Expand Up @@ -181,6 +181,7 @@ export class DeclarationReflection extends ContainerReflection implements Defaul

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
let result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/parameter.ts
Expand Up @@ -27,6 +27,7 @@ export class ParameterReflection extends Reflection implements DefaultValueConta

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/project.ts
Expand Up @@ -126,6 +126,7 @@ export class ProjectReflection extends ContainerReflection {

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/signature.ts
Expand Up @@ -70,6 +70,7 @@ export class SignatureReflection extends Reflection implements TypeContainer, Ty

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/reflections/type-parameter.ts
Expand Up @@ -17,6 +17,7 @@ export class TypeParameterReflection extends Reflection implements TypeContainer

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/abstract.ts
Expand Up @@ -29,6 +29,7 @@ export abstract class Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
let result: any = {};
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/array.ts
Expand Up @@ -53,6 +53,7 @@ export class ArrayType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/intersection.ts
Expand Up @@ -52,6 +52,7 @@ export class IntersectionType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/intrinsic.ts
Expand Up @@ -50,6 +50,7 @@ export class IntrinsicType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/reference.ts
Expand Up @@ -90,6 +90,7 @@ export class ReferenceType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/reflection.ts
Expand Up @@ -50,6 +50,7 @@ export class ReflectionType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/string-literal.ts
Expand Up @@ -50,6 +50,7 @@ export class StringLiteralType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/tuple.ts
Expand Up @@ -52,6 +52,7 @@ export class TupleType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/type-parameter.ts
Expand Up @@ -54,6 +54,7 @@ export class TypeParameterType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/union.ts
Expand Up @@ -52,6 +52,7 @@ export class UnionType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types/unknown.ts
Expand Up @@ -46,6 +46,7 @@ export class UnknownType extends Type {

/**
* Return a raw object representation of this type.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = super.toObject();
Expand Down

0 comments on commit 34377dc

Please sign in to comment.