Skip to content

Commit 34377dc

Browse files
shlomiassafaciccarello
authored andcommitted
Refactor JSON generation process to support a plugin architecture (TypeStrong#597)
* 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
1 parent a24e61e commit 34377dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1493
-1
lines changed

src/lib/application.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Minimatch, IMinimatch } from 'minimatch';
1313

1414
import { Converter } from './converter/index';
1515
import { Renderer } from './output/renderer';
16+
import { Serializer } from './serialization';
1617
import { ProjectReflection } from './models/index';
1718
import { Logger, ConsoleLogger, CallbackLogger, PluginHost, writeFile } from './utils/index';
1819

@@ -48,6 +49,11 @@ export class Application extends ChildableComponent<Application, AbstractCompone
4849
*/
4950
renderer: Renderer;
5051

52+
/**
53+
* The serializer used to generate JSON output.
54+
*/
55+
serializer: Serializer;
56+
5157
/**
5258
* The logger that should be used to output messages.
5359
*/
@@ -92,6 +98,7 @@ export class Application extends ChildableComponent<Application, AbstractCompone
9298

9399
this.logger = new ConsoleLogger();
94100
this.converter = this.addComponent<Converter>('converter', Converter);
101+
this.serializer = this.addComponent<Serializer>('serializer', Serializer);
95102
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
96103
this.plugins = this.addComponent('plugins', PluginHost);
97104
this.options = this.addComponent('options', Options);
@@ -221,7 +228,9 @@ export class Application extends ChildableComponent<Application, AbstractCompone
221228
}
222229

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

227236
return true;

src/lib/models/ReflectionCategory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class ReflectionCategory {
5151

5252
/**
5353
* Return a raw object representation of this reflection category.
54+
* @deprecated Use serializers instead
5455
*/
5556
toObject(): any {
5657
const result = {

src/lib/models/ReflectionGroup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class ReflectionGroup {
8989

9090
/**
9191
* Return a raw object representation of this reflection group.
92+
* @deprecated Use serializers instead
9293
*/
9394
toObject(): any {
9495
const result = {

src/lib/models/comments/comment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class Comment {
9999

100100
/**
101101
* Return a raw object representation of this comment.
102+
* @deprecated Use serializers instead
102103
*/
103104
toObject(): any {
104105
const result: any = {};

src/lib/models/comments/tag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class CommentTag {
3030

3131
/**
3232
* Return a raw object representation of this tag.
33+
* @deprecated Use serializers instead
3334
*/
3435
toObject(): any {
3536
const result: any = {

src/lib/models/reflections/abstract.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ export abstract class Reflection {
551551

552552
/**
553553
* Return a raw object representation of this reflection.
554+
* @deprecated Use serializers instead
554555
*/
555556
toObject(): any {
556557
const result: any = {

src/lib/models/reflections/container.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class ContainerReflection extends Reflection {
5454

5555
/**
5656
* Return a raw object representation of this reflection.
57+
* @deprecated Use serializers instead
5758
*/
5859
toObject(): any {
5960
const result = super.toObject();

src/lib/models/reflections/declaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export class DeclarationReflection extends ContainerReflection implements Defaul
181181

182182
/**
183183
* Return a raw object representation of this reflection.
184+
* @deprecated Use serializers instead
184185
*/
185186
toObject(): any {
186187
let result = super.toObject();

src/lib/models/reflections/parameter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class ParameterReflection extends Reflection implements DefaultValueConta
2727

2828
/**
2929
* Return a raw object representation of this reflection.
30+
* @deprecated Use serializers instead
3031
*/
3132
toObject(): any {
3233
const result = super.toObject();

src/lib/models/reflections/project.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export class ProjectReflection extends ContainerReflection {
126126

127127
/**
128128
* Return a raw object representation of this reflection.
129+
* @deprecated Use serializers instead
129130
*/
130131
toObject(): any {
131132
const result = super.toObject();

0 commit comments

Comments
 (0)