typescript-to-lua 1.0.1 → 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 +18 -0
- package/dist/CompilerOptions.d.ts +4 -3
- package/dist/LuaAST.js +4 -4
- package/dist/LuaLib.d.ts +1 -0
- package/dist/LuaLib.js +1 -0
- 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/Await.lua +26 -10
- package/dist/lualib/Promise.lua +20 -6
- package/dist/lualib/SourceMapTraceBack.lua +18 -8
- package/dist/lualib/StringReplace.lua +8 -13
- package/dist/lualib/StringReplaceAll.lua +35 -0
- package/dist/lualib/lualib_bundle.lua +106 -35
- 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 +33 -33
- 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.js +9 -9
- 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 -3
- package/dist/transformation/utils/annotations.js +0 -2
- package/dist/transformation/utils/assignment-validation.js +8 -7
- package/dist/transformation/utils/diagnostics.js +4 -4
- package/dist/transformation/utils/export.js +4 -4
- package/dist/transformation/utils/function-context.js +8 -8
- 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.js +5 -4
- 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 +2 -2
- package/dist/transformation/visitors/call.js +35 -35
- 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 +12 -12
- 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 -30
- package/dist/transformation/visitors/expression-statement.js +10 -6
- package/dist/transformation/visitors/function.js +26 -26
- package/dist/transformation/visitors/identifier.js +22 -22
- package/dist/transformation/visitors/index.js +2 -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 +3 -3
- 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 +40 -26
- package/dist/transformation/visitors/sourceFile.js +8 -8
- package/dist/transformation/visitors/spread.js +11 -11
- package/dist/transformation/visitors/switch.js +34 -11
- 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 +52 -10
- 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 +19 -12
- package/dist/transpilation/transformers.js +41 -24
- package/dist/transpilation/transpile.js +6 -6
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.literalVisitors = exports.
|
|
3
|
+
exports.literalVisitors = exports.createShorthandIdentifier = exports.transformPropertyName = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const utils_1 = require("../../utils");
|
|
@@ -13,7 +13,6 @@ const typescript_1 = require("../utils/typescript");
|
|
|
13
13
|
const function_1 = require("./function");
|
|
14
14
|
const call_1 = require("./call");
|
|
15
15
|
const multi_1 = require("./language-extensions/multi");
|
|
16
|
-
const jsx_1 = require("./jsx/jsx");
|
|
17
16
|
// TODO: Move to object-literal.ts?
|
|
18
17
|
function transformPropertyName(context, node) {
|
|
19
18
|
if (ts.isComputedPropertyName(node)) {
|
|
@@ -33,16 +32,16 @@ exports.transformPropertyName = transformPropertyName;
|
|
|
33
32
|
function createShorthandIdentifier(context, valueSymbol, propertyIdentifier) {
|
|
34
33
|
const propertyName = propertyIdentifier.text;
|
|
35
34
|
const isUnsafeName = valueSymbol
|
|
36
|
-
? safe_names_1.hasUnsafeSymbolName(context, valueSymbol, propertyIdentifier)
|
|
37
|
-
: safe_names_1.hasUnsafeIdentifierName(context, propertyIdentifier, false);
|
|
38
|
-
const name = isUnsafeName ? safe_names_1.createSafeName(propertyName) : propertyName;
|
|
35
|
+
? (0, safe_names_1.hasUnsafeSymbolName)(context, valueSymbol, propertyIdentifier)
|
|
36
|
+
: (0, safe_names_1.hasUnsafeIdentifierName)(context, propertyIdentifier, false);
|
|
37
|
+
const name = isUnsafeName ? (0, safe_names_1.createSafeName)(propertyName) : propertyName;
|
|
39
38
|
let identifier = context.transformExpression(ts.factory.createIdentifier(name));
|
|
40
39
|
lua.setNodeOriginal(identifier, propertyIdentifier);
|
|
41
40
|
if (valueSymbol !== undefined && lua.isIdentifier(identifier)) {
|
|
42
|
-
identifier.symbolId = symbols_1.getSymbolIdOfSymbol(context, valueSymbol);
|
|
43
|
-
const exportScope = export_1.getSymbolExportScope(context, valueSymbol);
|
|
41
|
+
identifier.symbolId = (0, symbols_1.getSymbolIdOfSymbol)(context, valueSymbol);
|
|
42
|
+
const exportScope = (0, export_1.getSymbolExportScope)(context, valueSymbol);
|
|
44
43
|
if (exportScope) {
|
|
45
|
-
identifier = export_1.createExportedIdentifier(context, identifier, exportScope);
|
|
44
|
+
identifier = (0, export_1.createExportedIdentifier)(context, identifier, exportScope);
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
return identifier;
|
|
@@ -56,10 +55,10 @@ const transformNumericLiteralExpression = expression => {
|
|
|
56
55
|
}
|
|
57
56
|
return lua.createNumericLiteral(Number(expression.text), expression);
|
|
58
57
|
};
|
|
59
|
-
const
|
|
60
|
-
const violations = multi_1.findMultiAssignmentViolations(context, expression);
|
|
58
|
+
const transformObjectLiteralExpression = (expression, context) => {
|
|
59
|
+
const violations = (0, multi_1.findMultiAssignmentViolations)(context, expression);
|
|
61
60
|
if (violations.length > 0) {
|
|
62
|
-
context.diagnostics.push(...violations.map(e => diagnostics_1.invalidMultiFunctionUse(e)));
|
|
61
|
+
context.diagnostics.push(...violations.map(e => (0, diagnostics_1.invalidMultiFunctionUse)(e)));
|
|
63
62
|
return lua.createNilLiteral(expression);
|
|
64
63
|
}
|
|
65
64
|
let properties = [];
|
|
@@ -70,39 +69,19 @@ const transformObjectLiteralExpressionOrJsxAttributes = (expression, context) =>
|
|
|
70
69
|
const expression = context.transformExpression(element.initializer);
|
|
71
70
|
properties.push(lua.createTableFieldExpression(expression, name, element));
|
|
72
71
|
}
|
|
73
|
-
else if (ts.isJsxAttribute(element)) {
|
|
74
|
-
const initializer = element.initializer;
|
|
75
|
-
let expression;
|
|
76
|
-
if (initializer === undefined) {
|
|
77
|
-
expression = lua.createBooleanLiteral(true);
|
|
78
|
-
}
|
|
79
|
-
else if (ts.isStringLiteral(initializer)) {
|
|
80
|
-
const text = jsx_1.formatJSXStringValueLiteral(initializer.text);
|
|
81
|
-
expression = lua.createStringLiteral(text, initializer);
|
|
82
|
-
}
|
|
83
|
-
else if (ts.isJsxExpression(initializer)) {
|
|
84
|
-
expression = initializer.expression
|
|
85
|
-
? context.transformExpression(initializer.expression)
|
|
86
|
-
: lua.createBooleanLiteral(true);
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
utils_1.assertNever(initializer);
|
|
90
|
-
}
|
|
91
|
-
properties.push(lua.createTableFieldExpression(expression, name, element));
|
|
92
|
-
}
|
|
93
72
|
else if (ts.isShorthandPropertyAssignment(element)) {
|
|
94
73
|
const valueSymbol = context.checker.getShorthandAssignmentValueSymbol(element);
|
|
95
74
|
if (valueSymbol) {
|
|
96
|
-
symbols_1.trackSymbolReference(context, valueSymbol, element.name);
|
|
75
|
+
(0, symbols_1.trackSymbolReference)(context, valueSymbol, element.name);
|
|
97
76
|
}
|
|
98
77
|
const identifier = createShorthandIdentifier(context, valueSymbol, element.name);
|
|
99
78
|
properties.push(lua.createTableFieldExpression(identifier, name, element));
|
|
100
79
|
}
|
|
101
80
|
else if (ts.isMethodDeclaration(element)) {
|
|
102
|
-
const expression = function_1.transformFunctionLikeDeclaration(element, context);
|
|
81
|
+
const expression = (0, function_1.transformFunctionLikeDeclaration)(element, context);
|
|
103
82
|
properties.push(lua.createTableFieldExpression(expression, name, element));
|
|
104
83
|
}
|
|
105
|
-
else if (ts.isSpreadAssignment(element)
|
|
84
|
+
else if (ts.isSpreadAssignment(element)) {
|
|
106
85
|
// Create a table for preceding properties to preserve property order
|
|
107
86
|
// { x: 0, ...{ y: 2 }, y: 1, z: 2 } --> __TS__ObjectAssign({x = 0}, {y = 2}, {y = 1, z = 2})
|
|
108
87
|
if (properties.length > 0) {
|
|
@@ -112,8 +91,8 @@ const transformObjectLiteralExpressionOrJsxAttributes = (expression, context) =>
|
|
|
112
91
|
}
|
|
113
92
|
const type = context.checker.getTypeAtLocation(element.expression);
|
|
114
93
|
let tableExpression;
|
|
115
|
-
if (typescript_1.isArrayType(context, type)) {
|
|
116
|
-
tableExpression = lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ArrayToObject, element.expression, context.transformExpression(element.expression));
|
|
94
|
+
if ((0, typescript_1.isArrayType)(context, type)) {
|
|
95
|
+
tableExpression = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayToObject, element.expression, context.transformExpression(element.expression));
|
|
117
96
|
}
|
|
118
97
|
else {
|
|
119
98
|
tableExpression = context.transformExpression(element.expression);
|
|
@@ -121,10 +100,10 @@ const transformObjectLiteralExpressionOrJsxAttributes = (expression, context) =>
|
|
|
121
100
|
tableExpressions.push(tableExpression);
|
|
122
101
|
}
|
|
123
102
|
else if (ts.isAccessor(element)) {
|
|
124
|
-
context.diagnostics.push(diagnostics_1.unsupportedAccessorInObjectLiteral(element));
|
|
103
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedAccessorInObjectLiteral)(element));
|
|
125
104
|
}
|
|
126
105
|
else {
|
|
127
|
-
utils_1.assertNever(element);
|
|
106
|
+
(0, utils_1.assertNever)(element);
|
|
128
107
|
}
|
|
129
108
|
}
|
|
130
109
|
if (tableExpressions.length === 0) {
|
|
@@ -138,14 +117,12 @@ const transformObjectLiteralExpressionOrJsxAttributes = (expression, context) =>
|
|
|
138
117
|
if (tableExpressions[0].kind !== lua.SyntaxKind.TableExpression) {
|
|
139
118
|
tableExpressions.unshift(lua.createTableExpression(undefined, expression));
|
|
140
119
|
}
|
|
141
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ObjectAssign, expression, ...tableExpressions);
|
|
120
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectAssign, expression, ...tableExpressions);
|
|
142
121
|
}
|
|
143
122
|
};
|
|
144
|
-
const transformObjectLiteralExpression = transformObjectLiteralExpressionOrJsxAttributes;
|
|
145
|
-
exports.transformJsxAttributes = transformObjectLiteralExpressionOrJsxAttributes;
|
|
146
123
|
const transformArrayLiteralExpression = (expression, context) => {
|
|
147
124
|
const filteredElements = expression.elements.map(e => ts.isOmittedExpression(e) ? ts.factory.createIdentifier("undefined") : e);
|
|
148
|
-
const values = call_1.flattenSpreadExpressions(context, filteredElements).map(e => lua.createTableFieldExpression(e));
|
|
125
|
+
const values = (0, call_1.flattenSpreadExpressions)(context, filteredElements).map(e => lua.createTableFieldExpression(e));
|
|
149
126
|
return lua.createTableExpression(values, expression);
|
|
150
127
|
};
|
|
151
128
|
exports.literalVisitors = {
|
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.transformDoStatement = exports.transformWhileStatement = void 0;
|
|
4
4
|
const lua = require("../../../LuaAST");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
|
-
const transformWhileStatement = (statement, context) => lua.createWhileStatement(lua.createBlock(utils_1.transformLoopBody(context, statement)), context.transformExpression(statement.expression), statement);
|
|
6
|
+
const transformWhileStatement = (statement, context) => lua.createWhileStatement(lua.createBlock((0, utils_1.transformLoopBody)(context, statement)), context.transformExpression(statement.expression), statement);
|
|
7
7
|
exports.transformWhileStatement = transformWhileStatement;
|
|
8
8
|
const transformDoStatement = (statement, context) => {
|
|
9
|
-
const body = lua.createDoStatement(utils_1.transformLoopBody(context, statement));
|
|
9
|
+
const body = lua.createDoStatement((0, utils_1.transformLoopBody)(context, statement));
|
|
10
10
|
let condition = context.transformExpression(statement.expression);
|
|
11
11
|
if (lua.isUnaryExpression(condition) && condition.operator === lua.SyntaxKind.NotOperator) {
|
|
12
12
|
condition = condition.operand;
|
|
@@ -6,15 +6,15 @@ const diagnostics_1 = require("../../utils/diagnostics");
|
|
|
6
6
|
const typescript_1 = require("../../utils/typescript");
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
8
|
const transformForInStatement = (statement, context) => {
|
|
9
|
-
if (typescript_1.isArrayType(context, context.checker.getTypeAtLocation(statement.expression))) {
|
|
10
|
-
context.diagnostics.push(diagnostics_1.forbiddenForIn(statement));
|
|
9
|
+
if ((0, typescript_1.isArrayType)(context, context.checker.getTypeAtLocation(statement.expression))) {
|
|
10
|
+
context.diagnostics.push((0, diagnostics_1.forbiddenForIn)(statement));
|
|
11
11
|
}
|
|
12
12
|
// Transpile expression
|
|
13
13
|
const pairsIdentifier = lua.createIdentifier("pairs");
|
|
14
14
|
const expression = context.transformExpression(statement.expression);
|
|
15
15
|
const pairsCall = lua.createCallExpression(pairsIdentifier, [expression]);
|
|
16
|
-
const body = lua.createBlock(utils_1.transformLoopBody(context, statement));
|
|
17
|
-
const valueVariable = utils_1.transformForInitializer(context, statement.initializer, body);
|
|
16
|
+
const body = lua.createBlock((0, utils_1.transformLoopBody)(context, statement));
|
|
17
|
+
const valueVariable = (0, utils_1.transformForInitializer)(context, statement.initializer, body);
|
|
18
18
|
return lua.createForInStatement(body, [valueVariable], [pairsCall], statement);
|
|
19
19
|
};
|
|
20
20
|
exports.transformForInStatement = transformForInStatement;
|
|
@@ -11,32 +11,32 @@ const iterable_1 = require("../language-extensions/iterable");
|
|
|
11
11
|
const range_1 = require("../language-extensions/range");
|
|
12
12
|
const utils_1 = require("./utils");
|
|
13
13
|
function transformForOfArrayStatement(context, statement, block) {
|
|
14
|
-
const valueVariable = utils_1.transformForInitializer(context, statement.initializer, block);
|
|
14
|
+
const valueVariable = (0, utils_1.transformForInitializer)(context, statement.initializer, block);
|
|
15
15
|
const ipairsCall = lua.createCallExpression(lua.createIdentifier("ipairs"), [
|
|
16
16
|
context.transformExpression(statement.expression),
|
|
17
17
|
]);
|
|
18
18
|
return lua.createForInStatement(block, [lua.createAnonymousIdentifier(), valueVariable], [ipairsCall], statement);
|
|
19
19
|
}
|
|
20
20
|
function transformForOfIteratorStatement(context, statement, block) {
|
|
21
|
-
const valueVariable = utils_1.transformForInitializer(context, statement.initializer, block);
|
|
22
|
-
const iterable = lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.Iterator, statement.expression, context.transformExpression(statement.expression));
|
|
21
|
+
const valueVariable = (0, utils_1.transformForInitializer)(context, statement.initializer, block);
|
|
22
|
+
const iterable = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Iterator, statement.expression, context.transformExpression(statement.expression));
|
|
23
23
|
return lua.createForInStatement(block, [lua.createAnonymousIdentifier(), valueVariable], [iterable], statement);
|
|
24
24
|
}
|
|
25
25
|
const transformForOfStatement = (node, context) => {
|
|
26
|
-
const body = lua.createBlock(utils_1.transformLoopBody(context, node));
|
|
27
|
-
if (ts.isCallExpression(node.expression) && range_1.isRangeFunction(context, node.expression)) {
|
|
28
|
-
return range_1.transformRangeStatement(context, node, body);
|
|
26
|
+
const body = lua.createBlock((0, utils_1.transformLoopBody)(context, node));
|
|
27
|
+
if (ts.isCallExpression(node.expression) && (0, range_1.isRangeFunction)(context, node.expression)) {
|
|
28
|
+
return (0, range_1.transformRangeStatement)(context, node, body);
|
|
29
29
|
}
|
|
30
|
-
else if (ts.isCallExpression(node.expression) && annotations_1.isForRangeType(context, node.expression.expression)) {
|
|
31
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node.expression, annotations_1.AnnotationKind.ForRange));
|
|
30
|
+
else if (ts.isCallExpression(node.expression) && (0, annotations_1.isForRangeType)(context, node.expression.expression)) {
|
|
31
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node.expression, annotations_1.AnnotationKind.ForRange));
|
|
32
32
|
}
|
|
33
|
-
else if (iterable_1.isIterableExpression(context, node.expression)) {
|
|
34
|
-
return iterable_1.transformForOfIterableStatement(context, node, body);
|
|
33
|
+
else if ((0, iterable_1.isIterableExpression)(context, node.expression)) {
|
|
34
|
+
return (0, iterable_1.transformForOfIterableStatement)(context, node, body);
|
|
35
35
|
}
|
|
36
|
-
else if (annotations_1.isLuaIteratorType(context, node.expression)) {
|
|
37
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node.expression, annotations_1.AnnotationKind.LuaIterator));
|
|
36
|
+
else if ((0, annotations_1.isLuaIteratorType)(context, node.expression)) {
|
|
37
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node.expression, annotations_1.AnnotationKind.LuaIterator));
|
|
38
38
|
}
|
|
39
|
-
else if (typescript_1.isArrayType(context, context.checker.getTypeAtLocation(node.expression))) {
|
|
39
|
+
else if ((0, typescript_1.isArrayType)(context, context.checker.getTypeAtLocation(node.expression))) {
|
|
40
40
|
return transformForOfArrayStatement(context, node, body);
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
@@ -9,9 +9,9 @@ const transformForStatement = (statement, context) => {
|
|
|
9
9
|
const result = [];
|
|
10
10
|
if (statement.initializer) {
|
|
11
11
|
if (ts.isVariableDeclarationList(statement.initializer)) {
|
|
12
|
-
variable_declaration_1.checkVariableDeclarationList(context, statement.initializer);
|
|
12
|
+
(0, variable_declaration_1.checkVariableDeclarationList)(context, statement.initializer);
|
|
13
13
|
// local initializer = value
|
|
14
|
-
result.push(...statement.initializer.declarations.flatMap(d => variable_declaration_1.transformVariableDeclaration(context, d)));
|
|
14
|
+
result.push(...statement.initializer.declarations.flatMap(d => (0, variable_declaration_1.transformVariableDeclaration)(context, d)));
|
|
15
15
|
}
|
|
16
16
|
else {
|
|
17
17
|
result.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.initializer)));
|
|
@@ -21,7 +21,7 @@ const transformForStatement = (statement, context) => {
|
|
|
21
21
|
? context.transformExpression(statement.condition)
|
|
22
22
|
: lua.createBooleanLiteral(true);
|
|
23
23
|
// Add body
|
|
24
|
-
const body = utils_1.transformLoopBody(context, statement);
|
|
24
|
+
const body = (0, utils_1.transformLoopBody)(context, statement);
|
|
25
25
|
if (statement.incrementor) {
|
|
26
26
|
body.push(...context.transformStatements(ts.factory.createExpressionStatement(statement.incrementor)));
|
|
27
27
|
}
|
|
@@ -11,9 +11,9 @@ const block_1 = require("../block");
|
|
|
11
11
|
const identifier_1 = require("../identifier");
|
|
12
12
|
const variable_declaration_1 = require("../variable-declaration");
|
|
13
13
|
function transformLoopBody(context, loop) {
|
|
14
|
-
scope_1.pushScope(context, scope_1.ScopeType.Loop);
|
|
15
|
-
const body = scope_1.performHoisting(context, block_1.transformBlockOrStatement(context, loop.statement));
|
|
16
|
-
const scope = scope_1.popScope(context);
|
|
14
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Loop);
|
|
15
|
+
const body = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, loop.statement));
|
|
16
|
+
const scope = (0, scope_1.popScope)(context);
|
|
17
17
|
const scopeId = scope.id;
|
|
18
18
|
if (!scope.loopContinued) {
|
|
19
19
|
return body;
|
|
@@ -25,7 +25,7 @@ function transformLoopBody(context, loop) {
|
|
|
25
25
|
}
|
|
26
26
|
exports.transformLoopBody = transformLoopBody;
|
|
27
27
|
function getVariableDeclarationBinding(context, node) {
|
|
28
|
-
variable_declaration_1.checkVariableDeclarationList(context, node);
|
|
28
|
+
(0, variable_declaration_1.checkVariableDeclarationList)(context, node);
|
|
29
29
|
if (node.declarations.length === 0) {
|
|
30
30
|
return ts.factory.createIdentifier("____");
|
|
31
31
|
}
|
|
@@ -38,18 +38,18 @@ function transformForInitializer(context, initializer, block) {
|
|
|
38
38
|
// Declaration of new variable
|
|
39
39
|
const binding = getVariableDeclarationBinding(context, initializer);
|
|
40
40
|
if (ts.isArrayBindingPattern(binding) || ts.isObjectBindingPattern(binding)) {
|
|
41
|
-
block.statements.unshift(...variable_declaration_1.transformBindingPattern(context, binding, valueVariable));
|
|
41
|
+
block.statements.unshift(...(0, variable_declaration_1.transformBindingPattern)(context, binding, valueVariable));
|
|
42
42
|
}
|
|
43
43
|
else {
|
|
44
44
|
// Single variable declared in for loop
|
|
45
|
-
return identifier_1.transformIdentifier(context, binding);
|
|
45
|
+
return (0, identifier_1.transformIdentifier)(context, binding);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
else {
|
|
49
49
|
// Assignment to existing variable(s)
|
|
50
|
-
block.statements.unshift(...(typescript_1.isAssignmentPattern(initializer)
|
|
51
|
-
? destructuring_assignments_1.transformAssignmentPattern(context, initializer, valueVariable)
|
|
52
|
-
: assignments_1.transformAssignment(context, initializer, valueVariable)));
|
|
50
|
+
block.statements.unshift(...((0, typescript_1.isAssignmentPattern)(initializer)
|
|
51
|
+
? (0, destructuring_assignments_1.transformAssignmentPattern)(context, initializer, valueVariable)
|
|
52
|
+
: (0, assignments_1.transformAssignment)(context, initializer, valueVariable)));
|
|
53
53
|
}
|
|
54
54
|
return valueVariable;
|
|
55
55
|
}
|
|
@@ -19,25 +19,25 @@ const transformExportAssignment = (node, context) => {
|
|
|
19
19
|
// export = [expression];
|
|
20
20
|
// ____exports = [expression];
|
|
21
21
|
if (node.isExportEquals) {
|
|
22
|
-
return lua.createVariableDeclarationStatement(lua_ast_1.createExportsIdentifier(), exportedValue, node);
|
|
22
|
+
return lua.createVariableDeclarationStatement((0, lua_ast_1.createExportsIdentifier)(), exportedValue, node);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
// export default [expression];
|
|
26
26
|
// ____exports.default = [expression];
|
|
27
|
-
return lua.createAssignmentStatement(lua.createTableIndexExpression(lua_ast_1.createExportsIdentifier(), export_1.createDefaultExportStringLiteral(node)), exportedValue, node);
|
|
27
|
+
return lua.createAssignmentStatement(lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), (0, export_1.createDefaultExportStringLiteral)(node)), exportedValue, node);
|
|
28
28
|
}
|
|
29
29
|
};
|
|
30
30
|
exports.transformExportAssignment = transformExportAssignment;
|
|
31
31
|
function transformExportAll(context, node) {
|
|
32
|
-
utils_1.assert(node.moduleSpecifier);
|
|
32
|
+
(0, utils_1.assert)(node.moduleSpecifier);
|
|
33
33
|
if (!context.resolver.moduleExportsSomeValue(node.moduleSpecifier)) {
|
|
34
34
|
return undefined;
|
|
35
35
|
}
|
|
36
|
-
const moduleRequire = import_1.createModuleRequire(context, node.moduleSpecifier);
|
|
36
|
+
const moduleRequire = (0, import_1.createModuleRequire)(context, node.moduleSpecifier);
|
|
37
37
|
// export * as ns from "...";
|
|
38
38
|
// exports.ns = require(...)
|
|
39
39
|
if (node.exportClause && ts.isNamespaceExport(node.exportClause)) {
|
|
40
|
-
const assignToExports = lua.createAssignmentStatement(lua.createTableIndexExpression(lua_ast_1.createExportsIdentifier(), lua.createStringLiteral(node.exportClause.name.text)), moduleRequire);
|
|
40
|
+
const assignToExports = lua.createAssignmentStatement(lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), lua.createStringLiteral(node.exportClause.name.text)), moduleRequire);
|
|
41
41
|
return assignToExports;
|
|
42
42
|
}
|
|
43
43
|
// export * from "...";
|
|
@@ -50,7 +50,7 @@ function transformExportAll(context, node) {
|
|
|
50
50
|
// ____exports[____exportKey] = ____exportValue
|
|
51
51
|
const forKey = lua.createIdentifier("____exportKey");
|
|
52
52
|
const forValue = lua.createIdentifier("____exportValue");
|
|
53
|
-
const leftAssignment = lua.createAssignmentStatement(lua.createTableIndexExpression(lua_ast_1.createExportsIdentifier(), forKey), forValue);
|
|
53
|
+
const leftAssignment = lua.createAssignmentStatement(lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), forKey), forValue);
|
|
54
54
|
// if key ~= "default" then
|
|
55
55
|
// -- export the value, do not export "default" values
|
|
56
56
|
// end
|
|
@@ -70,12 +70,12 @@ const isDefaultExportSpecifier = (node) => (node.name && node.name.originalKeywo
|
|
|
70
70
|
function transformExportSpecifier(context, node) {
|
|
71
71
|
const exportedSymbol = context.checker.getExportSpecifierLocalTargetSymbol(node);
|
|
72
72
|
const exportedIdentifier = node.propertyName ? node.propertyName : node.name;
|
|
73
|
-
const exportedExpression = literal_1.createShorthandIdentifier(context, exportedSymbol, exportedIdentifier);
|
|
73
|
+
const exportedExpression = (0, literal_1.createShorthandIdentifier)(context, exportedSymbol, exportedIdentifier);
|
|
74
74
|
const isDefault = isDefaultExportSpecifier(node);
|
|
75
75
|
const identifierToExport = isDefault
|
|
76
|
-
? export_1.createDefaultExportIdentifier(node)
|
|
77
|
-
: identifier_1.transformIdentifier(context, node.name);
|
|
78
|
-
const exportAssignmentLeftHandSide = export_1.createExportedIdentifier(context, identifierToExport);
|
|
76
|
+
? (0, export_1.createDefaultExportIdentifier)(node)
|
|
77
|
+
: (0, identifier_1.transformIdentifier)(context, node.name);
|
|
78
|
+
const exportAssignmentLeftHandSide = (0, export_1.createExportedIdentifier)(context, identifierToExport);
|
|
79
79
|
return lua.createAssignmentStatement(exportAssignmentLeftHandSide, exportedExpression, node);
|
|
80
80
|
}
|
|
81
81
|
function transformExportSpecifiersFrom(context, statement, moduleSpecifier, exportSpecifiers) {
|
|
@@ -83,11 +83,11 @@ function transformExportSpecifiersFrom(context, statement, moduleSpecifier, expo
|
|
|
83
83
|
const importClause = ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(exportSpecifiers.map(s => ts.factory.createImportSpecifier(s.propertyName, s.name))));
|
|
84
84
|
const importDeclaration = ts.factory.createImportDeclaration(statement.decorators, statement.modifiers, importClause, moduleSpecifier);
|
|
85
85
|
// Wrap in block to prevent imports from hoisting out of `do` statement
|
|
86
|
-
const [block] = block_1.transformScopeBlock(context, ts.factory.createBlock([importDeclaration]), scope_1.ScopeType.Block);
|
|
86
|
+
const [block] = (0, block_1.transformScopeBlock)(context, ts.factory.createBlock([importDeclaration]), scope_1.ScopeType.Block);
|
|
87
87
|
const result = block.statements;
|
|
88
88
|
// Now the module is imported, add the imports to the export table
|
|
89
89
|
for (const specifier of exportSpecifiers) {
|
|
90
|
-
result.push(lua.createAssignmentStatement(export_1.createExportedIdentifier(context, identifier_1.transformIdentifier(context, specifier.name)), identifier_1.transformIdentifier(context, specifier.name)));
|
|
90
|
+
result.push(lua.createAssignmentStatement((0, export_1.createExportedIdentifier)(context, (0, identifier_1.transformIdentifier)(context, specifier.name)), (0, identifier_1.transformIdentifier)(context, specifier.name)));
|
|
91
91
|
}
|
|
92
92
|
// Wrap this in a DoStatement to prevent polluting the scope.
|
|
93
93
|
return lua.createDoStatement(result, statement);
|
|
@@ -106,7 +106,7 @@ const transformExportDeclaration = (node, context) => {
|
|
|
106
106
|
// export * as ns from "...";
|
|
107
107
|
return transformExportAll(context, node);
|
|
108
108
|
}
|
|
109
|
-
const exportSpecifiers = exports.getExported(context, node.exportClause);
|
|
109
|
+
const exportSpecifiers = (0, exports.getExported)(context, node.exportClause);
|
|
110
110
|
// export { ... };
|
|
111
111
|
if (!node.moduleSpecifier) {
|
|
112
112
|
return exportSpecifiers.map(exportSpecifier => transformExportSpecifier(context, exportSpecifier));
|
|
@@ -15,7 +15,7 @@ function isNoResolutionPath(context, moduleSpecifier) {
|
|
|
15
15
|
const moduleOwnerSymbol = context.checker.getSymbolAtLocation(moduleSpecifier);
|
|
16
16
|
if (!moduleOwnerSymbol)
|
|
17
17
|
return false;
|
|
18
|
-
const annotations = annotations_1.getSymbolAnnotations(moduleOwnerSymbol);
|
|
18
|
+
const annotations = (0, annotations_1.getSymbolAnnotations)(moduleOwnerSymbol);
|
|
19
19
|
return annotations.has(annotations_1.AnnotationKind.NoResolution);
|
|
20
20
|
}
|
|
21
21
|
function createModuleRequire(context, moduleSpecifier, tsOriginal = moduleSpecifier) {
|
|
@@ -33,12 +33,12 @@ function shouldBeImported(context, importNode) {
|
|
|
33
33
|
return context.resolver.isReferencedAliasDeclaration(importNode);
|
|
34
34
|
}
|
|
35
35
|
function transformImportSpecifier(context, importSpecifier, moduleTableName) {
|
|
36
|
-
const leftIdentifier = identifier_1.transformIdentifier(context, importSpecifier.name);
|
|
37
|
-
const propertyName = literal_1.transformPropertyName(context, importSpecifier.propertyName ? importSpecifier.propertyName : importSpecifier.name);
|
|
36
|
+
const leftIdentifier = (0, identifier_1.transformIdentifier)(context, importSpecifier.name);
|
|
37
|
+
const propertyName = (0, literal_1.transformPropertyName)(context, importSpecifier.propertyName ? importSpecifier.propertyName : importSpecifier.name);
|
|
38
38
|
return lua.createVariableDeclarationStatement(leftIdentifier, lua.createTableIndexExpression(moduleTableName, propertyName), importSpecifier);
|
|
39
39
|
}
|
|
40
40
|
const transformImportDeclaration = (statement, context) => {
|
|
41
|
-
const scope = scope_1.peekScope(context);
|
|
41
|
+
const scope = (0, scope_1.peekScope)(context);
|
|
42
42
|
if (!scope.importStatements) {
|
|
43
43
|
scope.importStatements = [];
|
|
44
44
|
}
|
|
@@ -61,14 +61,14 @@ const transformImportDeclaration = (statement, context) => {
|
|
|
61
61
|
: "module";
|
|
62
62
|
// Create the require statement to extract values.
|
|
63
63
|
// local ____module = require("module")
|
|
64
|
-
const importUniqueName = lua.createIdentifier(safe_names_1.createSafeName(path.basename(importPath)));
|
|
64
|
+
const importUniqueName = lua.createIdentifier((0, safe_names_1.createSafeName)(path.basename(importPath)));
|
|
65
65
|
let usingRequireStatement = false;
|
|
66
66
|
// import defaultValue from "./module";
|
|
67
67
|
// local defaultValue = __module.default
|
|
68
68
|
if (statement.importClause.name) {
|
|
69
69
|
if (shouldBeImported(context, statement.importClause)) {
|
|
70
|
-
const propertyName = export_1.createDefaultExportStringLiteral(statement.importClause.name);
|
|
71
|
-
const defaultImportAssignmentStatement = lua.createVariableDeclarationStatement(identifier_1.transformIdentifier(context, statement.importClause.name), lua.createTableIndexExpression(importUniqueName, propertyName), statement.importClause.name);
|
|
70
|
+
const propertyName = (0, export_1.createDefaultExportStringLiteral)(statement.importClause.name);
|
|
71
|
+
const defaultImportAssignmentStatement = lua.createVariableDeclarationStatement((0, identifier_1.transformIdentifier)(context, statement.importClause.name), lua.createTableIndexExpression(importUniqueName, propertyName), statement.importClause.name);
|
|
72
72
|
result.push(defaultImportAssignmentStatement);
|
|
73
73
|
usingRequireStatement = true;
|
|
74
74
|
}
|
|
@@ -77,7 +77,7 @@ const transformImportDeclaration = (statement, context) => {
|
|
|
77
77
|
// local module = require("module")
|
|
78
78
|
if (statement.importClause.namedBindings && ts.isNamespaceImport(statement.importClause.namedBindings)) {
|
|
79
79
|
if (context.resolver.isReferencedAliasDeclaration(statement.importClause.namedBindings)) {
|
|
80
|
-
const requireStatement = lua.createVariableDeclarationStatement(identifier_1.transformIdentifier(context, statement.importClause.namedBindings.name), requireCall, statement);
|
|
80
|
+
const requireStatement = lua.createVariableDeclarationStatement((0, identifier_1.transformIdentifier)(context, statement.importClause.namedBindings.name), requireCall, statement);
|
|
81
81
|
result.push(requireStatement);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
@@ -118,9 +118,9 @@ const transformImportEqualsDeclaration = (node, context) => {
|
|
|
118
118
|
!context.resolver.isTopLevelValueImportEqualsWithEntityName(node))) {
|
|
119
119
|
return undefined;
|
|
120
120
|
}
|
|
121
|
-
const name = identifier_1.transformIdentifier(context, node.name);
|
|
121
|
+
const name = (0, identifier_1.transformIdentifier)(context, node.name);
|
|
122
122
|
const expression = context.transformExpression(node.moduleReference);
|
|
123
|
-
return lua_ast_1.createHoistableVariableDeclarationStatement(context, name, expression, node);
|
|
123
|
+
return (0, lua_ast_1.createHoistableVariableDeclarationStatement)(context, name, expression, node);
|
|
124
124
|
};
|
|
125
125
|
exports.transformImportEqualsDeclaration = transformImportEqualsDeclaration;
|
|
126
126
|
//# sourceMappingURL=import.js.map
|
|
@@ -13,11 +13,11 @@ const symbols_1 = require("../utils/symbols");
|
|
|
13
13
|
const identifier_1 = require("./identifier");
|
|
14
14
|
function createModuleLocalNameIdentifier(context, declaration) {
|
|
15
15
|
const moduleSymbol = context.checker.getSymbolAtLocation(declaration.name);
|
|
16
|
-
if (moduleSymbol !== undefined && safe_names_1.isUnsafeName(moduleSymbol.name)) {
|
|
17
|
-
return lua.createIdentifier(safe_names_1.createSafeName(declaration.name.text), declaration.name, moduleSymbol && symbols_1.getSymbolIdOfSymbol(context, moduleSymbol), declaration.name.text);
|
|
16
|
+
if (moduleSymbol !== undefined && (0, safe_names_1.isUnsafeName)(moduleSymbol.name)) {
|
|
17
|
+
return lua.createIdentifier((0, safe_names_1.createSafeName)(declaration.name.text), declaration.name, moduleSymbol && (0, symbols_1.getSymbolIdOfSymbol)(context, moduleSymbol), declaration.name.text);
|
|
18
18
|
}
|
|
19
19
|
// TODO: Should synthetic name nodes be escaped as well?
|
|
20
|
-
return identifier_1.transformIdentifier(context, declaration.name);
|
|
20
|
+
return (0, identifier_1.transformIdentifier)(context, declaration.name);
|
|
21
21
|
}
|
|
22
22
|
exports.createModuleLocalNameIdentifier = createModuleLocalNameIdentifier;
|
|
23
23
|
// TODO: Do it based on transform result?
|
|
@@ -37,17 +37,17 @@ function moduleHasEmittedBody(node) {
|
|
|
37
37
|
const currentNamespaces = new WeakMap();
|
|
38
38
|
const transformModuleDeclaration = (node, context) => {
|
|
39
39
|
var _a, _b;
|
|
40
|
-
const annotations = annotations_1.getTypeAnnotations(context.checker.getTypeAtLocation(node));
|
|
40
|
+
const annotations = (0, annotations_1.getTypeAnnotations)(context.checker.getTypeAtLocation(node));
|
|
41
41
|
// If phantom namespace elide the declaration and return the body
|
|
42
42
|
if (annotations.has(annotations_1.AnnotationKind.Phantom)) {
|
|
43
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node, annotations_1.AnnotationKind.Phantom));
|
|
43
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.Phantom));
|
|
44
44
|
}
|
|
45
45
|
const currentNamespace = currentNamespaces.get(context);
|
|
46
46
|
const result = [];
|
|
47
47
|
const symbol = context.checker.getSymbolAtLocation(node.name);
|
|
48
48
|
const hasExports = symbol !== undefined && context.checker.getExportsOfModule(symbol).length > 0;
|
|
49
|
-
const nameIdentifier = identifier_1.transformIdentifier(context, node.name);
|
|
50
|
-
const exportScope = export_1.getIdentifierExportScope(context, nameIdentifier);
|
|
49
|
+
const nameIdentifier = (0, identifier_1.transformIdentifier)(context, node.name);
|
|
50
|
+
const exportScope = (0, export_1.getIdentifierExportScope)(context, nameIdentifier);
|
|
51
51
|
// Non-module namespace could be merged if:
|
|
52
52
|
// - is top level
|
|
53
53
|
// - is nested and exported
|
|
@@ -60,17 +60,17 @@ const transformModuleDeclaration = (node, context) => {
|
|
|
60
60
|
ts.getOriginalNode(node) === ((_b = symbol.declarations) === null || _b === void 0 ? void 0 : _b.find(ts.isModuleDeclaration)));
|
|
61
61
|
if (isNonModuleMergeable) {
|
|
62
62
|
// 'local NS = NS or {}' or 'exportTable.NS = exportTable.NS or {}'
|
|
63
|
-
const localDeclaration = lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, nameIdentifier, lua.createBinaryExpression(export_1.addExportToIdentifier(context, nameIdentifier), lua.createTableExpression(), lua.SyntaxKind.OrOperator));
|
|
63
|
+
const localDeclaration = (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, nameIdentifier, lua.createBinaryExpression((0, export_1.addExportToIdentifier)(context, nameIdentifier), lua.createTableExpression(), lua.SyntaxKind.OrOperator));
|
|
64
64
|
result.push(...localDeclaration);
|
|
65
65
|
}
|
|
66
66
|
else if (isFirstDeclaration) {
|
|
67
67
|
// local NS = {} or exportTable.NS = {}
|
|
68
|
-
const localDeclaration = lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, nameIdentifier, lua.createTableExpression());
|
|
68
|
+
const localDeclaration = (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, nameIdentifier, lua.createTableExpression());
|
|
69
69
|
result.push(...localDeclaration);
|
|
70
70
|
}
|
|
71
71
|
if ((isNonModuleMergeable || isFirstDeclaration) && exportScope && hasExports && moduleHasEmittedBody(node)) {
|
|
72
72
|
// local NS = exportTable.NS
|
|
73
|
-
const localDeclaration = lua_ast_1.createHoistableVariableDeclarationStatement(context, createModuleLocalNameIdentifier(context, node), export_1.createExportedIdentifier(context, nameIdentifier, exportScope));
|
|
73
|
+
const localDeclaration = (0, lua_ast_1.createHoistableVariableDeclarationStatement)(context, createModuleLocalNameIdentifier(context, node), (0, export_1.createExportedIdentifier)(context, nameIdentifier, exportScope));
|
|
74
74
|
result.push(localDeclaration);
|
|
75
75
|
}
|
|
76
76
|
// Set current namespace for nested NS
|
|
@@ -78,9 +78,9 @@ const transformModuleDeclaration = (node, context) => {
|
|
|
78
78
|
currentNamespaces.set(context, node);
|
|
79
79
|
// Transform moduleblock to block and visit it
|
|
80
80
|
if (moduleHasEmittedBody(node)) {
|
|
81
|
-
scope_1.pushScope(context, scope_1.ScopeType.Block);
|
|
82
|
-
const statements = scope_1.performHoisting(context, context.transformStatements(ts.isModuleBlock(node.body) ? node.body.statements : node.body));
|
|
83
|
-
scope_1.popScope(context);
|
|
81
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Block);
|
|
82
|
+
const statements = (0, scope_1.performHoisting)(context, context.transformStatements(ts.isModuleBlock(node.body) ? node.body.statements : node.body));
|
|
83
|
+
(0, scope_1.popScope)(context);
|
|
84
84
|
result.push(lua.createDoStatement(statements));
|
|
85
85
|
}
|
|
86
86
|
currentNamespaces.set(context, currentNamespace);
|
|
@@ -3,3 +3,4 @@ import * as lua from "../../LuaAST";
|
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
4
|
export declare function transformExpressionBodyToReturnStatement(context: TransformationContext, node: ts.Expression): lua.Statement;
|
|
5
5
|
export declare const transformReturnStatement: FunctionVisitor<ts.ReturnStatement>;
|
|
6
|
+
export declare function createReturnStatement(context: TransformationContext, values: lua.Expression[], node: ts.Node): lua.ReturnStatement;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformReturnStatement = exports.transformExpressionBodyToReturnStatement = void 0;
|
|
3
|
+
exports.createReturnStatement = exports.transformReturnStatement = exports.transformExpressionBodyToReturnStatement = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const assignment_validation_1 = require("../utils/assignment-validation");
|
|
@@ -9,65 +9,79 @@ const scope_1 = require("../utils/scope");
|
|
|
9
9
|
const call_1 = require("./call");
|
|
10
10
|
const multi_1 = require("./language-extensions/multi");
|
|
11
11
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
12
|
+
const typescript_1 = require("../utils/typescript");
|
|
12
13
|
function transformExpressionsInReturn(context, node, insideTryCatch) {
|
|
13
14
|
const expressionType = context.checker.getTypeAtLocation(node);
|
|
14
15
|
if (ts.isCallExpression(node)) {
|
|
15
16
|
// $multi(...)
|
|
16
|
-
if (multi_1.isMultiFunctionCall(context, node)) {
|
|
17
|
+
if ((0, multi_1.isMultiFunctionCall)(context, node)) {
|
|
17
18
|
// Don't allow $multi to be implicitly cast to something other than LuaMultiReturn
|
|
18
19
|
const type = context.checker.getContextualType(node);
|
|
19
|
-
if (type && !multi_1.canBeMultiReturnType(type)) {
|
|
20
|
-
context.diagnostics.push(diagnostics_1.invalidMultiFunctionReturnType(node));
|
|
20
|
+
if (type && !(0, multi_1.canBeMultiReturnType)(type)) {
|
|
21
|
+
context.diagnostics.push((0, diagnostics_1.invalidMultiFunctionReturnType)(node));
|
|
21
22
|
}
|
|
22
|
-
let returnValues = call_1.transformArguments(context, node.arguments);
|
|
23
|
+
let returnValues = (0, call_1.transformArguments)(context, node.arguments);
|
|
23
24
|
if (insideTryCatch) {
|
|
24
|
-
returnValues = [lua_ast_1.wrapInTable(...returnValues)]; // Wrap results when returning inside try/catch
|
|
25
|
+
returnValues = [(0, lua_ast_1.wrapInTable)(...returnValues)]; // Wrap results when returning inside try/catch
|
|
25
26
|
}
|
|
26
27
|
return returnValues;
|
|
27
28
|
}
|
|
28
29
|
// Force-wrap LuaMultiReturn when returning inside try/catch
|
|
29
|
-
if (insideTryCatch && multi_1.returnsMultiType(context, node) && !multi_1.shouldMultiReturnCallBeWrapped(context, node)) {
|
|
30
|
-
return [lua_ast_1.wrapInTable(context.transformExpression(node))];
|
|
30
|
+
if (insideTryCatch && (0, multi_1.returnsMultiType)(context, node) && !(0, multi_1.shouldMultiReturnCallBeWrapped)(context, node)) {
|
|
31
|
+
return [(0, lua_ast_1.wrapInTable)(context.transformExpression(node))];
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
|
-
else if (multi_1.isInMultiReturnFunction(context, node) && multi_1.isMultiReturnType(expressionType)) {
|
|
34
|
+
else if ((0, multi_1.isInMultiReturnFunction)(context, node) && (0, multi_1.isMultiReturnType)(expressionType)) {
|
|
34
35
|
// Unpack objects typed as LuaMultiReturn
|
|
35
|
-
return [lua_ast_1.createUnpackCall(context, context.transformExpression(node), node)];
|
|
36
|
+
return [(0, lua_ast_1.createUnpackCall)(context, context.transformExpression(node), node)];
|
|
36
37
|
}
|
|
37
38
|
return [context.transformExpression(node)];
|
|
38
39
|
}
|
|
39
40
|
function transformExpressionBodyToReturnStatement(context, node) {
|
|
40
41
|
const expressions = transformExpressionsInReturn(context, node, false);
|
|
41
|
-
return
|
|
42
|
+
return createReturnStatement(context, expressions, node);
|
|
42
43
|
}
|
|
43
44
|
exports.transformExpressionBodyToReturnStatement = transformExpressionBodyToReturnStatement;
|
|
44
45
|
const transformReturnStatement = (statement, context) => {
|
|
45
|
-
// Bubble up explicit return flag and check if we're inside a try/catch block
|
|
46
|
-
let insideTryCatch = false;
|
|
47
|
-
for (const scope of scope_1.walkScopesUp(context)) {
|
|
48
|
-
scope.functionReturned = true;
|
|
49
|
-
if (scope.type === scope_1.ScopeType.Function) {
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
insideTryCatch = insideTryCatch || scope.type === scope_1.ScopeType.Try || scope.type === scope_1.ScopeType.Catch;
|
|
53
|
-
}
|
|
54
46
|
let results;
|
|
55
47
|
if (statement.expression) {
|
|
56
48
|
const expressionType = context.checker.getTypeAtLocation(statement.expression);
|
|
57
49
|
const returnType = context.checker.getContextualType(statement.expression);
|
|
58
50
|
if (returnType) {
|
|
59
|
-
assignment_validation_1.validateAssignment(context, statement, expressionType, returnType);
|
|
51
|
+
(0, assignment_validation_1.validateAssignment)(context, statement, expressionType, returnType);
|
|
60
52
|
}
|
|
61
|
-
results = transformExpressionsInReturn(context, statement.expression,
|
|
53
|
+
results = transformExpressionsInReturn(context, statement.expression, isInTryCatch(context));
|
|
62
54
|
}
|
|
63
55
|
else {
|
|
64
56
|
// Empty return
|
|
65
57
|
results = [];
|
|
66
58
|
}
|
|
67
|
-
|
|
68
|
-
results.unshift(lua.createBooleanLiteral(true));
|
|
69
|
-
}
|
|
70
|
-
return lua.createReturnStatement(results, statement);
|
|
59
|
+
return createReturnStatement(context, results, statement);
|
|
71
60
|
};
|
|
72
61
|
exports.transformReturnStatement = transformReturnStatement;
|
|
62
|
+
function createReturnStatement(context, values, node) {
|
|
63
|
+
const results = [...values];
|
|
64
|
+
if (isInTryCatch(context)) {
|
|
65
|
+
// Bubble up explicit return flag and check if we're inside a try/catch block
|
|
66
|
+
results.unshift(lua.createBooleanLiteral(true));
|
|
67
|
+
}
|
|
68
|
+
else if ((0, typescript_1.isInAsyncFunction)(node)) {
|
|
69
|
+
// Add nil error handler in async function and not in try
|
|
70
|
+
results.unshift(lua.createNilLiteral());
|
|
71
|
+
}
|
|
72
|
+
return lua.createReturnStatement(results, node);
|
|
73
|
+
}
|
|
74
|
+
exports.createReturnStatement = createReturnStatement;
|
|
75
|
+
function isInTryCatch(context) {
|
|
76
|
+
// Check if context is in a try or catch
|
|
77
|
+
let insideTryCatch = false;
|
|
78
|
+
for (const scope of (0, scope_1.walkScopesUp)(context)) {
|
|
79
|
+
scope.functionReturned = true;
|
|
80
|
+
if (scope.type === scope_1.ScopeType.Function) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
insideTryCatch = insideTryCatch || scope.type === scope_1.ScopeType.Try || scope.type === scope_1.ScopeType.Catch;
|
|
84
|
+
}
|
|
85
|
+
return insideTryCatch;
|
|
86
|
+
}
|
|
73
87
|
//# sourceMappingURL=return.js.map
|