typescript-to-lua 1.21.0 → 1.22.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.d.ts +1 -0
- package/dist/LuaLib.js +1 -0
- package/dist/lualib/5.0/NumberIsInteger.lua +3 -0
- package/dist/lualib/5.0/Unpack.lua +1 -1
- package/dist/lualib/5.0/lualib_bundle.lua +6 -1
- package/dist/lualib/5.0/lualib_module_info.json +8 -0
- package/dist/lualib/universal/NumberIsInteger.lua +3 -0
- package/dist/lualib/universal/lualib_bundle.lua +5 -0
- package/dist/lualib/universal/lualib_module_info.json +8 -0
- package/dist/transformation/builtins/number.js +2 -0
- package/dist/transformation/utils/function-context.d.ts +1 -1
- package/dist/transformation/utils/function-context.js +51 -4
- package/dist/transformation/visitors/call.d.ts +1 -1
- package/dist/transformation/visitors/call.js +8 -18
- package/dist/transformation/visitors/template.js +3 -5
- package/dist/transpilation/plugins.d.ts +8 -0
- package/dist/transpilation/transpiler.js +7 -1
- package/package.json +1 -1
package/dist/LuaLib.d.ts
CHANGED
package/dist/LuaLib.js
CHANGED
|
@@ -65,6 +65,7 @@ var LuaLibFeature;
|
|
|
65
65
|
LuaLibFeature["New"] = "New";
|
|
66
66
|
LuaLibFeature["Number"] = "Number";
|
|
67
67
|
LuaLibFeature["NumberIsFinite"] = "NumberIsFinite";
|
|
68
|
+
LuaLibFeature["NumberIsInteger"] = "NumberIsInteger";
|
|
68
69
|
LuaLibFeature["NumberIsNaN"] = "NumberIsNaN";
|
|
69
70
|
LuaLibFeature["NumberParseInt"] = "ParseInt";
|
|
70
71
|
LuaLibFeature["NumberParseFloat"] = "ParseFloat";
|
|
@@ -1241,7 +1241,7 @@ end
|
|
|
1241
1241
|
|
|
1242
1242
|
local function __TS__Unpack(list, i, j)
|
|
1243
1243
|
if i == 1 and j == nil then
|
|
1244
|
-
return unpack(
|
|
1244
|
+
return unpack(list)
|
|
1245
1245
|
else
|
|
1246
1246
|
if j == nil then
|
|
1247
1247
|
j = table.getn(list)
|
|
@@ -1526,6 +1526,10 @@ local function __TS__NumberIsFinite(value)
|
|
|
1526
1526
|
return type(value) == "number" and value == value and value ~= 1 / 0 and value ~= -(1 / 0)
|
|
1527
1527
|
end
|
|
1528
1528
|
|
|
1529
|
+
local function __TS__NumberIsInteger(value)
|
|
1530
|
+
return __TS__NumberIsFinite(value) and math.floor(value) == value
|
|
1531
|
+
end
|
|
1532
|
+
|
|
1529
1533
|
local function __TS__NumberIsNaN(value)
|
|
1530
1534
|
return value ~= value
|
|
1531
1535
|
end
|
|
@@ -2627,6 +2631,7 @@ return {
|
|
|
2627
2631
|
__TS__New = __TS__New,
|
|
2628
2632
|
__TS__Number = __TS__Number,
|
|
2629
2633
|
__TS__NumberIsFinite = __TS__NumberIsFinite,
|
|
2634
|
+
__TS__NumberIsInteger = __TS__NumberIsInteger,
|
|
2630
2635
|
__TS__NumberIsNaN = __TS__NumberIsNaN,
|
|
2631
2636
|
__TS__ParseInt = __TS__ParseInt,
|
|
2632
2637
|
__TS__ParseFloat = __TS__ParseFloat,
|
|
@@ -1466,6 +1466,10 @@ local function __TS__NumberIsFinite(value)
|
|
|
1466
1466
|
return type(value) == "number" and value == value and value ~= math.huge and value ~= -math.huge
|
|
1467
1467
|
end
|
|
1468
1468
|
|
|
1469
|
+
local function __TS__NumberIsInteger(value)
|
|
1470
|
+
return __TS__NumberIsFinite(value) and math.floor(value) == value
|
|
1471
|
+
end
|
|
1472
|
+
|
|
1469
1473
|
local function __TS__NumberIsNaN(value)
|
|
1470
1474
|
return value ~= value
|
|
1471
1475
|
end
|
|
@@ -2567,6 +2571,7 @@ return {
|
|
|
2567
2571
|
__TS__New = __TS__New,
|
|
2568
2572
|
__TS__Number = __TS__Number,
|
|
2569
2573
|
__TS__NumberIsFinite = __TS__NumberIsFinite,
|
|
2574
|
+
__TS__NumberIsInteger = __TS__NumberIsInteger,
|
|
2570
2575
|
__TS__NumberIsNaN = __TS__NumberIsNaN,
|
|
2571
2576
|
__TS__ParseInt = __TS__ParseInt,
|
|
2572
2577
|
__TS__ParseFloat = __TS__ParseFloat,
|
|
@@ -74,6 +74,8 @@ function transformNumberConstructorCall(context, node, calledMethod) {
|
|
|
74
74
|
const parameters = (0, call_1.transformArguments)(context, node.arguments);
|
|
75
75
|
const methodName = calledMethod.name.text;
|
|
76
76
|
switch (methodName) {
|
|
77
|
+
case "isInteger":
|
|
78
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsInteger, node, ...parameters);
|
|
77
79
|
case "isNaN":
|
|
78
80
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsNaN, node, ...parameters);
|
|
79
81
|
case "isFinite":
|
|
@@ -6,5 +6,5 @@ export declare enum ContextType {
|
|
|
6
6
|
NonVoid = 2,
|
|
7
7
|
Mixed = 3
|
|
8
8
|
}
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function getCallContextType(context: TransformationContext, callExpression: ts.CallLikeExpression): ContextType;
|
|
10
10
|
export declare function getFunctionContextType(context: TransformationContext, type: ts.Type): ContextType;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getFunctionContextType = exports.
|
|
3
|
+
exports.getFunctionContextType = exports.getCallContextType = exports.ContextType = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const annotations_1 = require("./annotations");
|
|
6
6
|
const typescript_1 = require("./typescript");
|
|
@@ -32,8 +32,33 @@ function getExplicitThisParameter(signatureDeclaration) {
|
|
|
32
32
|
return param;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
const callContextTypes = new WeakMap();
|
|
36
|
+
function getCallContextType(context, callExpression) {
|
|
37
|
+
const known = callContextTypes.get(callExpression);
|
|
38
|
+
if (known !== undefined)
|
|
39
|
+
return known;
|
|
40
|
+
const signature = context.checker.getResolvedSignature(callExpression);
|
|
41
|
+
const signatureDeclaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
|
|
42
|
+
let contextType = ContextType.None;
|
|
43
|
+
if (signatureDeclaration) {
|
|
44
|
+
contextType = computeDeclarationContextType(context, signatureDeclaration);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// No signature declaration could be resolved, so instead try to see if the declaration is in a
|
|
48
|
+
// noSelfInFile file
|
|
49
|
+
const declarations = findRootDeclarations(context, callExpression);
|
|
50
|
+
contextType = declarations.some(d => (0, annotations_1.getFileAnnotations)(d.getSourceFile()).has(annotations_1.AnnotationKind.NoSelfInFile))
|
|
51
|
+
? ContextType.Void
|
|
52
|
+
: context.options.noImplicitSelf
|
|
53
|
+
? ContextType.Void
|
|
54
|
+
: ContextType.NonVoid;
|
|
55
|
+
}
|
|
56
|
+
callContextTypes.set(callExpression, contextType);
|
|
57
|
+
return contextType;
|
|
58
|
+
}
|
|
59
|
+
exports.getCallContextType = getCallContextType;
|
|
35
60
|
const signatureDeclarationContextTypes = new WeakMap();
|
|
36
|
-
function
|
|
61
|
+
function getSignatureContextType(context, signatureDeclaration) {
|
|
37
62
|
const known = signatureDeclarationContextTypes.get(signatureDeclaration);
|
|
38
63
|
if (known !== undefined)
|
|
39
64
|
return known;
|
|
@@ -41,7 +66,29 @@ function getDeclarationContextType(context, signatureDeclaration) {
|
|
|
41
66
|
signatureDeclarationContextTypes.set(signatureDeclaration, contextType);
|
|
42
67
|
return contextType;
|
|
43
68
|
}
|
|
44
|
-
|
|
69
|
+
function findRootDeclarations(context, callExpression) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
const calledExpression = ts.isTaggedTemplateExpression(callExpression)
|
|
72
|
+
? callExpression.tag
|
|
73
|
+
: ts.isJsxSelfClosingElement(callExpression)
|
|
74
|
+
? callExpression.tagName
|
|
75
|
+
: ts.isJsxOpeningElement(callExpression)
|
|
76
|
+
? callExpression.tagName
|
|
77
|
+
: callExpression.expression;
|
|
78
|
+
const calledSymbol = context.checker.getSymbolAtLocation(calledExpression);
|
|
79
|
+
if (calledSymbol === undefined)
|
|
80
|
+
return [];
|
|
81
|
+
return ((_b = (_a = calledSymbol.getDeclarations()) === null || _a === void 0 ? void 0 : _a.flatMap(d => {
|
|
82
|
+
var _a;
|
|
83
|
+
if (ts.isImportSpecifier(d)) {
|
|
84
|
+
const aliasSymbol = context.checker.getAliasedSymbol(calledSymbol);
|
|
85
|
+
return (_a = aliasSymbol.getDeclarations()) !== null && _a !== void 0 ? _a : [];
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
return [d];
|
|
89
|
+
}
|
|
90
|
+
})) !== null && _b !== void 0 ? _b : []);
|
|
91
|
+
}
|
|
45
92
|
function computeDeclarationContextType(context, signatureDeclaration) {
|
|
46
93
|
const thisParameter = getExplicitThisParameter(signatureDeclaration);
|
|
47
94
|
if (thisParameter) {
|
|
@@ -140,6 +187,6 @@ function computeFunctionContextType(context, type) {
|
|
|
140
187
|
if (signatures.length === 0) {
|
|
141
188
|
return ContextType.None;
|
|
142
189
|
}
|
|
143
|
-
return reduceContextTypes(signatures.flatMap(s => getSignatureDeclarations(context, s)).map(s =>
|
|
190
|
+
return reduceContextTypes(signatures.flatMap(s => getSignatureDeclarations(context, s)).map(s => getSignatureContextType(context, s)));
|
|
144
191
|
}
|
|
145
192
|
//# sourceMappingURL=function-context.js.map
|
|
@@ -4,6 +4,6 @@ import { FunctionVisitor, TransformationContext } from "../context";
|
|
|
4
4
|
export declare function validateArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature): void;
|
|
5
5
|
export declare function transformArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature, callContext?: ts.Expression): lua.Expression[];
|
|
6
6
|
export declare function transformCallAndArguments(context: TransformationContext, callExpression: ts.Expression, params: readonly ts.Expression[], signature?: ts.Signature, callContext?: ts.Expression): [lua.Expression, lua.Expression[]];
|
|
7
|
-
export declare function transformContextualCallExpression(context: TransformationContext, node: ts.CallExpression | ts.TaggedTemplateExpression, args: ts.Expression[] | ts.NodeArray<ts.Expression
|
|
7
|
+
export declare function transformContextualCallExpression(context: TransformationContext, node: ts.CallExpression | ts.TaggedTemplateExpression, args: ts.Expression[] | ts.NodeArray<ts.Expression>): lua.Expression;
|
|
8
8
|
export declare const transformCallExpression: FunctionVisitor<ts.CallExpression>;
|
|
9
9
|
export declare function getCalledExpression(node: ts.CallExpression): ts.Expression;
|
|
@@ -78,12 +78,12 @@ function transformElementAccessCall(context, left, transformedArguments, argPrec
|
|
|
78
78
|
}
|
|
79
79
|
return lua.createCallExpression(index, [selfIdentifier, ...transformedArguments]);
|
|
80
80
|
}
|
|
81
|
-
function transformContextualCallExpression(context, node, args
|
|
81
|
+
function transformContextualCallExpression(context, node, args) {
|
|
82
82
|
if (ts.isOptionalChain(node)) {
|
|
83
83
|
return (0, optional_chaining_1.transformOptionalChain)(context, node);
|
|
84
84
|
}
|
|
85
85
|
const left = ts.isCallExpression(node) ? getCalledExpression(node) : node.tag;
|
|
86
|
-
let { precedingStatements: argPrecedingStatements, result: transformedArguments } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => transformArguments(context, args
|
|
86
|
+
let { precedingStatements: argPrecedingStatements, result: transformedArguments } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => transformArguments(context, args));
|
|
87
87
|
if (ts.isPropertyAccessExpression(left) &&
|
|
88
88
|
ts.isIdentifier(left.name) &&
|
|
89
89
|
(0, safe_names_1.isValidLuaIdentifier)(left.name.text, context.options) &&
|
|
@@ -126,10 +126,9 @@ function transformPropertyCall(context, node, calledMethod) {
|
|
|
126
126
|
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
|
|
127
127
|
return lua.createCallExpression(context.transformExpression(node.expression), parameters, node);
|
|
128
128
|
}
|
|
129
|
-
|
|
130
|
-
if (!signatureDeclaration || (0, function_context_1.getDeclarationContextType)(context, signatureDeclaration) !== function_context_1.ContextType.Void) {
|
|
129
|
+
if ((0, function_context_1.getCallContextType)(context, node) !== function_context_1.ContextType.Void) {
|
|
131
130
|
// table:name()
|
|
132
|
-
return transformContextualCallExpression(context, node, node.arguments
|
|
131
|
+
return transformContextualCallExpression(context, node, node.arguments);
|
|
133
132
|
}
|
|
134
133
|
else {
|
|
135
134
|
// table.name()
|
|
@@ -138,15 +137,13 @@ function transformPropertyCall(context, node, calledMethod) {
|
|
|
138
137
|
}
|
|
139
138
|
}
|
|
140
139
|
function transformElementCall(context, node) {
|
|
141
|
-
|
|
142
|
-
const signatureDeclaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
|
|
143
|
-
if (!signatureDeclaration || (0, function_context_1.getDeclarationContextType)(context, signatureDeclaration) !== function_context_1.ContextType.Void) {
|
|
140
|
+
if ((0, function_context_1.getCallContextType)(context, node) !== function_context_1.ContextType.Void) {
|
|
144
141
|
// A contextual parameter must be given to this call expression
|
|
145
|
-
return transformContextualCallExpression(context, node, node.arguments
|
|
142
|
+
return transformContextualCallExpression(context, node, node.arguments);
|
|
146
143
|
}
|
|
147
144
|
else {
|
|
148
145
|
// No context
|
|
149
|
-
const [expression, parameters] = transformCallAndArguments(context, node.expression, node.arguments
|
|
146
|
+
const [expression, parameters] = transformCallAndArguments(context, node.expression, node.arguments);
|
|
150
147
|
return lua.createCallExpression(expression, parameters, node);
|
|
151
148
|
}
|
|
152
149
|
}
|
|
@@ -186,7 +183,7 @@ const transformCallExpression = (node, context) => {
|
|
|
186
183
|
}
|
|
187
184
|
let callPath;
|
|
188
185
|
let parameters;
|
|
189
|
-
const isContextualCall =
|
|
186
|
+
const isContextualCall = (0, function_context_1.getCallContextType)(context, node) !== function_context_1.ContextType.Void;
|
|
190
187
|
if (!isContextualCall) {
|
|
191
188
|
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
|
|
192
189
|
}
|
|
@@ -203,13 +200,6 @@ const transformCallExpression = (node, context) => {
|
|
|
203
200
|
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(callExpression) : callExpression;
|
|
204
201
|
};
|
|
205
202
|
exports.transformCallExpression = transformCallExpression;
|
|
206
|
-
function isContextualCallExpression(context, signature) {
|
|
207
|
-
const declaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
|
|
208
|
-
if (!declaration) {
|
|
209
|
-
return !context.options.noImplicitSelf;
|
|
210
|
-
}
|
|
211
|
-
return (0, function_context_1.getDeclarationContextType)(context, declaration) !== function_context_1.ContextType.Void;
|
|
212
|
-
}
|
|
213
203
|
function getCalledExpression(node) {
|
|
214
204
|
return ts.skipOuterExpressions(node.expression);
|
|
215
205
|
}
|
|
@@ -66,14 +66,12 @@ const transformTaggedTemplateExpression = (expression, context) => {
|
|
|
66
66
|
]);
|
|
67
67
|
expressions.unshift(stringObject);
|
|
68
68
|
// Evaluate if there is a self parameter to be used.
|
|
69
|
-
const
|
|
70
|
-
const signatureDeclaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
|
|
71
|
-
const useSelfParameter = signatureDeclaration && (0, function_context_1.getDeclarationContextType)(context, signatureDeclaration) !== function_context_1.ContextType.Void;
|
|
69
|
+
const useSelfParameter = (0, function_context_1.getCallContextType)(context, expression) !== function_context_1.ContextType.Void;
|
|
72
70
|
if (useSelfParameter) {
|
|
73
|
-
return (0, call_1.transformContextualCallExpression)(context, expression, expressions
|
|
71
|
+
return (0, call_1.transformContextualCallExpression)(context, expression, expressions);
|
|
74
72
|
}
|
|
75
73
|
// Argument evaluation.
|
|
76
|
-
const callArguments = (0, call_1.transformArguments)(context, expressions
|
|
74
|
+
const callArguments = (0, call_1.transformArguments)(context, expressions);
|
|
77
75
|
const leftHandSideExpression = context.transformExpression(expression.tag);
|
|
78
76
|
return lua.createCallExpression(leftHandSideExpression, callArguments);
|
|
79
77
|
};
|
|
@@ -29,6 +29,14 @@ export interface Plugin {
|
|
|
29
29
|
* This function is called after translating the input program to Lua, after resolving dependencies and after bundling.
|
|
30
30
|
*/
|
|
31
31
|
beforeEmit?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost, result: EmitFile[]) => ts.Diagnostic[] | void;
|
|
32
|
+
/**
|
|
33
|
+
* This function is called after translating the input program to Lua, after resolving dependencies, after bundling and writing files to disk.
|
|
34
|
+
*/
|
|
35
|
+
afterEmit?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost, result: EmitFile[]) => ts.Diagnostic[] | void;
|
|
36
|
+
/**
|
|
37
|
+
* This function is called when trying to resolve the .lua file corresponding to a Lua require statement. Allows you to provide
|
|
38
|
+
* your own module resolution logic. If return value is undefined, regular module resolution is done.
|
|
39
|
+
*/
|
|
32
40
|
moduleResolution?: (moduleIdentifier: string, requiringFile: string, options: CompilerOptions, emitHost: EmitHost) => string | undefined;
|
|
33
41
|
}
|
|
34
42
|
export declare function getPlugins(program: ts.Program): {
|
|
@@ -31,7 +31,7 @@ class Transpiler {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
emitFiles(program, plugins, emitPlan, writeFile) {
|
|
34
|
-
var _a, _b;
|
|
34
|
+
var _a, _b, _c;
|
|
35
35
|
performance.startSection("emit");
|
|
36
36
|
const options = program.getCompilerOptions();
|
|
37
37
|
if (options.tstlVerbose) {
|
|
@@ -54,6 +54,12 @@ class Transpiler {
|
|
|
54
54
|
writeFile(outputPath + ".map", sourceMap, emitBOM, undefined, sourceFiles);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
+
for (const plugin of plugins) {
|
|
58
|
+
if (plugin.afterEmit) {
|
|
59
|
+
const afterEmitPluginDiagnostics = (_c = plugin.afterEmit(program, options, this.emitHost, emitPlan)) !== null && _c !== void 0 ? _c : [];
|
|
60
|
+
diagnostics.push(...afterEmitPluginDiagnostics);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
57
63
|
if (options.tstlVerbose) {
|
|
58
64
|
console.log("Emit finished!");
|
|
59
65
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.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/",
|