Skip to content

Commit b74dbd3

Browse files
authored
refactor: move toTypeScriptAst() method out of types
Refactor `toTypeScriptAst()` methods to use centralized router pattern
2 parents 3bac6b2 + 4429ac3 commit b74dbd3

File tree

15 files changed

+248
-188
lines changed

15 files changed

+248
-188
lines changed

src/type/classes/AbstractType.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ export abstract class AbstractType<S extends schema.Schema> implements BaseType<
288288
return random(this);
289289
}
290290

291-
public toTypeScriptAst(): ts.TsNode {
292-
const node: ts.TsUnknownKeyword = {node: 'UnknownKeyword'};
293-
return node;
291+
public toTypeScriptAst(): ts.TsType {
292+
// Use dynamic import to avoid circular dependency
293+
const converter = require('../../typescript/converter');
294+
return converter.toTypeScriptAst(this.getSchema());
294295
}
295296

296297
public toJson(value: unknown, system: TypeSystem | undefined = this.system): json_string<unknown> {

src/type/classes/AnyType.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,4 @@ export class AnyType extends AbstractType<schema.AnySchema> {
7070
public codegenJsonEncoder(ctx: JsonEncoderCodegenContext, value: JsExpression): void {
7171
this.codegenBinaryEncoder(ctx, value);
7272
}
73-
74-
public toTypeScriptAst(): ts.TsType {
75-
return {node: 'AnyKeyword'};
76-
}
7773
}

src/type/classes/ArrayType.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,6 @@ export class ArrayType<T extends Type> extends AbstractType<schema.ArraySchema<S
143143
);
144144
}
145145

146-
public toTypeScriptAst(): ts.TsArrayType {
147-
return {
148-
node: 'ArrayType',
149-
elementType: this.type.toTypeScriptAst() as ts.TsType,
150-
};
151-
}
152-
153146
public toJson(value: unknown, system: TypeSystem | undefined = this.system): json_string<unknown> {
154147
const length = (value as unknown[]).length;
155148
if (!length) return '[]' as json_string<unknown>;

src/type/classes/BinaryType.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,6 @@ export class BinaryType<T extends Type> extends AbstractType<schema.BinarySchema
103103
this.codegenBinaryEncoder(ctx, value);
104104
}
105105

106-
public toTypeScriptAst(): ts.TsGenericTypeAnnotation {
107-
return {
108-
node: 'GenericTypeAnnotation',
109-
id: {
110-
node: 'Identifier',
111-
name: 'Uint8Array',
112-
},
113-
};
114-
}
115-
116106
public toJson(value: unknown, system: TypeSystem | undefined = this.system): json_string<unknown> {
117107
return ('"' + stringifyBinary(value as Uint8Array) + '"') as json_string<unknown>;
118108
}

src/type/classes/BooleanType.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ export class BooleanType extends AbstractType<schema.BooleanSchema> {
5252
this.codegenBinaryEncoder(ctx, value);
5353
}
5454

55-
public toTypeScriptAst(): ts.TsBooleanKeyword {
56-
return {node: 'BooleanKeyword'};
57-
}
58-
5955
public toJson(value: unknown, system: TypeSystem | undefined = this.system) {
6056
return (value ? 'true' : 'false') as json_string<boolean>;
6157
}

src/type/classes/ConstType.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,6 @@ export class ConstType<V = any> extends AbstractType<schema.ConstSchema<V>> {
7070
this.codegenBinaryEncoder(ctx, value);
7171
}
7272

73-
public toTypeScriptAst() {
74-
const value = this.schema.value;
75-
if (value === null) {
76-
const node: ts.TsNullKeyword = {node: 'NullKeyword'};
77-
return node;
78-
}
79-
switch (typeof value) {
80-
case 'string': {
81-
const node: ts.TsStringLiteral = {node: 'StringLiteral', text: value};
82-
return node;
83-
}
84-
case 'number': {
85-
const node: ts.TsNumericLiteral = {
86-
node: 'NumericLiteral',
87-
text: value.toString(),
88-
};
89-
return node;
90-
}
91-
case 'boolean': {
92-
const node: ts.TsTrueKeyword | ts.TsFalseKeyword = {
93-
node: value ? 'TrueKeyword' : 'FalseKeyword',
94-
};
95-
return node;
96-
}
97-
case 'object': {
98-
const node: ts.TsObjectKeyword = {node: 'ObjectKeyword'};
99-
return node;
100-
}
101-
default: {
102-
const node: ts.TsUnknownKeyword = {node: 'UnknownKeyword'};
103-
return node;
104-
}
105-
}
106-
}
107-
10873
public toJson(value: unknown, system: TypeSystem | undefined = this.system) {
10974
return this.__json;
11075
}

src/type/classes/FunctionType.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,6 @@ export class FunctionType<Req extends Type, Res extends Type> extends AbstractTy
5656
return this;
5757
}
5858

59-
public toTypeScriptAst(): ts.TsFunctionType {
60-
const node: ts.TsFunctionType = {
61-
node: 'FunctionType',
62-
parameters: [
63-
{
64-
node: 'Parameter',
65-
name: {
66-
node: 'Identifier',
67-
name: 'request',
68-
},
69-
type: this.req.toTypeScriptAst(),
70-
},
71-
],
72-
type: {
73-
node: 'TypeReference',
74-
typeName: {
75-
node: 'Identifier',
76-
name: 'Promise',
77-
},
78-
typeArguments: [this.res.toTypeScriptAst()],
79-
},
80-
};
81-
return node;
82-
}
83-
8459
public toString(tab: string = ''): string {
8560
return super.toString(tab) + toStringTree(tab, this);
8661
}
@@ -124,38 +99,6 @@ export class FunctionStreamingType<Req extends Type, Res extends Type> extends A
12499
return this;
125100
}
126101

127-
public toTypeScriptAst(): ts.TsFunctionType {
128-
const node: ts.TsFunctionType = {
129-
node: 'FunctionType',
130-
parameters: [
131-
{
132-
node: 'Parameter',
133-
name: {
134-
node: 'Identifier',
135-
name: 'request$',
136-
},
137-
type: {
138-
node: 'TypeReference',
139-
typeName: {
140-
node: 'Identifier',
141-
name: 'Observable',
142-
},
143-
typeArguments: [this.req.toTypeScriptAst()],
144-
},
145-
},
146-
],
147-
type: {
148-
node: 'TypeReference',
149-
typeName: {
150-
node: 'Identifier',
151-
name: 'Observable',
152-
},
153-
typeArguments: [this.res.toTypeScriptAst()],
154-
},
155-
};
156-
return node;
157-
}
158-
159102
public toString(tab: string = ''): string {
160103
return super.toString(tab) + toStringTree(tab, this);
161104
}

src/type/classes/MapType.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@ export class MapType<T extends Type> extends AbstractType<schema.MapSchema<Schem
134134
ctx.blob(objEndBlob);
135135
}
136136

137-
public toTypeScriptAst(): ts.TsTypeReference {
138-
const node: ts.TsTypeReference = {
139-
node: 'TypeReference',
140-
typeName: 'Record',
141-
typeArguments: [{node: 'StringKeyword'}, this.type.toTypeScriptAst()],
142-
};
143-
// augmentWithComment(this.schema, node);
144-
return node;
145-
}
146-
147137
public toJson(value: unknown, system: TypeSystem | undefined = this.system): json_string<unknown> {
148138
const map = value as Record<string, unknown>;
149139
const keys = Object.keys(map);

src/type/classes/NumberType.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ export class NumberType extends AbstractType<schema.NumberSchema> {
114114
this.codegenBinaryEncoder(ctx, value);
115115
}
116116

117-
public toTypeScriptAst(): ts.TsNumberKeyword {
118-
return {node: 'NumberKeyword'};
119-
}
120-
121117
public toJson(value: unknown, system: TypeSystem | undefined = this.system) {
122118
return ('' + value) as json_string<number>;
123119
}

src/type/classes/ObjectType.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -460,31 +460,6 @@ if (${rLength}) {
460460
}
461461
}
462462

463-
public toTypeScriptAst(): ts.TsTypeLiteral {
464-
const node: ts.TsTypeLiteral = {
465-
node: 'TypeLiteral',
466-
members: [],
467-
};
468-
const fields = this.fields;
469-
for (const field of fields) {
470-
const member: ts.TsPropertySignature = {
471-
node: 'PropertySignature',
472-
name: field.key,
473-
type: field.value.toTypeScriptAst(),
474-
};
475-
if (field instanceof ObjectOptionalFieldType) member.optional = true;
476-
augmentWithComment(field.getSchema(), member);
477-
node.members.push(member);
478-
}
479-
if (this.schema.unknownFields || this.schema.encodeUnknownFields)
480-
node.members.push({
481-
node: 'IndexSignature',
482-
type: {node: 'UnknownKeyword'},
483-
});
484-
augmentWithComment(this.schema, node);
485-
return node;
486-
}
487-
488463
public toJson(value: unknown, system: TypeSystem | undefined = this.system): json_string<unknown> {
489464
const fields = this.fields;
490465
const length = fields.length;

0 commit comments

Comments
 (0)