typescript-to-lua 1.26.1 → 1.27.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/dist/lualib/5.0/ArrayFrom.lua +8 -2
- package/dist/lualib/5.0/Using.lua +1 -4
- package/dist/lualib/5.0/lualib_bundle.lua +9 -6
- package/dist/lualib/universal/ArrayFrom.lua +8 -2
- package/dist/lualib/universal/Using.lua +1 -4
- package/dist/lualib/universal/lualib_bundle.lua +9 -6
- package/dist/transformation/pre-transformers/using-transformer.js +5 -2
- package/dist/transformation/utils/function-context.js +3 -0
- package/dist/transformation/visitors/access.js +11 -1
- package/dist/transformation/visitors/class/index.js +7 -4
- package/dist/transformation/visitors/modules/export.js +44 -22
- package/dist/transformation/visitors/optional-chaining.js +12 -0
- package/package.json +4 -4
|
@@ -20,8 +20,14 @@ do
|
|
|
20
20
|
result[table.getn(result) + 1] = v
|
|
21
21
|
end
|
|
22
22
|
else
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
local i = 0
|
|
24
|
+
for ____, v in arrayLikeIterator(arrayLike) do
|
|
25
|
+
local ____mapFn_3 = mapFn
|
|
26
|
+
local ____thisArg_1 = thisArg
|
|
27
|
+
local ____v_2 = v
|
|
28
|
+
local ____i_0 = i
|
|
29
|
+
i = ____i_0 + 1
|
|
30
|
+
result[table.getn(result) + 1] = ____mapFn_3(____thisArg_1, ____v_2, ____i_0)
|
|
25
31
|
end
|
|
26
32
|
end
|
|
27
33
|
return result
|
|
@@ -2,10 +2,7 @@ local function __TS__Using(self, cb, ...)
|
|
|
2
2
|
local args = arg
|
|
3
3
|
local thrownError
|
|
4
4
|
local ok, result = xpcall(
|
|
5
|
-
function() return cb(
|
|
6
|
-
nil,
|
|
7
|
-
unpack(args)
|
|
8
|
-
) end,
|
|
5
|
+
function() return cb(unpack(args)) end,
|
|
9
6
|
function(err)
|
|
10
7
|
thrownError = err
|
|
11
8
|
return thrownError
|
|
@@ -193,8 +193,14 @@ do
|
|
|
193
193
|
result[table.getn(result) + 1] = v
|
|
194
194
|
end
|
|
195
195
|
else
|
|
196
|
-
|
|
197
|
-
|
|
196
|
+
local i = 0
|
|
197
|
+
for ____, v in arrayLikeIterator(arrayLike) do
|
|
198
|
+
local ____mapFn_3 = mapFn
|
|
199
|
+
local ____thisArg_1 = thisArg
|
|
200
|
+
local ____v_2 = v
|
|
201
|
+
local ____i_0 = i
|
|
202
|
+
i = ____i_0 + 1
|
|
203
|
+
result[table.getn(result) + 1] = ____mapFn_3(____thisArg_1, ____v_2, ____i_0)
|
|
198
204
|
end
|
|
199
205
|
end
|
|
200
206
|
return result
|
|
@@ -2602,10 +2608,7 @@ local function __TS__Using(self, cb, ...)
|
|
|
2602
2608
|
local args = arg
|
|
2603
2609
|
local thrownError
|
|
2604
2610
|
local ok, result = xpcall(
|
|
2605
|
-
function() return cb(
|
|
2606
|
-
nil,
|
|
2607
|
-
unpack(args)
|
|
2608
|
-
) end,
|
|
2611
|
+
function() return cb(unpack(args)) end,
|
|
2609
2612
|
function(err)
|
|
2610
2613
|
thrownError = err
|
|
2611
2614
|
return thrownError
|
|
@@ -20,8 +20,14 @@ do
|
|
|
20
20
|
result[#result + 1] = v
|
|
21
21
|
end
|
|
22
22
|
else
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
local i = 0
|
|
24
|
+
for ____, v in arrayLikeIterator(arrayLike) do
|
|
25
|
+
local ____mapFn_3 = mapFn
|
|
26
|
+
local ____thisArg_1 = thisArg
|
|
27
|
+
local ____v_2 = v
|
|
28
|
+
local ____i_0 = i
|
|
29
|
+
i = ____i_0 + 1
|
|
30
|
+
result[#result + 1] = ____mapFn_3(____thisArg_1, ____v_2, ____i_0)
|
|
25
31
|
end
|
|
26
32
|
end
|
|
27
33
|
return result
|
|
@@ -2,10 +2,7 @@ local function __TS__Using(self, cb, ...)
|
|
|
2
2
|
local args = {...}
|
|
3
3
|
local thrownError
|
|
4
4
|
local ok, result = xpcall(
|
|
5
|
-
function() return cb(
|
|
6
|
-
nil,
|
|
7
|
-
__TS__Unpack(args)
|
|
8
|
-
) end,
|
|
5
|
+
function() return cb(__TS__Unpack(args)) end,
|
|
9
6
|
function(err)
|
|
10
7
|
thrownError = err
|
|
11
8
|
return thrownError
|
|
@@ -193,8 +193,14 @@ do
|
|
|
193
193
|
result[#result + 1] = v
|
|
194
194
|
end
|
|
195
195
|
else
|
|
196
|
-
|
|
197
|
-
|
|
196
|
+
local i = 0
|
|
197
|
+
for ____, v in arrayLikeIterator(arrayLike) do
|
|
198
|
+
local ____mapFn_3 = mapFn
|
|
199
|
+
local ____thisArg_1 = thisArg
|
|
200
|
+
local ____v_2 = v
|
|
201
|
+
local ____i_0 = i
|
|
202
|
+
i = ____i_0 + 1
|
|
203
|
+
result[#result + 1] = ____mapFn_3(____thisArg_1, ____v_2, ____i_0)
|
|
198
204
|
end
|
|
199
205
|
end
|
|
200
206
|
return result
|
|
@@ -2542,10 +2548,7 @@ local function __TS__Using(self, cb, ...)
|
|
|
2542
2548
|
local args = {...}
|
|
2543
2549
|
local thrownError
|
|
2544
2550
|
local ok, result = xpcall(
|
|
2545
|
-
function() return cb(
|
|
2546
|
-
nil,
|
|
2547
|
-
__TS__Unpack(args)
|
|
2548
|
-
) end,
|
|
2551
|
+
function() return cb(__TS__Unpack(args)) end,
|
|
2549
2552
|
function(err)
|
|
2550
2553
|
thrownError = err
|
|
2551
2554
|
return thrownError
|
|
@@ -18,7 +18,7 @@ function usingTransformer(context) {
|
|
|
18
18
|
ts.setParent(node2, parent[parent.length - 1]);
|
|
19
19
|
parent.push(node2);
|
|
20
20
|
ts.visitEachChild(node2, setParent, ctx);
|
|
21
|
-
parent.
|
|
21
|
+
parent.pop();
|
|
22
22
|
return node2;
|
|
23
23
|
}
|
|
24
24
|
ts.visitEachChild(updatedBlock, setParent, ctx);
|
|
@@ -54,7 +54,10 @@ function transformBlockWithUsing(context, statements, block) {
|
|
|
54
54
|
const followingStatements = statements.slice(i + 1);
|
|
55
55
|
const [followingHasUsings, replacedFollowingStatements] = transformBlockWithUsing(context, followingStatements, block);
|
|
56
56
|
const callbackBody = ts.factory.createBlock(followingHasUsings ? replacedFollowingStatements : followingStatements);
|
|
57
|
-
const callback = ts.factory.createFunctionExpression(
|
|
57
|
+
const callback = ts.factory.createFunctionExpression(
|
|
58
|
+
// Put async keyword in front of callback when we are in an async using
|
|
59
|
+
isAwaitUsing ? [ts.factory.createModifier(ts.SyntaxKind.AsyncKeyword)] : undefined, undefined, undefined, undefined, variableNames, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), // Required for TS to not freak out trying to infer the type of synthetic nodes
|
|
60
|
+
callbackBody);
|
|
58
61
|
// Replace using variable list with call to lualib function with callback and followed by all variable initializers
|
|
59
62
|
const functionIdentifier = ts.factory.createIdentifier(isAwaitUsing ? "__TS__UsingAsync" : "__TS__Using");
|
|
60
63
|
let call = ts.factory.createCallExpression(functionIdentifier, [], [
|
|
@@ -119,6 +119,9 @@ function computeDeclarationContextType(context, signatureDeclaration) {
|
|
|
119
119
|
}
|
|
120
120
|
return ContextType.NonVoid;
|
|
121
121
|
}
|
|
122
|
+
if (signatureDeclaration.parent && ts.isTypeParameterDeclaration(signatureDeclaration.parent)) {
|
|
123
|
+
return ContextType.NonVoid;
|
|
124
|
+
}
|
|
122
125
|
// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
|
|
123
126
|
const program = context.program;
|
|
124
127
|
const options = program.getCompilerOptions();
|
|
@@ -20,6 +20,7 @@ const multi_1 = require("./language-extensions/multi");
|
|
|
20
20
|
const optional_chaining_1 = require("./optional-chaining");
|
|
21
21
|
const typescript_2 = require("typescript");
|
|
22
22
|
const identifier_1 = require("./identifier");
|
|
23
|
+
const export_1 = require("../utils/export");
|
|
23
24
|
function addOneToArrayAccessArgument(context, node, index) {
|
|
24
25
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
25
26
|
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
@@ -108,7 +109,16 @@ function transformPropertyAccessExpressionWithCapture(context, node, thisValueCa
|
|
|
108
109
|
return { expression };
|
|
109
110
|
}
|
|
110
111
|
else {
|
|
111
|
-
|
|
112
|
+
// Check if we need to account for enum being exported int his file
|
|
113
|
+
if ((0, export_1.isSymbolExported)(context, type.symbol) &&
|
|
114
|
+
(0, export_1.getSymbolExportScope)(context, type.symbol) === node.expression.getSourceFile()) {
|
|
115
|
+
return {
|
|
116
|
+
expression: lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), lua.createStringLiteral(property), node),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
return { expression: lua.createIdentifier(property, node) };
|
|
121
|
+
}
|
|
112
122
|
}
|
|
113
123
|
}
|
|
114
124
|
const builtinResult = (0, builtins_1.transformBuiltinPropertyAccessExpression)(context, node);
|
|
@@ -115,7 +115,10 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
|
|
|
115
115
|
for (const member of classDeclaration.members) {
|
|
116
116
|
if (ts.isAccessor(member)) {
|
|
117
117
|
// Accessors
|
|
118
|
-
const
|
|
118
|
+
const symbol = context.checker.getSymbolAtLocation(member.name);
|
|
119
|
+
if (!symbol)
|
|
120
|
+
continue;
|
|
121
|
+
const accessors = getAllAccessorDeclarations(classDeclaration, symbol, context);
|
|
119
122
|
if (accessors.firstAccessor !== member)
|
|
120
123
|
continue;
|
|
121
124
|
const accessorsResult = (0, accessors_1.transformAccessorDeclarations)(context, accessors, localClassName);
|
|
@@ -161,9 +164,9 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
|
|
|
161
164
|
context.classSuperInfos.pop();
|
|
162
165
|
return { statements: result, name: className };
|
|
163
166
|
}
|
|
164
|
-
function getAllAccessorDeclarations(classDeclaration) {
|
|
165
|
-
const getAccessor = classDeclaration.members.find(ts.isGetAccessor);
|
|
166
|
-
const setAccessor = classDeclaration.members.find(ts.isSetAccessor);
|
|
167
|
+
function getAllAccessorDeclarations(classDeclaration, symbol, context) {
|
|
168
|
+
const getAccessor = classDeclaration.members.find((m) => ts.isGetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol);
|
|
169
|
+
const setAccessor = classDeclaration.members.find((m) => ts.isSetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol);
|
|
167
170
|
// Get the first of the two (that is not undefined)
|
|
168
171
|
const firstAccessor = getAccessor && (!setAccessor || getAccessor.pos < setAccessor.pos) ? getAccessor : setAccessor;
|
|
169
172
|
return {
|
|
@@ -6,11 +6,10 @@ const lua = require("../../../LuaAST");
|
|
|
6
6
|
const utils_1 = require("../../../utils");
|
|
7
7
|
const export_1 = require("../../utils/export");
|
|
8
8
|
const lua_ast_1 = require("../../utils/lua-ast");
|
|
9
|
-
const scope_1 = require("../../utils/scope");
|
|
10
|
-
const block_1 = require("../block");
|
|
11
|
-
const identifier_1 = require("../identifier");
|
|
12
9
|
const literal_1 = require("../literal");
|
|
13
10
|
const import_1 = require("./import");
|
|
11
|
+
const safe_names_1 = require("../../utils/safe-names");
|
|
12
|
+
const path = require("path");
|
|
14
13
|
const transformExportAssignment = (node, context) => {
|
|
15
14
|
if (!context.resolver.isValueAliasDeclaration(node)) {
|
|
16
15
|
return undefined;
|
|
@@ -62,30 +61,53 @@ function transformExportAll(context, node) {
|
|
|
62
61
|
// Wrap this in a DoStatement to prevent polluting the scope.
|
|
63
62
|
return lua.createDoStatement(result, node);
|
|
64
63
|
}
|
|
65
|
-
const isDefaultExportSpecifier = (node) => (node.name &&
|
|
66
|
-
|
|
64
|
+
const isDefaultExportSpecifier = (node) => (node.name &&
|
|
65
|
+
ts.isIdentifier(node.name) &&
|
|
66
|
+
ts.identifierToKeywordKind(node.name) === ts.SyntaxKind.DefaultKeyword) ||
|
|
67
|
+
(node.propertyName &&
|
|
68
|
+
ts.isIdentifier(node.propertyName) &&
|
|
69
|
+
ts.identifierToKeywordKind(node.propertyName) === ts.SyntaxKind.DefaultKeyword);
|
|
67
70
|
function transformExportSpecifier(context, node) {
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
var _a;
|
|
72
|
+
const exportedName = node.name;
|
|
73
|
+
const exportedValue = (_a = node.propertyName) !== null && _a !== void 0 ? _a : node.name;
|
|
74
|
+
let rhs;
|
|
75
|
+
if (ts.isIdentifier(exportedValue)) {
|
|
76
|
+
const exportedSymbol = context.checker.getExportSpecifierLocalTargetSymbol(node);
|
|
77
|
+
rhs = (0, literal_1.createShorthandIdentifier)(context, exportedSymbol, exportedValue);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
rhs = lua.createStringLiteral(exportedName.text, exportedValue);
|
|
81
|
+
}
|
|
82
|
+
if (isDefaultExportSpecifier(node)) {
|
|
83
|
+
const lhs = (0, export_1.createDefaultExportExpression)(node);
|
|
84
|
+
return lua.createAssignmentStatement(lhs, rhs, node);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const exportsTable = (0, lua_ast_1.createExportsIdentifier)();
|
|
88
|
+
const lhs = lua.createTableIndexExpression(exportsTable, lua.createStringLiteral(exportedName.text), exportedName);
|
|
89
|
+
return lua.createAssignmentStatement(lhs, rhs, node);
|
|
90
|
+
}
|
|
76
91
|
}
|
|
77
92
|
function transformExportSpecifiersFrom(context, statement, moduleSpecifier, exportSpecifiers) {
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
93
|
+
var _a;
|
|
94
|
+
const result = [];
|
|
95
|
+
const importPath = ts.isStringLiteral(moduleSpecifier) ? moduleSpecifier.text.replace(/"/g, "") : "module";
|
|
96
|
+
// Create the require statement to extract values.
|
|
97
|
+
// local ____module = require("module")
|
|
98
|
+
const importUniqueName = lua.createIdentifier((0, safe_names_1.createSafeName)(path.basename(importPath)));
|
|
99
|
+
const requireCall = (0, import_1.createModuleRequire)(context, moduleSpecifier);
|
|
100
|
+
result.push(lua.createVariableDeclarationStatement(importUniqueName, requireCall, statement));
|
|
85
101
|
for (const specifier of exportSpecifiers) {
|
|
86
|
-
|
|
102
|
+
// Assign to exports table
|
|
103
|
+
const exportsTable = (0, lua_ast_1.createExportsIdentifier)();
|
|
104
|
+
const exportedName = specifier.name;
|
|
105
|
+
const exportedNameTransformed = (0, literal_1.transformPropertyName)(context, exportedName);
|
|
106
|
+
const lhs = lua.createTableIndexExpression(exportsTable, exportedNameTransformed, exportedName);
|
|
107
|
+
const exportedValue = (_a = specifier.propertyName) !== null && _a !== void 0 ? _a : specifier.name;
|
|
108
|
+
const rhs = lua.createTableIndexExpression(lua.cloneIdentifier(importUniqueName), (0, literal_1.transformPropertyName)(context, exportedValue), specifier);
|
|
109
|
+
result.push(lua.createAssignmentStatement(lhs, rhs, specifier));
|
|
87
110
|
}
|
|
88
|
-
// Wrap this in a DoStatement to prevent polluting the scope.
|
|
89
111
|
return lua.createDoStatement(result, statement);
|
|
90
112
|
}
|
|
91
113
|
const getExported = (context, exportSpecifiers) => exportSpecifiers.elements.filter(exportSpecifier => context.resolver.isValueAliasDeclaration(exportSpecifier));
|
|
@@ -124,6 +124,18 @@ function transformOptionalChainWithCapture(context, tsNode, thisValueCapture, is
|
|
|
124
124
|
}
|
|
125
125
|
return result;
|
|
126
126
|
});
|
|
127
|
+
// handle super calls by passing self as context
|
|
128
|
+
function getLeftMostChainItem(node) {
|
|
129
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
130
|
+
return getLeftMostChainItem(node.expression);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return node;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (getLeftMostChainItem(tsLeftExpression).kind === ts.SyntaxKind.SuperKeyword) {
|
|
137
|
+
capturedThisValue = lua.createIdentifier("self");
|
|
138
|
+
}
|
|
127
139
|
// handle context
|
|
128
140
|
if (rightContextualCall) {
|
|
129
141
|
if (capturedThisValue) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.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": ">=16.10.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"typescript": "5.
|
|
45
|
+
"typescript": "5.6.2"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@typescript-to-lua/language-extensions": "1.19.0",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"jest-circus": "^29.5.0",
|
|
68
68
|
"lua-types": "^2.13.0",
|
|
69
69
|
"lua-wasm-bindings": "^0.3.1",
|
|
70
|
-
"prettier": "^2.8.
|
|
70
|
+
"prettier": "^2.8.8",
|
|
71
71
|
"ts-jest": "^29.1.0",
|
|
72
72
|
"ts-node": "^10.9.1",
|
|
73
|
-
"typescript": "^5.
|
|
73
|
+
"typescript": "^5.6.2",
|
|
74
74
|
"typescript-eslint": "^7.13.1"
|
|
75
75
|
}
|
|
76
76
|
}
|