typescript-to-lua 1.31.4 → 1.33.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-build/plugin.d.ts +1 -1
- package/dist/transformation/utils/lua-ast.d.ts +1 -0
- package/dist/transformation/utils/lua-ast.js +16 -0
- package/dist/transformation/utils/scope.js +4 -6
- package/dist/transformation/visitors/access.js +1 -1
- package/dist/transformation/visitors/binary-expression/assignments.js +1 -1
- package/dist/transformation/visitors/call.js +1 -1
- package/dist/transformation/visitors/class/index.js +2 -4
- package/dist/transformation/visitors/enum.js +1 -3
- package/dist/transformation/visitors/function.js +3 -5
- package/dist/transformation/visitors/identifier.d.ts +1 -1
- package/dist/transformation/visitors/identifier.js +13 -2
- package/dist/transformation/visitors/modules/import.js +9 -5
- package/dist/transformation/visitors/variable-declaration.js +2 -2
- package/package.json +4 -4
|
@@ -7,7 +7,7 @@ export declare const lualibDiagnostic: ((message: string, file?: ts.SourceFile |
|
|
|
7
7
|
};
|
|
8
8
|
declare class LuaLibPlugin implements tstl.Plugin {
|
|
9
9
|
visitors: {
|
|
10
|
-
|
|
10
|
+
308: (file: ts.SourceFile, context: tstl.TransformationContext) => tstl.File;
|
|
11
11
|
};
|
|
12
12
|
printer: tstl.Printer;
|
|
13
13
|
afterPrint(program: ts.Program, options: tstl.CompilerOptions, emitHost: EmitHost, result: ProcessedFile[]): ts.Diagnostic[];
|
|
@@ -8,6 +8,7 @@ export declare function createExportsIdentifier(): lua.Identifier;
|
|
|
8
8
|
export declare function addToNumericExpression(expression: lua.Expression, change: number): lua.Expression;
|
|
9
9
|
export declare function getNumberLiteralValue(expression?: lua.Expression): number | undefined;
|
|
10
10
|
export declare function createUnpackCall(context: TransformationContext, expression: lua.Expression, tsOriginal?: ts.Node): lua.Expression;
|
|
11
|
+
export declare function createBoundedUnpackCall(context: TransformationContext, expression: lua.Expression, maxUnpackItem: number, tsOriginal?: ts.Node): lua.Expression;
|
|
11
12
|
export declare function isUnpackCall(node: lua.Node): boolean;
|
|
12
13
|
export declare function wrapInTable(...expressions: lua.Expression[]): lua.TableExpression;
|
|
13
14
|
export declare function wrapInToStringForConcat(expression: lua.Expression): lua.Expression;
|
|
@@ -7,6 +7,7 @@ exports.createExportsIdentifier = createExportsIdentifier;
|
|
|
7
7
|
exports.addToNumericExpression = addToNumericExpression;
|
|
8
8
|
exports.getNumberLiteralValue = getNumberLiteralValue;
|
|
9
9
|
exports.createUnpackCall = createUnpackCall;
|
|
10
|
+
exports.createBoundedUnpackCall = createBoundedUnpackCall;
|
|
10
11
|
exports.isUnpackCall = isUnpackCall;
|
|
11
12
|
exports.wrapInTable = wrapInTable;
|
|
12
13
|
exports.wrapInToStringForConcat = wrapInToStringForConcat;
|
|
@@ -72,6 +73,21 @@ function createUnpackCall(context, expression, tsOriginal) {
|
|
|
72
73
|
: lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("unpack"));
|
|
73
74
|
return lua.setNodeFlags(lua.createCallExpression(unpack, [expression], tsOriginal), lua.NodeFlags.TableUnpackCall);
|
|
74
75
|
}
|
|
76
|
+
function createBoundedUnpackCall(context, expression, maxUnpackItem, tsOriginal) {
|
|
77
|
+
if (context.luaTarget === CompilerOptions_1.LuaTarget.Universal) {
|
|
78
|
+
return (0, lualib_1.transformLuaLibFunction)(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
|
|
79
|
+
}
|
|
80
|
+
// Lua 5.0 does not support this signature, so don't add the arguments there
|
|
81
|
+
const extraArgs = context.luaTarget === CompilerOptions_1.LuaTarget.Lua50
|
|
82
|
+
? []
|
|
83
|
+
: [lua.createNumericLiteral(1), lua.createNumericLiteral(maxUnpackItem)];
|
|
84
|
+
const unpack = context.luaTarget === CompilerOptions_1.LuaTarget.Lua50 ||
|
|
85
|
+
context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 ||
|
|
86
|
+
context.luaTarget === CompilerOptions_1.LuaTarget.LuaJIT
|
|
87
|
+
? lua.createIdentifier("unpack")
|
|
88
|
+
: lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("unpack"));
|
|
89
|
+
return lua.setNodeFlags(lua.createCallExpression(unpack, [expression, ...extraArgs], tsOriginal), lua.NodeFlags.TableUnpackCall);
|
|
90
|
+
}
|
|
75
91
|
function isUnpackCall(node) {
|
|
76
92
|
return lua.isCallExpression(node) && (node.flags & lua.NodeFlags.TableUnpackCall) !== 0;
|
|
77
93
|
}
|
|
@@ -42,10 +42,9 @@ function* walkScopesUp(context) {
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
|
|
45
|
+
var _a;
|
|
45
46
|
for (const scope of context.scopeStack) {
|
|
46
|
-
|
|
47
|
-
scope.referencedSymbols = new Map();
|
|
48
|
-
}
|
|
47
|
+
(_a = scope.referencedSymbols) !== null && _a !== void 0 ? _a : (scope.referencedSymbols = new Map());
|
|
49
48
|
const references = (0, utils_1.getOrUpdate)(scope.referencedSymbols, symbolId, () => []);
|
|
50
49
|
references.push(identifier);
|
|
51
50
|
}
|
|
@@ -65,9 +64,8 @@ function findScope(context, scopeTypes) {
|
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
66
|
function addScopeVariableDeclaration(scope, declaration) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
67
|
+
var _a;
|
|
68
|
+
(_a = scope.variableDeclarations) !== null && _a !== void 0 ? _a : (scope.variableDeclarations = []);
|
|
71
69
|
scope.variableDeclarations.push(declaration);
|
|
72
70
|
}
|
|
73
71
|
function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
|
|
@@ -83,7 +83,7 @@ function transformPropertyAccessExpressionWithCapture(context, node, thisValueCa
|
|
|
83
83
|
const isOptionalLeft = (0, optional_chaining_1.isOptionalContinuation)(node.expression);
|
|
84
84
|
let property = node.name.text;
|
|
85
85
|
const symbol = context.checker.getSymbolAtLocation(node.name);
|
|
86
|
-
const customName = (0, identifier_1.getCustomNameFromSymbol)(symbol);
|
|
86
|
+
const customName = (0, identifier_1.getCustomNameFromSymbol)(context, symbol);
|
|
87
87
|
if (customName) {
|
|
88
88
|
property = customName;
|
|
89
89
|
}
|
|
@@ -165,7 +165,7 @@ function transformAssignmentStatement(context, expression) {
|
|
|
165
165
|
else {
|
|
166
166
|
right = context.transformExpression(expression.right);
|
|
167
167
|
if (!(0, multi_1.isMultiReturnCall)(context, expression.right) && (0, typescript_2.isArrayType)(context, rightType)) {
|
|
168
|
-
right = (0, lua_ast_1.
|
|
168
|
+
right = (0, lua_ast_1.createBoundedUnpackCall)(context, right, expression.left.elements.length, expression.right);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
const left = expression.left.elements.map(e => transformAssignmentLeftHandSideExpression(context, e));
|
|
@@ -94,7 +94,7 @@ function transformContextualCallExpression(context, node, args) {
|
|
|
94
94
|
const table = context.transformExpression(left.expression);
|
|
95
95
|
let name = left.name.text;
|
|
96
96
|
const symbol = context.checker.getSymbolAtLocation(left);
|
|
97
|
-
const customName = (0, identifier_1.getCustomNameFromSymbol)(symbol);
|
|
97
|
+
const customName = (0, identifier_1.getCustomNameFromSymbol)(context, symbol);
|
|
98
98
|
if (customName) {
|
|
99
99
|
name = customName;
|
|
100
100
|
}
|
|
@@ -191,10 +191,8 @@ const transformSuperExpression = (expression, context) => {
|
|
|
191
191
|
baseClassName = (0, identifier_1.transformIdentifier)(context, extendsExpression);
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
|
-
if
|
|
195
|
-
|
|
196
|
-
baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression);
|
|
197
|
-
}
|
|
194
|
+
// Use "className.____super" if the base is not a simple identifier
|
|
195
|
+
baseClassName !== null && baseClassName !== void 0 ? baseClassName : (baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression));
|
|
198
196
|
const f = (0, typescript_1.findFirstNodeAbove)(expression, ts.isFunctionLike);
|
|
199
197
|
if (f && ts.canHaveModifiers(f) && (0, utils_1.isStaticNode)(f)) {
|
|
200
198
|
// In static method, don't add prototype to super call
|
|
@@ -49,9 +49,7 @@ const transformEnumDeclaration = (node, context) => {
|
|
|
49
49
|
valueExpression = lua.createTableIndexExpression(enumReference, otherMemberName);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
valueExpression = context.transformExpression(member.initializer);
|
|
54
|
-
}
|
|
52
|
+
valueExpression !== null && valueExpression !== void 0 ? valueExpression : (valueExpression = context.transformExpression(member.initializer));
|
|
55
53
|
}
|
|
56
54
|
else {
|
|
57
55
|
valueExpression = lua.createNilLiteral();
|
|
@@ -234,7 +234,7 @@ function transformFunctionLikeDeclaration(node, context) {
|
|
|
234
234
|
: functionExpression;
|
|
235
235
|
}
|
|
236
236
|
const transformFunctionDeclaration = (node, context) => {
|
|
237
|
-
var _a;
|
|
237
|
+
var _a, _b;
|
|
238
238
|
// Don't transform functions without body (overload declarations)
|
|
239
239
|
if (node.body === undefined) {
|
|
240
240
|
return undefined;
|
|
@@ -248,10 +248,8 @@ const transformFunctionDeclaration = (node, context) => {
|
|
|
248
248
|
// Remember symbols referenced in this function for hoisting later
|
|
249
249
|
if (name.symbolId !== undefined) {
|
|
250
250
|
const scope = (0, scope_1.peekScope)(context);
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
const functionInfo = { referencedSymbols: (_a = functionScope.referencedSymbols) !== null && _a !== void 0 ? _a : new Map() };
|
|
251
|
+
(_a = scope.functionDefinitions) !== null && _a !== void 0 ? _a : (scope.functionDefinitions = new Map());
|
|
252
|
+
const functionInfo = { referencedSymbols: (_b = functionScope.referencedSymbols) !== null && _b !== void 0 ? _b : new Map() };
|
|
255
253
|
scope.functionDefinitions.set(name.symbolId, functionInfo);
|
|
256
254
|
}
|
|
257
255
|
// Wrap functions with properties into a callable table
|
|
@@ -2,6 +2,6 @@ import * as ts from "typescript";
|
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
4
|
export declare function transformIdentifier(context: TransformationContext, identifier: ts.Identifier): lua.Identifier;
|
|
5
|
-
export declare function getCustomNameFromSymbol(symbol?: ts.Symbol): undefined | string;
|
|
5
|
+
export declare function getCustomNameFromSymbol(context: TransformationContext, symbol?: ts.Symbol): undefined | string;
|
|
6
6
|
export declare function transformIdentifierWithSymbol(context: TransformationContext, node: ts.Identifier, symbol: ts.Symbol | undefined): lua.Expression;
|
|
7
7
|
export declare const transformIdentifierExpression: FunctionVisitor<ts.Identifier>;
|
|
@@ -22,7 +22,7 @@ const annotations_1 = require("../utils/annotations");
|
|
|
22
22
|
function transformIdentifier(context, identifier) {
|
|
23
23
|
return transformNonValueIdentifier(context, identifier, context.checker.getSymbolAtLocation(identifier));
|
|
24
24
|
}
|
|
25
|
-
function getCustomNameFromSymbol(symbol) {
|
|
25
|
+
function getCustomNameFromSymbol(context, symbol) {
|
|
26
26
|
let retVal;
|
|
27
27
|
if (symbol) {
|
|
28
28
|
const declarations = symbol.getDeclarations();
|
|
@@ -35,6 +35,17 @@ function getCustomNameFromSymbol(symbol) {
|
|
|
35
35
|
customNameAnnotation = foundAnnotation;
|
|
36
36
|
break;
|
|
37
37
|
}
|
|
38
|
+
// If the symbol is an imported value, check the original declaration
|
|
39
|
+
// beware of declaration.propertyName, this is the import name alias and should not be renamed!
|
|
40
|
+
if (ts.isImportSpecifier(declaration) && !declaration.propertyName) {
|
|
41
|
+
const importedType = context.checker.getTypeAtLocation(declaration);
|
|
42
|
+
if (importedType.symbol) {
|
|
43
|
+
const importedCustomName = getCustomNameFromSymbol(context, importedType.symbol);
|
|
44
|
+
if (importedCustomName) {
|
|
45
|
+
return importedCustomName;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
38
49
|
}
|
|
39
50
|
if (customNameAnnotation) {
|
|
40
51
|
retVal = customNameAnnotation.args[0];
|
|
@@ -73,7 +84,7 @@ function transformNonValueIdentifier(context, identifier, symbol) {
|
|
|
73
84
|
}
|
|
74
85
|
}
|
|
75
86
|
let text = (0, safe_names_1.hasUnsafeIdentifierName)(context, identifier, symbol) ? (0, safe_names_1.createSafeName)(identifier.text) : identifier.text;
|
|
76
|
-
const customName = getCustomNameFromSymbol(symbol);
|
|
87
|
+
const customName = getCustomNameFromSymbol(context, symbol);
|
|
77
88
|
if (customName)
|
|
78
89
|
text = customName;
|
|
79
90
|
const symbolId = (0, symbols_1.getIdentifierSymbolId)(context, identifier, symbol);
|
|
@@ -35,16 +35,20 @@ function shouldBeImported(context, importNode) {
|
|
|
35
35
|
return context.resolver.isReferencedAliasDeclaration(importNode);
|
|
36
36
|
}
|
|
37
37
|
function transformImportSpecifier(context, importSpecifier, moduleTableName) {
|
|
38
|
-
var _a;
|
|
38
|
+
var _a, _b;
|
|
39
|
+
const type = context.checker.getTypeAtLocation(importSpecifier.name);
|
|
39
40
|
const leftIdentifier = (0, identifier_1.transformIdentifier)(context, importSpecifier.name);
|
|
40
|
-
|
|
41
|
+
// If imported value has a customName annotation use that, otherwise use regular property
|
|
42
|
+
const customName = (0, identifier_1.getCustomNameFromSymbol)(context, type.getSymbol());
|
|
43
|
+
const propertyName = customName
|
|
44
|
+
? lua.createStringLiteral(customName, (_a = importSpecifier.propertyName) !== null && _a !== void 0 ? _a : importSpecifier.name)
|
|
45
|
+
: (0, literal_1.transformPropertyName)(context, (_b = importSpecifier.propertyName) !== null && _b !== void 0 ? _b : importSpecifier.name);
|
|
41
46
|
return lua.createVariableDeclarationStatement(leftIdentifier, lua.createTableIndexExpression(moduleTableName, propertyName), importSpecifier);
|
|
42
47
|
}
|
|
43
48
|
const transformImportDeclaration = (statement, context) => {
|
|
49
|
+
var _a;
|
|
44
50
|
const scope = (0, scope_1.peekScope)(context);
|
|
45
|
-
|
|
46
|
-
scope.importStatements = [];
|
|
47
|
-
}
|
|
51
|
+
(_a = scope.importStatements) !== null && _a !== void 0 ? _a : (scope.importStatements = []);
|
|
48
52
|
const result = [];
|
|
49
53
|
const requireCall = createModuleRequire(context, statement.moduleSpecifier);
|
|
50
54
|
// import "./module";
|
|
@@ -153,8 +153,8 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
|
|
|
153
153
|
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, values, initializer));
|
|
154
154
|
}
|
|
155
155
|
else {
|
|
156
|
-
//
|
|
157
|
-
const unpackedInitializer = (0, lua_ast_1.
|
|
156
|
+
// use unpack(thing, 1, #bindingItems) to unpack
|
|
157
|
+
const unpackedInitializer = (0, lua_ast_1.createBoundedUnpackCall)(context, context.transformExpression(initializer), bindingPattern.elements.length, initializer);
|
|
158
158
|
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, unpackedInitializer, initializer));
|
|
159
159
|
}
|
|
160
160
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.33.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.9.3"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@typescript-to-lua/language-extensions": "1.19.0",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"prettier": "^2.8.8",
|
|
70
70
|
"ts-jest": "^29.2.5",
|
|
71
71
|
"ts-node": "^10.9.2",
|
|
72
|
-
"typescript": "5.
|
|
73
|
-
"typescript-eslint": "^8.
|
|
72
|
+
"typescript": "5.9.3",
|
|
73
|
+
"typescript-eslint": "^8.46.3"
|
|
74
74
|
}
|
|
75
75
|
}
|