typescript-to-lua 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +10 -2
- package/dist/LuaLib.d.ts +3 -3
- package/dist/LuaLib.js +4 -4
- package/dist/LuaPrinter.d.ts +11 -4
- package/dist/LuaPrinter.js +54 -11
- package/dist/lualib/ArrayFlat.lua +1 -1
- package/dist/lualib/ArrayFlatMap.lua +1 -1
- package/dist/lualib/ArrayIsArray.lua +1 -1
- package/dist/lualib/ArrayReduce.lua +7 -1
- package/dist/lualib/ArrayReduceRight.lua +7 -1
- package/dist/lualib/ArraySetLength.lua +1 -1
- package/dist/lualib/ArraySplice.lua +5 -5
- package/dist/lualib/Await.lua +14 -10
- package/dist/lualib/CloneDescriptor.lua +8 -8
- package/dist/lualib/Decorate.lua +1 -1
- package/dist/lualib/DelegatedYield.lua +1 -3
- package/dist/lualib/Delete.lua +6 -6
- package/dist/lualib/Error.lua +47 -44
- package/dist/lualib/Generator.lua +1 -5
- package/dist/lualib/InstanceOf.lua +1 -1
- package/dist/lualib/InstanceOfObject.lua +1 -1
- package/dist/lualib/Map.lua +130 -134
- package/dist/lualib/Number.lua +1 -1
- package/dist/lualib/NumberIsFinite.lua +1 -1
- package/dist/lualib/NumberToString.lua +3 -5
- package/dist/lualib/ObjectDefineProperty.lua +40 -8
- package/dist/lualib/ParseFloat.lua +9 -5
- package/dist/lualib/ParseInt.lua +15 -3
- package/dist/lualib/Promise.lua +18 -22
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +1 -1
- package/dist/lualib/SparseArrayNew.lua +5 -0
- package/dist/lualib/SparseArrayPush.lua +9 -0
- package/dist/lualib/SparseArraySpread.lua +4 -0
- package/dist/lualib/StringAccess.lua +1 -1
- package/dist/lualib/StringCharCodeAt.lua +1 -1
- package/dist/lualib/StringEndsWith.lua +2 -2
- package/dist/lualib/StringPadEnd.lua +2 -2
- package/dist/lualib/StringPadStart.lua +2 -2
- package/dist/lualib/StringReplace.lua +7 -1
- package/dist/lualib/StringSlice.lua +2 -2
- package/dist/lualib/StringSplit.lua +3 -3
- package/dist/lualib/StringStartsWith.lua +1 -1
- package/dist/lualib/StringSubstr.lua +1 -1
- package/dist/lualib/StringSubstring.lua +2 -2
- package/dist/lualib/Symbol.lua +3 -5
- package/dist/lualib/WeakMap.lua +40 -42
- package/dist/lualib/WeakSet.lua +36 -38
- package/dist/lualib/lualib_bundle.lua +603 -555
- package/dist/transformation/builtins/array.js +1 -2
- package/dist/transformation/builtins/function.js +1 -2
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +37 -4
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +1 -2
- package/dist/transformation/builtins/string.js +1 -2
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +102 -3
- package/dist/transformation/utils/diagnostics.d.ts +6 -0
- package/dist/transformation/utils/diagnostics.js +3 -1
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +13 -22
- package/dist/transformation/utils/preceding-statements.d.ts +3 -0
- package/dist/transformation/utils/preceding-statements.js +11 -0
- package/dist/transformation/utils/scope.d.ts +1 -0
- package/dist/transformation/utils/scope.js +8 -1
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +63 -36
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +66 -57
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +66 -68
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +54 -25
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +64 -20
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +109 -122
- package/dist/transformation/visitors/class/index.js +4 -7
- package/dist/transformation/visitors/class/new.js +4 -6
- package/dist/transformation/visitors/conditional.js +28 -14
- package/dist/transformation/visitors/delete.js +4 -0
- package/dist/transformation/visitors/expression-list.d.ts +7 -0
- package/dist/transformation/visitors/expression-list.js +120 -0
- package/dist/transformation/visitors/function.js +6 -8
- package/dist/transformation/visitors/identifier.js +4 -0
- package/dist/transformation/visitors/literal.js +69 -16
- package/dist/transformation/visitors/loops/do-while.js +34 -8
- package/dist/transformation/visitors/loops/for.js +25 -5
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +13 -2
- package/dist/transformation/visitors/modules/export.js +1 -1
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/sourceFile.js +4 -1
- package/dist/transformation/visitors/switch.js +29 -9
- package/dist/transformation/visitors/template.js +5 -2
- package/dist/transformation/visitors/typeof.js +3 -1
- package/dist/transformation/visitors/variable-declaration.js +21 -7
- package/dist/transformation/visitors/void.js +4 -7
- package/dist/transpilation/transformers.js +4 -1
- package/dist/transpilation/utils.js +2 -1
- package/package.json +5 -5
- package/dist/lualib/OptionalChainAccess.lua +0 -6
- package/dist/lualib/OptionalFunctionCall.lua +0 -6
- package/dist/lualib/OptionalMethodCall.lua +0 -11
- package/dist/transformation/utils/transform.d.ts +0 -8
- package/dist/transformation/utils/transform.js +0 -14
|
@@ -7,6 +7,7 @@ const function_context_1 = require("../utils/function-context");
|
|
|
7
7
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
8
8
|
const types_1 = require("../utils/typescript/types");
|
|
9
9
|
const call_1 = require("./call");
|
|
10
|
+
const expression_list_1 = require("./expression-list");
|
|
10
11
|
// TODO: Source positions
|
|
11
12
|
function getRawLiteral(node) {
|
|
12
13
|
let text = node.getText();
|
|
@@ -21,8 +22,10 @@ const transformTemplateExpression = (node, context) => {
|
|
|
21
22
|
if (head.length > 0) {
|
|
22
23
|
parts.push(lua.createStringLiteral(head, node.head));
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
const transformedExpressions = (0, expression_list_1.transformOrderedExpressions)(context, node.templateSpans.map(s => s.expression));
|
|
26
|
+
for (let i = 0; i < node.templateSpans.length; ++i) {
|
|
27
|
+
const span = node.templateSpans[i];
|
|
28
|
+
const expression = transformedExpressions[i];
|
|
26
29
|
const spanType = context.checker.getTypeAtLocation(span.expression);
|
|
27
30
|
if ((0, types_1.isStringType)(context, spanType)) {
|
|
28
31
|
parts.push(expression);
|
|
@@ -43,7 +43,9 @@ function transformTypeOfBinaryExpression(context, node) {
|
|
|
43
43
|
}
|
|
44
44
|
const innerExpression = context.transformExpression(typeOfExpression.expression);
|
|
45
45
|
const typeCall = lua.createCallExpression(lua.createIdentifier("type"), [innerExpression], typeOfExpression);
|
|
46
|
-
|
|
46
|
+
const [precedingStatements, result] = (0, binary_expression_1.transformBinaryOperation)(context, typeCall, comparedExpression, [], operator, node);
|
|
47
|
+
context.addPrecedingStatements(precedingStatements);
|
|
48
|
+
return result;
|
|
47
49
|
}
|
|
48
50
|
exports.transformTypeOfBinaryExpression = transformTypeOfBinaryExpression;
|
|
49
51
|
//# sourceMappingURL=typeof.js.map
|
|
@@ -9,6 +9,7 @@ const diagnostics_1 = require("../utils/diagnostics");
|
|
|
9
9
|
const export_1 = require("../utils/export");
|
|
10
10
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
11
11
|
const lualib_1 = require("../utils/lualib");
|
|
12
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
12
13
|
const identifier_1 = require("./identifier");
|
|
13
14
|
const multi_1 = require("./language-extensions/multi");
|
|
14
15
|
const literal_1 = require("./literal");
|
|
@@ -20,10 +21,15 @@ function transformArrayBindingElement(context, name) {
|
|
|
20
21
|
return (0, identifier_1.transformIdentifier)(context, name);
|
|
21
22
|
}
|
|
22
23
|
else if (ts.isBindingElement(name)) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
if (ts.isIdentifier(name.name)) {
|
|
25
|
+
return (0, identifier_1.transformIdentifier)(context, name.name);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// ts.isBindingPattern(name.name)
|
|
29
|
+
const tempName = context.createTempNameForNode(name.name);
|
|
30
|
+
context.addPrecedingStatements(transformBindingPattern(context, name.name, tempName));
|
|
31
|
+
return tempName;
|
|
32
|
+
}
|
|
27
33
|
}
|
|
28
34
|
else {
|
|
29
35
|
(0, utils_1.assertNever)(name);
|
|
@@ -52,7 +58,9 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
52
58
|
// The identifier of the new variable
|
|
53
59
|
const variableName = (0, identifier_1.transformIdentifier)(context, element.name);
|
|
54
60
|
// The field to extract
|
|
55
|
-
const
|
|
61
|
+
const elementName = (_a = element.propertyName) !== null && _a !== void 0 ? _a : element.name;
|
|
62
|
+
const [precedingStatements, propertyName] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, literal_1.transformPropertyName)(context, elementName));
|
|
63
|
+
result.push(...precedingStatements); // Keep property's preceding statements in order
|
|
56
64
|
let expression;
|
|
57
65
|
if (element.dotDotDotToken) {
|
|
58
66
|
if (index !== pattern.elements.length - 1) {
|
|
@@ -90,8 +98,11 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
90
98
|
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, variableName, expression));
|
|
91
99
|
if (element.initializer) {
|
|
92
100
|
const identifier = (0, export_1.addExportToIdentifier)(context, variableName);
|
|
101
|
+
const tsInitializer = element.initializer;
|
|
102
|
+
const [initializerPrecedingStatements, initializer] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(tsInitializer));
|
|
93
103
|
result.push(lua.createIfStatement(lua.createBinaryExpression(identifier, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator), lua.createBlock([
|
|
94
|
-
|
|
104
|
+
...initializerPrecedingStatements,
|
|
105
|
+
lua.createAssignmentStatement(identifier, initializer),
|
|
95
106
|
])));
|
|
96
107
|
}
|
|
97
108
|
}
|
|
@@ -110,14 +121,17 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
|
|
|
110
121
|
}
|
|
111
122
|
else {
|
|
112
123
|
// Contain the expression in a temporary variable
|
|
113
|
-
table = lua.createAnonymousIdentifier();
|
|
114
124
|
if (initializer) {
|
|
125
|
+
table = context.createTempNameForNode(initializer);
|
|
115
126
|
let expression = context.transformExpression(initializer);
|
|
116
127
|
if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
|
|
117
128
|
expression = (0, lua_ast_1.wrapInTable)(expression);
|
|
118
129
|
}
|
|
119
130
|
statements.push(lua.createVariableDeclarationStatement(table, expression));
|
|
120
131
|
}
|
|
132
|
+
else {
|
|
133
|
+
table = lua.createAnonymousIdentifier();
|
|
134
|
+
}
|
|
121
135
|
}
|
|
122
136
|
statements.push(...transformBindingPattern(context, bindingPattern, table));
|
|
123
137
|
return statements;
|
|
@@ -3,17 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.transformVoidExpressionStatement = exports.transformVoidExpression = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
|
-
const lua_ast_1 = require("../utils/lua-ast");
|
|
7
6
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
|
|
8
7
|
const transformVoidExpression = (node, context) => {
|
|
9
8
|
// If content is a literal it is safe to replace the entire expression with nil
|
|
10
|
-
if (ts.isLiteralExpression(node.expression)) {
|
|
11
|
-
|
|
9
|
+
if (!ts.isLiteralExpression(node.expression)) {
|
|
10
|
+
// local ____ = <expression>
|
|
11
|
+
context.addPrecedingStatements(lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression)));
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
return (0, lua_ast_1.createImmediatelyInvokedFunctionExpression)([
|
|
15
|
-
lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression)),
|
|
16
|
-
], [], node);
|
|
13
|
+
return lua.createNilLiteral(node);
|
|
17
14
|
};
|
|
18
15
|
exports.transformVoidExpression = transformVoidExpression;
|
|
19
16
|
const transformVoidExpressionStatement = (node, context) =>
|
|
@@ -48,7 +48,7 @@ exports.noImplicitSelfTransformer = noImplicitSelfTransformer;
|
|
|
48
48
|
const stripParenthesisExpressionsTransformer = context => sourceFile => {
|
|
49
49
|
// Remove parenthesis expressions before transforming to Lua, so transpiler is not hindered by extra ParenthesizedExpression nodes
|
|
50
50
|
function unwrapParentheses(node) {
|
|
51
|
-
while (ts.isParenthesizedExpression(node)) {
|
|
51
|
+
while (ts.isParenthesizedExpression(node) && !ts.isOptionalChain(node.expression)) {
|
|
52
52
|
node = node.expression;
|
|
53
53
|
}
|
|
54
54
|
return node;
|
|
@@ -61,6 +61,9 @@ const stripParenthesisExpressionsTransformer = context => sourceFile => {
|
|
|
61
61
|
else if (ts.isVoidExpression(node)) {
|
|
62
62
|
return ts.factory.updateVoidExpression(node, unwrapParentheses(node.expression));
|
|
63
63
|
}
|
|
64
|
+
else if (ts.isDeleteExpression(node)) {
|
|
65
|
+
return ts.factory.updateDeleteExpression(node, unwrapParentheses(node.expression));
|
|
66
|
+
}
|
|
64
67
|
return ts.visitEachChild(node, visit, context);
|
|
65
68
|
}
|
|
66
69
|
return ts.visitNode(sourceFile, visit);
|
|
@@ -8,6 +8,7 @@ const cliDiagnostics = require("../cli/diagnostics");
|
|
|
8
8
|
const diagnosticFactories = require("./diagnostics");
|
|
9
9
|
const getConfigDirectory = (options) => options.configFilePath ? path.dirname(options.configFilePath) : process.cwd();
|
|
10
10
|
exports.getConfigDirectory = getConfigDirectory;
|
|
11
|
+
const getTstlDirectory = () => path.dirname(__dirname);
|
|
11
12
|
function resolvePlugin(kind, optionName, basedir, query, importName = "default") {
|
|
12
13
|
if (typeof query !== "string") {
|
|
13
14
|
return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "string") };
|
|
@@ -25,7 +26,7 @@ function resolvePlugin(kind, optionName, basedir, query, importName = "default")
|
|
|
25
26
|
const hasNoRequireHook = require.extensions[".ts"] === undefined;
|
|
26
27
|
if (hasNoRequireHook && (resolved.endsWith(".ts") || resolved.endsWith(".tsx"))) {
|
|
27
28
|
try {
|
|
28
|
-
const tsNodePath = resolve.sync("ts-node", { basedir });
|
|
29
|
+
const tsNodePath = resolve.sync("ts-node", { basedir: getTstlDirectory() });
|
|
29
30
|
const tsNode = require(tsNodePath);
|
|
30
31
|
tsNode.register({ transpileOnly: true });
|
|
31
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
|
|
5
5
|
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
|
|
6
6
|
"homepage": "https://typescripttolua.github.io/",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"node": ">=12.13.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"typescript": "~4.
|
|
45
|
+
"typescript": "~4.5.2"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"enhanced-resolve": "^5.8.2",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"@types/jest": "^25.1.3",
|
|
56
56
|
"@types/node": "^13.7.7",
|
|
57
57
|
"@types/resolve": "1.14.0",
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^4.
|
|
59
|
-
"@typescript-eslint/parser": "^4.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.4.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.4.0",
|
|
60
60
|
"eslint": "^7.32.0",
|
|
61
61
|
"eslint-plugin-import": "^2.24.2",
|
|
62
62
|
"eslint-plugin-jest": "^24.4.0",
|
|
@@ -69,6 +69,6 @@
|
|
|
69
69
|
"prettier": "^2.3.2",
|
|
70
70
|
"ts-jest": "^27.0.7",
|
|
71
71
|
"ts-node": "^10.3.0",
|
|
72
|
-
"typescript": "~4.
|
|
72
|
+
"typescript": "~4.5.2"
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
function __TS__OptionalMethodCall(____table, methodName, isMethodOptional, ...)
|
|
2
|
-
if ____table then
|
|
3
|
-
local method = ____table[methodName]
|
|
4
|
-
if method then
|
|
5
|
-
return method(____table, ...)
|
|
6
|
-
elseif not isMethodOptional then
|
|
7
|
-
error(methodName .. " is not a function", 0)
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
return nil
|
|
11
|
-
end
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import * as ts from "typescript";
|
|
2
|
-
import * as lua from "../../LuaAST";
|
|
3
|
-
import { TransformationContext } from "../context";
|
|
4
|
-
export interface ImmediatelyInvokedFunctionParameters {
|
|
5
|
-
statements: lua.Statement | lua.Statement[];
|
|
6
|
-
result: lua.Expression | lua.Expression[];
|
|
7
|
-
}
|
|
8
|
-
export declare function transformToImmediatelyInvokedFunctionExpression(context: TransformationContext, transformFunction: () => ImmediatelyInvokedFunctionParameters, tsOriginal?: ts.Node): lua.CallExpression;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformToImmediatelyInvokedFunctionExpression = void 0;
|
|
4
|
-
const utils_1 = require("../../utils");
|
|
5
|
-
const lua_ast_1 = require("./lua-ast");
|
|
6
|
-
const scope_1 = require("./scope");
|
|
7
|
-
function transformToImmediatelyInvokedFunctionExpression(context, transformFunction, tsOriginal) {
|
|
8
|
-
(0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
|
|
9
|
-
const { statements, result } = transformFunction();
|
|
10
|
-
(0, scope_1.popScope)(context);
|
|
11
|
-
return (0, lua_ast_1.createImmediatelyInvokedFunctionExpression)((0, utils_1.castArray)(statements), result, tsOriginal);
|
|
12
|
-
}
|
|
13
|
-
exports.transformToImmediatelyInvokedFunctionExpression = transformToImmediatelyInvokedFunctionExpression;
|
|
14
|
-
//# sourceMappingURL=transform.js.map
|