typescript-to-lua 1.13.3 → 1.15.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/LuaAST.d.ts CHANGED
@@ -32,31 +32,32 @@ export declare enum SyntaxKind {
32
32
  MethodCallExpression = 28,
33
33
  Identifier = 29,
34
34
  TableIndexExpression = 30,
35
- AdditionOperator = 31,
36
- SubtractionOperator = 32,
37
- MultiplicationOperator = 33,
38
- DivisionOperator = 34,
39
- FloorDivisionOperator = 35,
40
- ModuloOperator = 36,
41
- PowerOperator = 37,
42
- NegationOperator = 38,
43
- ConcatOperator = 39,
44
- LengthOperator = 40,
45
- EqualityOperator = 41,
46
- InequalityOperator = 42,
47
- LessThanOperator = 43,
48
- LessEqualOperator = 44,
49
- GreaterThanOperator = 45,
50
- GreaterEqualOperator = 46,
51
- AndOperator = 47,
52
- OrOperator = 48,
53
- NotOperator = 49,
54
- BitwiseAndOperator = 50,
55
- BitwiseOrOperator = 51,
56
- BitwiseExclusiveOrOperator = 52,
57
- BitwiseRightShiftOperator = 53,
58
- BitwiseLeftShiftOperator = 54,
59
- BitwiseNotOperator = 55
35
+ ParenthesizedExpression = 31,
36
+ AdditionOperator = 32,
37
+ SubtractionOperator = 33,
38
+ MultiplicationOperator = 34,
39
+ DivisionOperator = 35,
40
+ FloorDivisionOperator = 36,
41
+ ModuloOperator = 37,
42
+ PowerOperator = 38,
43
+ NegationOperator = 39,
44
+ ConcatOperator = 40,
45
+ LengthOperator = 41,
46
+ EqualityOperator = 42,
47
+ InequalityOperator = 43,
48
+ LessThanOperator = 44,
49
+ LessEqualOperator = 45,
50
+ GreaterThanOperator = 46,
51
+ GreaterEqualOperator = 47,
52
+ AndOperator = 48,
53
+ OrOperator = 49,
54
+ NotOperator = 50,
55
+ BitwiseAndOperator = 51,
56
+ BitwiseOrOperator = 52,
57
+ BitwiseExclusiveOrOperator = 53,
58
+ BitwiseRightShiftOperator = 54,
59
+ BitwiseLeftShiftOperator = 55,
60
+ BitwiseNotOperator = 56
60
61
  }
61
62
  export type UnaryBitwiseOperator = SyntaxKind.BitwiseNotOperator;
62
63
  export type UnaryOperator = SyntaxKind.NegationOperator | SyntaxKind.LengthOperator | SyntaxKind.NotOperator | UnaryBitwiseOperator;
@@ -314,3 +315,8 @@ export type InlineFunctionExpression = FunctionExpression & {
314
315
  };
315
316
  };
316
317
  export declare function isInlineFunctionExpression(expression: FunctionExpression): expression is InlineFunctionExpression;
318
+ export type ParenthesizedExpression = Expression & {
319
+ expression: Expression;
320
+ };
321
+ export declare function isParenthesizedExpression(node: Node): node is ParenthesizedExpression;
322
+ export declare function createParenthesizedExpression(expression: Expression, tsOriginal?: ts.Node): ParenthesizedExpression;
package/dist/LuaAST.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // https://www.lua.org/manual/5.3/manual.html#9 and the TS AST implementation
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.isStringLiteral = exports.createNumericLiteral = exports.isNumericLiteral = exports.createArgLiteral = exports.isArgLiteral = exports.createDotsLiteral = exports.isDotsLiteral = exports.createBooleanLiteral = exports.isBooleanLiteral = exports.createNilLiteral = exports.isNilLiteral = exports.createExpressionStatement = exports.isExpressionStatement = exports.createBreakStatement = exports.isBreakStatement = exports.createReturnStatement = exports.isReturnStatement = exports.createLabelStatement = exports.isLabelStatement = exports.createGotoStatement = exports.isGotoStatement = exports.createForInStatement = exports.isForInStatement = exports.createForStatement = exports.isForStatement = exports.createRepeatStatement = exports.isRepeatStatement = exports.createWhileStatement = exports.isWhileStatement = exports.isIterationStatement = exports.createIfStatement = exports.isIfStatement = exports.createAssignmentStatement = exports.isAssignmentStatement = exports.createVariableDeclarationStatement = exports.isVariableDeclarationStatement = exports.createDoStatement = exports.isDoStatement = exports.createBlock = exports.isBlock = exports.createFile = exports.isFile = exports.setNodeFlags = exports.getOriginalPos = exports.setNodeOriginal = exports.setNodePosition = exports.cloneNode = exports.createNode = exports.NodeFlags = exports.SyntaxKind = void 0;
6
- exports.isInlineFunctionExpression = exports.isFunctionDefinition = exports.isAssignmentLeftHandSideExpression = exports.createTableIndexExpression = exports.isTableIndexExpression = exports.createAnonymousIdentifier = exports.cloneIdentifier = exports.createIdentifier = exports.isIdentifier = exports.createMethodCallExpression = exports.isMethodCallExpression = exports.createCallExpression = exports.isCallExpression = exports.createBinaryExpression = exports.isBinaryExpression = exports.createUnaryExpression = exports.isUnaryExpression = exports.createTableExpression = exports.isTableExpression = exports.createTableFieldExpression = exports.isTableFieldExpression = exports.createFunctionExpression = exports.isFunctionExpression = exports.isLiteral = exports.createStringLiteral = void 0;
6
+ exports.createParenthesizedExpression = exports.isParenthesizedExpression = exports.isInlineFunctionExpression = exports.isFunctionDefinition = exports.isAssignmentLeftHandSideExpression = exports.createTableIndexExpression = exports.isTableIndexExpression = exports.createAnonymousIdentifier = exports.cloneIdentifier = exports.createIdentifier = exports.isIdentifier = exports.createMethodCallExpression = exports.isMethodCallExpression = exports.createCallExpression = exports.isCallExpression = exports.createBinaryExpression = exports.isBinaryExpression = exports.createUnaryExpression = exports.isUnaryExpression = exports.createTableExpression = exports.isTableExpression = exports.createTableFieldExpression = exports.isTableFieldExpression = exports.createFunctionExpression = exports.isFunctionExpression = exports.isLiteral = exports.createStringLiteral = void 0;
7
7
  // We can elide a lot of nodes especially tokens and keywords
8
8
  // because we don't create the AST from text
9
9
  const ts = require("typescript");
@@ -43,40 +43,41 @@ var SyntaxKind;
43
43
  SyntaxKind[SyntaxKind["MethodCallExpression"] = 28] = "MethodCallExpression";
44
44
  SyntaxKind[SyntaxKind["Identifier"] = 29] = "Identifier";
45
45
  SyntaxKind[SyntaxKind["TableIndexExpression"] = 30] = "TableIndexExpression";
46
+ SyntaxKind[SyntaxKind["ParenthesizedExpression"] = 31] = "ParenthesizedExpression";
46
47
  // Operators
47
48
  // Arithmetic
48
- SyntaxKind[SyntaxKind["AdditionOperator"] = 31] = "AdditionOperator";
49
- SyntaxKind[SyntaxKind["SubtractionOperator"] = 32] = "SubtractionOperator";
50
- SyntaxKind[SyntaxKind["MultiplicationOperator"] = 33] = "MultiplicationOperator";
51
- SyntaxKind[SyntaxKind["DivisionOperator"] = 34] = "DivisionOperator";
52
- SyntaxKind[SyntaxKind["FloorDivisionOperator"] = 35] = "FloorDivisionOperator";
53
- SyntaxKind[SyntaxKind["ModuloOperator"] = 36] = "ModuloOperator";
54
- SyntaxKind[SyntaxKind["PowerOperator"] = 37] = "PowerOperator";
55
- SyntaxKind[SyntaxKind["NegationOperator"] = 38] = "NegationOperator";
49
+ SyntaxKind[SyntaxKind["AdditionOperator"] = 32] = "AdditionOperator";
50
+ SyntaxKind[SyntaxKind["SubtractionOperator"] = 33] = "SubtractionOperator";
51
+ SyntaxKind[SyntaxKind["MultiplicationOperator"] = 34] = "MultiplicationOperator";
52
+ SyntaxKind[SyntaxKind["DivisionOperator"] = 35] = "DivisionOperator";
53
+ SyntaxKind[SyntaxKind["FloorDivisionOperator"] = 36] = "FloorDivisionOperator";
54
+ SyntaxKind[SyntaxKind["ModuloOperator"] = 37] = "ModuloOperator";
55
+ SyntaxKind[SyntaxKind["PowerOperator"] = 38] = "PowerOperator";
56
+ SyntaxKind[SyntaxKind["NegationOperator"] = 39] = "NegationOperator";
56
57
  // Concat
57
- SyntaxKind[SyntaxKind["ConcatOperator"] = 39] = "ConcatOperator";
58
+ SyntaxKind[SyntaxKind["ConcatOperator"] = 40] = "ConcatOperator";
58
59
  // Length
59
- SyntaxKind[SyntaxKind["LengthOperator"] = 40] = "LengthOperator";
60
+ SyntaxKind[SyntaxKind["LengthOperator"] = 41] = "LengthOperator";
60
61
  // Relational Ops
61
- SyntaxKind[SyntaxKind["EqualityOperator"] = 41] = "EqualityOperator";
62
- SyntaxKind[SyntaxKind["InequalityOperator"] = 42] = "InequalityOperator";
63
- SyntaxKind[SyntaxKind["LessThanOperator"] = 43] = "LessThanOperator";
64
- SyntaxKind[SyntaxKind["LessEqualOperator"] = 44] = "LessEqualOperator";
62
+ SyntaxKind[SyntaxKind["EqualityOperator"] = 42] = "EqualityOperator";
63
+ SyntaxKind[SyntaxKind["InequalityOperator"] = 43] = "InequalityOperator";
64
+ SyntaxKind[SyntaxKind["LessThanOperator"] = 44] = "LessThanOperator";
65
+ SyntaxKind[SyntaxKind["LessEqualOperator"] = 45] = "LessEqualOperator";
65
66
  // Syntax Sugar `x > y` <=> `not (y <= x)`
66
67
  // but we should probably use them to make the output code more readable
67
- SyntaxKind[SyntaxKind["GreaterThanOperator"] = 45] = "GreaterThanOperator";
68
- SyntaxKind[SyntaxKind["GreaterEqualOperator"] = 46] = "GreaterEqualOperator";
68
+ SyntaxKind[SyntaxKind["GreaterThanOperator"] = 46] = "GreaterThanOperator";
69
+ SyntaxKind[SyntaxKind["GreaterEqualOperator"] = 47] = "GreaterEqualOperator";
69
70
  // Logical
70
- SyntaxKind[SyntaxKind["AndOperator"] = 47] = "AndOperator";
71
- SyntaxKind[SyntaxKind["OrOperator"] = 48] = "OrOperator";
72
- SyntaxKind[SyntaxKind["NotOperator"] = 49] = "NotOperator";
71
+ SyntaxKind[SyntaxKind["AndOperator"] = 48] = "AndOperator";
72
+ SyntaxKind[SyntaxKind["OrOperator"] = 49] = "OrOperator";
73
+ SyntaxKind[SyntaxKind["NotOperator"] = 50] = "NotOperator";
73
74
  // Bitwise
74
- SyntaxKind[SyntaxKind["BitwiseAndOperator"] = 50] = "BitwiseAndOperator";
75
- SyntaxKind[SyntaxKind["BitwiseOrOperator"] = 51] = "BitwiseOrOperator";
76
- SyntaxKind[SyntaxKind["BitwiseExclusiveOrOperator"] = 52] = "BitwiseExclusiveOrOperator";
77
- SyntaxKind[SyntaxKind["BitwiseRightShiftOperator"] = 53] = "BitwiseRightShiftOperator";
78
- SyntaxKind[SyntaxKind["BitwiseLeftShiftOperator"] = 54] = "BitwiseLeftShiftOperator";
79
- SyntaxKind[SyntaxKind["BitwiseNotOperator"] = 55] = "BitwiseNotOperator";
75
+ SyntaxKind[SyntaxKind["BitwiseAndOperator"] = 51] = "BitwiseAndOperator";
76
+ SyntaxKind[SyntaxKind["BitwiseOrOperator"] = 52] = "BitwiseOrOperator";
77
+ SyntaxKind[SyntaxKind["BitwiseExclusiveOrOperator"] = 53] = "BitwiseExclusiveOrOperator";
78
+ SyntaxKind[SyntaxKind["BitwiseRightShiftOperator"] = 54] = "BitwiseRightShiftOperator";
79
+ SyntaxKind[SyntaxKind["BitwiseLeftShiftOperator"] = 55] = "BitwiseLeftShiftOperator";
80
+ SyntaxKind[SyntaxKind["BitwiseNotOperator"] = 56] = "BitwiseNotOperator";
80
81
  })(SyntaxKind = exports.SyntaxKind || (exports.SyntaxKind = {}));
81
82
  var NodeFlags;
82
83
  (function (NodeFlags) {
@@ -500,4 +501,14 @@ function isInlineFunctionExpression(expression) {
500
501
  (expression.flags & NodeFlags.Inline) !== 0);
501
502
  }
502
503
  exports.isInlineFunctionExpression = isInlineFunctionExpression;
504
+ function isParenthesizedExpression(node) {
505
+ return node.kind === SyntaxKind.ParenthesizedExpression;
506
+ }
507
+ exports.isParenthesizedExpression = isParenthesizedExpression;
508
+ function createParenthesizedExpression(expression, tsOriginal) {
509
+ const parenthesizedExpression = createNode(SyntaxKind.ParenthesizedExpression, tsOriginal);
510
+ parenthesizedExpression.expression = expression;
511
+ return parenthesizedExpression;
512
+ }
513
+ exports.createParenthesizedExpression = createParenthesizedExpression;
503
514
  //# sourceMappingURL=LuaAST.js.map
@@ -75,6 +75,7 @@ export declare class LuaPrinter {
75
75
  printMethodCallExpression(expression: lua.MethodCallExpression): SourceNode;
76
76
  printIdentifier(expression: lua.Identifier): SourceNode;
77
77
  printTableIndexExpression(expression: lua.TableIndexExpression): SourceNode;
78
+ printParenthesizedExpression(expression: lua.ParenthesizedExpression): SourceNode;
78
79
  printOperator(kind: lua.Operator): SourceNode;
79
80
  protected joinChunksWithComma(chunks: SourceChunk[]): SourceChunk[];
80
81
  /**
@@ -430,6 +430,8 @@ class LuaPrinter {
430
430
  return this.printIdentifier(expression);
431
431
  case lua.SyntaxKind.TableIndexExpression:
432
432
  return this.printTableIndexExpression(expression);
433
+ case lua.SyntaxKind.ParenthesizedExpression:
434
+ return this.printParenthesizedExpression(expression);
433
435
  default:
434
436
  throw new Error(`Tried to print unknown statement kind: ${lua.SyntaxKind[expression.kind]}`);
435
437
  }
@@ -582,6 +584,9 @@ class LuaPrinter {
582
584
  }
583
585
  return this.createSourceNode(expression, chunks);
584
586
  }
587
+ printParenthesizedExpression(expression) {
588
+ return this.createSourceNode(expression, ["(", this.printExpression(expression.expression), ")"]);
589
+ }
585
590
  printOperator(kind) {
586
591
  return new source_map_1.SourceNode(null, null, this.relativeSourcePath, LuaPrinter.operatorMap[kind]);
587
592
  }
@@ -670,7 +675,6 @@ class LuaPrinter {
670
675
  return map;
671
676
  }
672
677
  }
673
- exports.LuaPrinter = LuaPrinter;
674
678
  LuaPrinter.operatorMap = {
675
679
  [lua.SyntaxKind.AdditionOperator]: "+",
676
680
  [lua.SyntaxKind.SubtractionOperator]: "-",
@@ -727,4 +731,5 @@ LuaPrinter.operatorPrecedence = {
727
731
  };
728
732
  LuaPrinter.rightAssociativeOperators = new Set([lua.SyntaxKind.ConcatOperator, lua.SyntaxKind.PowerOperator]);
729
733
  LuaPrinter.sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
734
+ exports.LuaPrinter = LuaPrinter;
730
735
  //# sourceMappingURL=LuaPrinter.js.map
@@ -38,10 +38,45 @@ function locateConfigFile(commandLine) {
38
38
  }
39
39
  exports.locateConfigFile = locateConfigFile;
40
40
  function parseConfigFileWithSystem(configFileName, commandLineOptions, system = ts.sys) {
41
- const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(configFileName, system.readFile), system, path.dirname(configFileName), commandLineOptions, configFileName);
41
+ var _a;
42
+ const configRootDir = path.dirname(configFileName);
43
+ const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(ts.readJsonConfigFile(configFileName, system.readFile), system, configRootDir, commandLineOptions, configFileName);
44
+ const cycleCache = new Set();
45
+ const extendedTstlOptions = getExtendedTstlOptions(configFileName, configRootDir, cycleCache, system);
46
+ parsedConfigFile.raw.tstl = Object.assign(extendedTstlOptions, (_a = parsedConfigFile.raw.tstl) !== null && _a !== void 0 ? _a : {});
42
47
  return (0, parse_1.updateParsedConfigFile)(parsedConfigFile);
43
48
  }
44
49
  exports.parseConfigFileWithSystem = parseConfigFileWithSystem;
50
+ function getExtendedTstlOptions(configFilePath, configRootDir, cycleCache, system) {
51
+ const absolutePath = path.isAbsolute(configFilePath) ? configFilePath : path.resolve(configRootDir, configFilePath);
52
+ const newConfigRoot = path.dirname(absolutePath);
53
+ if (cycleCache.has(absolutePath)) {
54
+ return {};
55
+ }
56
+ cycleCache.add(absolutePath);
57
+ const fileContent = system.readFile(absolutePath);
58
+ const options = {};
59
+ if (fileContent) {
60
+ const { config: parsedConfig } = ts.parseConfigFileTextToJson(configFilePath, fileContent);
61
+ if (!parsedConfig) {
62
+ return {};
63
+ }
64
+ if (parsedConfig.extends) {
65
+ if (Array.isArray(parsedConfig.extends)) {
66
+ for (const extendedConfigFile of parsedConfig.extends) {
67
+ Object.assign(options, getExtendedTstlOptions(extendedConfigFile, newConfigRoot, cycleCache, system));
68
+ }
69
+ }
70
+ else {
71
+ Object.assign(options, getExtendedTstlOptions(parsedConfig.extends, newConfigRoot, cycleCache, system));
72
+ }
73
+ }
74
+ if (parsedConfig.tstl) {
75
+ Object.assign(options, parsedConfig.tstl);
76
+ }
77
+ }
78
+ return options;
79
+ }
45
80
  function createConfigFileUpdater(optionsToExtend) {
46
81
  const configFileMap = new WeakMap();
47
82
  return options => {
@@ -3,6 +3,6 @@ local function __TS__ParseFloat(numberString)
3
3
  if infinityMatch ~= nil then
4
4
  return __TS__StringAccess(infinityMatch, 0) == "-" and -(1 / 0) or 1 / 0
5
5
  end
6
- local number = tonumber(__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))
6
+ local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)")))
7
7
  return number or 0 / 0
8
8
  end
@@ -7,7 +7,7 @@ do
7
7
  local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]")
8
8
  if hexMatch ~= nil then
9
9
  base = 16
10
- numberString = __TS__Match(hexMatch, "-") and "-" .. __TS__StringSubstring(
10
+ numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(
11
11
  numberString,
12
12
  string.len(hexMatch)
13
13
  ) or __TS__StringSubstring(
@@ -21,10 +21,7 @@ do
21
21
  end
22
22
  local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10))
23
23
  local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
24
- local number = tonumber(
25
- __TS__Match(numberString, pattern),
26
- base
27
- )
24
+ local number = tonumber((__TS__Match(numberString, pattern)), base)
28
25
  if number == nil then
29
26
  return 0 / 0
30
27
  end
@@ -35,7 +35,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
35
35
  local function stringReplacer(____, file, line)
36
36
  local fileSourceMap = _G.__TS__sourcemap[file]
37
37
  if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then
38
- local chunkName = __TS__Match(file, "%[string \"([^\"]+)\"%]")
38
+ local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]"))
39
39
  local sourceName = string.gsub(chunkName, ".lua$", ".ts")
40
40
  local data = fileSourceMap[line]
41
41
  if type(data) == "number" then
@@ -1632,7 +1632,7 @@ local function __TS__ParseFloat(numberString)
1632
1632
  if infinityMatch ~= nil then
1633
1633
  return __TS__StringAccess(infinityMatch, 0) == "-" and -(1 / 0) or 1 / 0
1634
1634
  end
1635
- local number = tonumber(__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))
1635
+ local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)")))
1636
1636
  return number or 0 / 0
1637
1637
  end
1638
1638
 
@@ -1663,7 +1663,7 @@ do
1663
1663
  local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]")
1664
1664
  if hexMatch ~= nil then
1665
1665
  base = 16
1666
- numberString = __TS__Match(hexMatch, "-") and "-" .. __TS__StringSubstring(
1666
+ numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(
1667
1667
  numberString,
1668
1668
  string.len(hexMatch)
1669
1669
  ) or __TS__StringSubstring(
@@ -1677,10 +1677,7 @@ do
1677
1677
  end
1678
1678
  local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10))
1679
1679
  local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
1680
- local number = tonumber(
1681
- __TS__Match(numberString, pattern),
1682
- base
1683
- )
1680
+ local number = tonumber((__TS__Match(numberString, pattern)), base)
1684
1681
  if number == nil then
1685
1682
  return 0 / 0
1686
1683
  end
@@ -2133,7 +2130,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
2133
2130
  local function stringReplacer(____, file, line)
2134
2131
  local fileSourceMap = _G.__TS__sourcemap[file]
2135
2132
  if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then
2136
- local chunkName = __TS__Match(file, "%[string \"([^\"]+)\"%]")
2133
+ local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]"))
2137
2134
  local sourceName = string.gsub(chunkName, ".lua$", ".ts")
2138
2135
  local data = fileSourceMap[line]
2139
2136
  if type(data) == "number" then
@@ -3,6 +3,6 @@ local function __TS__ParseFloat(numberString)
3
3
  if infinityMatch ~= nil then
4
4
  return __TS__StringAccess(infinityMatch, 0) == "-" and -math.huge or math.huge
5
5
  end
6
- local number = tonumber(__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))
6
+ local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)")))
7
7
  return number or 0 / 0
8
8
  end
@@ -7,7 +7,7 @@ do
7
7
  local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]")
8
8
  if hexMatch ~= nil then
9
9
  base = 16
10
- numberString = __TS__Match(hexMatch, "-") and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch)
10
+ numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch)
11
11
  end
12
12
  end
13
13
  if base < 2 or base > 36 then
@@ -15,10 +15,7 @@ do
15
15
  end
16
16
  local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10))
17
17
  local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
18
- local number = tonumber(
19
- __TS__Match(numberString, pattern),
20
- base
21
- )
18
+ local number = tonumber((__TS__Match(numberString, pattern)), base)
22
19
  if number == nil then
23
20
  return 0 / 0
24
21
  end
@@ -35,7 +35,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
35
35
  local function stringReplacer(____, file, line)
36
36
  local fileSourceMap = _G.__TS__sourcemap[file]
37
37
  if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then
38
- local chunkName = __TS__Match(file, "%[string \"([^\"]+)\"%]")
38
+ local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]"))
39
39
  local sourceName = string.gsub(chunkName, ".lua$", ".ts")
40
40
  local data = fileSourceMap[line]
41
41
  if type(data) == "number" then
@@ -1574,7 +1574,7 @@ local function __TS__ParseFloat(numberString)
1574
1574
  if infinityMatch ~= nil then
1575
1575
  return __TS__StringAccess(infinityMatch, 0) == "-" and -math.huge or math.huge
1576
1576
  end
1577
- local number = tonumber(__TS__Match(numberString, "^%s*(-?%d+%.?%d*)"))
1577
+ local number = tonumber((__TS__Match(numberString, "^%s*(-?%d+%.?%d*)")))
1578
1578
  return number or 0 / 0
1579
1579
  end
1580
1580
 
@@ -1605,7 +1605,7 @@ do
1605
1605
  local hexMatch = __TS__Match(numberString, "^%s*-?0[xX]")
1606
1606
  if hexMatch ~= nil then
1607
1607
  base = 16
1608
- numberString = __TS__Match(hexMatch, "-") and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch)
1608
+ numberString = (__TS__Match(hexMatch, "-")) and "-" .. __TS__StringSubstring(numberString, #hexMatch) or __TS__StringSubstring(numberString, #hexMatch)
1609
1609
  end
1610
1610
  end
1611
1611
  if base < 2 or base > 36 then
@@ -1613,10 +1613,7 @@ do
1613
1613
  end
1614
1614
  local allowedDigits = base <= 10 and __TS__StringSubstring(parseIntBasePattern, 0, base) or __TS__StringSubstring(parseIntBasePattern, 0, 10 + 2 * (base - 10))
1615
1615
  local pattern = ("^%s*(-?[" .. allowedDigits) .. "]*)"
1616
- local number = tonumber(
1617
- __TS__Match(numberString, pattern),
1618
- base
1619
- )
1616
+ local number = tonumber((__TS__Match(numberString, pattern)), base)
1620
1617
  if number == nil then
1621
1618
  return 0 / 0
1622
1619
  end
@@ -2070,7 +2067,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
2070
2067
  local function stringReplacer(____, file, line)
2071
2068
  local fileSourceMap = _G.__TS__sourcemap[file]
2072
2069
  if fileSourceMap ~= nil and fileSourceMap[line] ~= nil then
2073
- local chunkName = __TS__Match(file, "%[string \"([^\"]+)\"%]")
2070
+ local chunkName = (__TS__Match(file, "%[string \"([^\"]+)\"%]"))
2074
2071
  local sourceName = string.gsub(chunkName, ".lua$", ".ts")
2075
2072
  local data = fileSourceMap[line]
2076
2073
  if type(data) == "number" then
@@ -103,7 +103,7 @@ function createExportedIdentifier(context, identifier, exportScope) {
103
103
  return identifier;
104
104
  }
105
105
  const exportTable = exportScope && ts.isModuleDeclaration(exportScope)
106
- ? (0, namespace_1.createModuleLocalNameIdentifier)(context, exportScope)
106
+ ? (0, namespace_1.createModuleLocalName)(context, exportScope)
107
107
  : (0, lua_ast_1.createExportsIdentifier)();
108
108
  return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text));
109
109
  }
@@ -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) {
@@ -53,14 +53,14 @@ function transformElementAccessExpressionWithCapture(context, node, thisValueCap
53
53
  if (!(0, typescript_1.isNumberType)(context, accessType)) {
54
54
  context.diagnostics.push((0, diagnostics_1.invalidMultiReturnAccess)(node));
55
55
  }
56
- // When selecting the first element, we can shortcut
57
- if (ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0") {
58
- return { expression: table };
59
- }
60
- else {
61
- const selectIdentifier = lua.createIdentifier("select");
62
- return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) };
56
+ const canOmitSelect = ts.isNumericLiteral(node.argumentExpression) && node.argumentExpression.text === "0";
57
+ if (canOmitSelect) {
58
+ // wrapping in parenthesis ensures only the first return value is used
59
+ // https://www.lua.org/manual/5.1/manual.html#2.5
60
+ return { expression: lua.createParenthesizedExpression(table) };
63
61
  }
62
+ const selectIdentifier = lua.createIdentifier("select");
63
+ return { expression: lua.createCallExpression(selectIdentifier, [updatedAccessExpression, table]) };
64
64
  }
65
65
  if (thisValueCapture) {
66
66
  const thisValue = (0, optional_chaining_1.captureThisValue)(context, table, thisValueCapture, node.expression);
@@ -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);
@@ -18,10 +18,14 @@ const identifier_1 = require("./identifier");
18
18
  const return_1 = require("./return");
19
19
  const variable_declaration_1 = require("./variable-declaration");
20
20
  function transformParameterDefaultValueDeclaration(context, parameterName, value, tsOriginal) {
21
- const parameterValue = value ? context.transformExpression(value) : undefined;
22
- const assignment = lua.createAssignmentStatement(parameterName, parameterValue);
21
+ const { precedingStatements: statements, result: parameterValue } = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(value));
22
+ if (!lua.isNilLiteral(parameterValue)) {
23
+ statements.push(lua.createAssignmentStatement(parameterName, parameterValue));
24
+ }
25
+ if (statements.length === 0)
26
+ return undefined;
23
27
  const nilCondition = lua.createBinaryExpression(parameterName, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator);
24
- const ifBlock = lua.createBlock([assignment]);
28
+ const ifBlock = lua.createBlock(statements, tsOriginal);
25
29
  return lua.createIfStatement(nilCondition, ifBlock, undefined, tsOriginal);
26
30
  }
27
31
  function isRestParameterReferenced(identifier, scope) {
@@ -86,7 +90,9 @@ function transformFunctionBodyHeader(context, bodyScope, parameters, spreadIdent
86
90
  const identifier = lua.createIdentifier(`____bindingPattern${bindPatternIndex++}`);
87
91
  if (declaration.initializer !== undefined) {
88
92
  // Default binding parameter
89
- headerStatements.push(transformParameterDefaultValueDeclaration(context, identifier, declaration.initializer));
93
+ const initializer = transformParameterDefaultValueDeclaration(context, identifier, declaration.initializer);
94
+ if (initializer)
95
+ headerStatements.push(initializer);
90
96
  }
91
97
  // Binding pattern
92
98
  const name = declaration.name;
@@ -95,7 +101,9 @@ function transformFunctionBodyHeader(context, bodyScope, parameters, spreadIdent
95
101
  }
96
102
  else if (declaration.initializer !== undefined) {
97
103
  // Default parameter
98
- headerStatements.push(transformParameterDefaultValueDeclaration(context, (0, identifier_1.transformIdentifier)(context, declaration.name), declaration.initializer));
104
+ const initializer = transformParameterDefaultValueDeclaration(context, (0, identifier_1.transformIdentifier)(context, declaration.name), declaration.initializer);
105
+ if (initializer)
106
+ headerStatements.push(initializer);
99
107
  }
100
108
  }
101
109
  // Push spread operator here
@@ -1,5 +1,6 @@
1
1
  import * as ts from "typescript";
2
2
  import * as lua from "../../LuaAST";
3
3
  import { FunctionVisitor, TransformationContext } from "../context";
4
+ export declare function createModuleLocalName(context: TransformationContext, module: ts.ModuleDeclaration): lua.Expression;
4
5
  export declare function createModuleLocalNameIdentifier(context: TransformationContext, declaration: ts.ModuleDeclaration): lua.Identifier;
5
6
  export declare const transformModuleDeclaration: FunctionVisitor<ts.ModuleDeclaration>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformModuleDeclaration = exports.createModuleLocalNameIdentifier = void 0;
3
+ exports.transformModuleDeclaration = exports.createModuleLocalNameIdentifier = exports.createModuleLocalName = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
6
  const export_1 = require("../utils/export");
@@ -9,12 +9,20 @@ const safe_names_1 = require("../utils/safe-names");
9
9
  const scope_1 = require("../utils/scope");
10
10
  const symbols_1 = require("../utils/symbols");
11
11
  const identifier_1 = require("./identifier");
12
+ function createModuleLocalName(context, module) {
13
+ if (!ts.isSourceFile(module.parent) && ts.isModuleDeclaration(module.parent)) {
14
+ const parentDeclaration = createModuleLocalName(context, module.parent);
15
+ const name = createModuleLocalNameIdentifier(context, module);
16
+ return lua.createTableIndexExpression(parentDeclaration, lua.createStringLiteral(name.text), module.name);
17
+ }
18
+ return createModuleLocalNameIdentifier(context, module);
19
+ }
20
+ exports.createModuleLocalName = createModuleLocalName;
12
21
  function createModuleLocalNameIdentifier(context, declaration) {
13
22
  const moduleSymbol = context.checker.getSymbolAtLocation(declaration.name);
14
23
  if (moduleSymbol !== undefined && (0, safe_names_1.isUnsafeName)(moduleSymbol.name, context.options)) {
15
24
  return lua.createIdentifier((0, safe_names_1.createSafeName)(declaration.name.text), declaration.name, moduleSymbol && (0, symbols_1.getSymbolIdOfSymbol)(context, moduleSymbol), declaration.name.text);
16
25
  }
17
- // TODO: Should synthetic name nodes be escaped as well?
18
26
  return (0, identifier_1.transformIdentifier)(context, declaration.name);
19
27
  }
20
28
  exports.createModuleLocalNameIdentifier = createModuleLocalNameIdentifier;
@@ -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.3",
3
+ "version": "1.15.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
- "jest": "^28.1.3",
67
- "jest-circus": "^29.4.2",
66
+ "jest": "^29.5.0",
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",
71
- "ts-jest": "^28.0.8",
70
+ "prettier": "^2.8.4",
71
+ "ts-jest": "^29.1.0",
72
72
  "ts-node": "^10.9.1",
73
- "typescript": "~4.9.3"
73
+ "typescript": "^5.0.2"
74
74
  }
75
75
  }