diff --git a/.eslintrc.js b/.eslintrc.cjs
similarity index 90%
rename from .eslintrc.js
rename to .eslintrc.cjs
index 1b03825..b419f8e 100644
--- a/.eslintrc.js
+++ b/.eslintrc.cjs
@@ -8,12 +8,6 @@ module.exports = {
amd: true,
},
rules: {
- "prettier/prettier": [
- "warn",
- {
- printWidth: 120,
- },
- ],
"max-len": ["warn", { code: 120, ignoreComments: true, ignoreStrings: true }],
"prefer-const": [
"error",
diff --git a/package.json b/package.json
index 7a3efd2..902e79f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@xiangnanscu/lua2js",
- "version": "0.29.0",
+ "version": "0.30.0",
"main": "src/lua2js.mjs",
"type": "module",
"bin": {
diff --git a/src/components/MainPage.vue b/src/components/MainPage.vue
index 93bed9d..4e9dfd1 100644
--- a/src/components/MainPage.vue
+++ b/src/components/MainPage.vue
@@ -1,94 +1,28 @@
diff --git a/src/components/code.lua b/src/components/code.lua
new file mode 100644
index 0000000..2d19335
--- /dev/null
+++ b/src/components/code.lua
@@ -0,0 +1,67 @@
+local x = [[`\`\\`]]
+for i, e in ipairs(t) do
+ print(i, e)
+end
+for i, e in ipairs(t) do
+ print(e)
+end
+local a = dict({a=1}, b)
+local a = list({1,2}, b)
+print(a[1])
+local a = {unpack(t)}
+local function snake_case_name(x, y)
+ if x > 0 or y > 0then
+ return nil, string.format('error: x is %s and y is %s', x, y)
+ else
+ return x + y, x - y
+ end
+end
+console.log(string.format("hello %s", world))
+console.log(string.format([[hello:
+ you are multiple line %s]], world))
+type(x+1)
+table.concat(t, ",")
+table_concat(t, ",")
+table_concat({1,2,3}, ",")
+t[#t+1] = 1
+table_insert(t,1,a)
+table.insert(t,1,a)
+table_insert(t, 1)
+table.insert(t, 1)
+local array = {1,2}
+local dict = {a=1, b=2}
+Test = class {
+ a = {1, 2}
+}
+local function foo(x, y)
+ return x + y
+end
+local c = {a=1}
+function c.foo(x, y)
+ return x + y
+end
+function c.foo(self, x, y)
+ return x + y + self.n
+end
+function c:foo(x, y)
+ return x + y + self.n
+end
+local TestClass = class {
+ p1 = 'Hi class property p1',
+ p2 = 'Hi class property p2',
+ static_func = function(x, y)
+ return x + y
+ end,
+ class_method = function(cls)
+ cls:say_class_hi()
+ end,
+ say_class_hi = function(cls)
+ console.log(cls.p1)
+ end,
+ instance_method = function(self)
+ self:say_instance_hi()
+ end,
+ say_instance_hi = function(self)
+ console.log(this.p2)
+ end
+}
diff --git a/src/lua2js.mjs b/src/lua2js.mjs
index 40f925a..0fc94c0 100644
--- a/src/lua2js.mjs
+++ b/src/lua2js.mjs
@@ -6,21 +6,21 @@ import parserBabel from "prettier/parser-babel.js";
const defaultOptions = {
printToConsoleLog: true,
tryUseOfLoop: true,
- indexMinusOne: true,
- returnNilToThrow: true,
+ indexMinusOne: false,
+ returnNilToThrow: false,
errorToThrow: true,
tostring: true,
- dict: true,
- list: true,
+ dict: false,
+ list: false,
unpack: true,
tonumber: true,
- class: true,
+ class: false,
selfToThis: true,
clsToThis: true,
typeToTypeof: true,
- stringFormat: true,
- tableConcat: true,
- tableInsert: true,
+ stringFormat: false,
+ tableConcat: false,
+ tableInsert: false,
camelStyle: false,
};
function joinUnderscore(length) {
@@ -84,6 +84,7 @@ const IdentifierMap = {
constructor: "_constructor",
extends: "_extends",
class: "_class",
+ super: "_super",
default: "_js_default",
debugger: "_debugger",
};
@@ -299,7 +300,7 @@ function luaLiteral2Js(s) {
const head = s[0];
const res = getLuaStringToken(s);
if (head == "[") {
- return "`" + res.replaceAll("\\", "\\\\").replaceAll("`", "\\`") + "`";
+ return "`" + res.replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("$", "\\$") + "`";
} else {
return head + res + head;
}
@@ -450,7 +451,7 @@ function ast2js(ast, opts = {}) {
}
function _ast2js(ast) {
if (ast instanceof Array) {
- return ast.map(_ast2js).join(";");
+ return ast.map(_ast2js).join(";\n");
}
switch (ast.type) {
case "Chunk":
@@ -573,11 +574,11 @@ function ast2js(ast, opts = {}) {
case "IfStatement":
return ast.clauses.map(_ast2js).join("\n");
case "IfClause":
- return `if (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;
+ return `if (${_ast2js(ast.condition)}) {\n${_ast2js(ast.body)}\n}`;
case "ElseClause":
- return `else {${ast.body.map(_ast2js).join(";")}}`;
+ return `else {\n${ast.body.map(_ast2js).join(";\n")}\n}`;
case "ElseifClause":
- return `else if (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;
+ return `else if (${_ast2js(ast.condition)}) {\n${_ast2js(ast.body)}\n}`;
case "FunctionDeclaration":
tagVarargAsSpread(ast.parameters);
if (ast.isClassMode) {
@@ -617,7 +618,7 @@ function ast2js(ast, opts = {}) {
ast.parameters = ast.parameters.slice(1);
traverseAst(ast.body, clsToThis);
}
- const main = `(${ast.parameters.map(_ast2js).join(", ")}){${_ast2js(ast.body)}}`;
+ const main = `(${ast.parameters.map(_ast2js).join(", ")}){\n${_ast2js(ast.body)}}`;
if (ast.identifier == null) {
return `function ${main}`;
} else {
@@ -641,17 +642,17 @@ function ast2js(ast, opts = {}) {
if (ast.asExport) {
return `export default ${smartPack(ast.arguments)}`;
} else {
- return `return ${smartPack(ast.arguments)}`;
+ return `return ${smartPack(ast.arguments)};`;
}
case "CallStatement":
return _ast2js(ast.expression);
case "CallExpression":
- if (ast.base.type == "Identifier" && ast.base.name == "class" && ast.className) {
+ if (opts.class && ast.base.type == "Identifier" && ast.base.name == "class" && ast.className) {
ast.arguments[0].isClassMode = true;
const extendsToken = ast.arguments.length == 1 ? "" : "extends " + _ast2js(ast.arguments[1]);
return `class ${ast.className} ${extendsToken} ${_ast2js(ast.arguments[0])}`;
- } else if (isClassExtends(ast)) {
+ } else if (opts.class && isClassExtends(ast)) {
const [cls, pcls] = ast.arguments;
cls.isClassMode = true;
return `class ${_ast2js(cls)} extends ${_ast2js(pcls)}`;
@@ -733,9 +734,9 @@ function ast2js(ast, opts = {}) {
} else {
compare_op = step < 0 ? ">=" : "<=";
}
- return `for (let ${v}=${start}; ${v} ${compare_op} ${_ast2js(ast.end)}; ${v}=${v}+${step}) {${_ast2js(
+ return `for (let ${v}=${start}; ${v} ${compare_op} ${_ast2js(ast.end)}; ${v}=${v}+${step}) {\n${_ast2js(
ast.body
- )}}`;
+ )}\n}`;
}
case "ForGenericStatement": {
@@ -762,7 +763,7 @@ function ast2js(ast, opts = {}) {
} else {
iter = ast.iterators.map(_ast2js);
}
- return `for (let ${smartPack(ast.variables)} of ${iter}) {${_ast2js(ast.body)}}`;
+ return `for (let ${smartPack(ast.variables)} of ${iter}) {\n${_ast2js(ast.body)}}`;
}
case "WhileStatement":
return `while (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;