typescript-to-lua 1.13.1 → 1.13.2

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.
@@ -55,6 +55,7 @@ function transformSingleElementArrayPush(context, node, caller, param) {
55
55
  return expressionIsUsed ? lengthExpression : lua.createNilLiteral();
56
56
  }
57
57
  function transformArrayPrototypeCall(context, node, calledMethod) {
58
+ var _a, _b, _c;
58
59
  const signature = context.checker.getResolvedSignature(node);
59
60
  const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature);
60
61
  const expressionName = calledMethod.name.text;
@@ -65,9 +66,9 @@ function transformArrayPrototypeCall(context, node, calledMethod) {
65
66
  return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayEntries, node, caller);
66
67
  case "push":
67
68
  if (node.arguments.length === 1) {
68
- const param = params[0];
69
+ const param = (_a = params[0]) !== null && _a !== void 0 ? _a : lua.createNilLiteral();
69
70
  if ((0, lua_ast_1.isUnpackCall)(param)) {
70
- return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayPushArray, node, caller, param.params[0]);
71
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayPushArray, node, caller, (_b = param.params[0]) !== null && _b !== void 0 ? _b : lua.createNilLiteral());
71
72
  }
72
73
  if (!lua.isDotsLiteral(param)) {
73
74
  return transformSingleElementArrayPush(context, node, caller, param);
@@ -116,7 +117,7 @@ function transformArrayPrototypeCall(context, node, calledMethod) {
116
117
  if (elementType &&
117
118
  (0, typescript_1.typeAlwaysHasSomeOfFlags)(context, elementType, ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike)) {
118
119
  const defaultSeparatorLiteral = lua.createStringLiteral(",");
119
- const param = params[0];
120
+ const param = (_c = params[0]) !== null && _c !== void 0 ? _c : lua.createNilLiteral();
120
121
  const parameters = [
121
122
  caller,
122
123
  node.arguments.length === 0
@@ -27,6 +27,7 @@ function transformMathProperty(context, node) {
27
27
  }
28
28
  exports.transformMathProperty = transformMathProperty;
29
29
  function transformMathCall(context, node, calledMethod) {
30
+ var _a, _b, _c, _d;
30
31
  const signature = context.checker.getResolvedSignature(node);
31
32
  const params = (0, call_1.transformArguments)(context, node.arguments, signature);
32
33
  const math = lua.createIdentifier("math");
@@ -53,18 +54,18 @@ function transformMathCall(context, node, calledMethod) {
53
54
  case "log1p": {
54
55
  const log = lua.createStringLiteral("log");
55
56
  const one = lua.createNumericLiteral(1);
56
- const add = lua.createBinaryExpression(one, params[0], lua.SyntaxKind.AdditionOperator);
57
+ const add = lua.createBinaryExpression(one, (_a = params[0]) !== null && _a !== void 0 ? _a : lua.createNilLiteral(), lua.SyntaxKind.AdditionOperator);
57
58
  return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
58
59
  }
59
60
  case "pow": {
60
61
  // Translate to base ^ power
61
- return lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.PowerOperator, node);
62
+ return lua.createBinaryExpression((_b = params[0]) !== null && _b !== void 0 ? _b : lua.createNilLiteral(), (_c = params[1]) !== null && _c !== void 0 ? _c : lua.createNilLiteral(), lua.SyntaxKind.PowerOperator, node);
62
63
  }
63
64
  // math.floor(x + 0.5)
64
65
  case "round": {
65
66
  const floor = lua.createStringLiteral("floor");
66
67
  const half = lua.createNumericLiteral(0.5);
67
- const add = lua.createBinaryExpression(params[0], half, lua.SyntaxKind.AdditionOperator);
68
+ const add = lua.createBinaryExpression((_d = params[0]) !== null && _d !== void 0 ? _d : lua.createNilLiteral(), half, lua.SyntaxKind.AdditionOperator);
68
69
  return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
69
70
  }
70
71
  case "sign": {
@@ -12,6 +12,7 @@ function createStringCall(methodName, tsOriginal, ...params) {
12
12
  return lua.createCallExpression(lua.createTableIndexExpression(stringIdentifier, lua.createStringLiteral(methodName)), params, tsOriginal);
13
13
  }
14
14
  function transformStringPrototypeCall(context, node, calledMethod) {
15
+ var _a, _b, _c, _d;
15
16
  const signature = context.checker.getResolvedSignature(node);
16
17
  const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature);
17
18
  const expressionName = calledMethod.name.text;
@@ -23,7 +24,7 @@ function transformStringPrototypeCall(context, node, calledMethod) {
23
24
  case "concat":
24
25
  return lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("concat")), [(0, lua_ast_1.wrapInTable)(caller, ...params)], node);
25
26
  case "indexOf": {
26
- const stringExpression = createStringCall("find", node, caller, params[0], params[1]
27
+ const stringExpression = createStringCall("find", node, caller, (_a = params[0]) !== null && _a !== void 0 ? _a : lua.createNilLiteral(), params[1]
27
28
  ? // string.find handles negative indexes by making it relative to string end, but for indexOf it's the same as 0
28
29
  lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("math"), lua.createStringLiteral("max")), [(0, lua_ast_1.addToNumericExpression)(params[1], 1), lua.createNumericLiteral(1)])
29
30
  : lua.createNilLiteral(), lua.createBooleanLiteral(true));
@@ -73,7 +74,7 @@ function transformStringPrototypeCall(context, node, calledMethod) {
73
74
  const literalValue = (0, lua_ast_1.getNumberLiteralValue)(params[0]);
74
75
  // Inline string.sub call if we know that parameter is pure and isn't negative
75
76
  if (literalValue !== undefined && literalValue >= 0) {
76
- const firstParamPlusOne = (0, lua_ast_1.addToNumericExpression)(params[0], 1);
77
+ const firstParamPlusOne = (0, lua_ast_1.addToNumericExpression)((_b = params[0]) !== null && _b !== void 0 ? _b : lua.createNilLiteral(), 1);
77
78
  return createStringCall("sub", node, caller, firstParamPlusOne, firstParamPlusOne);
78
79
  }
79
80
  return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringCharAt, node, caller, ...params);
@@ -82,7 +83,7 @@ function transformStringPrototypeCall(context, node, calledMethod) {
82
83
  const literalValue = (0, lua_ast_1.getNumberLiteralValue)(params[0]);
83
84
  // Inline string.sub call if we know that parameter is pure and isn't negative
84
85
  if (literalValue !== undefined && literalValue >= 0) {
85
- return lua.createBinaryExpression(createStringCall("byte", node, caller, (0, lua_ast_1.addToNumericExpression)(params[0], 1)), (0, lua_ast_1.createNaN)(), lua.SyntaxKind.OrOperator);
86
+ return lua.createBinaryExpression(createStringCall("byte", node, caller, (0, lua_ast_1.addToNumericExpression)((_c = params[0]) !== null && _c !== void 0 ? _c : lua.createNilLiteral(), 1)), (0, lua_ast_1.createNaN)(), lua.SyntaxKind.OrOperator);
86
87
  }
87
88
  return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringCharCodeAt, node, caller, ...params);
88
89
  }
@@ -95,7 +96,9 @@ function transformStringPrototypeCall(context, node, calledMethod) {
95
96
  case "repeat":
96
97
  const math = lua.createIdentifier("math");
97
98
  const floor = lua.createStringLiteral("floor");
98
- const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [params[0]]);
99
+ const parameter = lua.createCallExpression(lua.createTableIndexExpression(math, floor), [
100
+ (_d = params[0]) !== null && _d !== void 0 ? _d : lua.createNilLiteral(),
101
+ ]);
99
102
  return createStringCall("rep", node, caller, parameter);
100
103
  case "padStart":
101
104
  return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringPadStart, node, caller, ...params);
@@ -1,4 +1,5 @@
1
1
  import * as ts from "typescript";
2
+ import * as lua from "../../LuaAST";
2
3
  import { LuaTarget, TypeScriptToLuaOptions } from "../../CompilerOptions";
3
4
  import { AnnotationKind } from "./annotations";
4
5
  export declare const unsupportedNodeKind: ((node: ts.Node, kind: ts.SyntaxKind) => ts.Diagnostic) & {
@@ -97,3 +98,6 @@ export declare const invalidMethodCallExtensionUse: ((node: ts.Node, ...args: an
97
98
  export declare const invalidSpreadInCallExtension: ((node: ts.Node, ...args: any[]) => ts.Diagnostic) & {
98
99
  code: number;
99
100
  };
101
+ export declare const cannotAssignToNodeOfKind: ((node: ts.Node, kind: lua.SyntaxKind) => ts.Diagnostic) & {
102
+ code: number;
103
+ };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.invalidSpreadInCallExtension = exports.invalidMethodCallExtensionUse = exports.undefinedInArrayLiteral = exports.unsupportedOptionalCompileMembersOnly = exports.unsupportedBuiltinOptionalCall = exports.awaitMustBeInAsyncFunction = exports.notAllowedOptionalAssignment = exports.truthyOnlyConditionalValue = exports.annotationDeprecated = exports.invalidCallExtensionUse = exports.invalidMultiReturnAccess = exports.invalidMultiFunctionReturnType = exports.invalidMultiFunctionUse = exports.unsupportedVarDeclaration = exports.invalidAmbientIdentifierName = exports.unsupportedProperty = exports.unsupportedForTargetButOverrideAvailable = exports.unsupportedForTarget = exports.unsupportedRightShiftOperator = exports.unsupportedAccessorInObjectLiteral = exports.invalidPairsIterableWithoutDestructuring = exports.invalidMultiIterableWithoutDestructuring = exports.invalidRangeControlVariable = exports.invalidVarargUse = exports.invalidRangeUse = exports.annotationInvalidArgumentCount = exports.decoratorInvalidContext = exports.unsupportedOverloadAssignment = exports.unsupportedSelfFunctionConversion = exports.unsupportedNoSelfFunctionConversion = exports.forbiddenForIn = exports.unsupportedNodeKind = void 0;
3
+ exports.cannotAssignToNodeOfKind = exports.invalidSpreadInCallExtension = exports.invalidMethodCallExtensionUse = exports.undefinedInArrayLiteral = exports.unsupportedOptionalCompileMembersOnly = exports.unsupportedBuiltinOptionalCall = exports.awaitMustBeInAsyncFunction = exports.notAllowedOptionalAssignment = exports.truthyOnlyConditionalValue = exports.annotationDeprecated = exports.invalidCallExtensionUse = exports.invalidMultiReturnAccess = exports.invalidMultiFunctionReturnType = exports.invalidMultiFunctionUse = exports.unsupportedVarDeclaration = exports.invalidAmbientIdentifierName = exports.unsupportedProperty = exports.unsupportedForTargetButOverrideAvailable = exports.unsupportedForTarget = exports.unsupportedRightShiftOperator = exports.unsupportedAccessorInObjectLiteral = exports.invalidPairsIterableWithoutDestructuring = exports.invalidMultiIterableWithoutDestructuring = exports.invalidRangeControlVariable = exports.invalidVarargUse = exports.invalidRangeUse = exports.annotationInvalidArgumentCount = exports.decoratorInvalidContext = exports.unsupportedOverloadAssignment = exports.unsupportedSelfFunctionConversion = exports.unsupportedNoSelfFunctionConversion = exports.forbiddenForIn = exports.unsupportedNodeKind = void 0;
4
4
  const ts = require("typescript");
5
+ const lua = require("../../LuaAST");
5
6
  const CompilerOptions_1 = require("../../CompilerOptions");
6
7
  const utils_1 = require("../../utils");
7
8
  const createDiagnosticFactory = (category, message) => (0, utils_1.createSerialDiagnosticFactory)((node, ...args) => ({
@@ -61,4 +62,5 @@ exports.unsupportedOptionalCompileMembersOnly = createErrorDiagnosticFactory("Op
61
62
  exports.undefinedInArrayLiteral = createErrorDiagnosticFactory("Array literals may not contain undefined or null.");
62
63
  exports.invalidMethodCallExtensionUse = createErrorDiagnosticFactory("This language extension must be called as a method.");
63
64
  exports.invalidSpreadInCallExtension = createErrorDiagnosticFactory("Spread elements are not supported in call extensions.");
65
+ exports.cannotAssignToNodeOfKind = createErrorDiagnosticFactory((kind) => `Cannot create assignment assigning to a node of type ${lua.SyntaxKind[kind]}.`);
64
66
  //# sourceMappingURL=diagnostics.js.map
@@ -251,15 +251,17 @@ function getJSDocCommentFromTSNode(context, tsOriginal) {
251
251
  // https://stevedonovan.github.io/ldoc/manual/doc.md.html
252
252
  // LDoc comments require that the first line starts with three hyphens.
253
253
  // Thus, need to add a hyphen to the first line.
254
- const firstLine = lines[0];
255
- if (firstLine.startsWith(" @")) {
256
- lines.unshift("-");
257
- }
258
- else {
259
- lines.shift();
260
- lines.unshift("-" + firstLine);
254
+ if (lines.length > 0) {
255
+ const firstLine = lines[0];
256
+ if (firstLine.startsWith(" @")) {
257
+ lines.unshift("-");
258
+ }
259
+ else {
260
+ lines.shift();
261
+ lines.unshift("-" + firstLine);
262
+ }
263
+ return lines;
261
264
  }
262
- return lines;
263
265
  }
264
266
  const createNaN = (tsOriginal) => lua.createBinaryExpression(lua.createNumericLiteral(0), lua.createNumericLiteral(0), lua.SyntaxKind.DivisionOperator, tsOriginal);
265
267
  exports.createNaN = createNaN;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformAssignmentStatement = exports.transformAssignmentExpression = exports.transformAssignmentWithRightPrecedingStatements = exports.transformAssignment = exports.transformAssignmentLeftHandSideExpression = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../../LuaAST");
6
- const utils_1 = require("../../../utils");
7
6
  const assignment_validation_1 = require("../../utils/assignment-validation");
8
7
  const export_1 = require("../../utils/export");
9
8
  const lua_ast_1 = require("../../utils/lua-ast");
@@ -32,9 +31,16 @@ function transformAssignmentLeftHandSideExpression(context, node, rightHasPreced
32
31
  }
33
32
  const symbol = context.checker.getSymbolAtLocation(node);
34
33
  const left = context.transformExpression(node);
35
- return lua.isIdentifier(left) && symbol && (0, export_1.isSymbolExported)(context, symbol)
36
- ? (0, export_1.createExportedIdentifier)(context, left)
37
- : (0, utils_1.cast)(left, lua.isAssignmentLeftHandSideExpression);
34
+ if (lua.isIdentifier(left) && symbol && (0, export_1.isSymbolExported)(context, symbol)) {
35
+ return (0, export_1.createExportedIdentifier)(context, left);
36
+ }
37
+ if (lua.isAssignmentLeftHandSideExpression(left)) {
38
+ return left;
39
+ }
40
+ else {
41
+ context.diagnostics.push((0, diagnostics_1.cannotAssignToNodeOfKind)(node, left.kind));
42
+ return lua.createAnonymousIdentifier();
43
+ }
38
44
  }
39
45
  exports.transformAssignmentLeftHandSideExpression = transformAssignmentLeftHandSideExpression;
40
46
  function transformAssignment(context,
@@ -50,35 +50,36 @@ function shouldMultiReturnCallBeWrapped(context, node) {
50
50
  if (!returnsMultiType(context, node)) {
51
51
  return false;
52
52
  }
53
+ const parent = (0, typescript_1.findFirstNonOuterParent)(node);
53
54
  // Variable declaration with destructuring
54
- if (ts.isVariableDeclaration(node.parent) && ts.isArrayBindingPattern(node.parent.name)) {
55
+ if (ts.isVariableDeclaration(parent) && ts.isArrayBindingPattern(parent.name)) {
55
56
  return false;
56
57
  }
57
58
  // Variable assignment with destructuring
58
- if (ts.isBinaryExpression(node.parent) &&
59
- node.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
60
- ts.isArrayLiteralExpression(node.parent.left)) {
59
+ if (ts.isBinaryExpression(parent) &&
60
+ parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
61
+ ts.isArrayLiteralExpression(parent.left)) {
61
62
  return false;
62
63
  }
63
64
  // Spread operator
64
- if (ts.isSpreadElement(node.parent)) {
65
+ if (ts.isSpreadElement(parent)) {
65
66
  return false;
66
67
  }
67
68
  // Stand-alone expression
68
- if (ts.isExpressionStatement(node.parent)) {
69
+ if (ts.isExpressionStatement(parent)) {
69
70
  return false;
70
71
  }
71
72
  // Forwarded multi-return call
72
- if ((ts.isReturnStatement(node.parent) || ts.isArrowFunction(node.parent)) && // Body-less arrow func
73
+ if ((ts.isReturnStatement(parent) || ts.isArrowFunction(parent)) && // Body-less arrow func
73
74
  isInMultiReturnFunction(context, node)) {
74
75
  return false;
75
76
  }
76
77
  // Element access expression 'foo()[0]' will be optimized using 'select'
77
- if (ts.isElementAccessExpression(node.parent)) {
78
+ if (ts.isElementAccessExpression(parent)) {
78
79
  return false;
79
80
  }
80
81
  // LuaIterable in for...of
81
- if (ts.isForOfStatement(node.parent) &&
82
+ if (ts.isForOfStatement(parent) &&
82
83
  (0, language_extensions_1.getIterableExtensionKindForNode)(context, node) === language_extensions_1.IterableExtensionKind.Iterable) {
83
84
  return false;
84
85
  }
@@ -4,6 +4,7 @@ exports.transformSpreadElement = exports.isOptimizedVarArgSpread = void 0;
4
4
  const ts = require("typescript");
5
5
  const CompilerOptions_1 = require("../../CompilerOptions");
6
6
  const lua = require("../../LuaAST");
7
+ const utils_1 = require("../../utils");
7
8
  const language_extensions_1 = require("../utils/language-extensions");
8
9
  const lua_ast_1 = require("../utils/lua-ast");
9
10
  const lualib_1 = require("../utils/lualib");
@@ -63,10 +64,24 @@ const transformSpreadElement = (node, context) => {
63
64
  const innerExpression = context.transformExpression(node.expression);
64
65
  if ((0, multi_1.isMultiReturnCall)(context, tsInnerExpression))
65
66
  return innerExpression;
66
- const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
67
- if ((0, language_extensions_1.isLuaIterable)(context, type)) {
68
- return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.LuaIteratorSpread, node, innerExpression);
67
+ const iterableExtensionType = (0, language_extensions_1.getIterableExtensionKindForNode)(context, node.expression);
68
+ if (iterableExtensionType) {
69
+ if (iterableExtensionType === language_extensions_1.IterableExtensionKind.Iterable) {
70
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.LuaIteratorSpread, node, innerExpression);
71
+ }
72
+ else if (iterableExtensionType === language_extensions_1.IterableExtensionKind.Pairs) {
73
+ const objectEntries = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectEntries, node, innerExpression);
74
+ return (0, lua_ast_1.createUnpackCall)(context, objectEntries, node);
75
+ }
76
+ else if (iterableExtensionType === language_extensions_1.IterableExtensionKind.PairsKey) {
77
+ const objectKeys = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectKeys, node, innerExpression);
78
+ return (0, lua_ast_1.createUnpackCall)(context, objectKeys, node);
79
+ }
80
+ else {
81
+ (0, utils_1.assertNever)(iterableExtensionType);
82
+ }
69
83
  }
84
+ const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
70
85
  if ((0, typescript_1.isArrayType)(context, type)) {
71
86
  return (0, lua_ast_1.createUnpackCall)(context, innerExpression, node);
72
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.13.1",
3
+ "version": "1.13.2",
4
4
  "description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
5
5
  "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
6
6
  "homepage": "https://typescripttolua.github.io/",