Skip to content

Commit 1c3fd47

Browse files
lollekoPerryvw
authored andcommitted
Added support for the 2 static string functions that currently exist (#23)
* Added support for the 2 static string functions that currently exist in JS Fixed minor issue in default import error handling. * Updated comment * Updated Dist
1 parent 3f97c79 commit 1c3fd47

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

dist/Transpiler.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ var LuaTranspiler = /** @class */ (function () {
145145
};
146146
LuaTranspiler.prototype.transpileImport = function (node) {
147147
var importFile = this.transpileExpression(node.moduleSpecifier);
148-
if (!node.importClause) {
148+
if (!node.importClause || !node.importClause.namedBindings) {
149149
throw new TranspileError("Default Imports are not supported, please use named imports instead!", node);
150150
}
151151
var imports = node.importClause.namedBindings;
@@ -542,16 +542,20 @@ var LuaTranspiler = /** @class */ (function () {
542542
var params_1 = this.transpileArguments(node.arguments);
543543
return this.transpileMathExpression(node.expression.name) + ("(" + params_1 + ")");
544544
}
545+
if (ts.isIdentifier(node.expression) && node.expression.escapedText == "String") {
546+
var params_2 = this.transpileArguments(node.arguments);
547+
return this.transpileStringExpression(node.expression.name) + ("(" + params_2 + ")");
548+
}
545549
// Include context parameter if present
546550
var callPath_1 = (expType && expType.symbol) ? expType.symbol.name + "." + node.expression.name.escapedText : this.transpileExpression(node.expression);
547-
var params_2 = this.transpileArguments(node.arguments, node.expression.expression);
548-
return callPath_1 + "(" + params_2 + ")";
551+
var params_3 = this.transpileArguments(node.arguments, node.expression.expression);
552+
return callPath_1 + "(" + params_3 + ")";
549553
}
550554
// Handle super calls properly
551555
if (node.expression.kind == ts.SyntaxKind.SuperKeyword) {
552556
var callPath_2 = this.transpileExpression(node.expression);
553-
var params_3 = this.transpileArguments(node.arguments, ts.createNode(ts.SyntaxKind.ThisKeyword));
554-
return "self.__base.constructor(" + params_3 + ")";
557+
var params_4 = this.transpileArguments(node.arguments, ts.createNode(ts.SyntaxKind.ThisKeyword));
558+
return "self.__base.constructor(" + params_4 + ")";
555559
}
556560
var callPath = this.transpileExpression(node.expression);
557561
var params = this.transpileArguments(node.arguments);
@@ -584,6 +588,21 @@ var LuaTranspiler = /** @class */ (function () {
584588
throw new TranspileError("Unsupported string function: " + expression.name.escapedText, node);
585589
}
586590
};
591+
// Transpile a String._ property
592+
LuaTranspiler.prototype.transpileStringExpression = function (identifier) {
593+
var translation = {
594+
fromCharCode: "string.char",
595+
fromCodePoint: "utf8.char"
596+
};
597+
// TODO at check if compiler options is LUA 5.3
598+
// should throw an exception if codepoint is used sub 5.3
599+
if (translation[identifier.escapedText]) {
600+
return "" + translation[identifier.escapedText];
601+
}
602+
else {
603+
throw new TranspileError("Unsupported string property " + identifier.escapedText + ".", identifier);
604+
}
605+
};
587606
LuaTranspiler.prototype.transpileArrayCallExpression = function (node) {
588607
var expression = node.expression;
589608
var params = this.transpileArguments(node.arguments);

src/Transpiler.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class LuaTranspiler {
151151

152152
transpileImport(node: ts.ImportDeclaration): string {
153153
const importFile = this.transpileExpression(node.moduleSpecifier);
154-
if (!node.importClause) {
154+
if (!node.importClause || !node.importClause.namedBindings) {
155155
throw new TranspileError("Default Imports are not supported, please use named imports instead!", node);
156156
}
157157

@@ -595,10 +595,15 @@ export class LuaTranspiler {
595595
return this.transpileMathExpression(node.expression.name) + `(${params})`;
596596
}
597597

598+
if (ts.isIdentifier(node.expression) && node.expression.escapedText == "String") {
599+
const params = this.transpileArguments(node.arguments);
600+
return this.transpileStringExpression(node.expression.name) + `(${params})`;
601+
}
602+
598603
// Include context parameter if present
599604
let callPath = (expType && expType.symbol) ? `${expType.symbol.name}.${node.expression.name.escapedText}` : this.transpileExpression(node.expression);
600605
let params = this.transpileArguments(node.arguments, node.expression.expression);
601-
606+
602607
return `${callPath}(${params})`;
603608
}
604609

@@ -640,6 +645,23 @@ export class LuaTranspiler {
640645
}
641646
}
642647

648+
// Transpile a String._ property
649+
transpileStringExpression(identifier: ts.Identifier): string {
650+
const translation = {
651+
fromCharCode: "string.char",
652+
fromCodePoint: "utf8.char"
653+
};
654+
655+
// TODO at check if compiler options is LUA 5.3
656+
// should throw an exception if codepoint is used sub 5.3
657+
658+
if (translation[<string>identifier.escapedText]) {
659+
return `${translation[<string>identifier.escapedText]}`;
660+
} else {
661+
throw new TranspileError(`Unsupported string property ${identifier.escapedText}.`, identifier);
662+
}
663+
}
664+
643665
transpileArrayCallExpression(node: ts.CallExpression): string {
644666
const expression = <ts.PropertyAccessExpression>node.expression;
645667
const params = this.transpileArguments(node.arguments);

0 commit comments

Comments
 (0)