typescript-to-lua 1.13.2 → 1.14.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/dist/LuaLib.d.ts CHANGED
@@ -55,6 +55,7 @@ export declare enum LuaLibFeature {
55
55
  NumberIsFinite = "NumberIsFinite",
56
56
  NumberIsNaN = "NumberIsNaN",
57
57
  NumberToString = "NumberToString",
58
+ NumberToFixed = "NumberToFixed",
58
59
  ObjectAssign = "ObjectAssign",
59
60
  ObjectDefineProperty = "ObjectDefineProperty",
60
61
  ObjectEntries = "ObjectEntries",
package/dist/LuaLib.js CHANGED
@@ -60,6 +60,7 @@ var LuaLibFeature;
60
60
  LuaLibFeature["NumberIsFinite"] = "NumberIsFinite";
61
61
  LuaLibFeature["NumberIsNaN"] = "NumberIsNaN";
62
62
  LuaLibFeature["NumberToString"] = "NumberToString";
63
+ LuaLibFeature["NumberToFixed"] = "NumberToFixed";
63
64
  LuaLibFeature["ObjectAssign"] = "ObjectAssign";
64
65
  LuaLibFeature["ObjectDefineProperty"] = "ObjectDefineProperty";
65
66
  LuaLibFeature["ObjectEntries"] = "ObjectEntries";
@@ -670,7 +670,6 @@ class LuaPrinter {
670
670
  return map;
671
671
  }
672
672
  }
673
- exports.LuaPrinter = LuaPrinter;
674
673
  LuaPrinter.operatorMap = {
675
674
  [lua.SyntaxKind.AdditionOperator]: "+",
676
675
  [lua.SyntaxKind.SubtractionOperator]: "-",
@@ -727,4 +726,5 @@ LuaPrinter.operatorPrecedence = {
727
726
  };
728
727
  LuaPrinter.rightAssociativeOperators = new Set([lua.SyntaxKind.ConcatOperator, lua.SyntaxKind.PowerOperator]);
729
728
  LuaPrinter.sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
729
+ exports.LuaPrinter = LuaPrinter;
730
730
  //# sourceMappingURL=LuaPrinter.js.map
@@ -0,0 +1,13 @@
1
+ local function __TS__NumberToFixed(self, fractionDigits)
2
+ if math.abs(self) >= 1e+21 or self ~= self then
3
+ return tostring(self)
4
+ end
5
+ local f = math.floor(fractionDigits or 0)
6
+ if f < 0 or f > 99 then
7
+ error("toFixed() digits argument must be between 0 and 99", 0)
8
+ end
9
+ return string.format(
10
+ ("%." .. tostring(f)) .. "f",
11
+ self
12
+ )
13
+ end
@@ -1498,6 +1498,20 @@ do
1498
1498
  end
1499
1499
  end
1500
1500
 
1501
+ local function __TS__NumberToFixed(self, fractionDigits)
1502
+ if math.abs(self) >= 1e+21 or self ~= self then
1503
+ return tostring(self)
1504
+ end
1505
+ local f = math.floor(fractionDigits or 0)
1506
+ if f < 0 or f > 99 then
1507
+ error("toFixed() digits argument must be between 0 and 99", 0)
1508
+ end
1509
+ return string.format(
1510
+ ("%." .. tostring(f)) .. "f",
1511
+ self
1512
+ )
1513
+ end
1514
+
1501
1515
  local function __TS__ObjectDefineProperty(target, key, desc)
1502
1516
  local luaKey = type(key) == "number" and key + 1 or key
1503
1517
  local value = rawget(target, luaKey)
@@ -2467,6 +2481,7 @@ return {
2467
2481
  __TS__NumberIsFinite = __TS__NumberIsFinite,
2468
2482
  __TS__NumberIsNaN = __TS__NumberIsNaN,
2469
2483
  __TS__NumberToString = __TS__NumberToString,
2484
+ __TS__NumberToFixed = __TS__NumberToFixed,
2470
2485
  __TS__ObjectAssign = __TS__ObjectAssign,
2471
2486
  __TS__ObjectDefineProperty = __TS__ObjectDefineProperty,
2472
2487
  __TS__ObjectEntries = __TS__ObjectEntries,
@@ -348,6 +348,11 @@
348
348
  "MathModf"
349
349
  ]
350
350
  },
351
+ "NumberToFixed": {
352
+ "exports": [
353
+ "__TS__NumberToFixed"
354
+ ]
355
+ },
351
356
  "ObjectAssign": {
352
357
  "exports": [
353
358
  "__TS__ObjectAssign"
@@ -0,0 +1,13 @@
1
+ local function __TS__NumberToFixed(self, fractionDigits)
2
+ if math.abs(self) >= 1e+21 or self ~= self then
3
+ return tostring(self)
4
+ end
5
+ local f = math.floor(fractionDigits or 0)
6
+ if f < 0 or f > 99 then
7
+ error("toFixed() digits argument must be between 0 and 99", 0)
8
+ end
9
+ return string.format(
10
+ ("%." .. tostring(f)) .. "f",
11
+ self
12
+ )
13
+ end
@@ -1440,6 +1440,20 @@ do
1440
1440
  end
1441
1441
  end
1442
1442
 
1443
+ local function __TS__NumberToFixed(self, fractionDigits)
1444
+ if math.abs(self) >= 1e+21 or self ~= self then
1445
+ return tostring(self)
1446
+ end
1447
+ local f = math.floor(fractionDigits or 0)
1448
+ if f < 0 or f > 99 then
1449
+ error("toFixed() digits argument must be between 0 and 99", 0)
1450
+ end
1451
+ return string.format(
1452
+ ("%." .. tostring(f)) .. "f",
1453
+ self
1454
+ )
1455
+ end
1456
+
1443
1457
  local function __TS__ObjectDefineProperty(target, key, desc)
1444
1458
  local luaKey = type(key) == "number" and key + 1 or key
1445
1459
  local value = rawget(target, luaKey)
@@ -2412,6 +2426,7 @@ return {
2412
2426
  __TS__NumberIsFinite = __TS__NumberIsFinite,
2413
2427
  __TS__NumberIsNaN = __TS__NumberIsNaN,
2414
2428
  __TS__NumberToString = __TS__NumberToString,
2429
+ __TS__NumberToFixed = __TS__NumberToFixed,
2415
2430
  __TS__ObjectAssign = __TS__ObjectAssign,
2416
2431
  __TS__ObjectDefineProperty = __TS__ObjectDefineProperty,
2417
2432
  __TS__ObjectEntries = __TS__ObjectEntries,
@@ -348,6 +348,11 @@
348
348
  "MathModf"
349
349
  ]
350
350
  },
351
+ "NumberToFixed": {
352
+ "exports": [
353
+ "__TS__NumberToFixed"
354
+ ]
355
+ },
351
356
  "ObjectAssign": {
352
357
  "exports": [
353
358
  "__TS__ObjectAssign"
@@ -15,6 +15,8 @@ function transformNumberPrototypeCall(context, node, calledMethod) {
15
15
  return params.length === 0
16
16
  ? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
17
17
  : (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberToString, node, caller, ...params);
18
+ case "toFixed":
19
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberToFixed, node, caller, ...params);
18
20
  default:
19
21
  context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "number", expressionName));
20
22
  }
@@ -94,7 +94,13 @@ function reduceContextTypes(contexts) {
94
94
  return type;
95
95
  }
96
96
  function getSignatureDeclarations(context, signature) {
97
+ if (signature.compositeSignatures) {
98
+ return signature.compositeSignatures.flatMap(s => getSignatureDeclarations(context, s));
99
+ }
97
100
  const signatureDeclaration = signature.getDeclaration();
101
+ if (signatureDeclaration === undefined) {
102
+ return [];
103
+ }
98
104
  let inferredType;
99
105
  if (ts.isMethodDeclaration(signatureDeclaration) &&
100
106
  ts.isObjectLiteralExpression(signatureDeclaration.parent) &&
@@ -30,7 +30,12 @@ function isInAsyncFunction(node) {
30
30
  if (!declaration) {
31
31
  return false;
32
32
  }
33
- return (_b = (_a = declaration.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
33
+ if (ts.canHaveModifiers(declaration)) {
34
+ return (_b = (_a = ts.getModifiers(declaration)) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
35
+ }
36
+ else {
37
+ return false;
38
+ }
34
39
  }
35
40
  exports.isInAsyncFunction = isInAsyncFunction;
36
41
  function isInGeneratorFunction(node) {
@@ -9,6 +9,7 @@ const index_1 = require("./index");
9
9
  const assignments_1 = require("./assignments");
10
10
  const destructuring_assignments_1 = require("./destructuring-assignments");
11
11
  const lualib_1 = require("../../utils/lualib");
12
+ const diagnostics_1 = require("../../utils/diagnostics");
12
13
  function isLuaExpressionWithSideEffect(expression) {
13
14
  return !(lua.isLiteral(expression) || lua.isIdentifier(expression));
14
15
  }
@@ -43,7 +44,11 @@ function transformCompoundAssignment(context, expression, lhs, rhs, operator, is
43
44
  const { precedingStatements, result: lengthSetterStatement } = transformCompoundLengthSetter(context, expression, lhs, rhs, operator);
44
45
  return { precedingStatements, result: lengthSetterStatement.expression };
45
46
  }
46
- const left = (0, utils_1.cast)(context.transformExpression(lhs), lua.isAssignmentLeftHandSideExpression);
47
+ const left = context.transformExpression(lhs);
48
+ if (!lua.isAssignmentLeftHandSideExpression(left)) {
49
+ context.diagnostics.push((0, diagnostics_1.cannotAssignToNodeOfKind)(expression, left.kind));
50
+ return { precedingStatements: [], result: left };
51
+ }
47
52
  const { precedingStatements: rightPrecedingStatements, result: right } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(rhs));
48
53
  if (lua.isTableIndexExpression(left)) {
49
54
  // Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
@@ -128,7 +133,11 @@ function transformCompoundAssignmentStatement(context, node, lhs, rhs, operator)
128
133
  const { precedingStatements, result: lengthSetterStatement } = transformCompoundLengthSetter(context, node, lhs, rhs, operator);
129
134
  return [...precedingStatements, lengthSetterStatement];
130
135
  }
131
- const left = (0, utils_1.cast)(context.transformExpression(lhs), lua.isAssignmentLeftHandSideExpression);
136
+ const left = context.transformExpression(lhs);
137
+ if (!lua.isAssignmentLeftHandSideExpression(left)) {
138
+ context.diagnostics.push((0, diagnostics_1.cannotAssignToNodeOfKind)(node, left.kind));
139
+ return [];
140
+ }
132
141
  const { precedingStatements: rightPrecedingStatements, result: right } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(rhs));
133
142
  if (lua.isTableIndexExpression(left) && shouldCacheTableIndexExpressions(left, rightPrecedingStatements)) {
134
143
  // Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformNewExpression = void 0;
4
- const ts = require("typescript");
5
4
  const lua = require("../../../LuaAST");
6
5
  const annotations_1 = require("../../utils/annotations");
7
6
  const diagnostics_1 = require("../../utils/diagnostics");
@@ -14,7 +13,7 @@ const transformNewExpression = (node, context) => {
14
13
  return lua.createTableExpression(undefined, node);
15
14
  }
16
15
  const signature = context.checker.getResolvedSignature(node);
17
- const [name, params] = (0, call_1.transformCallAndArguments)(context, node.expression, (_a = node.arguments) !== null && _a !== void 0 ? _a : [ts.factory.createTrue()], signature);
16
+ const [name, params] = (0, call_1.transformCallAndArguments)(context, node.expression, (_a = node.arguments) !== null && _a !== void 0 ? _a : [], signature);
18
17
  const type = context.checker.getTypeAtLocation(node);
19
18
  const annotations = (0, annotations_1.getTypeAnnotations)(type);
20
19
  const customConstructorAnnotation = annotations.get(annotations_1.AnnotationKind.CustomConstructor);
@@ -31,7 +31,8 @@ function isOptimizedVarArgSpread(context, symbol, identifier) {
31
31
  return false;
32
32
  }
33
33
  // Scope cannot be an async function
34
- if ((_a = scope.node.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) {
34
+ if (ts.canHaveModifiers(scope.node) &&
35
+ ((_a = ts.getModifiers(scope.node)) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword))) {
35
36
  return false;
36
37
  }
37
38
  // Identifier must be a vararg in the local function scope's parameters
@@ -66,7 +66,7 @@ const stripParenthesisExpressionsTransformer = context => sourceFile => {
66
66
  }
67
67
  return ts.visitEachChild(node, visit, context);
68
68
  }
69
- return ts.visitNode(sourceFile, visit);
69
+ return ts.visitEachChild(sourceFile, visit, context);
70
70
  };
71
71
  exports.stripParenthesisExpressionsTransformer = stripParenthesisExpressionsTransformer;
72
72
  function loadTransformersFromOptions(program, diagnostics) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.13.2",
3
+ "version": "1.14.0",
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/",
@@ -42,7 +42,7 @@
42
42
  "node": ">=16.10.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "typescript": "~4.9.3"
45
+ "typescript": "^5.0.2"
46
46
  },
47
47
  "dependencies": {
48
48
  "@typescript-to-lua/language-extensions": "1.0.0",
@@ -56,20 +56,20 @@
56
56
  "@types/jest": "^27.5.2",
57
57
  "@types/node": "^13.7.7",
58
58
  "@types/resolve": "1.14.0",
59
- "@typescript-eslint/eslint-plugin": "^5.52.0",
60
- "@typescript-eslint/parser": "^5.52.0",
61
- "eslint": "^8.34.0",
59
+ "@typescript-eslint/eslint-plugin": "^5.55.0",
60
+ "@typescript-eslint/parser": "^5.55.0",
61
+ "eslint": "^8.36.0",
62
62
  "eslint-plugin-import": "^2.27.5",
63
63
  "eslint-plugin-jest": "^26.9.0",
64
64
  "fs-extra": "^8.1.0",
65
65
  "javascript-stringify": "^2.0.1",
66
66
  "jest": "^28.1.3",
67
- "jest-circus": "^29.4.2",
67
+ "jest-circus": "^29.5.0",
68
68
  "lua-types": "^2.13.0",
69
69
  "lua-wasm-bindings": "^0.3.1",
70
- "prettier": "^2.3.2",
70
+ "prettier": "^2.8.4",
71
71
  "ts-jest": "^28.0.8",
72
72
  "ts-node": "^10.9.1",
73
- "typescript": "~4.9.3"
73
+ "typescript": "^5.0.2"
74
74
  }
75
75
  }