typescript-to-lua 1.0.1 → 1.3.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.d.ts +1 -0
- package/dist/LuaAST.js +14 -6
- package/dist/LuaLib.d.ts +4 -3
- package/dist/LuaLib.js +6 -5
- package/dist/LuaPrinter.d.ts +15 -4
- package/dist/LuaPrinter.js +81 -29
- package/dist/cli/diagnostics.js +2 -2
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- package/dist/cli/report.js +1 -1
- package/dist/cli/tsconfig.js +4 -4
- package/dist/lualib/ArrayFlat.lua +1 -1
- package/dist/lualib/ArrayFlatMap.lua +1 -1
- package/dist/lualib/ArrayIsArray.lua +1 -1
- package/dist/lualib/ArrayReduce.lua +7 -1
- package/dist/lualib/ArrayReduceRight.lua +7 -1
- package/dist/lualib/ArraySetLength.lua +1 -1
- package/dist/lualib/ArraySplice.lua +5 -5
- package/dist/lualib/Await.lua +43 -19
- package/dist/lualib/CloneDescriptor.lua +7 -7
- package/dist/lualib/Decorate.lua +1 -1
- package/dist/lualib/DelegatedYield.lua +1 -3
- package/dist/lualib/Delete.lua +6 -6
- package/dist/lualib/Error.lua +47 -44
- package/dist/lualib/Generator.lua +1 -5
- package/dist/lualib/InstanceOf.lua +1 -1
- package/dist/lualib/InstanceOfObject.lua +1 -1
- package/dist/lualib/Map.lua +130 -134
- package/dist/lualib/Number.lua +1 -1
- package/dist/lualib/NumberIsFinite.lua +1 -1
- package/dist/lualib/NumberToString.lua +3 -5
- package/dist/lualib/ObjectDefineProperty.lua +40 -8
- package/dist/lualib/ParseFloat.lua +9 -5
- package/dist/lualib/ParseInt.lua +15 -3
- package/dist/lualib/Promise.lua +44 -26
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +19 -9
- package/dist/lualib/SparseArrayNew.lua +5 -0
- package/dist/lualib/SparseArrayPush.lua +9 -0
- package/dist/lualib/SparseArraySpread.lua +4 -0
- package/dist/lualib/StringAccess.lua +1 -1
- package/dist/lualib/StringCharCodeAt.lua +1 -1
- package/dist/lualib/StringEndsWith.lua +2 -2
- package/dist/lualib/StringPadEnd.lua +2 -2
- package/dist/lualib/StringPadStart.lua +2 -2
- package/dist/lualib/StringReplace.lua +12 -11
- package/dist/lualib/StringReplaceAll.lua +35 -0
- package/dist/lualib/StringSlice.lua +2 -2
- package/dist/lualib/StringSplit.lua +3 -3
- package/dist/lualib/StringStartsWith.lua +1 -1
- package/dist/lualib/StringSubstr.lua +1 -1
- package/dist/lualib/StringSubstring.lua +2 -2
- package/dist/lualib/Symbol.lua +3 -5
- package/dist/lualib/WeakMap.lua +40 -42
- package/dist/lualib/WeakSet.lua +36 -38
- package/dist/lualib/lualib_bundle.lua +876 -745
- package/dist/transformation/builtins/array.js +29 -30
- package/dist/transformation/builtins/console.js +2 -2
- package/dist/transformation/builtins/function.js +14 -12
- package/dist/transformation/builtins/global.js +8 -8
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +69 -36
- package/dist/transformation/builtins/math.js +4 -4
- package/dist/transformation/builtins/number.js +7 -7
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +12 -13
- package/dist/transformation/builtins/promise.js +9 -9
- package/dist/transformation/builtins/string.js +32 -31
- package/dist/transformation/builtins/symbol.js +3 -3
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +105 -6
- 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.d.ts +9 -0
- package/dist/transformation/utils/diagnostics.js +8 -5
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +14 -7
- package/dist/transformation/utils/function-context.js +8 -8
- package/dist/transformation/utils/language-extensions.d.ts +2 -0
- package/dist/transformation/utils/language-extensions.js +7 -1
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +25 -35
- package/dist/transformation/utils/lualib.js +1 -1
- package/dist/transformation/utils/preceding-statements.d.ts +3 -0
- package/dist/transformation/utils/preceding-statements.js +11 -0
- package/dist/transformation/utils/safe-names.d.ts +4 -2
- package/dist/transformation/utils/safe-names.js +13 -7
- package/dist/transformation/utils/scope.d.ts +9 -1
- package/dist/transformation/utils/scope.js +78 -40
- package/dist/transformation/utils/symbols.js +6 -6
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/nodes.d.ts +2 -0
- package/dist/transformation/utils/typescript/nodes.js +21 -1
- package/dist/transformation/utils/typescript/types.d.ts +3 -1
- package/dist/transformation/utils/typescript/types.js +40 -13
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +74 -47
- package/dist/transformation/visitors/async-await.js +5 -4
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +86 -77
- package/dist/transformation/visitors/binary-expression/bit.js +4 -4
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +69 -71
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +65 -36
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +84 -42
- package/dist/transformation/visitors/block.js +6 -6
- package/dist/transformation/visitors/break-continue.js +2 -2
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +130 -143
- package/dist/transformation/visitors/class/decorators.js +3 -3
- package/dist/transformation/visitors/class/index.js +43 -39
- 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 +14 -16
- package/dist/transformation/visitors/class/setup.js +11 -11
- package/dist/transformation/visitors/class/utils.js +2 -2
- package/dist/transformation/visitors/conditional.js +36 -43
- package/dist/transformation/visitors/delete.js +8 -4
- package/dist/transformation/visitors/enum.js +8 -8
- package/dist/transformation/visitors/errors.js +35 -33
- package/dist/transformation/visitors/expression-list.d.ts +7 -0
- package/dist/transformation/visitors/expression-list.js +120 -0
- package/dist/transformation/visitors/expression-statement.js +10 -6
- package/dist/transformation/visitors/function.d.ts +2 -0
- package/dist/transformation/visitors/function.js +80 -33
- package/dist/transformation/visitors/identifier.js +26 -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/pairsIterable.d.ts +5 -0
- package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
- 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 +87 -57
- package/dist/transformation/visitors/loops/do-while.js +35 -9
- package/dist/transformation/visitors/loops/for-in.js +4 -4
- package/dist/transformation/visitors/loops/for-of.js +17 -13
- package/dist/transformation/visitors/loops/for.js +28 -8
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +26 -10
- package/dist/transformation/visitors/modules/export.js +14 -15
- package/dist/transformation/visitors/modules/import.js +10 -10
- package/dist/transformation/visitors/namespace.js +13 -13
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/return.d.ts +1 -0
- package/dist/transformation/visitors/return.js +40 -26
- package/dist/transformation/visitors/sourceFile.js +12 -9
- package/dist/transformation/visitors/spread.js +23 -16
- package/dist/transformation/visitors/switch.js +63 -20
- package/dist/transformation/visitors/template.js +10 -7
- package/dist/transformation/visitors/typeof.js +4 -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 +55 -32
- package/dist/transformation/visitors/void.d.ts +6 -0
- package/dist/transformation/visitors/void.js +20 -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 +20 -13
- package/dist/transpilation/transformers.js +45 -25
- package/dist/transpilation/transpile.js +6 -6
- package/dist/transpilation/transpiler.js +8 -8
- package/dist/transpilation/utils.js +5 -3
- package/dist/tstl.js +8 -8
- package/dist/utils.js +2 -2
- package/language-extensions/index.d.ts +11 -1
- package/package.json +15 -12
- package/dist/lualib/OptionalChainAccess.lua +0 -6
- package/dist/lualib/OptionalFunctionCall.lua +0 -6
- package/dist/lualib/OptionalMethodCall.lua +0 -11
- package/dist/transformation/utils/transform.d.ts +0 -8
- package/dist/transformation/utils/transform.js +0 -14
- 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
|
@@ -8,7 +8,7 @@ const lualib_1 = require("../utils/lualib");
|
|
|
8
8
|
const binary_expression_1 = require("./binary-expression");
|
|
9
9
|
const transformTypeOfExpression = (node, context) => {
|
|
10
10
|
const innerExpression = context.transformExpression(node.expression);
|
|
11
|
-
return lualib_1.transformLuaLibFunction(context, LuaLib_1.LuaLibFeature.TypeOf, node, innerExpression);
|
|
11
|
+
return (0, lualib_1.transformLuaLibFunction)(context, LuaLib_1.LuaLibFeature.TypeOf, node, innerExpression);
|
|
12
12
|
};
|
|
13
13
|
exports.transformTypeOfExpression = transformTypeOfExpression;
|
|
14
14
|
function transformTypeOfBinaryExpression(context, node) {
|
|
@@ -43,7 +43,9 @@ function transformTypeOfBinaryExpression(context, node) {
|
|
|
43
43
|
}
|
|
44
44
|
const innerExpression = context.transformExpression(typeOfExpression.expression);
|
|
45
45
|
const typeCall = lua.createCallExpression(lua.createIdentifier("type"), [innerExpression], typeOfExpression);
|
|
46
|
-
|
|
46
|
+
const [precedingStatements, result] = (0, binary_expression_1.transformBinaryOperation)(context, typeCall, comparedExpression, [], operator, node);
|
|
47
|
+
context.addPrecedingStatements(precedingStatements);
|
|
48
|
+
return result;
|
|
47
49
|
}
|
|
48
50
|
exports.transformTypeOfBinaryExpression = transformTypeOfBinaryExpression;
|
|
49
51
|
//# sourceMappingURL=typeof.js.map
|
|
@@ -5,7 +5,7 @@ const ts = require("typescript");
|
|
|
5
5
|
const assignment_validation_1 = require("../utils/assignment-validation");
|
|
6
6
|
const transformAssertionExpression = (expression, context) => {
|
|
7
7
|
if (!ts.isConstTypeReference(expression.type)) {
|
|
8
|
-
assignment_validation_1.validateAssignment(context, expression, context.checker.getTypeAtLocation(expression.expression), context.checker.getTypeAtLocation(expression.type));
|
|
8
|
+
(0, assignment_validation_1.validateAssignment)(context, expression, context.checker.getTypeAtLocation(expression.expression), context.checker.getTypeAtLocation(expression.type));
|
|
9
9
|
}
|
|
10
10
|
return context.transformExpression(expression.expression);
|
|
11
11
|
};
|
|
@@ -12,32 +12,32 @@ function transformUnaryExpressionStatement(context, node) {
|
|
|
12
12
|
(expression.operator === ts.SyntaxKind.PlusPlusToken || expression.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
13
13
|
// ++i, --i
|
|
14
14
|
const replacementOperator = expression.operator === ts.SyntaxKind.PlusPlusToken ? ts.SyntaxKind.PlusToken : ts.SyntaxKind.MinusToken;
|
|
15
|
-
return compound_1.transformCompoundAssignmentStatement(context, expression, expression.operand, ts.factory.createNumericLiteral(1), replacementOperator);
|
|
15
|
+
return (0, compound_1.transformCompoundAssignmentStatement)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), replacementOperator);
|
|
16
16
|
}
|
|
17
17
|
else if (ts.isPostfixUnaryExpression(expression)) {
|
|
18
18
|
// i++, i--
|
|
19
19
|
const replacementOperator = expression.operator === ts.SyntaxKind.PlusPlusToken ? ts.SyntaxKind.PlusToken : ts.SyntaxKind.MinusToken;
|
|
20
|
-
return compound_1.transformCompoundAssignmentStatement(context, expression, expression.operand, ts.factory.createNumericLiteral(1), replacementOperator);
|
|
20
|
+
return (0, compound_1.transformCompoundAssignmentStatement)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), replacementOperator);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
exports.transformUnaryExpressionStatement = transformUnaryExpressionStatement;
|
|
24
24
|
const transformPostfixUnaryExpression = (expression, context) => {
|
|
25
25
|
switch (expression.operator) {
|
|
26
26
|
case ts.SyntaxKind.PlusPlusToken:
|
|
27
|
-
return compound_1.transformCompoundAssignmentExpression(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.PlusToken, true);
|
|
27
|
+
return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.PlusToken, true);
|
|
28
28
|
case ts.SyntaxKind.MinusMinusToken:
|
|
29
|
-
return compound_1.transformCompoundAssignmentExpression(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.MinusToken, true);
|
|
29
|
+
return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.MinusToken, true);
|
|
30
30
|
default:
|
|
31
|
-
utils_1.assertNever(expression.operator);
|
|
31
|
+
(0, utils_1.assertNever)(expression.operator);
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
exports.transformPostfixUnaryExpression = transformPostfixUnaryExpression;
|
|
35
35
|
const transformPrefixUnaryExpression = (expression, context) => {
|
|
36
36
|
switch (expression.operator) {
|
|
37
37
|
case ts.SyntaxKind.PlusPlusToken:
|
|
38
|
-
return compound_1.transformCompoundAssignmentExpression(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.PlusToken, false);
|
|
38
|
+
return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.PlusToken, false);
|
|
39
39
|
case ts.SyntaxKind.MinusMinusToken:
|
|
40
|
-
return compound_1.transformCompoundAssignmentExpression(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.MinusToken, false);
|
|
40
|
+
return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.MinusToken, false);
|
|
41
41
|
case ts.SyntaxKind.PlusToken:
|
|
42
42
|
// TODO: Wrap with `Number`
|
|
43
43
|
return context.transformExpression(expression.operand);
|
|
@@ -46,9 +46,9 @@ const transformPrefixUnaryExpression = (expression, context) => {
|
|
|
46
46
|
case ts.SyntaxKind.ExclamationToken:
|
|
47
47
|
return lua.createUnaryExpression(context.transformExpression(expression.operand), lua.SyntaxKind.NotOperator);
|
|
48
48
|
case ts.SyntaxKind.TildeToken:
|
|
49
|
-
return bit_1.transformUnaryBitOperation(context, expression, context.transformExpression(expression.operand), lua.SyntaxKind.BitwiseNotOperator);
|
|
49
|
+
return (0, bit_1.transformUnaryBitOperation)(context, expression, context.transformExpression(expression.operand), lua.SyntaxKind.BitwiseNotOperator);
|
|
50
50
|
default:
|
|
51
|
-
utils_1.assertNever(expression.operator);
|
|
51
|
+
(0, utils_1.assertNever)(expression.operator);
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
exports.transformPrefixUnaryExpression = transformPrefixUnaryExpression;
|
|
@@ -9,6 +9,8 @@ const diagnostics_1 = require("../utils/diagnostics");
|
|
|
9
9
|
const export_1 = require("../utils/export");
|
|
10
10
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
11
11
|
const lualib_1 = require("../utils/lualib");
|
|
12
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
13
|
+
const function_1 = require("./function");
|
|
12
14
|
const identifier_1 = require("./identifier");
|
|
13
15
|
const multi_1 = require("./language-extensions/multi");
|
|
14
16
|
const literal_1 = require("./literal");
|
|
@@ -17,16 +19,21 @@ function transformArrayBindingElement(context, name) {
|
|
|
17
19
|
return lua.createAnonymousIdentifier(name);
|
|
18
20
|
}
|
|
19
21
|
else if (ts.isIdentifier(name)) {
|
|
20
|
-
return identifier_1.transformIdentifier(context, name);
|
|
22
|
+
return (0, identifier_1.transformIdentifier)(context, name);
|
|
21
23
|
}
|
|
22
24
|
else if (ts.isBindingElement(name)) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
if (ts.isIdentifier(name.name)) {
|
|
26
|
+
return (0, identifier_1.transformIdentifier)(context, name.name);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// ts.isBindingPattern(name.name)
|
|
30
|
+
const tempName = context.createTempNameForNode(name.name);
|
|
31
|
+
context.addPrecedingStatements(transformBindingPattern(context, name.name, tempName));
|
|
32
|
+
return tempName;
|
|
33
|
+
}
|
|
27
34
|
}
|
|
28
35
|
else {
|
|
29
|
-
utils_1.assertNever(name);
|
|
36
|
+
(0, utils_1.assertNever)(name);
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
39
|
exports.transformArrayBindingElement = transformArrayBindingElement;
|
|
@@ -48,11 +55,13 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
48
55
|
continue;
|
|
49
56
|
}
|
|
50
57
|
// Build the path to the table
|
|
51
|
-
const tableExpression = propertyAccessStack.reduce((path, property) => lua.createTableIndexExpression(path, literal_1.transformPropertyName(context, property)), table);
|
|
58
|
+
const tableExpression = propertyAccessStack.reduce((path, property) => lua.createTableIndexExpression(path, (0, literal_1.transformPropertyName)(context, property)), table);
|
|
52
59
|
// The identifier of the new variable
|
|
53
|
-
const variableName = identifier_1.transformIdentifier(context, element.name);
|
|
60
|
+
const variableName = (0, identifier_1.transformIdentifier)(context, element.name);
|
|
54
61
|
// The field to extract
|
|
55
|
-
const
|
|
62
|
+
const elementName = (_a = element.propertyName) !== null && _a !== void 0 ? _a : element.name;
|
|
63
|
+
const [precedingStatements, propertyName] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, literal_1.transformPropertyName)(context, elementName));
|
|
64
|
+
result.push(...precedingStatements); // Keep property's preceding statements in order
|
|
56
65
|
let expression;
|
|
57
66
|
if (element.dotDotDotToken) {
|
|
58
67
|
if (index !== pattern.elements.length - 1) {
|
|
@@ -78,20 +87,23 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
78
87
|
}
|
|
79
88
|
}
|
|
80
89
|
const excludedPropertiesTable = excludedProperties.map(e => lua.createTableFieldExpression(lua.createBooleanLiteral(true), lua.createStringLiteral(e.text, e)));
|
|
81
|
-
expression = lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ObjectRest, undefined, tableExpression, lua.createTableExpression(excludedPropertiesTable));
|
|
90
|
+
expression = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectRest, undefined, tableExpression, lua.createTableExpression(excludedPropertiesTable));
|
|
82
91
|
}
|
|
83
92
|
else {
|
|
84
|
-
expression = lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ArraySlice, undefined, tableExpression, lua.createNumericLiteral(index));
|
|
93
|
+
expression = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySlice, undefined, tableExpression, lua.createNumericLiteral(index));
|
|
85
94
|
}
|
|
86
95
|
}
|
|
87
96
|
else {
|
|
88
97
|
expression = lua.createTableIndexExpression(tableExpression, ts.isObjectBindingPattern(pattern) ? propertyName : lua.createNumericLiteral(index + 1));
|
|
89
98
|
}
|
|
90
|
-
result.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, variableName, expression));
|
|
99
|
+
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, variableName, expression));
|
|
91
100
|
if (element.initializer) {
|
|
92
|
-
const identifier = export_1.addExportToIdentifier(context, variableName);
|
|
101
|
+
const identifier = (0, export_1.addExportToIdentifier)(context, variableName);
|
|
102
|
+
const tsInitializer = element.initializer;
|
|
103
|
+
const [initializerPrecedingStatements, initializer] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(tsInitializer));
|
|
93
104
|
result.push(lua.createIfStatement(lua.createBinaryExpression(identifier, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator), lua.createBlock([
|
|
94
|
-
|
|
105
|
+
...initializerPrecedingStatements,
|
|
106
|
+
lua.createAssignmentStatement(identifier, initializer),
|
|
95
107
|
])));
|
|
96
108
|
}
|
|
97
109
|
}
|
|
@@ -106,18 +118,21 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
|
|
|
106
118
|
if (ts.isObjectBindingPattern(bindingPattern) || bindingPattern.elements.some(isComplexBindingElement)) {
|
|
107
119
|
let table;
|
|
108
120
|
if (initializer !== undefined && ts.isIdentifier(initializer)) {
|
|
109
|
-
table = identifier_1.transformIdentifier(context, initializer);
|
|
121
|
+
table = (0, identifier_1.transformIdentifier)(context, initializer);
|
|
110
122
|
}
|
|
111
123
|
else {
|
|
112
124
|
// Contain the expression in a temporary variable
|
|
113
|
-
table = lua.createAnonymousIdentifier();
|
|
114
125
|
if (initializer) {
|
|
126
|
+
table = context.createTempNameForNode(initializer);
|
|
115
127
|
let expression = context.transformExpression(initializer);
|
|
116
|
-
if (multi_1.isMultiReturnCall(context, initializer)) {
|
|
117
|
-
expression = lua_ast_1.wrapInTable(expression);
|
|
128
|
+
if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
|
|
129
|
+
expression = (0, lua_ast_1.wrapInTable)(expression);
|
|
118
130
|
}
|
|
119
131
|
statements.push(lua.createVariableDeclarationStatement(table, expression));
|
|
120
132
|
}
|
|
133
|
+
else {
|
|
134
|
+
table = lua.createAnonymousIdentifier();
|
|
135
|
+
}
|
|
121
136
|
}
|
|
122
137
|
statements.push(...transformBindingPattern(context, bindingPattern, table));
|
|
123
138
|
return statements;
|
|
@@ -126,30 +141,30 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
|
|
|
126
141
|
? bindingPattern.elements.map(e => transformArrayBindingElement(context, e))
|
|
127
142
|
: lua.createAnonymousIdentifier();
|
|
128
143
|
if (initializer) {
|
|
129
|
-
if (multi_1.isMultiReturnCall(context, initializer)) {
|
|
144
|
+
if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
|
|
130
145
|
// Don't unpack LuaMultiReturn functions
|
|
131
|
-
statements.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, vars, context.transformExpression(initializer), initializer));
|
|
146
|
+
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, context.transformExpression(initializer), initializer));
|
|
132
147
|
}
|
|
133
148
|
else if (ts.isArrayLiteralExpression(initializer)) {
|
|
134
149
|
// Don't unpack array literals
|
|
135
150
|
const values = initializer.elements.length > 0
|
|
136
151
|
? initializer.elements.map(e => context.transformExpression(e))
|
|
137
152
|
: lua.createNilLiteral();
|
|
138
|
-
statements.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, vars, values, initializer));
|
|
153
|
+
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, values, initializer));
|
|
139
154
|
}
|
|
140
155
|
else {
|
|
141
156
|
// local vars = this.transpileDestructingAssignmentValue(node.initializer);
|
|
142
|
-
const unpackedInitializer = lua_ast_1.createUnpackCall(context, context.transformExpression(initializer), initializer);
|
|
143
|
-
statements.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, vars, unpackedInitializer, initializer));
|
|
157
|
+
const unpackedInitializer = (0, lua_ast_1.createUnpackCall)(context, context.transformExpression(initializer), initializer);
|
|
158
|
+
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, unpackedInitializer, initializer));
|
|
144
159
|
}
|
|
145
160
|
}
|
|
146
161
|
else {
|
|
147
|
-
statements.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, vars, lua.createNilLiteral(), initializer));
|
|
162
|
+
statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, lua.createNilLiteral(), initializer));
|
|
148
163
|
}
|
|
149
164
|
for (const element of bindingPattern.elements) {
|
|
150
165
|
if (!ts.isOmittedExpression(element) && element.initializer) {
|
|
151
|
-
const variableName = identifier_1.transformIdentifier(context, element.name);
|
|
152
|
-
const identifier = export_1.addExportToIdentifier(context, variableName);
|
|
166
|
+
const variableName = (0, identifier_1.transformIdentifier)(context, element.name);
|
|
167
|
+
const identifier = (0, export_1.addExportToIdentifier)(context, variableName);
|
|
153
168
|
statements.push(lua.createIfStatement(lua.createBinaryExpression(identifier, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator), lua.createBlock([
|
|
154
169
|
lua.createAssignmentStatement(identifier, context.transformExpression(element.initializer)),
|
|
155
170
|
])));
|
|
@@ -163,27 +178,35 @@ function transformVariableDeclaration(context, statement) {
|
|
|
163
178
|
if (statement.initializer && statement.type) {
|
|
164
179
|
const initializerType = context.checker.getTypeAtLocation(statement.initializer);
|
|
165
180
|
const varType = context.checker.getTypeFromTypeNode(statement.type);
|
|
166
|
-
assignment_validation_1.validateAssignment(context, statement.initializer, initializerType, varType);
|
|
181
|
+
(0, assignment_validation_1.validateAssignment)(context, statement.initializer, initializerType, varType);
|
|
167
182
|
}
|
|
168
183
|
if (ts.isIdentifier(statement.name)) {
|
|
169
184
|
// Find variable identifier
|
|
170
|
-
const identifierName = identifier_1.transformIdentifier(context, statement.name);
|
|
185
|
+
const identifierName = (0, identifier_1.transformIdentifier)(context, statement.name);
|
|
171
186
|
const value = statement.initializer && context.transformExpression(statement.initializer);
|
|
172
|
-
|
|
187
|
+
// Wrap functions being assigned to a type that contains additional properties in a callable table
|
|
188
|
+
// This catches 'const foo = function() {}; foo.bar = "FOOBAR";'
|
|
189
|
+
const wrappedValue = value &&
|
|
190
|
+
// Skip named function expressions because they will have been wrapped already
|
|
191
|
+
!(statement.initializer && ts.isFunctionExpression(statement.initializer) && statement.initializer.name) &&
|
|
192
|
+
(0, function_1.isFunctionTypeWithProperties)(context.checker.getTypeAtLocation(statement.name))
|
|
193
|
+
? (0, function_1.createCallableTable)(value)
|
|
194
|
+
: value;
|
|
195
|
+
return (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, identifierName, wrappedValue, statement);
|
|
173
196
|
}
|
|
174
197
|
else if (ts.isArrayBindingPattern(statement.name) || ts.isObjectBindingPattern(statement.name)) {
|
|
175
198
|
return transformBindingVariableDeclaration(context, statement.name, statement.initializer);
|
|
176
199
|
}
|
|
177
200
|
else {
|
|
178
|
-
return utils_1.assertNever(statement.name);
|
|
201
|
+
return (0, utils_1.assertNever)(statement.name);
|
|
179
202
|
}
|
|
180
203
|
}
|
|
181
204
|
exports.transformVariableDeclaration = transformVariableDeclaration;
|
|
182
205
|
function checkVariableDeclarationList(context, node) {
|
|
183
206
|
if ((node.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) === 0) {
|
|
184
207
|
const token = node.getFirstToken();
|
|
185
|
-
utils_1.assert(token);
|
|
186
|
-
context.diagnostics.push(diagnostics_1.unsupportedVarDeclaration(token));
|
|
208
|
+
(0, utils_1.assert)(token);
|
|
209
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedVarDeclaration)(token));
|
|
187
210
|
}
|
|
188
211
|
}
|
|
189
212
|
exports.checkVariableDeclarationList = checkVariableDeclarationList;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as lua from "../../LuaAST";
|
|
3
|
+
import { TransformationContext } from "../context";
|
|
4
|
+
import { FunctionVisitor } from "../context/visitors";
|
|
5
|
+
export declare const transformVoidExpression: FunctionVisitor<ts.VoidExpression>;
|
|
6
|
+
export declare const transformVoidExpressionStatement: (node: ts.VoidExpression, context: TransformationContext) => lua.VariableDeclarationStatement;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformVoidExpressionStatement = exports.transformVoidExpression = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const lua = require("../../LuaAST");
|
|
6
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
|
|
7
|
+
const transformVoidExpression = (node, context) => {
|
|
8
|
+
// If content is a literal it is safe to replace the entire expression with nil
|
|
9
|
+
if (!ts.isLiteralExpression(node.expression)) {
|
|
10
|
+
// local ____ = <expression>
|
|
11
|
+
context.addPrecedingStatements(lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression)));
|
|
12
|
+
}
|
|
13
|
+
return lua.createNilLiteral(node);
|
|
14
|
+
};
|
|
15
|
+
exports.transformVoidExpression = transformVoidExpression;
|
|
16
|
+
const transformVoidExpressionStatement = (node, context) =>
|
|
17
|
+
// In case of a void expression statement we can omit the IIFE
|
|
18
|
+
lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression), node);
|
|
19
|
+
exports.transformVoidExpressionStatement = transformVoidExpressionStatement;
|
|
20
|
+
//# sourceMappingURL=void.js.map
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { SourceNode } from "source-map";
|
|
1
2
|
import * as ts from "typescript";
|
|
2
3
|
import { EmitFile, ProcessedFile } from "./utils";
|
|
4
|
+
export declare const sourceMapTracebackBundlePlaceholder = "{#SourceMapTracebackBundle}";
|
|
5
|
+
export declare function printStackTraceBundleOverride(rootNode: SourceNode): string;
|
|
3
6
|
export declare function getBundleResult(program: ts.Program, files: ProcessedFile[]): [ts.Diagnostic[], EmitFile];
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getBundleResult = void 0;
|
|
3
|
+
exports.getBundleResult = exports.printStackTraceBundleOverride = exports.sourceMapTracebackBundlePlaceholder = void 0;
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const source_map_1 = require("source-map");
|
|
6
6
|
const LuaPrinter_1 = require("../LuaPrinter");
|
|
7
7
|
const utils_1 = require("../utils");
|
|
8
8
|
const diagnostics_1 = require("./diagnostics");
|
|
9
9
|
const transpiler_1 = require("./transpiler");
|
|
10
|
-
const createModulePath = (pathToResolve, program) => LuaPrinter_1.escapeString(utils_1.formatPathToLuaPath(utils_1.trimExtension(transpiler_1.getEmitPathRelativeToOutDir(pathToResolve, program))));
|
|
10
|
+
const createModulePath = (pathToResolve, program) => (0, LuaPrinter_1.escapeString)((0, utils_1.formatPathToLuaPath)((0, utils_1.trimExtension)((0, transpiler_1.getEmitPathRelativeToOutDir)(pathToResolve, program))));
|
|
11
11
|
// Override `require` to read from ____modules table.
|
|
12
12
|
const requireOverride = `
|
|
13
13
|
local ____modules = {}
|
|
@@ -30,16 +30,51 @@ local function require(file, ...)
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
`;
|
|
33
|
+
exports.sourceMapTracebackBundlePlaceholder = "{#SourceMapTracebackBundle}";
|
|
34
|
+
function printStackTraceBundleOverride(rootNode) {
|
|
35
|
+
const map = {};
|
|
36
|
+
const getLineNumber = (line, fallback) => {
|
|
37
|
+
const data = map[line];
|
|
38
|
+
if (data === undefined) {
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
if (typeof data === "number") {
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
return data.line;
|
|
45
|
+
};
|
|
46
|
+
const transformLineData = (data) => {
|
|
47
|
+
if (typeof data === "number") {
|
|
48
|
+
return data;
|
|
49
|
+
}
|
|
50
|
+
return `{line = ${data.line}, file = "${data.file}"}`;
|
|
51
|
+
};
|
|
52
|
+
let currentLine = 1;
|
|
53
|
+
rootNode.walk((chunk, mappedPosition) => {
|
|
54
|
+
if (mappedPosition.line !== undefined && mappedPosition.line > 0) {
|
|
55
|
+
const line = getLineNumber(currentLine, mappedPosition.line);
|
|
56
|
+
map[currentLine] = {
|
|
57
|
+
line,
|
|
58
|
+
file: path.basename(mappedPosition.source),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
currentLine += chunk.split("\n").length - 1;
|
|
62
|
+
});
|
|
63
|
+
const mapItems = Object.entries(map).map(([line, original]) => `["${line}"] = ${transformLineData(original)}`);
|
|
64
|
+
const mapString = "{" + mapItems.join(",") + "}";
|
|
65
|
+
return `__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${mapString});`;
|
|
66
|
+
}
|
|
67
|
+
exports.printStackTraceBundleOverride = printStackTraceBundleOverride;
|
|
33
68
|
function getBundleResult(program, files) {
|
|
34
69
|
const diagnostics = [];
|
|
35
70
|
const options = program.getCompilerOptions();
|
|
36
|
-
const bundleFile = utils_1.cast(options.luaBundle, utils_1.isNonNull);
|
|
37
|
-
const entryModule = utils_1.cast(options.luaBundleEntry, utils_1.isNonNull);
|
|
71
|
+
const bundleFile = (0, utils_1.cast)(options.luaBundle, utils_1.isNonNull);
|
|
72
|
+
const entryModule = (0, utils_1.cast)(options.luaBundleEntry, utils_1.isNonNull);
|
|
38
73
|
// Resolve project settings relative to project file.
|
|
39
|
-
const resolvedEntryModule = path.resolve(transpiler_1.getSourceDir(program), entryModule);
|
|
40
|
-
const outputPath = path.resolve(transpiler_1.getEmitOutDir(program), bundleFile);
|
|
74
|
+
const resolvedEntryModule = path.resolve((0, transpiler_1.getSourceDir)(program), entryModule);
|
|
75
|
+
const outputPath = path.resolve((0, transpiler_1.getEmitOutDir)(program), bundleFile);
|
|
41
76
|
if (program.getSourceFile(resolvedEntryModule) === undefined && program.getSourceFile(entryModule) === undefined) {
|
|
42
|
-
diagnostics.push(diagnostics_1.couldNotFindBundleEntryPoint(entryModule));
|
|
77
|
+
diagnostics.push((0, diagnostics_1.couldNotFindBundleEntryPoint)(entryModule));
|
|
43
78
|
}
|
|
44
79
|
// For each file: ["<module path>"] = function() <lua content> end,
|
|
45
80
|
const moduleTableEntries = files.map(f => moduleSourceNode(f, createModulePath(f.fileName, program)));
|
|
@@ -47,12 +82,19 @@ function getBundleResult(program, files) {
|
|
|
47
82
|
const moduleTable = createModuleTableNode(moduleTableEntries);
|
|
48
83
|
// return require("<entry module path>")
|
|
49
84
|
const entryPoint = `return require(${createModulePath(entryModule, program)}, ...)\n`;
|
|
50
|
-
const
|
|
85
|
+
const footers = [];
|
|
86
|
+
if (options.sourceMapTraceback) {
|
|
87
|
+
// Generates SourceMapTraceback for the entire file
|
|
88
|
+
footers.push('require("lualib_bundle")\n');
|
|
89
|
+
footers.push(`${exports.sourceMapTracebackBundlePlaceholder}\n`);
|
|
90
|
+
}
|
|
91
|
+
const sourceChunks = [requireOverride, moduleTable, ...footers, entryPoint];
|
|
51
92
|
if (!options.noHeader) {
|
|
52
93
|
sourceChunks.unshift(LuaPrinter_1.tstlHeader);
|
|
53
94
|
}
|
|
54
95
|
const bundleNode = joinSourceChunks(sourceChunks);
|
|
55
|
-
|
|
96
|
+
let { code, map } = bundleNode.toStringWithSourceMap();
|
|
97
|
+
code = code.replace(exports.sourceMapTracebackBundlePlaceholder, printStackTraceBundleOverride(bundleNode));
|
|
56
98
|
return [
|
|
57
99
|
diagnostics,
|
|
58
100
|
{
|
|
@@ -65,7 +107,7 @@ function getBundleResult(program, files) {
|
|
|
65
107
|
}
|
|
66
108
|
exports.getBundleResult = getBundleResult;
|
|
67
109
|
function moduleSourceNode({ code, sourceMapNode }, modulePath) {
|
|
68
|
-
const tableEntryHead = `[${modulePath}] = function(...) `;
|
|
110
|
+
const tableEntryHead = `[${modulePath}] = function(...) \n`;
|
|
69
111
|
const tableEntryTail = " end,\n";
|
|
70
112
|
return joinSourceChunks([tableEntryHead, sourceMapNode !== null && sourceMapNode !== void 0 ? sourceMapNode : code, tableEntryTail]);
|
|
71
113
|
}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.unsupportedJsxEmit = exports.cannotBundleLibrary = exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = exports.luaBundleEntryIsRequired = exports.couldNotFindBundleEntryPoint = exports.transformerShouldBeATsTransformerFactory = exports.shouldHaveAExport = exports.couldNotResolveFrom = exports.toLoadItShouldBeTranspiled = exports.couldNotReadDependency = exports.couldNotResolveRequire = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
|
-
const createDiagnosticFactory = (getMessage, category = ts.DiagnosticCategory.Error) => utils_1.createSerialDiagnosticFactory((...args) => ({ messageText: getMessage(...args), category }));
|
|
7
|
-
exports.couldNotResolveRequire = createDiagnosticFactory((requirePath, containingFile) => `Could not resolve require path '${requirePath}' in file ${containingFile}.`);
|
|
6
|
+
const createDiagnosticFactory = (getMessage, category = ts.DiagnosticCategory.Error) => (0, utils_1.createSerialDiagnosticFactory)((...args) => ({ messageText: getMessage(...args), category }));
|
|
7
|
+
exports.couldNotResolveRequire = createDiagnosticFactory((requirePath, containingFile) => `Could not resolve lua source files for require path '${requirePath}' in file ${containingFile}.`);
|
|
8
8
|
exports.couldNotReadDependency = createDiagnosticFactory((dependency) => `Could not read content of resolved dependency ${dependency}.`);
|
|
9
9
|
exports.toLoadItShouldBeTranspiled = createDiagnosticFactory((kind, transform) => `To load "${transform}" ${kind} it should be transpiled or "ts-node" should be installed.`);
|
|
10
10
|
exports.couldNotResolveFrom = createDiagnosticFactory((kind, transform, base) => `Could not resolve "${transform}" ${kind} from "${base}".`);
|
|
@@ -12,7 +12,7 @@ exports.shouldHaveAExport = createDiagnosticFactory((kind, transform, importName
|
|
|
12
12
|
exports.transformerShouldBeATsTransformerFactory = createDiagnosticFactory((transform) => `"${transform}" transformer should be a ts.TransformerFactory or an object with ts.TransformerFactory values.`);
|
|
13
13
|
exports.couldNotFindBundleEntryPoint = createDiagnosticFactory((entryPoint) => `Could not find bundle entry point '${entryPoint}'. It should be a file in the project.`);
|
|
14
14
|
exports.luaBundleEntryIsRequired = createDiagnosticFactory(() => "'luaBundleEntry' is required when 'luaBundle' is enabled.");
|
|
15
|
-
exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = utils_1.createSerialDiagnosticFactory(() => ({
|
|
15
|
+
exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = (0, utils_1.createSerialDiagnosticFactory)(() => ({
|
|
16
16
|
category: ts.DiagnosticCategory.Warning,
|
|
17
17
|
messageText: "Using 'luaBundle' with 'luaLibImport: \"inline\"' might generate duplicate code. " +
|
|
18
18
|
"It is recommended to use 'luaLibImport: \"require\"'.",
|
|
@@ -30,7 +30,7 @@ function transpileFiles(rootNames, options = {}, writeFile) {
|
|
|
30
30
|
}
|
|
31
31
|
exports.transpileFiles = transpileFiles;
|
|
32
32
|
function transpileProject(configFileName, optionsToExtend, writeFile) {
|
|
33
|
-
const parseResult = tsconfig_1.parseConfigFileWithSystem(configFileName, optionsToExtend);
|
|
33
|
+
const parseResult = (0, tsconfig_1.parseConfigFileWithSystem)(configFileName, optionsToExtend);
|
|
34
34
|
if (parseResult.errors.length > 0) {
|
|
35
35
|
return { diagnostics: parseResult.errors, emitSkipped: true };
|
|
36
36
|
}
|
|
@@ -76,7 +76,7 @@ function createVirtualProgram(input, options = {}) {
|
|
|
76
76
|
exports.createVirtualProgram = createVirtualProgram;
|
|
77
77
|
function transpileVirtualProject(files, options = {}) {
|
|
78
78
|
const program = createVirtualProgram(files, options);
|
|
79
|
-
const collector = output_collector_1.createEmitOutputCollector();
|
|
79
|
+
const collector = (0, output_collector_1.createEmitOutputCollector)();
|
|
80
80
|
const { diagnostics: transpileDiagnostics } = new transpiler_1.Transpiler().emit({ program, writeFile: collector.writeFile });
|
|
81
81
|
const diagnostics = ts.sortAndDeduplicateDiagnostics([
|
|
82
82
|
...ts.getPreEmitDiagnostics(program),
|
|
@@ -5,13 +5,13 @@ const utils_1 = require("../utils");
|
|
|
5
5
|
function createEmitOutputCollector() {
|
|
6
6
|
const files = [];
|
|
7
7
|
const writeFile = (fileName, data, _bom, _onError, sourceFiles = []) => {
|
|
8
|
-
let file = files.find(f => utils_1.intersection(f.sourceFiles, sourceFiles).length > 0);
|
|
8
|
+
let file = files.find(f => (0, utils_1.intersection)(f.sourceFiles, sourceFiles).length > 0);
|
|
9
9
|
if (!file) {
|
|
10
10
|
file = { outPath: fileName, sourceFiles: [...sourceFiles] };
|
|
11
11
|
files.push(file);
|
|
12
12
|
}
|
|
13
13
|
else {
|
|
14
|
-
file.sourceFiles = utils_1.union(file.sourceFiles, sourceFiles);
|
|
14
|
+
file.sourceFiles = (0, utils_1.union)(file.sourceFiles, sourceFiles);
|
|
15
15
|
}
|
|
16
16
|
if (fileName.endsWith(".lua")) {
|
|
17
17
|
file.lua = data;
|
|
@@ -8,7 +8,7 @@ function getPlugins(program, diagnostics, customPlugins) {
|
|
|
8
8
|
const options = program.getCompilerOptions();
|
|
9
9
|
for (const [index, pluginOption] of ((_a = options.luaPlugins) !== null && _a !== void 0 ? _a : []).entries()) {
|
|
10
10
|
const optionName = `tstl.luaPlugins[${index}]`;
|
|
11
|
-
const { error: resolveError, result: factory } = utils_1.resolvePlugin("plugin", `${optionName}.name`, utils_1.getConfigDirectory(options), pluginOption.name, pluginOption.import);
|
|
11
|
+
const { error: resolveError, result: factory } = (0, utils_1.resolvePlugin)("plugin", `${optionName}.name`, (0, utils_1.getConfigDirectory)(options), pluginOption.name, pluginOption.import);
|
|
12
12
|
if (resolveError)
|
|
13
13
|
diagnostics.push(resolveError);
|
|
14
14
|
if (factory === undefined)
|
|
@@ -21,15 +21,22 @@ class ResolutionContext {
|
|
|
21
21
|
this.options = options;
|
|
22
22
|
this.emitHost = emitHost;
|
|
23
23
|
this.resultsCache = new Map();
|
|
24
|
+
this.noResolvePaths = new Set(options.noResolvePaths);
|
|
24
25
|
}
|
|
25
26
|
resolve(file, required) {
|
|
27
|
+
if (this.noResolvePaths.has(required)) {
|
|
28
|
+
if (this.options.tstlVerbose) {
|
|
29
|
+
console.log(`Skipping module resolution of ${required} as it is in the tsconfig noResolvePaths.`);
|
|
30
|
+
}
|
|
31
|
+
return { resolvedFiles: [], diagnostics: [] };
|
|
32
|
+
}
|
|
26
33
|
const resolvedDependency = resolveDependency(file, required, this.program, this.emitHost);
|
|
27
34
|
if (resolvedDependency) {
|
|
28
35
|
if (this.options.tstlVerbose) {
|
|
29
|
-
console.log(`Resolved ${required} to ${utils_1.normalizeSlashes(resolvedDependency)}`);
|
|
36
|
+
console.log(`Resolved ${required} to ${(0, utils_1.normalizeSlashes)(resolvedDependency)}`);
|
|
30
37
|
}
|
|
31
38
|
// Figure out resolved require path and dependency output path
|
|
32
|
-
const resolvedRequire = transpiler_1.getEmitPathRelativeToOutDir(resolvedDependency, this.program);
|
|
39
|
+
const resolvedRequire = (0, transpiler_1.getEmitPathRelativeToOutDir)(resolvedDependency, this.program);
|
|
33
40
|
if (shouldRewriteRequires(resolvedDependency, this.program)) {
|
|
34
41
|
replaceRequireInCode(file, required, resolvedRequire);
|
|
35
42
|
replaceRequireInSourceMap(file, required, resolvedRequire);
|
|
@@ -37,7 +44,7 @@ class ResolutionContext {
|
|
|
37
44
|
// Check cache to prevent resolving nested dependencies double to break dependency loops
|
|
38
45
|
if (this.resultsCache.has(resolvedDependency)) {
|
|
39
46
|
if (this.options.tstlVerbose) {
|
|
40
|
-
console.log(`Resolution cache hit for ${utils_1.normalizeSlashes(resolvedDependency)}`);
|
|
47
|
+
console.log(`Resolution cache hit for ${(0, utils_1.normalizeSlashes)(resolvedDependency)}`);
|
|
41
48
|
}
|
|
42
49
|
return this.resultsCache.get(resolvedDependency);
|
|
43
50
|
}
|
|
@@ -46,7 +53,7 @@ class ResolutionContext {
|
|
|
46
53
|
// If dependency resolved successfully, read its content
|
|
47
54
|
const dependencyContent = this.emitHost.readFile(resolvedDependency);
|
|
48
55
|
if (dependencyContent === undefined) {
|
|
49
|
-
return { resolvedFiles: [], diagnostics: [diagnostics_1.couldNotReadDependency(resolvedDependency)] };
|
|
56
|
+
return { resolvedFiles: [], diagnostics: [(0, diagnostics_1.couldNotReadDependency)(resolvedDependency)] };
|
|
50
57
|
}
|
|
51
58
|
const dependency = {
|
|
52
59
|
fileName: resolvedDependency,
|
|
@@ -71,13 +78,13 @@ class ResolutionContext {
|
|
|
71
78
|
}
|
|
72
79
|
}
|
|
73
80
|
else {
|
|
74
|
-
const fallbackRequire = fallbackResolve(required, transpiler_1.getSourceDir(this.program), path.dirname(file.fileName));
|
|
81
|
+
const fallbackRequire = fallbackResolve(required, (0, transpiler_1.getSourceDir)(this.program), path.dirname(file.fileName));
|
|
75
82
|
replaceRequireInCode(file, required, fallbackRequire);
|
|
76
83
|
replaceRequireInSourceMap(file, required, fallbackRequire);
|
|
77
84
|
return {
|
|
78
85
|
resolvedFiles: [],
|
|
79
86
|
diagnostics: [
|
|
80
|
-
diagnostics_1.couldNotResolveRequire(required, path.relative(transpiler_1.getProjectRoot(this.program), file.fileName)),
|
|
87
|
+
(0, diagnostics_1.couldNotResolveRequire)(required, path.relative((0, transpiler_1.getProjectRoot)(this.program), file.fileName)),
|
|
81
88
|
],
|
|
82
89
|
};
|
|
83
90
|
}
|
|
@@ -91,7 +98,7 @@ function resolveDependencies(program, files, emitHost) {
|
|
|
91
98
|
// Resolve dependencies for all processed files
|
|
92
99
|
for (const file of files) {
|
|
93
100
|
if (options.tstlVerbose) {
|
|
94
|
-
console.log(`Resolving dependencies for ${utils_1.normalizeSlashes(file.fileName)}`);
|
|
101
|
+
console.log(`Resolving dependencies for ${(0, utils_1.normalizeSlashes)(file.fileName)}`);
|
|
95
102
|
}
|
|
96
103
|
const resolutionResult = resolveFileDependencies(file, resolutionContext);
|
|
97
104
|
outFiles.push(...resolutionResult.resolvedFiles);
|
|
@@ -135,7 +142,7 @@ function resolveDependency(requiringFile, dependency, program, emitHost) {
|
|
|
135
142
|
const options = program.getCompilerOptions();
|
|
136
143
|
const fileDirectory = path.dirname(requiringFile.fileName);
|
|
137
144
|
if (options.tstlVerbose) {
|
|
138
|
-
console.log(`Resolving "${dependency}" from ${utils_1.normalizeSlashes(fileDirectory)}`);
|
|
145
|
+
console.log(`Resolving "${dependency}" from ${(0, utils_1.normalizeSlashes)(fileDirectory)}`);
|
|
139
146
|
}
|
|
140
147
|
// Check if the import is relative
|
|
141
148
|
const isRelative = ["/", "./", "../"].some(p => dependency.startsWith(p));
|
|
@@ -237,7 +244,7 @@ function isBuildModeLibrary(program) {
|
|
|
237
244
|
function findRequiredPaths(code) {
|
|
238
245
|
// Find all require("<path>") paths in a lua code string
|
|
239
246
|
const paths = [];
|
|
240
|
-
const pattern = /(^|\s|;|=)require\("(
|
|
247
|
+
const pattern = /(^|\s|;|=)require\("(.+?)"\)/g;
|
|
241
248
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
242
249
|
let match;
|
|
243
250
|
while ((match = pattern.exec(code))) {
|
|
@@ -246,13 +253,13 @@ function findRequiredPaths(code) {
|
|
|
246
253
|
return paths;
|
|
247
254
|
}
|
|
248
255
|
function replaceRequireInCode(file, originalRequire, newRequire) {
|
|
249
|
-
const requirePath = utils_1.formatPathToLuaPath(newRequire.replace(".lua", ""));
|
|
256
|
+
const requirePath = (0, utils_1.formatPathToLuaPath)(newRequire.replace(".lua", ""));
|
|
250
257
|
// Escape special characters to prevent the regex from breaking...
|
|
251
258
|
const escapedRequire = originalRequire.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
252
259
|
file.code = file.code.replace(new RegExp(`(^|\\s|;|=)require\\("${escapedRequire}"\\)`), `$1require("${requirePath}")`);
|
|
253
260
|
}
|
|
254
261
|
function replaceRequireInSourceMap(file, originalRequire, newRequire) {
|
|
255
|
-
const requirePath = utils_1.formatPathToLuaPath(newRequire.replace(".lua", ""));
|
|
262
|
+
const requirePath = (0, utils_1.formatPathToLuaPath)(newRequire.replace(".lua", ""));
|
|
256
263
|
if (file.sourceMapNode) {
|
|
257
264
|
replaceInSourceMap(file.sourceMapNode, file.sourceMapNode, `"${originalRequire}"`, `"${requirePath}"`);
|
|
258
265
|
}
|
|
@@ -281,14 +288,14 @@ function isProjectFile(file, program) {
|
|
|
281
288
|
return program.getSourceFile(file) !== undefined;
|
|
282
289
|
}
|
|
283
290
|
function hasSourceFileInProject(filePath, program) {
|
|
284
|
-
const pathWithoutExtension = utils_1.trimExtension(filePath);
|
|
291
|
+
const pathWithoutExtension = (0, utils_1.trimExtension)(filePath);
|
|
285
292
|
return (isProjectFile(pathWithoutExtension + ".ts", program) ||
|
|
286
293
|
isProjectFile(pathWithoutExtension + ".tsx", program) ||
|
|
287
294
|
isProjectFile(pathWithoutExtension + ".json", program));
|
|
288
295
|
}
|
|
289
296
|
// Transform an import path to a lua require that is probably not correct, but can be used as fallback when regular resolution fails
|
|
290
297
|
function fallbackResolve(required, sourceRootDir, fileDir) {
|
|
291
|
-
return utils_1.formatPathToLuaPath(path
|
|
298
|
+
return (0, utils_1.formatPathToLuaPath)(path
|
|
292
299
|
.normalize(path.join(path.relative(sourceRootDir, fileDir), required))
|
|
293
300
|
.split(path.sep)
|
|
294
301
|
.filter(s => s !== "." && s !== "..")
|