diff --git a/src/shared/element.ts b/src/shared/element.ts index 2af5dfa..bd748b5 100644 --- a/src/shared/element.ts +++ b/src/shared/element.ts @@ -12,12 +12,12 @@ import { ElementFactory, Factory, FactoryParams } from "./types/factory"; import { SourceElement } from "./types/source-element"; export abstract class Element implements SourceElement { - private parent!: SourceElement; private elementFactory: ElementFactory; private values: ElementValues = new ElementValues(); public name!: string; public kind!: ElementKind; + public parent!: SourceElement; protected constructor(params: FactoryParams) { this.elementFactory = params.elementFactory; @@ -25,7 +25,7 @@ export abstract class Element implements SourceEleme private addElementAndParse(kind: ElementKind, node: Declaration): void { let element = this.createElement(kind); - element.setParent(this); + element.parent = this; if (node) { element.parse(node); } @@ -65,8 +65,4 @@ export abstract class Element implements SourceEleme public parse(node: K): void { this.name = (node.name as Identifier)?.escapedText ?? ""; } - - public setParent(element: SourceElement): void { - this.parent = element; - } } diff --git a/src/shared/types/source-element.ts b/src/shared/types/source-element.ts index 495b502..a0dc95b 100644 --- a/src/shared/types/source-element.ts +++ b/src/shared/types/source-element.ts @@ -26,7 +26,7 @@ export type SourceElementNamed = Parseable< export interface SourceElement extends SourceElementNamed { kind: ElementKind; - setParent(element: SourceElement): void; + parent: SourceElement; }; export interface VisibilitySourceElement extends SourceElement { diff --git a/src/templating/go/go-helpers.ts b/src/templating/go/go-helpers.ts index 755b76f..36309ec 100644 --- a/src/templating/go/go-helpers.ts +++ b/src/templating/go/go-helpers.ts @@ -169,11 +169,11 @@ export function getGoHelpers(helpers: TemplateHelper & GoHelpers): GoHelpers { let type = helpers.mapType(p); return `${p.name} ${type}`; }).join(", "); - - let result = `func New${helpers.capitalize(v.name)}(${params}) *${helpers.fixName(v)} {`; + let newPrefix = v.visibility == "private" ? "new" : "New"; + let result = `func ${newPrefix}${helpers.capitalize(v.name)}(${params}) *${helpers.fixName(v.parent as VisibilitySourceElement)} {`; const firstLetter = v.name.charAt(0).toLowerCase(); - const init = `${firstLetter} := ${helpers.fixName(v)}{}`; + const init = `${firstLetter} := ${helpers.fixName(v.parent as VisibilitySourceElement)}{}`; result = `${result}\n\t${init}`; result = `${result}${helpers.printMethodBody(v)}`; result = `${result}\n\treturn &${firstLetter}`; diff --git a/src/test/go/constructor.spec.ts b/src/test/go/constructor.spec.ts index 825463f..e96a376 100644 --- a/src/test/go/constructor.spec.ts +++ b/src/test/go/constructor.spec.ts @@ -90,4 +90,30 @@ describe("GO: constructor", () => { expect(strWritter.getString()).toBe(expected.getString()); }); + + test("private", () => { + const code = ` + export class Test { + private constructor() { + } + }`; + let compilationResult = compileTypeScriptCode(code, "test.ts"); + + const strWritter = new StringWritter(); + printFile(compilationResult, new GoGenerator(), strWritter); + + const expected = new StringWritter(); + expected.write(`package test`); + expected.write(""); + expected.write("type Test struct {"); + expected.write("}"); + expected.write(""); + expected.write("func newTest() *Test {"); + expected.write("\tt := Test{}"); + expected.write("\treturn &t"); + expected.write("}"); + expected.write(""); + + expect(strWritter.getString()).toBe(expected.getString()); + }); });