Skip to content

Commit a12ad13

Browse files
committed
String contatenation, template strings and 'as' casting
1 parent adec7e0 commit a12ad13

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

dist/Transpiler.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ var LuaTranspiler = /** @class */ (function () {
299299
case ts.SyntaxKind.StringLiteral:
300300
var text = node.text;
301301
return "\"" + text + "\"";
302+
case ts.SyntaxKind.TemplateExpression:
303+
return this.transpileTemplateExpression(node);
302304
case ts.SyntaxKind.NumericLiteral:
303305
return node.text;
304306
case ts.SyntaxKind.TrueKeyword:
@@ -329,6 +331,9 @@ var LuaTranspiler = /** @class */ (function () {
329331
case ts.SyntaxKind.TypeAssertionExpression:
330332
// Simply ignore the type assertion
331333
return this.transpileExpression(node.expression);
334+
case ts.SyntaxKind.AsExpression:
335+
// Also ignore as casts
336+
return this.transpileExpression(node.expression);
332337
default:
333338
throw new TranspileError("Unsupported expression kind: " + TSHelper_1.TSHelper.enumName(node.kind, ts.SyntaxKind), node);
334339
}
@@ -358,6 +363,11 @@ var LuaTranspiler = /** @class */ (function () {
358363
case ts.SyntaxKind.BarToken:
359364
result = "bit.bor(" + lhs + "," + rhs + ")";
360365
break;
366+
case ts.SyntaxKind.PlusToken:
367+
// Replace string + with ..
368+
var typeLeft = this.checker.getTypeAtLocation(node.left);
369+
if (typeLeft.flags & ts.TypeFlags.String || ts.isStringLiteral(node.left))
370+
return lhs + ".." + rhs;
361371
default:
362372
result = lhs + this.transpileOperator(node.operatorToken) + rhs;
363373
}
@@ -369,6 +379,20 @@ var LuaTranspiler = /** @class */ (function () {
369379
return result;
370380
}
371381
};
382+
LuaTranspiler.prototype.transpileTemplateExpression = function (node) {
383+
var _this = this;
384+
var parts = ["\"" + node.head.text + "\""];
385+
node.templateSpans.forEach(function (span) {
386+
var expr = _this.transpileExpression(span.expression, true);
387+
if (ts.isTemplateTail(span.literal)) {
388+
parts.push(expr + ("..\"" + span.literal.text + "\""));
389+
}
390+
else {
391+
parts.push(expr + ("..\"" + span.literal.text + "\""));
392+
}
393+
});
394+
return parts.join("..");
395+
};
372396
LuaTranspiler.prototype.transpileConditionalExpression = function (node, brackets) {
373397
var condition = this.transpileExpression(node.condition);
374398
var val1 = this.transpileExpression(node.whenTrue);

src/Transpiler.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ export class LuaTranspiler {
331331
case ts.SyntaxKind.StringLiteral:
332332
const text = (<ts.StringLiteral>node).text;
333333
return `"${text}"`;
334+
case ts.SyntaxKind.TemplateExpression:
335+
return this.transpileTemplateExpression(<ts.TemplateExpression>node);
334336
case ts.SyntaxKind.NumericLiteral:
335337
return (<ts.NumericLiteral>node).text;
336338
case ts.SyntaxKind.TrueKeyword:
@@ -361,6 +363,9 @@ export class LuaTranspiler {
361363
case ts.SyntaxKind.TypeAssertionExpression:
362364
// Simply ignore the type assertion
363365
return this.transpileExpression((<ts.TypeAssertion>node).expression);
366+
case ts.SyntaxKind.AsExpression:
367+
// Also ignore as casts
368+
return this.transpileExpression((<ts.AsExpression>node).expression);
364369
default:
365370
throw new TranspileError("Unsupported expression kind: " + tsEx.enumName(node.kind, ts.SyntaxKind), node);
366371
}
@@ -392,6 +397,11 @@ export class LuaTranspiler {
392397
case ts.SyntaxKind.BarToken:
393398
result = `bit.bor(${lhs},${rhs})`;
394399
break;
400+
case ts.SyntaxKind.PlusToken:
401+
// Replace string + with ..
402+
const typeLeft = this.checker.getTypeAtLocation(node.left);
403+
if (typeLeft.flags & ts.TypeFlags.String || ts.isStringLiteral(node.left))
404+
return lhs + ".." + rhs;
395405
default:
396406
result = lhs + this.transpileOperator(node.operatorToken) + rhs;
397407
}
@@ -404,6 +414,19 @@ export class LuaTranspiler {
404414
}
405415
}
406416

417+
transpileTemplateExpression(node: ts.TemplateExpression) {
418+
let parts = [`"${node.head.text}"`];
419+
node.templateSpans.forEach(span => {
420+
const expr = this.transpileExpression(span.expression, true);
421+
if (ts.isTemplateTail(span.literal)) {
422+
parts.push(expr + `.."${span.literal.text}"`);
423+
} else {
424+
parts.push(expr + `.."${span.literal.text}"`);
425+
}
426+
});
427+
return parts.join("..");
428+
}
429+
407430
transpileConditionalExpression(node: ts.ConditionalExpression, brackets?: boolean): string {
408431
let condition = this.transpileExpression(node.condition);
409432
let val1 = this.transpileExpression(node.whenTrue);

0 commit comments

Comments
 (0)