Skip to content

Commit

Permalink
Merge pull request #55 from southworks/issue_51
Browse files Browse the repository at this point in the history
parameters type inference
  • Loading branch information
agustinseifert authored Oct 6, 2022
2 parents 034cef6 + 4e721eb commit c8aa2b7
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@southworks/codeverter",
"version": "2.0.2",
"version": "2.0.3",
"description": "Convert TypeScript code into different languages",
"main": "lib/lib.js",
"bin": {
Expand Down Expand Up @@ -67,4 +67,4 @@
}
}
}
}
}
8 changes: 8 additions & 0 deletions src/shared/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ import { ParameterDeclaration } from "typescript";
import { TypedClassElement } from "./types/typed-class-element";

export class Parameter extends TypedClassElement<ParameterDeclaration> {
public parse(node: ParameterDeclaration): void {
super.parse(node);
// if still void use any instead
if (this.knownType == "void") {
this.knownType = "any";
this.type = "any";
}
}
}
4 changes: 3 additions & 1 deletion src/shared/type-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface InitializerNode extends TypedDeclaration {

export type TypeMapperResult = { knownType: KnownTypes, type: string | KnownTypes };

export type KnownTypes = "number" | "string" | "boolean" | "date" | "reference" | "void" | "array";
export type KnownTypes = "number" | "string" | "boolean" | "date" | "reference" | "void" | "array" | "any";

export class TypeMapper {
private static isGenericArray(node?: TypeNode): node is NodeWithTypeArguments {
Expand Down Expand Up @@ -78,6 +78,8 @@ export class TypeMapper {
case SyntaxKind.ArrayType:
case SyntaxKind.TupleType:
return "array";
case SyntaxKind.AnyKeyword:
return "any";
default: {
const isReference = typeNode?.kind == SyntaxKind.TypeReference
|| typeNode?.kind == SyntaxKind.Identifier
Expand Down
1 change: 1 addition & 0 deletions src/templating/csharp/csharp-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function getCSharpHelpers(helpers: TemplateHelper & CSharpHelpers): CShar
case "reference": return t;
case "void": return "void";
case "array": return `${fn(t, "")}[]`;
case "any": return "object";
default:
return "error";
}
Expand Down
1 change: 1 addition & 0 deletions src/templating/go/go-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function getGoHelpers(helpers: TemplateHelper & GoHelpers): GoHelpers {
case "reference": return `*${t}`;
case "void": return "";
case "array": return `[]${fn(t, "")}`;
case "any": return "interface{}";
default:
return "error";
}
Expand Down
92 changes: 92 additions & 0 deletions src/test/csharp/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,4 +1345,96 @@ describe("CSharp: method", () => {
expect(strWritter.getString()).toBe(expected.getString());
});
});

describe("parameters", () => {
test("explicit", () => {
const code = new StringWritter();
code.write("export class Test {");
code.write(" public method(val: number) {");
code.write(" return val + 2;");
code.write(" }");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new CSharpGenerator(), strWritter);

const expected = new StringWritter();
expected.write("namespace Test");
expected.write("{");
expected.write(" public class Test");
expected.write(" {");
expected.write(" public int Method(int val)");
expected.write(" {");
expected.write(" // return val + 2;");
expected.write(" return 0;");
expected.write(" }");
expected.write(" }");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});

test("infer any", () => {
const code = new StringWritter();
code.write("export class Test {");
code.write(" public method(val) {");
code.write(" return val + 1;");
code.write(" }");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new CSharpGenerator(), strWritter);

const expected = new StringWritter();
expected.write("namespace Test");
expected.write("{");
expected.write(" public class Test");
expected.write(" {");
expected.write(" public void Method(object val)");
expected.write(" {");
expected.write(" // return val + 1;");
expected.write(" return;");
expected.write(" }");
expected.write(" }");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});

test("explicit any", () => {
const code = new StringWritter();
code.write("export class Test {");
code.write(" public method(val: any) {");
code.write(" return val + 1;");
code.write(" }");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new CSharpGenerator(), strWritter);

const expected = new StringWritter();
expected.write("namespace Test");
expected.write("{");
expected.write(" public class Test");
expected.write(" {");
expected.write(" public void Method(object val)");
expected.write(" {");
expected.write(" // return val + 1;");
expected.write(" return;");
expected.write(" }");
expected.write(" }");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});
});
});
71 changes: 71 additions & 0 deletions src/test/go/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,4 +1200,75 @@ describe("GO: method", () => {
expect(strWritter.getString()).toBe(expected.getString());
});
});

describe("parameters", () => {
test("explicit", () => {
const code = new StringWritter();
code.write("function method(val: number) {");
code.write(" return val + 2;");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new GoGenerator(), strWritter);

const expected = new StringWritter();
expected.write("package test");
expected.write("");
expected.write("func Method(val int) int {");
expected.write("\t// return val + 2;");
expected.write("\treturn 0");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});

test("infer any", () => {
const code = new StringWritter();
code.write("function method(val) {");
code.write(" return val + 1;");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new GoGenerator(), strWritter);

const expected = new StringWritter();
expected.write("package test");
expected.write("");
expected.write("func Method(val interface{}) {");
expected.write("\t// return val + 1;");
expected.write("\treturn");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});

test("explicit any", () => {
const code = new StringWritter();
code.write("function method(val: any) {");
code.write(" return val + 1;");
code.write("}");

let compilationResult = compileTypeScriptCode(code.getString(), "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new GoGenerator(), strWritter);

const expected = new StringWritter();
expected.write("package test");
expected.write("");
expected.write("func Method(val interface{}) {");
expected.write("\t// return val + 1;");
expected.write("\treturn");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});
});
});

0 comments on commit c8aa2b7

Please sign in to comment.