typescript-to-lua 0.42.0 → 1.1.1
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 +31 -1
- package/dist/CompilerOptions.d.ts +4 -3
- package/dist/LuaAST.js +4 -4
- package/dist/LuaLib.d.ts +7 -0
- package/dist/LuaLib.js +32 -1
- package/dist/LuaPrinter.d.ts +6 -2
- package/dist/LuaPrinter.js +25 -16
- package/dist/cli/diagnostics.js +2 -2
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- package/dist/cli/report.js +1 -1
- package/dist/cli/tsconfig.js +4 -4
- package/dist/lualib/Await.lua +59 -0
- package/dist/lualib/OptionalMethodCall.lua +3 -1
- package/dist/lualib/Promise.lua +172 -0
- package/dist/lualib/PromiseAll.lua +44 -0
- package/dist/lualib/PromiseAllSettled.lua +48 -0
- package/dist/lualib/PromiseAny.lua +41 -0
- package/dist/lualib/PromiseRace.lua +28 -0
- package/dist/lualib/SourceMapTraceBack.lua +20 -10
- package/dist/lualib/StringReplace.lua +8 -13
- package/dist/lualib/StringReplaceAll.lua +35 -0
- package/dist/lualib/lualib_bundle.lua +503 -62
- package/dist/transformation/builtins/array.js +29 -29
- package/dist/transformation/builtins/console.js +2 -2
- package/dist/transformation/builtins/function.js +9 -9
- package/dist/transformation/builtins/global.js +8 -8
- package/dist/transformation/builtins/index.js +35 -32
- package/dist/transformation/builtins/math.js +4 -4
- package/dist/transformation/builtins/number.js +7 -7
- package/dist/transformation/builtins/object.js +11 -11
- package/dist/transformation/builtins/promise.d.ts +7 -0
- package/dist/transformation/builtins/promise.js +46 -0
- package/dist/transformation/builtins/string.js +32 -30
- package/dist/transformation/builtins/symbol.js +3 -3
- package/dist/transformation/context/context.js +5 -5
- package/dist/transformation/index.js +1 -1
- package/dist/transformation/utils/annotations.d.ts +1 -4
- package/dist/transformation/utils/annotations.js +2 -39
- package/dist/transformation/utils/assignment-validation.js +8 -7
- package/dist/transformation/utils/diagnostics.d.ts +3 -0
- package/dist/transformation/utils/diagnostics.js +6 -5
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +14 -7
- package/dist/transformation/utils/function-context.js +19 -13
- package/dist/transformation/utils/lua-ast.js +18 -19
- package/dist/transformation/utils/lualib.js +1 -1
- package/dist/transformation/utils/safe-names.js +5 -5
- package/dist/transformation/utils/scope.d.ts +6 -0
- package/dist/transformation/utils/scope.js +70 -40
- package/dist/transformation/utils/symbols.js +6 -6
- package/dist/transformation/utils/transform.js +3 -3
- package/dist/transformation/utils/typescript/nodes.d.ts +2 -0
- package/dist/transformation/utils/typescript/nodes.js +21 -1
- package/dist/transformation/visitors/access.js +16 -16
- package/dist/transformation/visitors/async-await.d.ts +6 -0
- package/dist/transformation/visitors/async-await.js +38 -0
- package/dist/transformation/visitors/binary-expression/assignments.js +29 -29
- package/dist/transformation/visitors/binary-expression/bit.js +4 -4
- package/dist/transformation/visitors/binary-expression/compound.js +18 -18
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +17 -17
- package/dist/transformation/visitors/binary-expression/index.js +21 -21
- package/dist/transformation/visitors/block.js +6 -6
- package/dist/transformation/visitors/break-continue.js +4 -9
- package/dist/transformation/visitors/call.js +38 -33
- package/dist/transformation/visitors/class/decorators.js +3 -3
- package/dist/transformation/visitors/class/index.js +41 -34
- package/dist/transformation/visitors/class/members/accessors.js +6 -6
- package/dist/transformation/visitors/class/members/constructor.js +7 -7
- package/dist/transformation/visitors/class/members/fields.js +6 -6
- package/dist/transformation/visitors/class/members/method.js +6 -6
- package/dist/transformation/visitors/class/new.js +13 -13
- package/dist/transformation/visitors/class/setup.js +11 -11
- package/dist/transformation/visitors/class/utils.js +2 -2
- package/dist/transformation/visitors/conditional.js +6 -6
- package/dist/transformation/visitors/delete.js +4 -4
- package/dist/transformation/visitors/enum.js +8 -8
- package/dist/transformation/visitors/errors.js +30 -31
- package/dist/transformation/visitors/expression-statement.js +10 -6
- package/dist/transformation/visitors/function.js +33 -21
- package/dist/transformation/visitors/identifier.js +25 -19
- package/dist/transformation/visitors/index.js +4 -2
- package/dist/transformation/visitors/language-extensions/iterable.js +7 -7
- package/dist/transformation/visitors/language-extensions/multi.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/multi.js +5 -7
- package/dist/transformation/visitors/language-extensions/operators.js +6 -6
- package/dist/transformation/visitors/language-extensions/range.js +6 -6
- package/dist/transformation/visitors/language-extensions/table.js +5 -5
- package/dist/transformation/visitors/literal.d.ts +1 -2
- package/dist/transformation/visitors/literal.js +19 -42
- package/dist/transformation/visitors/loops/do-while.js +2 -2
- package/dist/transformation/visitors/loops/for-in.js +4 -4
- package/dist/transformation/visitors/loops/for-of.js +13 -13
- package/dist/transformation/visitors/loops/for.js +3 -3
- package/dist/transformation/visitors/loops/utils.js +9 -9
- package/dist/transformation/visitors/modules/export.js +13 -14
- package/dist/transformation/visitors/modules/import.js +10 -10
- package/dist/transformation/visitors/namespace.js +13 -13
- package/dist/transformation/visitors/return.d.ts +1 -0
- package/dist/transformation/visitors/return.js +41 -49
- package/dist/transformation/visitors/sourceFile.js +8 -8
- package/dist/transformation/visitors/spread.js +16 -11
- package/dist/transformation/visitors/switch.js +132 -30
- package/dist/transformation/visitors/template.js +5 -5
- package/dist/transformation/visitors/typeof.js +2 -2
- package/dist/transformation/visitors/typescript.js +1 -1
- package/dist/transformation/visitors/unary-expression.js +9 -9
- package/dist/transformation/visitors/variable-declaration.js +28 -28
- package/dist/transformation/visitors/void.d.ts +6 -0
- package/dist/transformation/visitors/void.js +23 -0
- package/dist/transpilation/bundle.d.ts +3 -0
- package/dist/transpilation/bundle.js +56 -13
- package/dist/transpilation/diagnostics.js +3 -3
- package/dist/transpilation/index.js +2 -2
- package/dist/transpilation/output-collector.js +2 -2
- package/dist/transpilation/plugins.js +1 -1
- package/dist/transpilation/resolve.js +28 -15
- package/dist/transpilation/transformers.d.ts +1 -0
- package/dist/transpilation/transformers.js +61 -25
- package/dist/transpilation/transpile.js +7 -7
- package/dist/transpilation/transpiler.js +8 -8
- package/dist/transpilation/utils.js +3 -2
- package/dist/tstl.js +8 -8
- package/dist/utils.js +2 -2
- package/package.json +15 -12
- package/dist/transformation/visitors/jsx/jsx.d.ts +0 -9
- package/dist/transformation/visitors/jsx/jsx.js +0 -237
- package/dist/transformation/visitors/jsx/xhtml.d.ts +0 -3
- package/dist/transformation/visitors/jsx/xhtml.js +0 -260
|
@@ -10,7 +10,7 @@ const scope_1 = require("./scope");
|
|
|
10
10
|
const lualib_1 = require("./lualib");
|
|
11
11
|
const LuaLib_1 = require("../../LuaLib");
|
|
12
12
|
function unwrapVisitorResult(result) {
|
|
13
|
-
return result === undefined ? [] : utils_1.castArray(result);
|
|
13
|
+
return result === undefined ? [] : (0, utils_1.castArray)(result);
|
|
14
14
|
}
|
|
15
15
|
exports.unwrapVisitorResult = unwrapVisitorResult;
|
|
16
16
|
function createSelfIdentifier(tsOriginal) {
|
|
@@ -58,7 +58,7 @@ exports.getNumberLiteralValue = getNumberLiteralValue;
|
|
|
58
58
|
// Prefer use of transformToImmediatelyInvokedFunctionExpression to maintain correct scope. If you use this directly,
|
|
59
59
|
// ensure you push/pop a function scope appropriately to avoid incorrect vararg optimization.
|
|
60
60
|
function createImmediatelyInvokedFunctionExpression(statements, result, tsOriginal) {
|
|
61
|
-
const body = [...statements, lua.createReturnStatement(utils_1.castArray(result))];
|
|
61
|
+
const body = [...statements, lua.createReturnStatement((0, utils_1.castArray)(result))];
|
|
62
62
|
const flags = statements.length === 0 ? lua.FunctionExpressionFlags.Inline : lua.FunctionExpressionFlags.None;
|
|
63
63
|
const iife = lua.createFunctionExpression(lua.createBlock(body), undefined, undefined, flags);
|
|
64
64
|
return lua.createCallExpression(iife, [], tsOriginal);
|
|
@@ -66,7 +66,7 @@ function createImmediatelyInvokedFunctionExpression(statements, result, tsOrigin
|
|
|
66
66
|
exports.createImmediatelyInvokedFunctionExpression = createImmediatelyInvokedFunctionExpression;
|
|
67
67
|
function createUnpackCall(context, expression, tsOriginal) {
|
|
68
68
|
if (context.luaTarget === CompilerOptions_1.LuaTarget.Universal) {
|
|
69
|
-
return lualib_1.transformLuaLibFunction(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
|
|
69
|
+
return (0, lualib_1.transformLuaLibFunction)(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
|
|
70
70
|
}
|
|
71
71
|
const unpack = context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 || context.luaTarget === CompilerOptions_1.LuaTarget.LuaJIT
|
|
72
72
|
? lua.createIdentifier("unpack")
|
|
@@ -91,8 +91,8 @@ exports.wrapInToStringForConcat = wrapInToStringForConcat;
|
|
|
91
91
|
function createHoistableVariableDeclarationStatement(context, identifier, initializer, tsOriginal) {
|
|
92
92
|
const declaration = lua.createVariableDeclarationStatement(identifier, initializer, tsOriginal);
|
|
93
93
|
if (identifier.symbolId !== undefined) {
|
|
94
|
-
const scope = scope_1.peekScope(context);
|
|
95
|
-
utils_1.assert(scope.type !== scope_1.ScopeType.Switch);
|
|
94
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
95
|
+
(0, utils_1.assert)(scope.type !== scope_1.ScopeType.Switch);
|
|
96
96
|
if (!scope.variableDeclarations) {
|
|
97
97
|
scope.variableDeclarations = [];
|
|
98
98
|
}
|
|
@@ -106,32 +106,32 @@ function hasMultipleReferences(scope, identifiers) {
|
|
|
106
106
|
if (!scopeSymbols) {
|
|
107
107
|
return false;
|
|
108
108
|
}
|
|
109
|
-
const referenceLists = utils_1.castArray(identifiers).map(i => i.symbolId && scopeSymbols.get(i.symbolId));
|
|
109
|
+
const referenceLists = (0, utils_1.castArray)(identifiers).map(i => i.symbolId && scopeSymbols.get(i.symbolId));
|
|
110
110
|
return referenceLists.some(symbolRefs => symbolRefs && symbolRefs.length > 1);
|
|
111
111
|
}
|
|
112
112
|
function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal, overrideExportScope) {
|
|
113
113
|
let declaration;
|
|
114
114
|
let assignment;
|
|
115
115
|
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);
|
|
116
|
-
const identifiers = utils_1.castArray(lhs);
|
|
116
|
+
const identifiers = (0, utils_1.castArray)(lhs);
|
|
117
117
|
if (identifiers.length === 0) {
|
|
118
118
|
return [];
|
|
119
119
|
}
|
|
120
|
-
const exportScope = overrideExportScope !== null && overrideExportScope !== void 0 ? overrideExportScope : export_1.getIdentifierExportScope(context, identifiers[0]);
|
|
120
|
+
const exportScope = overrideExportScope !== null && overrideExportScope !== void 0 ? overrideExportScope : (0, export_1.getIdentifierExportScope)(context, identifiers[0]);
|
|
121
121
|
if (exportScope) {
|
|
122
122
|
// exported
|
|
123
123
|
if (!rhs) {
|
|
124
124
|
return [];
|
|
125
125
|
}
|
|
126
126
|
else {
|
|
127
|
-
assignment = lua.createAssignmentStatement(identifiers.map(identifier => export_1.createExportedIdentifier(context, identifier, exportScope)), rhs, tsOriginal);
|
|
127
|
+
assignment = lua.createAssignmentStatement(identifiers.map(identifier => (0, export_1.createExportedIdentifier)(context, identifier, exportScope)), rhs, tsOriginal);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
else {
|
|
131
|
-
const scope = scope_1.peekScope(context);
|
|
131
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
132
132
|
const isTopLevelVariable = scope.type === scope_1.ScopeType.File;
|
|
133
133
|
if (context.isModule || !isTopLevelVariable) {
|
|
134
|
-
if (
|
|
134
|
+
if (!isFunctionDeclaration && hasMultipleReferences(scope, lhs)) {
|
|
135
135
|
// Split declaration and assignment of identifiers that reference themselves in their declaration
|
|
136
136
|
declaration = lua.createVariableDeclarationStatement(lhs, undefined, tsOriginal);
|
|
137
137
|
if (rhs) {
|
|
@@ -141,13 +141,12 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
141
141
|
else {
|
|
142
142
|
declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal);
|
|
143
143
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
scope.variableDeclarations
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
declaration = undefined;
|
|
144
|
+
if (!isFunctionDeclaration) {
|
|
145
|
+
// Remember local variable declarations for hoisting later
|
|
146
|
+
if (!scope.variableDeclarations) {
|
|
147
|
+
scope.variableDeclarations = [];
|
|
148
|
+
}
|
|
149
|
+
scope.variableDeclarations.push(declaration);
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
152
|
else if (rhs) {
|
|
@@ -161,7 +160,7 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
161
160
|
if (isFunctionDeclaration) {
|
|
162
161
|
// Remember function definitions for hoisting later
|
|
163
162
|
const functionSymbolId = lhs.symbolId;
|
|
164
|
-
const scope = scope_1.peekScope(context);
|
|
163
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
165
164
|
if (functionSymbolId && scope.functionDefinitions) {
|
|
166
165
|
const definitions = scope.functionDefinitions.get(functionSymbolId);
|
|
167
166
|
if (definitions) {
|
|
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "LuaLibFeature", { enumerable: true, get: functio
|
|
|
7
7
|
const utils_1 = require("../../utils");
|
|
8
8
|
const luaLibFeatures = new WeakMap();
|
|
9
9
|
function getUsedLuaLibFeatures(context) {
|
|
10
|
-
return utils_1.getOrUpdate(luaLibFeatures, context, () => new Set());
|
|
10
|
+
return (0, utils_1.getOrUpdate)(luaLibFeatures, context, () => new Set());
|
|
11
11
|
}
|
|
12
12
|
exports.getUsedLuaLibFeatures = getUsedLuaLibFeatures;
|
|
13
13
|
function importLuaLibFeature(context, feature) {
|
|
@@ -51,27 +51,27 @@ const luaBuiltins = new Set([
|
|
|
51
51
|
"type",
|
|
52
52
|
"unpack",
|
|
53
53
|
]);
|
|
54
|
-
const isUnsafeName = (name) => !exports.isValidLuaIdentifier(name) || luaBuiltins.has(name);
|
|
54
|
+
const isUnsafeName = (name) => !(0, exports.isValidLuaIdentifier)(name) || luaBuiltins.has(name);
|
|
55
55
|
exports.isUnsafeName = isUnsafeName;
|
|
56
56
|
function checkName(context, name, node) {
|
|
57
|
-
const isInvalid = !exports.isValidLuaIdentifier(name);
|
|
57
|
+
const isInvalid = !(0, exports.isValidLuaIdentifier)(name);
|
|
58
58
|
if (isInvalid) {
|
|
59
59
|
// Empty identifier is a TypeScript error
|
|
60
60
|
if (name !== "") {
|
|
61
|
-
context.diagnostics.push(diagnostics_1.invalidAmbientIdentifierName(node, name));
|
|
61
|
+
context.diagnostics.push((0, diagnostics_1.invalidAmbientIdentifierName)(node, name));
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
return isInvalid;
|
|
65
65
|
}
|
|
66
66
|
function hasUnsafeSymbolName(context, symbol, tsOriginal) {
|
|
67
67
|
var _a, _b;
|
|
68
|
-
const isAmbient = (_b = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => typescript_1.isAmbientNode(d))) !== null && _b !== void 0 ? _b : false;
|
|
68
|
+
const isAmbient = (_b = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => (0, typescript_1.isAmbientNode)(d))) !== null && _b !== void 0 ? _b : false;
|
|
69
69
|
// Catch ambient declarations of identifiers with bad names
|
|
70
70
|
if (isAmbient && checkName(context, symbol.name, tsOriginal)) {
|
|
71
71
|
return true;
|
|
72
72
|
}
|
|
73
73
|
// only unsafe when non-ambient and not exported
|
|
74
|
-
return exports.isUnsafeName(symbol.name) && !isAmbient && !export_1.isSymbolExported(context, symbol);
|
|
74
|
+
return (0, exports.isUnsafeName)(symbol.name) && !isAmbient && !(0, export_1.isSymbolExported)(context, symbol);
|
|
75
75
|
}
|
|
76
76
|
exports.hasUnsafeSymbolName = hasUnsafeSymbolName;
|
|
77
77
|
function hasUnsafeIdentifierName(context, identifier, checkSymbol = true) {
|
|
@@ -26,6 +26,11 @@ export interface Scope {
|
|
|
26
26
|
loopContinued?: boolean;
|
|
27
27
|
functionReturned?: boolean;
|
|
28
28
|
}
|
|
29
|
+
export interface HoistingResult {
|
|
30
|
+
statements: lua.Statement[];
|
|
31
|
+
hoistedStatements: lua.Statement[];
|
|
32
|
+
hoistedIdentifiers: lua.Identifier[];
|
|
33
|
+
}
|
|
29
34
|
export declare function walkScopesUp(context: TransformationContext): IterableIterator<Scope>;
|
|
30
35
|
export declare function markSymbolAsReferencedInCurrentScopes(context: TransformationContext, symbolId: lua.SymbolId, identifier: ts.Identifier): void;
|
|
31
36
|
export declare function peekScope(context: TransformationContext): Scope;
|
|
@@ -37,5 +42,6 @@ export declare function hasReferencedSymbol(context: TransformationContext, scop
|
|
|
37
42
|
export declare function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & {
|
|
38
43
|
node: ts.SignatureDeclaration;
|
|
39
44
|
};
|
|
45
|
+
export declare function separateHoistedStatements(context: TransformationContext, statements: lua.Statement[]): HoistingResult;
|
|
40
46
|
export declare function performHoisting(context: TransformationContext, statements: lua.Statement[]): lua.Statement[];
|
|
41
47
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.performHoisting = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.popScope = exports.pushScope = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
|
|
3
|
+
exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.popScope = exports.pushScope = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
@@ -19,7 +19,7 @@ var ScopeType;
|
|
|
19
19
|
})(ScopeType = exports.ScopeType || (exports.ScopeType = {}));
|
|
20
20
|
const scopeStacks = new WeakMap();
|
|
21
21
|
function getScopeStack(context) {
|
|
22
|
-
return utils_1.getOrUpdate(scopeStacks, context, () => []);
|
|
22
|
+
return (0, utils_1.getOrUpdate)(scopeStacks, context, () => []);
|
|
23
23
|
}
|
|
24
24
|
function* walkScopesUp(context) {
|
|
25
25
|
const scopeStack = getScopeStack(context);
|
|
@@ -34,7 +34,7 @@ function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
|
|
|
34
34
|
if (!scope.referencedSymbols) {
|
|
35
35
|
scope.referencedSymbols = new Map();
|
|
36
36
|
}
|
|
37
|
-
const references = utils_1.getOrUpdate(scope.referencedSymbols, symbolId, () => []);
|
|
37
|
+
const references = (0, utils_1.getOrUpdate)(scope.referencedSymbols, symbolId, () => []);
|
|
38
38
|
references.push(identifier);
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -42,7 +42,7 @@ exports.markSymbolAsReferencedInCurrentScopes = markSymbolAsReferencedInCurrentS
|
|
|
42
42
|
function peekScope(context) {
|
|
43
43
|
const scopeStack = getScopeStack(context);
|
|
44
44
|
const scope = scopeStack[scopeStack.length - 1];
|
|
45
|
-
utils_1.assert(scope);
|
|
45
|
+
(0, utils_1.assert)(scope);
|
|
46
46
|
return scope;
|
|
47
47
|
}
|
|
48
48
|
exports.peekScope = peekScope;
|
|
@@ -64,13 +64,13 @@ exports.pushScope = pushScope;
|
|
|
64
64
|
function popScope(context) {
|
|
65
65
|
const scopeStack = getScopeStack(context);
|
|
66
66
|
const scope = scopeStack.pop();
|
|
67
|
-
utils_1.assert(scope);
|
|
67
|
+
(0, utils_1.assert)(scope);
|
|
68
68
|
return scope;
|
|
69
69
|
}
|
|
70
70
|
exports.popScope = popScope;
|
|
71
71
|
function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
|
|
72
72
|
var _a;
|
|
73
|
-
return (_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => ts.isFunctionDeclaration(d) && typescript_1.findFirstNodeAbove(d, (n) => n === scopeNode));
|
|
73
|
+
return (_a = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _a === void 0 ? void 0 : _a.some(d => ts.isFunctionDeclaration(d) && (0, typescript_1.findFirstNodeAbove)(d, (n) => n === scopeNode));
|
|
74
74
|
}
|
|
75
75
|
// Checks for references to local functions which haven't been defined yet,
|
|
76
76
|
// and thus will be hoisted above the current position.
|
|
@@ -106,21 +106,43 @@ function isFunctionScopeWithDefinition(scope) {
|
|
|
106
106
|
return scope.node !== undefined && ts.isFunctionLike(scope.node);
|
|
107
107
|
}
|
|
108
108
|
exports.isFunctionScopeWithDefinition = isFunctionScopeWithDefinition;
|
|
109
|
-
function
|
|
109
|
+
function separateHoistedStatements(context, statements) {
|
|
110
110
|
const scope = peekScope(context);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
const allHoistedStatments = [];
|
|
112
|
+
const allHoistedIdentifiers = [];
|
|
113
|
+
let { unhoistedStatements, hoistedStatements, hoistedIdentifiers } = hoistFunctionDefinitions(context, scope, statements);
|
|
114
|
+
allHoistedStatments.push(...hoistedStatements);
|
|
115
|
+
allHoistedIdentifiers.push(...hoistedIdentifiers);
|
|
116
|
+
({ unhoistedStatements, hoistedIdentifiers } = hoistVariableDeclarations(context, scope, unhoistedStatements));
|
|
117
|
+
allHoistedIdentifiers.push(...hoistedIdentifiers);
|
|
118
|
+
({ unhoistedStatements, hoistedStatements } = hoistImportStatements(scope, unhoistedStatements));
|
|
119
|
+
allHoistedStatments.unshift(...hoistedStatements);
|
|
120
|
+
return {
|
|
121
|
+
statements: unhoistedStatements,
|
|
122
|
+
hoistedStatements: allHoistedStatments,
|
|
123
|
+
hoistedIdentifiers: allHoistedIdentifiers,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
exports.separateHoistedStatements = separateHoistedStatements;
|
|
127
|
+
function performHoisting(context, statements) {
|
|
128
|
+
const result = separateHoistedStatements(context, statements);
|
|
129
|
+
const modifiedStatements = [...result.hoistedStatements, ...result.statements];
|
|
130
|
+
if (result.hoistedIdentifiers.length > 0) {
|
|
131
|
+
modifiedStatements.unshift(lua.createVariableDeclarationStatement(result.hoistedIdentifiers));
|
|
132
|
+
}
|
|
133
|
+
return modifiedStatements;
|
|
116
134
|
}
|
|
117
135
|
exports.performHoisting = performHoisting;
|
|
118
136
|
function shouldHoistSymbol(context, symbolId, scope) {
|
|
119
|
-
|
|
137
|
+
// Always hoist in top-level of switch statements
|
|
138
|
+
if (scope.type === ScopeType.Switch) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
const symbolInfo = (0, symbols_1.getSymbolInfo)(context, symbolId);
|
|
120
142
|
if (!symbolInfo) {
|
|
121
143
|
return false;
|
|
122
144
|
}
|
|
123
|
-
const declaration = typescript_1.getFirstDeclarationInFile(symbolInfo.symbol, context.sourceFile);
|
|
145
|
+
const declaration = (0, typescript_1.getFirstDeclarationInFile)(symbolInfo.symbol, context.sourceFile);
|
|
124
146
|
if (!declaration) {
|
|
125
147
|
return false;
|
|
126
148
|
}
|
|
@@ -129,7 +151,7 @@ function shouldHoistSymbol(context, symbolId, scope) {
|
|
|
129
151
|
}
|
|
130
152
|
if (scope.functionDefinitions) {
|
|
131
153
|
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
|
|
132
|
-
utils_1.assert(functionDefinition.definition);
|
|
154
|
+
(0, utils_1.assert)(functionDefinition.definition);
|
|
133
155
|
const { line, column } = lua.getOriginalPos(functionDefinition.definition);
|
|
134
156
|
if (line !== undefined && column !== undefined) {
|
|
135
157
|
const definitionPos = ts.getPositionOfLineAndCharacter(context.sourceFile, line, column);
|
|
@@ -146,52 +168,60 @@ function shouldHoistSymbol(context, symbolId, scope) {
|
|
|
146
168
|
}
|
|
147
169
|
function hoistVariableDeclarations(context, scope, statements) {
|
|
148
170
|
if (!scope.variableDeclarations) {
|
|
149
|
-
return statements;
|
|
171
|
+
return { unhoistedStatements: statements, hoistedIdentifiers: [] };
|
|
150
172
|
}
|
|
151
|
-
const
|
|
152
|
-
const
|
|
173
|
+
const unhoistedStatements = [...statements];
|
|
174
|
+
const hoistedIdentifiers = [];
|
|
153
175
|
for (const declaration of scope.variableDeclarations) {
|
|
154
176
|
const symbols = declaration.left.map(i => i.symbolId).filter(utils_1.isNonNull);
|
|
155
177
|
if (symbols.some(s => shouldHoistSymbol(context, s, scope))) {
|
|
156
|
-
const index =
|
|
157
|
-
|
|
178
|
+
const index = unhoistedStatements.indexOf(declaration);
|
|
179
|
+
if (index < 0) {
|
|
180
|
+
continue; // statements array may not contain all statements in the scope (switch-case)
|
|
181
|
+
}
|
|
158
182
|
if (declaration.right) {
|
|
159
183
|
const assignment = lua.createAssignmentStatement(declaration.left, declaration.right);
|
|
160
184
|
lua.setNodePosition(assignment, declaration); // Preserve position info for sourcemap
|
|
161
|
-
|
|
185
|
+
unhoistedStatements.splice(index, 1, assignment);
|
|
162
186
|
}
|
|
163
187
|
else {
|
|
164
|
-
|
|
188
|
+
unhoistedStatements.splice(index, 1);
|
|
165
189
|
}
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
else if (scope.type === ScopeType.Switch) {
|
|
169
|
-
utils_1.assert(!declaration.right);
|
|
170
|
-
hoistedLocals.push(...declaration.left);
|
|
190
|
+
hoistedIdentifiers.push(...declaration.left);
|
|
171
191
|
}
|
|
172
192
|
}
|
|
173
|
-
|
|
174
|
-
result.unshift(lua.createVariableDeclarationStatement(hoistedLocals));
|
|
175
|
-
}
|
|
176
|
-
return result;
|
|
193
|
+
return { unhoistedStatements, hoistedIdentifiers };
|
|
177
194
|
}
|
|
178
195
|
function hoistFunctionDefinitions(context, scope, statements) {
|
|
179
196
|
if (!scope.functionDefinitions) {
|
|
180
|
-
return statements;
|
|
197
|
+
return { unhoistedStatements: statements, hoistedStatements: [], hoistedIdentifiers: [] };
|
|
181
198
|
}
|
|
182
|
-
const
|
|
183
|
-
const
|
|
199
|
+
const unhoistedStatements = [...statements];
|
|
200
|
+
const hoistedStatements = [];
|
|
201
|
+
const hoistedIdentifiers = [];
|
|
184
202
|
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
|
|
185
|
-
utils_1.assert(functionDefinition.definition);
|
|
203
|
+
(0, utils_1.assert)(functionDefinition.definition);
|
|
186
204
|
if (shouldHoistSymbol(context, functionSymbolId, scope)) {
|
|
187
|
-
const index =
|
|
188
|
-
|
|
189
|
-
|
|
205
|
+
const index = unhoistedStatements.indexOf(functionDefinition.definition);
|
|
206
|
+
if (index < 0) {
|
|
207
|
+
continue; // statements array may not contain all statements in the scope (switch-case)
|
|
208
|
+
}
|
|
209
|
+
unhoistedStatements.splice(index, 1);
|
|
210
|
+
if (lua.isVariableDeclarationStatement(functionDefinition.definition)) {
|
|
211
|
+
// Separate function definition and variable declaration
|
|
212
|
+
(0, utils_1.assert)(functionDefinition.definition.right);
|
|
213
|
+
hoistedIdentifiers.push(...functionDefinition.definition.left);
|
|
214
|
+
hoistedStatements.push(lua.createAssignmentStatement(functionDefinition.definition.left, functionDefinition.definition.right));
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
hoistedStatements.push(functionDefinition.definition);
|
|
218
|
+
}
|
|
190
219
|
}
|
|
191
220
|
}
|
|
192
|
-
return
|
|
221
|
+
return { unhoistedStatements, hoistedStatements, hoistedIdentifiers };
|
|
193
222
|
}
|
|
194
223
|
function hoistImportStatements(scope, statements) {
|
|
195
|
-
|
|
224
|
+
var _a;
|
|
225
|
+
return { unhoistedStatements: statements, hoistedStatements: (_a = scope.importStatements) !== null && _a !== void 0 ? _a : [] };
|
|
196
226
|
}
|
|
197
227
|
//# sourceMappingURL=scope.js.map
|
|
@@ -14,27 +14,27 @@ function nextSymbolId(context) {
|
|
|
14
14
|
const symbolInfoMap = new WeakMap();
|
|
15
15
|
const symbolIdMaps = new WeakMap();
|
|
16
16
|
function getSymbolInfo(context, symbolId) {
|
|
17
|
-
return utils_1.getOrUpdate(symbolInfoMap, context, () => new Map()).get(symbolId);
|
|
17
|
+
return (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map()).get(symbolId);
|
|
18
18
|
}
|
|
19
19
|
exports.getSymbolInfo = getSymbolInfo;
|
|
20
20
|
function getSymbolIdOfSymbol(context, symbol) {
|
|
21
|
-
return utils_1.getOrUpdate(symbolIdMaps, context, () => new Map()).get(symbol);
|
|
21
|
+
return (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map()).get(symbol);
|
|
22
22
|
}
|
|
23
23
|
exports.getSymbolIdOfSymbol = getSymbolIdOfSymbol;
|
|
24
24
|
function trackSymbolReference(context, symbol, identifier) {
|
|
25
|
-
const symbolIds = utils_1.getOrUpdate(symbolIdMaps, context, () => new Map());
|
|
25
|
+
const symbolIds = (0, utils_1.getOrUpdate)(symbolIdMaps, context, () => new Map());
|
|
26
26
|
// Track first time symbols are seen
|
|
27
27
|
let symbolId = symbolIds.get(symbol);
|
|
28
28
|
if (symbolId === undefined) {
|
|
29
29
|
symbolId = nextSymbolId(context);
|
|
30
30
|
symbolIds.set(symbol, symbolId);
|
|
31
|
-
const symbolInfo = utils_1.getOrUpdate(symbolInfoMap, context, () => new Map());
|
|
31
|
+
const symbolInfo = (0, utils_1.getOrUpdate)(symbolInfoMap, context, () => new Map());
|
|
32
32
|
symbolInfo.set(symbolId, { symbol, firstSeenAtPos: identifier.pos });
|
|
33
33
|
}
|
|
34
34
|
// If isOptimizedVarArgSpread returns true, the identifier will not appear in the resulting Lua.
|
|
35
35
|
// Only the optimized ellipses (...) will be used.
|
|
36
|
-
if (!spread_1.isOptimizedVarArgSpread(context, symbol, identifier)) {
|
|
37
|
-
scope_1.markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier);
|
|
36
|
+
if (!(0, spread_1.isOptimizedVarArgSpread)(context, symbol, identifier)) {
|
|
37
|
+
(0, scope_1.markSymbolAsReferencedInCurrentScopes)(context, symbolId, identifier);
|
|
38
38
|
}
|
|
39
39
|
return symbolId;
|
|
40
40
|
}
|
|
@@ -5,10 +5,10 @@ const utils_1 = require("../../utils");
|
|
|
5
5
|
const lua_ast_1 = require("./lua-ast");
|
|
6
6
|
const scope_1 = require("./scope");
|
|
7
7
|
function transformToImmediatelyInvokedFunctionExpression(context, transformFunction, tsOriginal) {
|
|
8
|
-
scope_1.pushScope(context, scope_1.ScopeType.Function);
|
|
8
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
|
|
9
9
|
const { statements, result } = transformFunction();
|
|
10
|
-
scope_1.popScope(context);
|
|
11
|
-
return lua_ast_1.createImmediatelyInvokedFunctionExpression(utils_1.castArray(statements), result, tsOriginal);
|
|
10
|
+
(0, scope_1.popScope)(context);
|
|
11
|
+
return (0, lua_ast_1.createImmediatelyInvokedFunctionExpression)((0, utils_1.castArray)(statements), result, tsOriginal);
|
|
12
12
|
}
|
|
13
13
|
exports.transformToImmediatelyInvokedFunctionExpression = transformToImmediatelyInvokedFunctionExpression;
|
|
14
14
|
//# sourceMappingURL=transform.js.map
|
|
@@ -4,6 +4,8 @@ export declare function isAssignmentPattern(node: ts.Node): node is ts.Assignmen
|
|
|
4
4
|
export declare function isDestructuringAssignment(node: ts.Node): node is ts.DestructuringAssignment;
|
|
5
5
|
export declare function isAmbientNode(node: ts.Declaration): boolean;
|
|
6
6
|
export declare function isInDestructingAssignment(node: ts.Node): boolean;
|
|
7
|
+
export declare function isInAsyncFunction(node: ts.Node): boolean;
|
|
8
|
+
export declare function isInGeneratorFunction(node: ts.Node): boolean;
|
|
7
9
|
/**
|
|
8
10
|
* Quite hacky, avoid unless absolutely necessary!
|
|
9
11
|
*/
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isFirstDeclaration = exports.getSymbolOfNode = exports.isInDestructingAssignment = exports.isAmbientNode = exports.isDestructuringAssignment = exports.isAssignmentPattern = void 0;
|
|
3
|
+
exports.isFirstDeclaration = exports.getSymbolOfNode = exports.isInGeneratorFunction = exports.isInAsyncFunction = exports.isInDestructingAssignment = exports.isAmbientNode = exports.isDestructuringAssignment = exports.isAssignmentPattern = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
|
+
const _1 = require(".");
|
|
5
6
|
function isAssignmentPattern(node) {
|
|
6
7
|
return ts.isObjectLiteralExpression(node) || ts.isArrayLiteralExpression(node);
|
|
7
8
|
}
|
|
@@ -22,6 +23,25 @@ function isInDestructingAssignment(node) {
|
|
|
22
23
|
(ts.isBinaryExpression(node.parent) && ts.isArrayLiteralExpression(node.parent.left))));
|
|
23
24
|
}
|
|
24
25
|
exports.isInDestructingAssignment = isInDestructingAssignment;
|
|
26
|
+
function isInAsyncFunction(node) {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
// Check if node is in function declaration with `async`
|
|
29
|
+
const declaration = (0, _1.findFirstNodeAbove)(node, ts.isFunctionLike);
|
|
30
|
+
if (!declaration) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return (_b = (_a = declaration.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
|
|
34
|
+
}
|
|
35
|
+
exports.isInAsyncFunction = isInAsyncFunction;
|
|
36
|
+
function isInGeneratorFunction(node) {
|
|
37
|
+
// Check if node is in function declaration with `async`
|
|
38
|
+
const declaration = (0, _1.findFirstNodeAbove)(node, ts.isFunctionDeclaration);
|
|
39
|
+
if (!declaration) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return declaration.asteriskToken !== undefined;
|
|
43
|
+
}
|
|
44
|
+
exports.isInGeneratorFunction = isInGeneratorFunction;
|
|
25
45
|
/**
|
|
26
46
|
* Quite hacky, avoid unless absolutely necessary!
|
|
27
47
|
*/
|
|
@@ -15,29 +15,29 @@ function transformElementAccessArgument(context, node) {
|
|
|
15
15
|
const index = context.transformExpression(node.argumentExpression);
|
|
16
16
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
17
17
|
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
18
|
-
if (typescript_1.isArrayType(context, type) && typescript_1.isNumberType(context, argumentType)) {
|
|
19
|
-
return lua_ast_1.addToNumericExpression(index, 1);
|
|
18
|
+
if ((0, typescript_1.isArrayType)(context, type) && (0, typescript_1.isNumberType)(context, argumentType)) {
|
|
19
|
+
return (0, lua_ast_1.addToNumericExpression)(index, 1);
|
|
20
20
|
}
|
|
21
21
|
return index;
|
|
22
22
|
}
|
|
23
23
|
exports.transformElementAccessArgument = transformElementAccessArgument;
|
|
24
24
|
const transformElementAccessExpression = (node, context) => {
|
|
25
|
-
const constEnumValue = enum_1.tryGetConstEnumValue(context, node);
|
|
25
|
+
const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
|
|
26
26
|
if (constEnumValue) {
|
|
27
27
|
return constEnumValue;
|
|
28
28
|
}
|
|
29
29
|
const table = context.transformExpression(node.expression);
|
|
30
30
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
31
31
|
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
32
|
-
if (typescript_1.isStringType(context, type) && typescript_1.isNumberType(context, argumentType)) {
|
|
32
|
+
if ((0, typescript_1.isStringType)(context, type) && (0, typescript_1.isNumberType)(context, argumentType)) {
|
|
33
33
|
const index = context.transformExpression(node.argumentExpression);
|
|
34
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.StringAccess, node, table, index);
|
|
34
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringAccess, node, table, index);
|
|
35
35
|
}
|
|
36
36
|
const accessExpression = transformElementAccessArgument(context, node);
|
|
37
|
-
if (multi_1.isMultiReturnCall(context, node.expression)) {
|
|
37
|
+
if ((0, multi_1.isMultiReturnCall)(context, node.expression)) {
|
|
38
38
|
const accessType = context.checker.getTypeAtLocation(node.argumentExpression);
|
|
39
|
-
if (!typescript_1.isNumberType(context, accessType)) {
|
|
40
|
-
context.diagnostics.push(diagnostics_1.invalidMultiReturnAccess(node));
|
|
39
|
+
if (!(0, typescript_1.isNumberType)(context, accessType)) {
|
|
40
|
+
context.diagnostics.push((0, diagnostics_1.invalidMultiReturnAccess)(node));
|
|
41
41
|
}
|
|
42
42
|
// When selecting the first element, we can shortcut
|
|
43
43
|
if (ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0") {
|
|
@@ -50,7 +50,7 @@ const transformElementAccessExpression = (node, context) => {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
if (ts.isOptionalChain(node)) {
|
|
53
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.OptionalChainAccess, node, table, accessExpression);
|
|
53
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.OptionalChainAccess, node, table, accessExpression);
|
|
54
54
|
}
|
|
55
55
|
return lua.createTableIndexExpression(table, accessExpression, node);
|
|
56
56
|
};
|
|
@@ -58,20 +58,20 @@ exports.transformElementAccessExpression = transformElementAccessExpression;
|
|
|
58
58
|
const transformPropertyAccessExpression = (node, context) => {
|
|
59
59
|
const property = node.name.text;
|
|
60
60
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
61
|
-
const annotations = annotations_1.getTypeAnnotations(type);
|
|
61
|
+
const annotations = (0, annotations_1.getTypeAnnotations)(type);
|
|
62
62
|
if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
|
|
63
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node, annotations_1.AnnotationKind.LuaTable));
|
|
63
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
|
|
64
64
|
}
|
|
65
|
-
const constEnumValue = enum_1.tryGetConstEnumValue(context, node);
|
|
65
|
+
const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
|
|
66
66
|
if (constEnumValue) {
|
|
67
67
|
return constEnumValue;
|
|
68
68
|
}
|
|
69
|
-
const builtinResult = builtins_1.transformBuiltinPropertyAccessExpression(context, node);
|
|
69
|
+
const builtinResult = (0, builtins_1.transformBuiltinPropertyAccessExpression)(context, node);
|
|
70
70
|
if (builtinResult) {
|
|
71
71
|
return builtinResult;
|
|
72
72
|
}
|
|
73
|
-
if (ts.isCallExpression(node.expression) && multi_1.returnsMultiType(context, node.expression)) {
|
|
74
|
-
context.diagnostics.push(diagnostics_1.invalidMultiReturnAccess(node));
|
|
73
|
+
if (ts.isCallExpression(node.expression) && (0, multi_1.returnsMultiType)(context, node.expression)) {
|
|
74
|
+
context.diagnostics.push((0, diagnostics_1.invalidMultiReturnAccess)(node));
|
|
75
75
|
}
|
|
76
76
|
// Do not output path for member only enums
|
|
77
77
|
if (annotations.has(annotations_1.AnnotationKind.CompileMembersOnly)) {
|
|
@@ -94,7 +94,7 @@ exports.transformPropertyAccessExpression = transformPropertyAccessExpression;
|
|
|
94
94
|
function transformOptionalChain(context, node) {
|
|
95
95
|
const left = context.transformExpression(node.expression);
|
|
96
96
|
const right = lua.createStringLiteral(node.name.text, node.name);
|
|
97
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.OptionalChainAccess, node, left, right);
|
|
97
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.OptionalChainAccess, node, left, right);
|
|
98
98
|
}
|
|
99
99
|
const transformQualifiedName = (node, context) => {
|
|
100
100
|
const right = lua.createStringLiteral(node.right.text, node.right);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as lua from "../../LuaAST";
|
|
3
|
+
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
|
+
export declare const transformAwaitExpression: FunctionVisitor<ts.AwaitExpression>;
|
|
5
|
+
export declare function isAsyncFunction(declaration: ts.FunctionLikeDeclaration): boolean;
|
|
6
|
+
export declare function wrapInAsyncAwaiter(context: TransformationContext, statements: lua.Statement[]): lua.Statement[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapInAsyncAwaiter = exports.isAsyncFunction = exports.transformAwaitExpression = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const lua = require("../../LuaAST");
|
|
6
|
+
const diagnostics_1 = require("../utils/diagnostics");
|
|
7
|
+
const lualib_1 = require("../utils/lualib");
|
|
8
|
+
const typescript_1 = require("../utils/typescript");
|
|
9
|
+
const transformAwaitExpression = (node, context) => {
|
|
10
|
+
var _a;
|
|
11
|
+
// Check if await is inside an async function, it is not allowed at top level or in non-async functions
|
|
12
|
+
const containingFunction = (0, typescript_1.findFirstNodeAbove)(node, ts.isFunctionLike);
|
|
13
|
+
if (containingFunction === undefined ||
|
|
14
|
+
!((_a = containingFunction.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword))) {
|
|
15
|
+
context.diagnostics.push((0, diagnostics_1.awaitMustBeInAsyncFunction)(node));
|
|
16
|
+
}
|
|
17
|
+
const expression = context.transformExpression(node.expression);
|
|
18
|
+
const catchIdentifier = lua.createIdentifier("____catch");
|
|
19
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Await, node, catchIdentifier, expression);
|
|
20
|
+
};
|
|
21
|
+
exports.transformAwaitExpression = transformAwaitExpression;
|
|
22
|
+
function isAsyncFunction(declaration) {
|
|
23
|
+
var _a, _b;
|
|
24
|
+
return (_b = (_a = declaration.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
|
|
25
|
+
}
|
|
26
|
+
exports.isAsyncFunction = isAsyncFunction;
|
|
27
|
+
function wrapInAsyncAwaiter(context, statements) {
|
|
28
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Await);
|
|
29
|
+
return [
|
|
30
|
+
lua.createReturnStatement([
|
|
31
|
+
lua.createCallExpression(lua.createIdentifier("__TS__AsyncAwaiter"), [
|
|
32
|
+
lua.createFunctionExpression(lua.createBlock(statements)),
|
|
33
|
+
]),
|
|
34
|
+
]),
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
exports.wrapInAsyncAwaiter = wrapInAsyncAwaiter;
|
|
38
|
+
//# sourceMappingURL=async-await.js.map
|