Skip to content

Commit

Permalink
fix: render TS class properties in proper way (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 23, 2021
1 parent a626b9f commit b0ad5ba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 61 deletions.
21 changes: 14 additions & 7 deletions src/generators/typescript/renderers/ClassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,39 @@ ${this.indent(this.renderBlock(content, 2))}
}

export const TS_DEFAULT_CLASS_PRESET: ClassPreset<ClassRenderer> = {
self({ renderer }) {
return renderer.defaultSelf();
async self({ renderer }) {
return `export ${await renderer.defaultSelf()}`;
},
ctor({ renderer, model }) {
const properties = model.properties || {};
const assigments = Object.keys(properties).map(property => {
property = FormatHelpers.toCamelCase(property);
return `this.${property} = input.${property};`;
return `this._${property} = input.${property};`;
});
const ctorProperties: string[] = [];
for (const [propertyName, property] of Object.entries(properties)) {
const rendererProperty = renderer.renderProperty(propertyName, property, model).replace(';', ',');
ctorProperties.push(rendererProperty);
}

return `constructor(input: ${model.$id}Input) {
return `constructor(input: {
${renderer.indent(renderer.renderBlock(ctorProperties))}
}) {
${renderer.indent(renderer.renderBlock(assigments))}
}`;
},
property({ renderer, propertyName, property, parentModel }) {
return `private ${renderer.renderProperty(propertyName, property, parentModel)}`;
return `private _${renderer.renderProperty(propertyName, property, parentModel)}`;
},
getter({ renderer, propertyName, property }) {
propertyName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property);
return `get ${propertyName}()${signature} { return this.${propertyName}; }`;
return `get ${propertyName}()${signature} { return this._${propertyName}; }`;
},
setter({ renderer, propertyName, property }) {
propertyName = FormatHelpers.toCamelCase(propertyName);
const signature = renderer.renderTypeSignature(property);
const arg = `${propertyName}${signature}`;
return `set ${propertyName}(${arg}) { this.${propertyName} = ${propertyName}; }`;
return `set ${propertyName}(${arg}) { this._${propertyName} = ${propertyName}; }`;
},
};
4 changes: 2 additions & 2 deletions src/generators/typescript/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ ${this.indent(this.renderBlock(content, 2))}
}

export const TS_DEFAULT_ENUM_PRESET: EnumPreset<EnumRenderer> = {
self({ renderer }) {
return renderer.defaultSelf();
async self({ renderer }) {
return `export ${await renderer.defaultSelf()}`;
},
item({ item }) {
return `${FormatHelpers.toConstantCase(`${item}`)} = "${item}",`;
Expand Down
4 changes: 2 additions & 2 deletions src/generators/typescript/renderers/InterfaceRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ ${this.indent(this.renderBlock(content, 2))}
}

export const TS_DEFAULT_INTERFACE_PRESET: InterfacePreset<InterfaceRenderer> = {
self({ renderer }) {
return renderer.defaultSelf();
async self({ renderer }) {
return `export ${await renderer.defaultSelf()}`;
},
property({ renderer, propertyName, property, parentModel }) {
return renderer.renderProperty(propertyName, property, parentModel);
Expand Down
4 changes: 2 additions & 2 deletions src/generators/typescript/renderers/TypeRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TypeRenderer extends TypeScriptRenderer {
}

export const TS_DEFAULT_TYPE_PRESET: TypePreset<TypeRenderer> = {
self({ renderer }) {
return renderer.defaultSelf();
async self({ renderer }) {
return `export ${await renderer.defaultSelf()}`;
},
};
103 changes: 55 additions & 48 deletions test/generators/typescript/TypeScriptGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,53 @@ describe('TypeScriptGenerator', function() {
},
required: ["street_name", "city", "state", "house_number", "array_type"],
};
const expected = `class Address {
private streetName: string;
private city: string;
private state: string;
private houseNumber: number;
private marriage?: boolean;
private members?: string | number | boolean;
private arrayType: Array<string | number>;
constructor(input: AddressInput) {
this.streetName = input.streetName;
this.city = input.city;
this.state = input.state;
this.houseNumber = input.houseNumber;
this.marriage = input.marriage;
this.members = input.members;
this.arrayType = input.arrayType;
const expected = `export class Address {
private _streetName: string;
private _city: string;
private _state: string;
private _houseNumber: number;
private _marriage?: boolean;
private _members?: string | number | boolean;
private _arrayType: Array<string | number>;
constructor(input: {
streetName: string,
city: string,
state: string,
houseNumber: number,
marriage?: boolean,
members?: string | number | boolean,
arrayType: Array<string | number>,
}) {
this._streetName = input.streetName;
this._city = input.city;
this._state = input.state;
this._houseNumber = input.houseNumber;
this._marriage = input.marriage;
this._members = input.members;
this._arrayType = input.arrayType;
}
get streetName(): string { return this.streetName; }
set streetName(streetName: string) { this.streetName = streetName; }
get streetName(): string { return this._streetName; }
set streetName(streetName: string) { this._streetName = streetName; }
get city(): string { return this.city; }
set city(city: string) { this.city = city; }
get city(): string { return this._city; }
set city(city: string) { this._city = city; }
get state(): string { return this.state; }
set state(state: string) { this.state = state; }
get state(): string { return this._state; }
set state(state: string) { this._state = state; }
get houseNumber(): number { return this.houseNumber; }
set houseNumber(houseNumber: number) { this.houseNumber = houseNumber; }
get houseNumber(): number { return this._houseNumber; }
set houseNumber(houseNumber: number) { this._houseNumber = houseNumber; }
get marriage(): boolean { return this.marriage; }
set marriage(marriage: boolean) { this.marriage = marriage; }
get marriage(): boolean { return this._marriage; }
set marriage(marriage: boolean) { this._marriage = marriage; }
get members(): string | number | boolean { return this.members; }
set members(members: string | number | boolean) { this.members = members; }
get members(): string | number | boolean { return this._members; }
set members(members: string | number | boolean) { this._members = members; }
get arrayType(): Array<string | number> { return this.arrayType; }
set arrayType(arrayType: Array<string | number>) { this.arrayType = arrayType; }
get arrayType(): Array<string | number> { return this._arrayType; }
set arrayType(arrayType: Array<string | number>) { this._arrayType = arrayType; }
}`;

const inputModel = await generator.process(doc);
Expand All @@ -82,22 +90,21 @@ describe('TypeScriptGenerator', function() {
};
const expected = `export class CustomClass {
@JsonProperty("property")
private property?: string;
private _property?: string;
constructor(input: CustomClassInput) {
this.property = input.property;
constructor(input: {
property?: string,
}) {
this._property = input.property;
}
get property(): string { return this.property; }
set property(property: string) { this.property = property; }
get property(): string { return this._property; }
set property(property: string) { this._property = property; }
}`;

generator = new TypeScriptGenerator({ presets: [
{
class: {
self({ content }) {
return `export ${content}`;
},
property({ propertyName, content }) {
return `@JsonProperty("${propertyName}")
${content}`;
Expand Down Expand Up @@ -128,7 +135,7 @@ ${content}`;
},
required: ["street_name", "city", "state", "house_number", "array_type"],
};
const expected = `interface Address {
const expected = `export interface Address {
streetName: string;
city: string;
state: string;
Expand Down Expand Up @@ -161,7 +168,7 @@ ${content}`;
{
interface: {
self({ content }) {
return `export ${content}`;
return `${content}`;
},
}
}
Expand All @@ -180,7 +187,7 @@ ${content}`;
type: "string",
enum: ["Texas", "Alabama", "California"],
};
const expected = `enum States {
const expected = `export enum States {
TEXAS = "Texas",
ALABAMA = "Alabama",
CALIFORNIA = "California",
Expand Down Expand Up @@ -212,7 +219,7 @@ ${content}`;
{
enum: {
self({ content }) {
return `export ${content}`;
return `${content}`;
},
}
}
Expand All @@ -233,7 +240,7 @@ ${content}`;
$id: "TypePrimitive",
type: "string",
};
const expected = `type TypePrimitive = string;`
const expected = `export type TypePrimitive = string;`

const inputModel = await generator.process(doc);
const model = inputModel.models["TypePrimitive"];
Expand All @@ -250,7 +257,7 @@ ${content}`;
$id: "TypeEnum",
enum: ["Texas", "Alabama", "California", 0, 1, false, true],
};
const expected = `type TypeEnum = "Texas" | "Alabama" | "California" | 0 | 1 | false | true;`
const expected = `export type TypeEnum = "Texas" | "Alabama" | "California" | 0 | 1 | false | true;`

const inputModel = await generator.process(doc);
const model = inputModel.models["TypeEnum"];
Expand All @@ -264,7 +271,7 @@ ${content}`;
$id: "TypeUnion",
type: ["string", "number", "boolean"],
};
const expected = `type TypeUnion = string | number | boolean;`
const expected = `export type TypeUnion = string | number | boolean;`

const inputModel = await generator.process(doc);
const model = inputModel.models["TypeUnion"];
Expand All @@ -282,7 +289,7 @@ ${content}`;
type: "string",
}
};
const expected = `type TypeArray = Array<string>;`
const expected = `export type TypeArray = Array<string>;`

const inputModel = await generator.process(doc);
const model = inputModel.models["TypeArray"];
Expand All @@ -300,7 +307,7 @@ ${content}`;
type: ["string", "number", "boolean"],
}
};
const expected = `type TypeArray = Array<string | number | boolean>;`
const expected = `export type TypeArray = Array<string | number | boolean>;`

const inputModel = await generator.process(doc);
const model = inputModel.models["TypeArray"];
Expand Down

0 comments on commit b0ad5ba

Please sign in to comment.