Skip to content
Merged
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
18 changes: 2 additions & 16 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ export class LuaTranspiler {
return this.transpileExpression((node as ts.DeleteExpression).expression) + "=nil";
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.ArrowFunction:
return this.transpileArrowFunction(node as ts.ArrowFunction);
return this.transpileFunctionExpression(node as ts.ArrowFunction);
case ts.SyntaxKind.NewExpression:
return this.transpileNewExpression(node as ts.NewExpression);
case ts.SyntaxKind.ComputedPropertyName:
Expand Down Expand Up @@ -1363,21 +1363,7 @@ export class LuaTranspiler {
return "{" + properties.join(",") + "}";
}

public transpileFunctionExpression(node: ts.FunctionExpression): string {
// Build parameter string
const paramNames: string[] = [];
node.parameters.forEach((param) => {
paramNames.push((param.name as ts.Identifier).escapedText as string);
});

let result = `function(${paramNames.join(",")})\n`;
this.pushIndent();
result += this.transpileBlock(node.body);
this.popIndent();
return result + this.indent + "end\n";
}

public transpileArrowFunction(node: ts.ArrowFunction): string {
public transpileFunctionExpression(node: ts.ArrowFunction): string {
// Build parameter string
const paramNames: string[] = [];
node.parameters.forEach((param) => {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,28 @@ export class ExpressionTests {
public conditional(input: string, lua: string) {
Expect(util.transpileString(input)).toBe(lua);
}

@Test("Arrow Function Expression")
public arrowFunctionExpression(input: string) {
// Transpile
const lua = util.transpileString(`let add = (a, b) => a+b; return add(1,2);`);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(3);
}

@Test("Function Expression")
public functionExpression(input: string) {
// Transpile
const lua = util.transpileString(`let add = function(a, b) {return a+b}; return add(1,2);`);

// Execute
const result = util.executeLua(lua);

// Assert
Expect(result).toBe(3);
}
}
22 changes: 22 additions & 0 deletions test/unit/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ export class StringTests {
Expect(result).toBe(String.fromCharCode(...inp));
}

@TestCase(12, 23, 43)
@TestCase("test", "hello", "bye")
@TestCase("test", 42, "bye")
@TestCase("test", 42, 12)
@Test("Template Strings")
public templateStrings(a: any, b: any, c: any) {
// Transpile
const a1 = typeof(a) === "string" ? "'" + a + "'" : a;
const b1 = typeof(b) === "string" ? "'" + b + "'" : b;
const c1 = typeof(c) === "string" ? "'" + c + "'" : c;

let lua = util.transpileString(
"let a = " + a1 + "; let b = " + b1 + "; let c = " + c1 + "; return `${a} ${b} test ${c}`;"
);

// Execute
let result = util.executeLua(lua);

// Assert
Expect(result).toBe(`${a} ${b} test ${c}`);
}

@TestCase("hello test", "", "")
@TestCase("hello test", " ", "")
@TestCase("hello test", "hello", "")
Expand Down