Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/goTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,22 @@ export class GoTranspiler extends BaseTranspiler {

printStruct(node, indentation) {
const className = node.name.escapedText;
let tokens = [
`type ${className} struct {`
];

// check if we have heritage
let heritageName = '';
if (node?.heritageClauses?.length > 0) {
const heritage = node.heritageClauses[0];
const heritageType = heritage.types[0];
heritageName = this.getIden(indentation+1) + heritageType.expression.escapedText + '\n';
heritageName = this.getIden(indentation+1) + heritageType.expression.escapedText;
tokens.push(heritageName);
}

const propDeclarations = node.members.filter(member => member.kind === SyntaxKind.PropertyDeclaration);
return `type ${className} struct {\n${heritageName}${propDeclarations.map(member => this.printNode(member, indentation+1)).join("\n")}\n}`;
const propDeclarations = node.members.filter(member => member.kind === SyntaxKind.PropertyDeclaration).map((member) => this.printNode(member, indentation + 1));
tokens = tokens.concat(propDeclarations);
tokens.push('}');
return tokens.join('\n');
}

printNewStructMethod(node){
Expand Down
25 changes: 24 additions & 1 deletion tests/goTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('go transpiling tests', () => {
"}";
const go =
"type Test struct {\n"+
"\n"+
"}\n"+
"\n"+
"func NewTest() Test {\n"+
Expand Down Expand Up @@ -88,6 +87,30 @@ describe('go transpiling tests', () => {
const output = transpiler.transpileGo(ts).content;
expect(output).toBe(go);
});
test('class extends declaration', () => {
const ts =
"class Toyota extends Car {\n" +
" main() {\n" +
" return 1\n" +
" }\n" +
"}";
const go =
"type Toyota struct {\n"+
" Car\n"+
"}\n"+
"\n"+
"func NewToyota() Toyota {\n"+
" p := Toyota{}\n"+
" setDefaults(&p)\n"+
" return p\n"+
"}\n"+
"\n"+
"func (this *Toyota) Main() interface{} {\n"+
" return 1\n"+
"}";
const output = transpiler.transpileGo(ts).content;
expect(output).toBe(go);
});
// test('basic try catch', () => {
// assert true
// const ts =
Expand Down
Loading