typescript-to-lua 0.41.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/dist/CompilerOptions.d.ts +4 -3
- package/dist/LuaAST.js +4 -4
- package/dist/LuaLib.d.ts +7 -0
- package/dist/LuaLib.js +41 -5
- package/dist/LuaPrinter.d.ts +6 -2
- package/dist/LuaPrinter.js +25 -16
- package/dist/cli/diagnostics.js +2 -2
- package/dist/cli/report.js +1 -1
- package/dist/cli/tsconfig.js +4 -4
- package/dist/lualib/ArrayJoin.lua +2 -4
- package/dist/lualib/Await.lua +59 -0
- package/dist/lualib/Iterator.lua +1 -5
- package/dist/lualib/NumberToString.lua +4 -8
- package/dist/lualib/OptionalMethodCall.lua +3 -1
- package/dist/lualib/ParseInt.lua +1 -3
- 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 +18 -8
- package/dist/lualib/StringConcat.lua +1 -1
- package/dist/lualib/StringPadEnd.lua +7 -11
- package/dist/lualib/StringPadStart.lua +8 -12
- package/dist/lualib/StringReplace.lua +8 -13
- package/dist/lualib/StringReplaceAll.lua +35 -0
- package/dist/lualib/StringSubstring.lua +1 -1
- package/dist/lualib/lualib_bundle.lua +660 -239
- 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 +1 -32
- 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.js +4 -4
- 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 +25 -19
- 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 +32 -31
- 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 +22 -20
- package/dist/transformation/visitors/block.js +6 -6
- package/dist/transformation/visitors/break-continue.js +4 -9
- package/dist/transformation/visitors/call.js +38 -38
- package/dist/transformation/visitors/class/decorators.js +3 -3
- package/dist/transformation/visitors/class/index.js +34 -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 +2 -1
- package/dist/transformation/visitors/language-extensions/multi.js +8 -4
- 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 -13
- 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 -13
- 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 +29 -30
- 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 -14
- 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 +13 -11
- package/dist/transpilation/utils.js +3 -2
- package/dist/tstl.js +9 -9
- package/dist/utils.js +2 -2
- package/package.json +16 -13
- 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
|
@@ -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,36 +15,42 @@ 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 (
|
|
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
|
+
}
|
|
42
|
+
// When selecting the first element, we can shortcut
|
|
43
|
+
if (ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0") {
|
|
44
|
+
return table;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const selectIdentifier = lua.createIdentifier("select");
|
|
48
|
+
const selectCall = lua.createCallExpression(selectIdentifier, [accessExpression, table]);
|
|
49
|
+
return selectCall;
|
|
41
50
|
}
|
|
42
|
-
const selectIdentifier = lua.createIdentifier("select");
|
|
43
|
-
const selectCall = lua.createCallExpression(selectIdentifier, [accessExpression, table]);
|
|
44
|
-
return selectCall;
|
|
45
51
|
}
|
|
46
52
|
if (ts.isOptionalChain(node)) {
|
|
47
|
-
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);
|
|
48
54
|
}
|
|
49
55
|
return lua.createTableIndexExpression(table, accessExpression, node);
|
|
50
56
|
};
|
|
@@ -52,20 +58,20 @@ exports.transformElementAccessExpression = transformElementAccessExpression;
|
|
|
52
58
|
const transformPropertyAccessExpression = (node, context) => {
|
|
53
59
|
const property = node.name.text;
|
|
54
60
|
const type = context.checker.getTypeAtLocation(node.expression);
|
|
55
|
-
const annotations = annotations_1.getTypeAnnotations(type);
|
|
61
|
+
const annotations = (0, annotations_1.getTypeAnnotations)(type);
|
|
56
62
|
if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
|
|
57
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node, annotations_1.AnnotationKind.LuaTable));
|
|
63
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
|
|
58
64
|
}
|
|
59
|
-
const constEnumValue = enum_1.tryGetConstEnumValue(context, node);
|
|
65
|
+
const constEnumValue = (0, enum_1.tryGetConstEnumValue)(context, node);
|
|
60
66
|
if (constEnumValue) {
|
|
61
67
|
return constEnumValue;
|
|
62
68
|
}
|
|
63
|
-
const builtinResult = builtins_1.transformBuiltinPropertyAccessExpression(context, node);
|
|
69
|
+
const builtinResult = (0, builtins_1.transformBuiltinPropertyAccessExpression)(context, node);
|
|
64
70
|
if (builtinResult) {
|
|
65
71
|
return builtinResult;
|
|
66
72
|
}
|
|
67
|
-
if (ts.isCallExpression(node.expression) && multi_1.returnsMultiType(context, node.expression)) {
|
|
68
|
-
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));
|
|
69
75
|
}
|
|
70
76
|
// Do not output path for member only enums
|
|
71
77
|
if (annotations.has(annotations_1.AnnotationKind.CompileMembersOnly)) {
|
|
@@ -88,7 +94,7 @@ exports.transformPropertyAccessExpression = transformPropertyAccessExpression;
|
|
|
88
94
|
function transformOptionalChain(context, node) {
|
|
89
95
|
const left = context.transformExpression(node.expression);
|
|
90
96
|
const right = lua.createStringLiteral(node.name.text, node.name);
|
|
91
|
-
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);
|
|
92
98
|
}
|
|
93
99
|
const transformQualifiedName = (node, context) => {
|
|
94
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
|
|
@@ -4,7 +4,6 @@ exports.transformAssignmentStatement = exports.transformAssignmentExpression = e
|
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../../utils");
|
|
7
|
-
const annotations_1 = require("../../utils/annotations");
|
|
8
7
|
const assignment_validation_1 = require("../../utils/assignment-validation");
|
|
9
8
|
const export_1 = require("../../utils/export");
|
|
10
9
|
const lua_ast_1 = require("../../utils/lua-ast");
|
|
@@ -19,33 +18,33 @@ const diagnostics_1 = require("../../utils/diagnostics");
|
|
|
19
18
|
function transformAssignmentLeftHandSideExpression(context, node) {
|
|
20
19
|
const symbol = context.checker.getSymbolAtLocation(node);
|
|
21
20
|
const left = context.transformExpression(node);
|
|
22
|
-
return lua.isIdentifier(left) && symbol && export_1.isSymbolExported(context, symbol)
|
|
23
|
-
? export_1.createExportedIdentifier(context, left)
|
|
24
|
-
: utils_1.cast(left, lua.isAssignmentLeftHandSideExpression);
|
|
21
|
+
return lua.isIdentifier(left) && symbol && (0, export_1.isSymbolExported)(context, symbol)
|
|
22
|
+
? (0, export_1.createExportedIdentifier)(context, left)
|
|
23
|
+
: (0, utils_1.cast)(left, lua.isAssignmentLeftHandSideExpression);
|
|
25
24
|
}
|
|
26
25
|
exports.transformAssignmentLeftHandSideExpression = transformAssignmentLeftHandSideExpression;
|
|
27
26
|
function transformAssignment(context,
|
|
28
27
|
// TODO: Change type to ts.LeftHandSideExpression?
|
|
29
28
|
lhs, right, parent) {
|
|
30
29
|
if (ts.isOptionalChain(lhs)) {
|
|
31
|
-
context.diagnostics.push(diagnostics_1.notAllowedOptionalAssignment(lhs));
|
|
30
|
+
context.diagnostics.push((0, diagnostics_1.notAllowedOptionalAssignment)(lhs));
|
|
32
31
|
return [];
|
|
33
32
|
}
|
|
34
|
-
if (destructuring_assignments_1.isArrayLength(context, lhs)) {
|
|
35
|
-
const arrayLengthAssignment = lua.createExpressionStatement(lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ArraySetLength, parent, context.transformExpression(lhs.expression), right));
|
|
33
|
+
if ((0, destructuring_assignments_1.isArrayLength)(context, lhs)) {
|
|
34
|
+
const arrayLengthAssignment = lua.createExpressionStatement((0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySetLength, parent, context.transformExpression(lhs.expression), right));
|
|
36
35
|
return [arrayLengthAssignment];
|
|
37
36
|
}
|
|
38
37
|
const symbol = ts.isShorthandPropertyAssignment(lhs.parent)
|
|
39
38
|
? context.checker.getShorthandAssignmentValueSymbol(lhs.parent)
|
|
40
39
|
: context.checker.getSymbolAtLocation(lhs);
|
|
41
|
-
const dependentSymbols = symbol ? export_1.getDependenciesOfSymbol(context, symbol) : [];
|
|
40
|
+
const dependentSymbols = symbol ? (0, export_1.getDependenciesOfSymbol)(context, symbol) : [];
|
|
42
41
|
const left = transformAssignmentLeftHandSideExpression(context, lhs);
|
|
43
42
|
const rootAssignment = lua.createAssignmentStatement(left, right, lhs.parent);
|
|
44
43
|
return [
|
|
45
44
|
rootAssignment,
|
|
46
45
|
...dependentSymbols.map(symbol => {
|
|
47
46
|
const [left] = rootAssignment.left;
|
|
48
|
-
const identifierToAssign = export_1.createExportedIdentifier(context, lua.createIdentifier(symbol.name));
|
|
47
|
+
const identifierToAssign = (0, export_1.createExportedIdentifier)(context, lua.createIdentifier(symbol.name));
|
|
49
48
|
return lua.createAssignmentStatement(identifierToAssign, left);
|
|
50
49
|
}),
|
|
51
50
|
];
|
|
@@ -54,12 +53,12 @@ exports.transformAssignment = transformAssignment;
|
|
|
54
53
|
function transformDestructuredAssignmentExpression(context, expression) {
|
|
55
54
|
const rootIdentifier = lua.createAnonymousIdentifier(expression.left);
|
|
56
55
|
let right = context.transformExpression(expression.right);
|
|
57
|
-
if (
|
|
58
|
-
right = lua_ast_1.wrapInTable(right);
|
|
56
|
+
if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
|
|
57
|
+
right = (0, lua_ast_1.wrapInTable)(right);
|
|
59
58
|
}
|
|
60
59
|
const statements = [
|
|
61
60
|
lua.createVariableDeclarationStatement(rootIdentifier, right),
|
|
62
|
-
...destructuring_assignments_1.transformDestructuringAssignment(context, expression, rootIdentifier),
|
|
61
|
+
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier),
|
|
63
62
|
];
|
|
64
63
|
return { statements, result: rootIdentifier };
|
|
65
64
|
}
|
|
@@ -67,13 +66,13 @@ function transformAssignmentExpression(context, expression) {
|
|
|
67
66
|
// Validate assignment
|
|
68
67
|
const rightType = context.checker.getTypeAtLocation(expression.right);
|
|
69
68
|
const leftType = context.checker.getTypeAtLocation(expression.left);
|
|
70
|
-
assignment_validation_1.validateAssignment(context, expression.right, rightType, leftType);
|
|
71
|
-
if (destructuring_assignments_1.isArrayLength(context, expression.left)) {
|
|
69
|
+
(0, assignment_validation_1.validateAssignment)(context, expression.right, rightType, leftType);
|
|
70
|
+
if ((0, destructuring_assignments_1.isArrayLength)(context, expression.left)) {
|
|
72
71
|
// array.length = x
|
|
73
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ArraySetLength, expression, context.transformExpression(expression.left.expression), context.transformExpression(expression.right));
|
|
72
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySetLength, expression, context.transformExpression(expression.left.expression), context.transformExpression(expression.right));
|
|
74
73
|
}
|
|
75
|
-
if (typescript_1.isDestructuringAssignment(expression)) {
|
|
76
|
-
return transform_1.transformToImmediatelyInvokedFunctionExpression(context, () => transformDestructuredAssignmentExpression(context, expression), expression);
|
|
74
|
+
if ((0, typescript_1.isDestructuringAssignment)(expression)) {
|
|
75
|
+
return (0, transform_1.transformToImmediatelyInvokedFunctionExpression)(context, () => transformDestructuredAssignmentExpression(context, expression), expression);
|
|
77
76
|
}
|
|
78
77
|
if (ts.isPropertyAccessExpression(expression.left) || ts.isElementAccessExpression(expression.left)) {
|
|
79
78
|
// Left is property/element access: cache result while maintaining order of evaluation
|
|
@@ -91,7 +90,7 @@ function transformAssignmentExpression(context, expression) {
|
|
|
91
90
|
indexParameter,
|
|
92
91
|
valueParameter,
|
|
93
92
|
]);
|
|
94
|
-
scope_1.pushScope(context, scope_1.ScopeType.Function);
|
|
93
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
|
|
95
94
|
const objExpression = context.transformExpression(expression.left.expression);
|
|
96
95
|
let indexExpression;
|
|
97
96
|
if (ts.isPropertyAccessExpression(expression.left)) {
|
|
@@ -100,14 +99,14 @@ function transformAssignmentExpression(context, expression) {
|
|
|
100
99
|
}
|
|
101
100
|
else {
|
|
102
101
|
// Element access
|
|
103
|
-
indexExpression = access_1.transformElementAccessArgument(context, expression.left);
|
|
102
|
+
indexExpression = (0, access_1.transformElementAccessArgument)(context, expression.left);
|
|
104
103
|
}
|
|
105
104
|
const args = [objExpression, indexExpression, context.transformExpression(expression.right)];
|
|
106
|
-
scope_1.popScope(context);
|
|
105
|
+
(0, scope_1.popScope)(context);
|
|
107
106
|
return lua.createCallExpression(iife, args, expression);
|
|
108
107
|
}
|
|
109
108
|
else {
|
|
110
|
-
return transform_1.transformToImmediatelyInvokedFunctionExpression(context, () => {
|
|
109
|
+
return (0, transform_1.transformToImmediatelyInvokedFunctionExpression)(context, () => {
|
|
111
110
|
// Simple assignment
|
|
112
111
|
// (function() ${left} = ${right}; return ${left} end)()
|
|
113
112
|
const left = context.transformExpression(expression.left);
|
|
@@ -120,7 +119,7 @@ function transformAssignmentExpression(context, expression) {
|
|
|
120
119
|
exports.transformAssignmentExpression = transformAssignmentExpression;
|
|
121
120
|
const canBeTransformedToLuaAssignmentStatement = (context, node) => ts.isArrayLiteralExpression(node.left) &&
|
|
122
121
|
node.left.elements.every(element => {
|
|
123
|
-
if (destructuring_assignments_1.isArrayLength(context, element)) {
|
|
122
|
+
if ((0, destructuring_assignments_1.isArrayLength)(context, element)) {
|
|
124
123
|
return false;
|
|
125
124
|
}
|
|
126
125
|
if (ts.isPropertyAccessExpression(element) || ts.isElementAccessExpression(element)) {
|
|
@@ -129,7 +128,7 @@ const canBeTransformedToLuaAssignmentStatement = (context, node) => ts.isArrayLi
|
|
|
129
128
|
if (ts.isIdentifier(element)) {
|
|
130
129
|
const symbol = context.checker.getSymbolAtLocation(element);
|
|
131
130
|
if (symbol) {
|
|
132
|
-
const aliases = export_1.getDependenciesOfSymbol(context, symbol);
|
|
131
|
+
const aliases = (0, export_1.getDependenciesOfSymbol)(context, symbol);
|
|
133
132
|
return aliases.length === 0;
|
|
134
133
|
}
|
|
135
134
|
}
|
|
@@ -138,26 +137,28 @@ function transformAssignmentStatement(context, expression) {
|
|
|
138
137
|
// Validate assignment
|
|
139
138
|
const rightType = context.checker.getTypeAtLocation(expression.right);
|
|
140
139
|
const leftType = context.checker.getTypeAtLocation(expression.left);
|
|
141
|
-
assignment_validation_1.validateAssignment(context, expression.right, rightType, leftType);
|
|
142
|
-
if (typescript_1.isDestructuringAssignment(expression)) {
|
|
140
|
+
(0, assignment_validation_1.validateAssignment)(context, expression.right, rightType, leftType);
|
|
141
|
+
if ((0, typescript_1.isDestructuringAssignment)(expression)) {
|
|
143
142
|
if (canBeTransformedToLuaAssignmentStatement(context, expression)) {
|
|
144
143
|
const rightType = context.checker.getTypeAtLocation(expression.right);
|
|
145
144
|
let right = context.transformExpression(expression.right);
|
|
146
|
-
if (
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
if (ts.isArrayLiteralExpression(expression.right)) {
|
|
146
|
+
right = expression.right.elements.map(e => context.transformExpression(e));
|
|
147
|
+
}
|
|
148
|
+
else if (!(0, multi_1.isMultiReturnCall)(context, expression.right) && (0, typescript_1.isArrayType)(context, rightType)) {
|
|
149
|
+
right = (0, lua_ast_1.createUnpackCall)(context, right, expression.right);
|
|
149
150
|
}
|
|
150
151
|
const left = expression.left.elements.map(e => transformAssignmentLeftHandSideExpression(context, e));
|
|
151
152
|
return [lua.createAssignmentStatement(left, right, expression)];
|
|
152
153
|
}
|
|
153
154
|
let right = context.transformExpression(expression.right);
|
|
154
|
-
if (
|
|
155
|
-
right = lua_ast_1.wrapInTable(right);
|
|
155
|
+
if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
|
|
156
|
+
right = (0, lua_ast_1.wrapInTable)(right);
|
|
156
157
|
}
|
|
157
158
|
const rootIdentifier = lua.createAnonymousIdentifier(expression.left);
|
|
158
159
|
return [
|
|
159
160
|
lua.createVariableDeclarationStatement(rootIdentifier, right),
|
|
160
|
-
...destructuring_assignments_1.transformDestructuringAssignment(context, expression, rootIdentifier),
|
|
161
|
+
...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier),
|
|
161
162
|
];
|
|
162
163
|
}
|
|
163
164
|
else {
|