Skip to content

Commit 791f136

Browse files
committed
Fix for arrow functions
1 parent 80ceca3 commit 791f136

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

dist/Transpiler.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var LuaTranspiler = /** @class */ (function () {
3333
// Transpile a source file
3434
LuaTranspiler.transpileSourceFile = function (node, checker) {
3535
var transpiler = new LuaTranspiler(checker);
36-
return transpiler.transpileBlock(node);
36+
return "require(\"lib.typescript\")\n" + transpiler.transpileBlock(node);
3737
};
3838
LuaTranspiler.prototype.pushIndent = function () {
3939
this.indent = this.indent + " ";
@@ -311,7 +311,7 @@ var LuaTranspiler = /** @class */ (function () {
311311
return this.transpileObjectLiteral(node);
312312
case ts.SyntaxKind.FunctionExpression:
313313
case ts.SyntaxKind.ArrowFunction:
314-
return this.transpileFunctionExpression(node);
314+
return this.transpileArrowFunction(node);
315315
case ts.SyntaxKind.NewExpression:
316316
return this.transpileNewExpression(node);
317317
case ts.SyntaxKind.ComputedPropertyName:
@@ -365,7 +365,7 @@ var LuaTranspiler = /** @class */ (function () {
365365
var condition = this.transpileExpression(node.condition);
366366
var val1 = this.transpileExpression(node.whenTrue);
367367
var val2 = this.transpileExpression(node.whenFalse);
368-
return "ITE(" + condition + ",function() return " + val1 + " end, function() return " + val2 + " end)";
368+
return "TS_ITE(" + condition + ",function() return " + val1 + " end, function() return " + val2 + " end)";
369369
};
370370
// Replace some missmatching operators
371371
LuaTranspiler.prototype.transpileOperator = function (operator) {
@@ -699,6 +699,23 @@ var LuaTranspiler = /** @class */ (function () {
699699
this.popIndent();
700700
return result + this.indent + "end ";
701701
};
702+
LuaTranspiler.prototype.transpileArrowFunction = function (node) {
703+
// Build parameter string
704+
var paramNames = [];
705+
node.parameters.forEach(function (param) {
706+
paramNames.push(param.name.escapedText);
707+
});
708+
if (ts.isBlock(node.body)) {
709+
var result = "function(" + paramNames.join(",") + ")\n";
710+
this.pushIndent();
711+
result += this.transpileBlock(node.body);
712+
this.popIndent();
713+
return result + this.indent + "end ";
714+
}
715+
else {
716+
return "function(" + paramNames.join(",") + ") return " + this.transpileExpression(node.body) + " end";
717+
}
718+
};
702719
return LuaTranspiler;
703720
}());
704721
exports.LuaTranspiler = LuaTranspiler;

src/Transpiler.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class LuaTranspiler {
1515
// Transpile a source file
1616
static transpileSourceFile(node: ts.SourceFile, checker: ts.TypeChecker): string {
1717
let transpiler = new LuaTranspiler(checker);
18-
return transpiler.transpileBlock(node);
18+
return "require(\"lib.typescript\")\n" + transpiler.transpileBlock(node);
1919
}
2020

2121
indent: string;
@@ -343,7 +343,7 @@ export class LuaTranspiler {
343343
return this.transpileObjectLiteral(<ts.ObjectLiteralExpression>node);
344344
case ts.SyntaxKind.FunctionExpression:
345345
case ts.SyntaxKind.ArrowFunction:
346-
return this.transpileFunctionExpression(<ts.FunctionExpression>node);
346+
return this.transpileArrowFunction(<ts.ArrowFunction>node);
347347
case ts.SyntaxKind.NewExpression:
348348
return this.transpileNewExpression(<ts.NewExpression>node);
349349
case ts.SyntaxKind.ComputedPropertyName:
@@ -401,7 +401,7 @@ export class LuaTranspiler {
401401
let val1 = this.transpileExpression(node.whenTrue);
402402
let val2 = this.transpileExpression(node.whenFalse);
403403

404-
return `ITE(${condition},function() return ${val1} end, function() return ${val2} end)`;
404+
return `TS_ITE(${condition},function() return ${val1} end, function() return ${val2} end)`;
405405
}
406406

407407
// Replace some missmatching operators
@@ -786,4 +786,22 @@ export class LuaTranspiler {
786786
this.popIndent();
787787
return result + this.indent + "end ";
788788
}
789+
790+
transpileArrowFunction(node: ts.ArrowFunction): string {
791+
// Build parameter string
792+
let paramNames: string[] = [];
793+
node.parameters.forEach(param => {
794+
paramNames.push(<string>(<ts.Identifier>param.name).escapedText);
795+
});
796+
797+
if (ts.isBlock(node.body)) {
798+
let result = `function(${paramNames.join(",")})\n`;
799+
this.pushIndent();
800+
result += this.transpileBlock(node.body);
801+
this.popIndent();
802+
return result + this.indent + "end ";
803+
} else {
804+
return `function(${paramNames.join(",")}) return ` + this.transpileExpression(node.body) + " end";
805+
}
806+
}
789807
}

0 commit comments

Comments
 (0)