typescript-to-lua 1.1.0 → 1.3.1

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/LuaAST.d.ts +1 -0
  3. package/dist/LuaAST.js +10 -2
  4. package/dist/LuaLib.d.ts +3 -3
  5. package/dist/LuaLib.js +5 -5
  6. package/dist/LuaPrinter.d.ts +11 -4
  7. package/dist/LuaPrinter.js +58 -15
  8. package/dist/cli/parse.d.ts +1 -1
  9. package/dist/cli/parse.js +14 -0
  10. package/dist/lualib/ArrayFlat.lua +1 -1
  11. package/dist/lualib/ArrayFlatMap.lua +1 -1
  12. package/dist/lualib/ArrayIsArray.lua +1 -1
  13. package/dist/lualib/ArrayReduce.lua +7 -1
  14. package/dist/lualib/ArrayReduceRight.lua +7 -1
  15. package/dist/lualib/ArraySetLength.lua +1 -1
  16. package/dist/lualib/ArraySplice.lua +5 -5
  17. package/dist/lualib/Await.lua +21 -13
  18. package/dist/lualib/CloneDescriptor.lua +7 -7
  19. package/dist/lualib/Decorate.lua +1 -1
  20. package/dist/lualib/DelegatedYield.lua +1 -3
  21. package/dist/lualib/Delete.lua +6 -6
  22. package/dist/lualib/Error.lua +47 -44
  23. package/dist/lualib/Generator.lua +1 -5
  24. package/dist/lualib/InstanceOf.lua +1 -1
  25. package/dist/lualib/InstanceOfObject.lua +1 -1
  26. package/dist/lualib/Map.lua +130 -134
  27. package/dist/lualib/Number.lua +1 -1
  28. package/dist/lualib/NumberIsFinite.lua +1 -1
  29. package/dist/lualib/NumberToString.lua +3 -5
  30. package/dist/lualib/ObjectDefineProperty.lua +40 -8
  31. package/dist/lualib/ParseFloat.lua +9 -5
  32. package/dist/lualib/ParseInt.lua +15 -3
  33. package/dist/lualib/Promise.lua +28 -24
  34. package/dist/lualib/Set.lua +119 -123
  35. package/dist/lualib/SetDescriptor.lua +7 -1
  36. package/dist/lualib/SourceMapTraceBack.lua +1 -1
  37. package/dist/lualib/SparseArrayNew.lua +5 -0
  38. package/dist/lualib/SparseArrayPush.lua +9 -0
  39. package/dist/lualib/SparseArraySpread.lua +4 -0
  40. package/dist/lualib/StringAccess.lua +1 -1
  41. package/dist/lualib/StringCharCodeAt.lua +1 -1
  42. package/dist/lualib/StringEndsWith.lua +2 -2
  43. package/dist/lualib/StringPadEnd.lua +2 -2
  44. package/dist/lualib/StringPadStart.lua +2 -2
  45. package/dist/lualib/StringReplace.lua +7 -1
  46. package/dist/lualib/StringSlice.lua +2 -2
  47. package/dist/lualib/StringSplit.lua +3 -3
  48. package/dist/lualib/StringStartsWith.lua +1 -1
  49. package/dist/lualib/StringSubstr.lua +1 -1
  50. package/dist/lualib/StringSubstring.lua +2 -2
  51. package/dist/lualib/Symbol.lua +3 -5
  52. package/dist/lualib/WeakMap.lua +40 -42
  53. package/dist/lualib/WeakSet.lua +36 -38
  54. package/dist/lualib/lualib_bundle.lua +795 -735
  55. package/dist/transformation/builtins/array.js +1 -2
  56. package/dist/transformation/builtins/function.js +6 -4
  57. package/dist/transformation/builtins/index.d.ts +1 -1
  58. package/dist/transformation/builtins/index.js +41 -6
  59. package/dist/transformation/builtins/object.d.ts +2 -1
  60. package/dist/transformation/builtins/object.js +1 -2
  61. package/dist/transformation/builtins/string.js +1 -2
  62. package/dist/transformation/context/context.d.ts +12 -0
  63. package/dist/transformation/context/context.js +102 -3
  64. package/dist/transformation/utils/diagnostics.d.ts +9 -6
  65. package/dist/transformation/utils/diagnostics.js +4 -3
  66. package/dist/transformation/utils/export.d.ts +3 -2
  67. package/dist/transformation/utils/export.js +10 -3
  68. package/dist/transformation/utils/language-extensions.d.ts +2 -0
  69. package/dist/transformation/utils/language-extensions.js +7 -1
  70. package/dist/transformation/utils/lua-ast.d.ts +0 -1
  71. package/dist/transformation/utils/lua-ast.js +14 -23
  72. package/dist/transformation/utils/preceding-statements.d.ts +3 -0
  73. package/dist/transformation/utils/preceding-statements.js +11 -0
  74. package/dist/transformation/utils/safe-names.d.ts +4 -2
  75. package/dist/transformation/utils/safe-names.js +11 -5
  76. package/dist/transformation/utils/scope.d.ts +3 -1
  77. package/dist/transformation/utils/scope.js +9 -1
  78. package/dist/transformation/utils/typescript/index.d.ts +1 -0
  79. package/dist/transformation/utils/typescript/index.js +16 -1
  80. package/dist/transformation/utils/typescript/types.d.ts +3 -1
  81. package/dist/transformation/utils/typescript/types.js +40 -13
  82. package/dist/transformation/visitors/access.d.ts +3 -0
  83. package/dist/transformation/visitors/access.js +63 -36
  84. package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
  85. package/dist/transformation/visitors/binary-expression/assignments.js +66 -57
  86. package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
  87. package/dist/transformation/visitors/binary-expression/compound.js +66 -68
  88. package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
  89. package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +54 -25
  90. package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
  91. package/dist/transformation/visitors/binary-expression/index.js +65 -23
  92. package/dist/transformation/visitors/call.d.ts +3 -2
  93. package/dist/transformation/visitors/call.js +95 -141
  94. package/dist/transformation/visitors/class/index.js +13 -9
  95. package/dist/transformation/visitors/class/new.js +4 -6
  96. package/dist/transformation/visitors/conditional.js +30 -37
  97. package/dist/transformation/visitors/delete.js +4 -0
  98. package/dist/transformation/visitors/errors.js +11 -9
  99. package/dist/transformation/visitors/expression-list.d.ts +7 -0
  100. package/dist/transformation/visitors/expression-list.js +120 -0
  101. package/dist/transformation/visitors/expression-statement.d.ts +3 -1
  102. package/dist/transformation/visitors/expression-statement.js +21 -19
  103. package/dist/transformation/visitors/function.d.ts +2 -0
  104. package/dist/transformation/visitors/function.js +63 -13
  105. package/dist/transformation/visitors/identifier.js +5 -0
  106. package/dist/transformation/visitors/language-extensions/index.d.ts +4 -0
  107. package/dist/transformation/visitors/language-extensions/index.js +17 -0
  108. package/dist/transformation/visitors/language-extensions/operators.d.ts +1 -1
  109. package/dist/transformation/visitors/language-extensions/operators.js +7 -2
  110. package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +5 -0
  111. package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
  112. package/dist/transformation/visitors/language-extensions/table.d.ts +1 -8
  113. package/dist/transformation/visitors/language-extensions/table.js +55 -53
  114. package/dist/transformation/visitors/literal.js +69 -16
  115. package/dist/transformation/visitors/loops/do-while.js +34 -8
  116. package/dist/transformation/visitors/loops/for-of.js +4 -0
  117. package/dist/transformation/visitors/loops/for.js +25 -5
  118. package/dist/transformation/visitors/loops/utils.d.ts +1 -0
  119. package/dist/transformation/visitors/loops/utils.js +19 -3
  120. package/dist/transformation/visitors/modules/export.js +4 -5
  121. package/dist/transformation/visitors/namespace.js +1 -1
  122. package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
  123. package/dist/transformation/visitors/optional-chaining.js +172 -0
  124. package/dist/transformation/visitors/sourceFile.js +4 -1
  125. package/dist/transformation/visitors/spread.js +14 -7
  126. package/dist/transformation/visitors/switch.js +29 -9
  127. package/dist/transformation/visitors/template.js +5 -2
  128. package/dist/transformation/visitors/typeof.js +3 -1
  129. package/dist/transformation/visitors/variable-declaration.js +37 -8
  130. package/dist/transformation/visitors/void.d.ts +1 -4
  131. package/dist/transformation/visitors/void.js +7 -12
  132. package/dist/transpilation/resolve.js +1 -1
  133. package/dist/transpilation/transformers.js +4 -1
  134. package/dist/transpilation/utils.js +2 -1
  135. package/language-extensions/index.d.ts +11 -1
  136. package/package.json +5 -5
  137. package/dist/lualib/OptionalChainAccess.lua +0 -6
  138. package/dist/lualib/OptionalFunctionCall.lua +0 -6
  139. package/dist/lualib/OptionalMethodCall.lua +0 -11
  140. package/dist/transformation/utils/transform.d.ts +0 -8
  141. package/dist/transformation/utils/transform.js +0 -14
package/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ - Added `LuaPairsIterable` language extension to mark objects as iterable with Lua's `pairs`.
6
+ - Added support for properties on functions.
7
+ - Unicode is no longer escaped and used as-is for `"luaTarget": "JIT"`.
8
+ - Fixed some bugs related to destructuring in loops and function parameters.
9
+ - Fixed incorrect global being generated for some `try` statements.
10
+ - Fixed some incorrect behavior for `??` and `? :` when generic types were involved.
11
+ - Added missing `...` optimization in cases where casts or parentheses are used.
12
+ - Fixed a bug for `Promise` when resolving with another Promise. This also fixes some unexpected behavior with `async` which is built with Promises.
13
+ - Fixed `async` functions not aborting after returning from a `catch` block.
14
+
15
+ ## 1.2.0
16
+
17
+ - Upgraded to TypeScript 4.5.x.
18
+ - Improved general output formatting.
19
+ - Added support for more complicated (nested) destructuring patterns, also fixing an exception they caused before.
20
+ - Fixed incorrect interactions between standard library functionality and optional chaining, e.g. `myArray?.forEach()`.
21
+ - Fixed rejected promises sometimes not getting the correct rejection reason.
22
+ - Fixed some `delete` behavior that was different in Lua compared to JS.
23
+ - Fixed a bug causing exported classes to lose their decorators.
24
+ - Fixed plugins checking for ts-node from the wrong location (tsconfig directory), plugins will now check for ts-node relative to the tstl directory.
25
+
26
+ Under the hood:
27
+
28
+ - We can now transform using preceding statements, allowing all kinds of optimizations and improvements to output Lua.
29
+ - Updated various language constructs to use preceding statements instead of inline immediately-invoked functions.
30
+
3
31
  ## 1.1.0
4
32
 
5
33
  - **[Breaking]** We now use TypeScript's JSX transformer instead of maintaining our own. As a result, `React.createElement` now requires a self parameter, so remove `@noSelf`, `this: void` if necessary.
package/dist/LuaAST.d.ts CHANGED
@@ -216,6 +216,7 @@ export interface StringLiteral extends Expression {
216
216
  }
217
217
  export declare function isStringLiteral(node: Node): node is StringLiteral;
218
218
  export declare function createStringLiteral(value: string, tsOriginal?: ts.Node): StringLiteral;
219
+ export declare function isLiteral(node: Node): node is NilLiteral | DotsLiteral | BooleanLiteral | NumericLiteral | StringLiteral;
219
220
  export declare enum FunctionExpressionFlags {
220
221
  None = 1,
221
222
  Inline = 2,
package/dist/LuaAST.js CHANGED
@@ -2,8 +2,8 @@
2
2
  // Simplified Lua AST based roughly on http://lua-users.org/wiki/MetaLuaAbstractSyntaxTree,
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
- exports.createFunctionExpression = exports.isFunctionExpression = exports.FunctionExpressionFlags = exports.createStringLiteral = exports.isStringLiteral = exports.createNumericLiteral = exports.isNumericLiteral = 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.getOriginalPos = exports.setNodeOriginal = exports.setNodePosition = exports.cloneNode = exports.createNode = 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 = void 0;
5
+ exports.isFunctionExpression = exports.FunctionExpressionFlags = exports.isLiteral = exports.createStringLiteral = exports.isStringLiteral = exports.createNumericLiteral = exports.isNumericLiteral = 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.getOriginalPos = exports.setNodeOriginal = exports.setNodePosition = exports.cloneNode = exports.createNode = 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 = 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");
@@ -338,6 +338,14 @@ function createStringLiteral(value, tsOriginal) {
338
338
  return expression;
339
339
  }
340
340
  exports.createStringLiteral = createStringLiteral;
341
+ function isLiteral(node) {
342
+ return (isNilLiteral(node) ||
343
+ isDotsLiteral(node) ||
344
+ isBooleanLiteral(node) ||
345
+ isNumericLiteral(node) ||
346
+ isStringLiteral(node));
347
+ }
348
+ exports.isLiteral = isLiteral;
341
349
  var FunctionExpressionFlags;
342
350
  (function (FunctionExpressionFlags) {
343
351
  FunctionExpressionFlags[FunctionExpressionFlags["None"] = 1] = "None";
package/dist/LuaLib.d.ts CHANGED
@@ -56,9 +56,6 @@ export declare enum LuaLibFeature {
56
56
  ObjectKeys = "ObjectKeys",
57
57
  ObjectRest = "ObjectRest",
58
58
  ObjectValues = "ObjectValues",
59
- OptionalChainAccess = "OptionalChainAccess",
60
- OptionalFunctionCall = "OptionalFunctionCall",
61
- OptionalMethodCall = "OptionalMethodCall",
62
59
  ParseFloat = "ParseFloat",
63
60
  ParseInt = "ParseInt",
64
61
  Promise = "Promise",
@@ -68,6 +65,9 @@ export declare enum LuaLibFeature {
68
65
  PromiseRace = "PromiseRace",
69
66
  Set = "Set",
70
67
  SetDescriptor = "SetDescriptor",
68
+ SparseArrayNew = "SparseArrayNew",
69
+ SparseArrayPush = "SparseArrayPush",
70
+ SparseArraySpread = "SparseArraySpread",
71
71
  WeakMap = "WeakMap",
72
72
  WeakSet = "WeakSet",
73
73
  SourceMapTraceBack = "SourceMapTraceBack",
package/dist/LuaLib.js CHANGED
@@ -60,9 +60,6 @@ var LuaLibFeature;
60
60
  LuaLibFeature["ObjectKeys"] = "ObjectKeys";
61
61
  LuaLibFeature["ObjectRest"] = "ObjectRest";
62
62
  LuaLibFeature["ObjectValues"] = "ObjectValues";
63
- LuaLibFeature["OptionalChainAccess"] = "OptionalChainAccess";
64
- LuaLibFeature["OptionalFunctionCall"] = "OptionalFunctionCall";
65
- LuaLibFeature["OptionalMethodCall"] = "OptionalMethodCall";
66
63
  LuaLibFeature["ParseFloat"] = "ParseFloat";
67
64
  LuaLibFeature["ParseInt"] = "ParseInt";
68
65
  LuaLibFeature["Promise"] = "Promise";
@@ -72,6 +69,9 @@ var LuaLibFeature;
72
69
  LuaLibFeature["PromiseRace"] = "PromiseRace";
73
70
  LuaLibFeature["Set"] = "Set";
74
71
  LuaLibFeature["SetDescriptor"] = "SetDescriptor";
72
+ LuaLibFeature["SparseArrayNew"] = "SparseArrayNew";
73
+ LuaLibFeature["SparseArrayPush"] = "SparseArrayPush";
74
+ LuaLibFeature["SparseArraySpread"] = "SparseArraySpread";
75
75
  LuaLibFeature["WeakMap"] = "WeakMap";
76
76
  LuaLibFeature["WeakSet"] = "WeakSet";
77
77
  LuaLibFeature["SourceMapTraceBack"] = "SourceMapTraceBack";
@@ -104,10 +104,10 @@ const luaLibDependencies = {
104
104
  ArrayConcat: [LuaLibFeature.ArrayIsArray],
105
105
  ArrayFlat: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray],
106
106
  ArrayFlatMap: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray],
107
- Await: [LuaLibFeature.InstanceOf, LuaLibFeature.New],
107
+ Await: [LuaLibFeature.InstanceOf, LuaLibFeature.New, LuaLibFeature.Promise],
108
108
  Decorate: [LuaLibFeature.ObjectGetOwnPropertyDescriptor, LuaLibFeature.SetDescriptor, LuaLibFeature.ObjectAssign],
109
109
  DelegatedYield: [LuaLibFeature.StringAccess],
110
- Delete: [LuaLibFeature.ObjectGetOwnPropertyDescriptors],
110
+ Delete: [LuaLibFeature.ObjectGetOwnPropertyDescriptors, LuaLibFeature.Error, LuaLibFeature.New],
111
111
  Error: [LuaLibFeature.Class, LuaLibFeature.ClassExtends, LuaLibFeature.New],
112
112
  FunctionBind: [LuaLibFeature.Unpack],
113
113
  Generator: [LuaLibFeature.Symbol],
@@ -1,5 +1,6 @@
1
1
  import { SourceNode } from "source-map";
2
2
  import * as ts from "typescript";
3
+ import { CompilerOptions } from "./CompilerOptions";
3
4
  import * as lua from "./LuaAST";
4
5
  import { EmitHost } from "./transpilation";
5
6
  export declare const escapeString: (value: string) => string;
@@ -17,16 +18,18 @@ export declare class LuaPrinter {
17
18
  private program;
18
19
  private sourceFile;
19
20
  private static operatorMap;
21
+ private static operatorPrecedence;
22
+ private static rightAssociativeOperators;
20
23
  private currentIndent;
21
- private luaFile;
22
- private relativeSourcePath;
23
- private options;
24
+ protected luaFile: string;
25
+ protected relativeSourcePath: string;
26
+ protected options: CompilerOptions;
24
27
  static readonly sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
25
28
  constructor(emitHost: EmitHost, program: ts.Program, sourceFile: string);
26
29
  print(file: lua.File): PrintResult;
27
30
  private printInlineSourceMap;
28
31
  private printStackTraceOverride;
29
- private printFile;
32
+ protected printFile(file: lua.File): SourceNode;
30
33
  protected pushIndent(): void;
31
34
  protected popIndent(): void;
32
35
  protected indent(input?: SourceChunk): SourceChunk;
@@ -73,6 +76,10 @@ export declare class LuaPrinter {
73
76
  printTableIndexExpression(expression: lua.TableIndexExpression): SourceNode;
74
77
  printOperator(kind: lua.Operator): SourceNode;
75
78
  protected joinChunksWithComma(chunks: SourceChunk[]): SourceChunk[];
79
+ /**
80
+ * Returns true if the expression list (table field or parameters) should be printed on one line.
81
+ */
82
+ protected isSimpleExpressionList(expressions: lua.Expression[]): boolean;
76
83
  protected printExpressionList(expressions: lua.Expression[]): SourceChunk[];
77
84
  private buildSourceMap;
78
85
  }
@@ -32,7 +32,7 @@ exports.tstlHeader = "--[[ Generated with https://github.com/TypeScriptToLua/Typ
32
32
  * `foo.bar` => passes (`function foo.bar()` is valid)
33
33
  * `getFoo().bar` => fails (`function getFoo().bar()` would be illegal)
34
34
  */
35
- const isValidLuaFunctionDeclarationName = (str) => /^[a-zA-Z0-9_.]+$/.test(str);
35
+ const isValidLuaFunctionDeclarationName = (str, options) => ((0, safe_names_1.shouldAllowUnicode)(options) ? /^[a-zA-Z0-9_\u00FF-\uFFFD.]+$/ : /^[a-zA-Z0-9_.]+$/).test(str);
36
36
  /**
37
37
  * Returns true if expression contains no function calls.
38
38
  */
@@ -294,7 +294,7 @@ class LuaPrinter {
294
294
  (statement.right[0].flags & lua.FunctionExpressionFlags.Declaration) !== 0) {
295
295
  // Use `function foo()` instead of `foo = function()`
296
296
  const name = this.printExpression(statement.left[0]);
297
- if (isValidLuaFunctionDeclarationName(name.toString())) {
297
+ if (isValidLuaFunctionDeclarationName(name.toString(), this.options)) {
298
298
  chunks.push(this.printFunctionDefinition(statement));
299
299
  return this.createSourceNode(statement, chunks);
300
300
  }
@@ -494,7 +494,7 @@ class LuaPrinter {
494
494
  const chunks = [];
495
495
  const value = this.printExpression(expression.value);
496
496
  if (expression.key) {
497
- if (lua.isStringLiteral(expression.key) && (0, safe_names_1.isValidLuaIdentifier)(expression.key.value)) {
497
+ if (lua.isStringLiteral(expression.key) && (0, safe_names_1.isValidLuaIdentifier)(expression.key.value, this.options)) {
498
498
  chunks.push(expression.key.value, " = ", value);
499
499
  }
500
500
  else {
@@ -512,26 +512,31 @@ class LuaPrinter {
512
512
  printUnaryExpression(expression) {
513
513
  const chunks = [];
514
514
  chunks.push(this.printOperator(expression.operator));
515
- chunks.push(this.printExpressionInParenthesesIfNeeded(expression.operand));
515
+ chunks.push(this.printExpressionInParenthesesIfNeeded(expression.operand, LuaPrinter.operatorPrecedence[expression.operator]));
516
516
  return this.createSourceNode(expression, chunks);
517
517
  }
518
518
  printBinaryExpression(expression) {
519
519
  const chunks = [];
520
- chunks.push(this.printExpressionInParenthesesIfNeeded(expression.left));
520
+ const isRightAssociative = LuaPrinter.rightAssociativeOperators.has(expression.operator);
521
+ const precedence = LuaPrinter.operatorPrecedence[expression.operator];
522
+ chunks.push(this.printExpressionInParenthesesIfNeeded(expression.left, isRightAssociative ? precedence + 1 : precedence));
521
523
  chunks.push(" ", this.printOperator(expression.operator), " ");
522
- chunks.push(this.printExpressionInParenthesesIfNeeded(expression.right));
524
+ chunks.push(this.printExpressionInParenthesesIfNeeded(expression.right, isRightAssociative ? precedence : precedence + 1));
523
525
  return this.createSourceNode(expression, chunks);
524
526
  }
525
- printExpressionInParenthesesIfNeeded(expression) {
526
- return this.needsParenthesis(expression)
527
+ printExpressionInParenthesesIfNeeded(expression, minPrecedenceToOmit) {
528
+ return this.needsParenthesis(expression, minPrecedenceToOmit)
527
529
  ? this.createSourceNode(expression, ["(", this.printExpression(expression), ")"])
528
530
  : this.printExpression(expression);
529
531
  }
530
- needsParenthesis(expression) {
531
- return (lua.isBinaryExpression(expression) ||
532
- lua.isFunctionExpression(expression) ||
533
- lua.isTableExpression(expression) ||
534
- (lua.isUnaryExpression(expression) && expression.operator === lua.SyntaxKind.NotOperator));
532
+ needsParenthesis(expression, minPrecedenceToOmit) {
533
+ if (lua.isBinaryExpression(expression) || lua.isUnaryExpression(expression)) {
534
+ return (minPrecedenceToOmit === undefined ||
535
+ LuaPrinter.operatorPrecedence[expression.operator] < minPrecedenceToOmit);
536
+ }
537
+ else {
538
+ return lua.isFunctionExpression(expression) || lua.isTableExpression(expression);
539
+ }
535
540
  }
536
541
  printCallExpression(expression) {
537
542
  const chunks = [];
@@ -561,7 +566,7 @@ class LuaPrinter {
561
566
  printTableIndexExpression(expression) {
562
567
  const chunks = [];
563
568
  chunks.push(this.printExpressionInParenthesesIfNeeded(expression.table));
564
- if (lua.isStringLiteral(expression.index) && (0, safe_names_1.isValidLuaIdentifier)(expression.index.value)) {
569
+ if (lua.isStringLiteral(expression.index) && (0, safe_names_1.isValidLuaIdentifier)(expression.index.value, this.options)) {
565
570
  chunks.push(".", this.createSourceNode(expression.index, expression.index.value));
566
571
  }
567
572
  else {
@@ -575,9 +580,19 @@ class LuaPrinter {
575
580
  joinChunksWithComma(chunks) {
576
581
  return (0, utils_1.intersperse)(chunks, ", ");
577
582
  }
583
+ /**
584
+ * Returns true if the expression list (table field or parameters) should be printed on one line.
585
+ */
586
+ isSimpleExpressionList(expressions) {
587
+ if (expressions.length <= 1)
588
+ return true;
589
+ if (expressions.length > 4)
590
+ return false;
591
+ return expressions.every(isSimpleExpression);
592
+ }
578
593
  printExpressionList(expressions) {
579
594
  const chunks = [];
580
- if (expressions.every(isSimpleExpression)) {
595
+ if (this.isSimpleExpressionList(expressions)) {
581
596
  chunks.push(...this.joinChunksWithComma(expressions.map(e => this.printExpression(e))));
582
597
  }
583
598
  else {
@@ -675,5 +690,33 @@ LuaPrinter.operatorMap = {
675
690
  [lua.SyntaxKind.BitwiseLeftShiftOperator]: "<<",
676
691
  [lua.SyntaxKind.BitwiseNotOperator]: "~",
677
692
  };
693
+ LuaPrinter.operatorPrecedence = {
694
+ [lua.SyntaxKind.OrOperator]: 1,
695
+ [lua.SyntaxKind.AndOperator]: 2,
696
+ [lua.SyntaxKind.EqualityOperator]: 3,
697
+ [lua.SyntaxKind.InequalityOperator]: 3,
698
+ [lua.SyntaxKind.LessThanOperator]: 3,
699
+ [lua.SyntaxKind.LessEqualOperator]: 3,
700
+ [lua.SyntaxKind.GreaterThanOperator]: 3,
701
+ [lua.SyntaxKind.GreaterEqualOperator]: 3,
702
+ [lua.SyntaxKind.BitwiseOrOperator]: 4,
703
+ [lua.SyntaxKind.BitwiseExclusiveOrOperator]: 5,
704
+ [lua.SyntaxKind.BitwiseAndOperator]: 6,
705
+ [lua.SyntaxKind.BitwiseLeftShiftOperator]: 7,
706
+ [lua.SyntaxKind.BitwiseRightShiftOperator]: 7,
707
+ [lua.SyntaxKind.ConcatOperator]: 8,
708
+ [lua.SyntaxKind.AdditionOperator]: 9,
709
+ [lua.SyntaxKind.SubtractionOperator]: 9,
710
+ [lua.SyntaxKind.MultiplicationOperator]: 10,
711
+ [lua.SyntaxKind.DivisionOperator]: 10,
712
+ [lua.SyntaxKind.FloorDivisionOperator]: 10,
713
+ [lua.SyntaxKind.ModuloOperator]: 10,
714
+ [lua.SyntaxKind.NotOperator]: 11,
715
+ [lua.SyntaxKind.LengthOperator]: 11,
716
+ [lua.SyntaxKind.NegationOperator]: 11,
717
+ [lua.SyntaxKind.BitwiseNotOperator]: 11,
718
+ [lua.SyntaxKind.PowerOperator]: 12,
719
+ };
720
+ LuaPrinter.rightAssociativeOperators = new Set([lua.SyntaxKind.ConcatOperator, lua.SyntaxKind.PowerOperator]);
678
721
  LuaPrinter.sourceMapTracebackPlaceholder = "{#SourceMapTraceback}";
679
722
  //# sourceMappingURL=LuaPrinter.js.map
@@ -13,7 +13,7 @@ interface CommandLineOptionOfEnum extends CommandLineOptionBase {
13
13
  choices: string[];
14
14
  }
15
15
  interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
16
- type: "boolean" | "string" | "object";
16
+ type: "boolean" | "string" | "object" | "array";
17
17
  }
18
18
  declare type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;
19
19
  export declare const optionDeclarations: CommandLineOption[];
package/dist/cli/parse.js CHANGED
@@ -59,6 +59,11 @@ exports.optionDeclarations = [
59
59
  description: "Provide verbose output useful for diagnosing problems.",
60
60
  type: "boolean",
61
61
  },
62
+ {
63
+ name: "noResolvePaths",
64
+ description: "An array of paths that tstl should not resolve and keep as-is.",
65
+ type: "array",
66
+ },
62
67
  ];
63
68
  function updateParsedConfigFile(parsedConfigFile) {
64
69
  let hasRootLevelOptions = false;
@@ -162,6 +167,15 @@ function readValue(option, value) {
162
167
  }
163
168
  return { value };
164
169
  }
170
+ case "array": {
171
+ if (!Array.isArray(value)) {
172
+ return {
173
+ value: undefined,
174
+ error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type),
175
+ };
176
+ }
177
+ return { value };
178
+ }
165
179
  case "enum": {
166
180
  if (typeof value !== "string") {
167
181
  return {
@@ -4,7 +4,7 @@ function __TS__ArrayFlat(array, depth)
4
4
  end
5
5
  local result = {}
6
6
  for ____, value in ipairs(array) do
7
- if (depth > 0) and __TS__ArrayIsArray(value) then
7
+ if depth > 0 and __TS__ArrayIsArray(value) then
8
8
  result = __TS__ArrayConcat(
9
9
  result,
10
10
  __TS__ArrayFlat(value, depth - 1)
@@ -4,7 +4,7 @@ function __TS__ArrayFlatMap(array, callback)
4
4
  local i = 0
5
5
  while i < #array do
6
6
  local value = callback(_G, array[i + 1], i, array)
7
- if (type(value) == "table") and __TS__ArrayIsArray(value) then
7
+ if type(value) == "table" and __TS__ArrayIsArray(value) then
8
8
  result = __TS__ArrayConcat(result, value)
9
9
  else
10
10
  result[#result + 1] = value
@@ -1,3 +1,3 @@
1
1
  function __TS__ArrayIsArray(value)
2
- return (type(value) == "table") and ((value[1] ~= nil) or (next(value, nil) == nil))
2
+ return type(value) == "table" and (value[1] ~= nil or next(value, nil) == nil)
3
3
  end
@@ -11,7 +11,13 @@ function __TS__ArrayReduce(arr, callbackFn, ...)
11
11
  error("Reduce of empty array with no initial value", 0)
12
12
  end
13
13
  for i = k, len - 1 do
14
- accumulator = callbackFn(_G, accumulator, arr[i + 1], i, arr)
14
+ accumulator = callbackFn(
15
+ _G,
16
+ accumulator,
17
+ arr[i + 1],
18
+ i,
19
+ arr
20
+ )
15
21
  end
16
22
  return accumulator
17
23
  end
@@ -11,7 +11,13 @@ function __TS__ArrayReduceRight(arr, callbackFn, ...)
11
11
  error("Reduce of empty array with no initial value", 0)
12
12
  end
13
13
  for i = k, 0, -1 do
14
- accumulator = callbackFn(_G, accumulator, arr[i + 1], i, arr)
14
+ accumulator = callbackFn(
15
+ _G,
16
+ accumulator,
17
+ arr[i + 1],
18
+ i,
19
+ arr
20
+ )
15
21
  end
16
22
  return accumulator
17
23
  end
@@ -1,5 +1,5 @@
1
1
  function __TS__ArraySetLength(arr, length)
2
- if (((length < 0) or (length ~= length)) or (length == math.huge)) or (math.floor(length) ~= length) then
2
+ if length < 0 or length ~= length or length == math.huge or math.floor(length) ~= length then
3
3
  error(
4
4
  "invalid array length: " .. tostring(length),
5
5
  0
@@ -35,7 +35,7 @@ function __TS__ArraySplice(list, ...)
35
35
  if itemCount < actualDeleteCount then
36
36
  do
37
37
  local k = actualStart
38
- while k < (len - actualDeleteCount) do
38
+ while k < len - actualDeleteCount do
39
39
  local from = k + actualDeleteCount
40
40
  local to = k + itemCount
41
41
  if list[from + 1] then
@@ -48,7 +48,7 @@ function __TS__ArraySplice(list, ...)
48
48
  end
49
49
  do
50
50
  local k = len
51
- while k > ((len - actualDeleteCount) + itemCount) do
51
+ while k > len - actualDeleteCount + itemCount do
52
52
  list[k] = nil
53
53
  k = k - 1
54
54
  end
@@ -57,8 +57,8 @@ function __TS__ArraySplice(list, ...)
57
57
  do
58
58
  local k = len - actualDeleteCount
59
59
  while k > actualStart do
60
- local from = (k + actualDeleteCount) - 1
61
- local to = (k + itemCount) - 1
60
+ local from = k + actualDeleteCount - 1
61
+ local to = k + itemCount - 1
62
62
  if list[from + 1] then
63
63
  list[to + 1] = list[from + 1]
64
64
  else
@@ -75,7 +75,7 @@ function __TS__ArraySplice(list, ...)
75
75
  end
76
76
  do
77
77
  local k = #list - 1
78
- while k >= ((len - actualDeleteCount) + itemCount) do
78
+ while k >= len - actualDeleteCount + itemCount do
79
79
  list[k + 1] = nil
80
80
  k = k - 1
81
81
  end
@@ -4,24 +4,34 @@ function __TS__AsyncAwaiter(generator)
4
4
  function(____, resolve, reject)
5
5
  local adopt, fulfilled, rejected, step, asyncCoroutine
6
6
  function adopt(self, value)
7
- return ((__TS__InstanceOf(value, __TS__Promise) and (function() return value end)) or (function() return __TS__Promise.resolve(value) end))()
7
+ local ____temp_0
8
+ if __TS__InstanceOf(value, __TS__Promise) then
9
+ ____temp_0 = value
10
+ else
11
+ ____temp_0 = __TS__Promise.resolve(value)
12
+ end
13
+ return ____temp_0
8
14
  end
9
15
  function fulfilled(self, value)
10
16
  local success, errorOrErrorHandler, resultOrError = coroutine.resume(asyncCoroutine, value)
11
17
  if success then
12
18
  step(_G, resultOrError, errorOrErrorHandler)
13
19
  else
14
- reject(_G, resultOrError)
20
+ reject(_G, errorOrErrorHandler)
15
21
  end
16
22
  end
17
23
  function rejected(self, handler)
18
24
  if handler then
19
25
  return function(____, value)
20
- local success, valueOrError = pcall(handler, value)
26
+ local success, hasReturnedOrError, returnedValue = pcall(handler, value)
21
27
  if success then
22
- step(_G, valueOrError, handler)
28
+ if hasReturnedOrError then
29
+ resolve(_G, returnedValue)
30
+ else
31
+ step(_G, hasReturnedOrError, handler)
32
+ end
23
33
  else
24
- reject(_G, valueOrError)
34
+ reject(_G, hasReturnedOrError)
25
35
  end
26
36
  end
27
37
  else
@@ -34,14 +44,12 @@ function __TS__AsyncAwaiter(generator)
34
44
  if coroutine.status(asyncCoroutine) == "dead" then
35
45
  resolve(_G, result)
36
46
  else
37
- (function()
38
- local ____self = adopt(_G, result)
39
- return ____self["then"](
40
- ____self,
41
- fulfilled,
42
- rejected(_G, errorHandler)
43
- )
44
- end)()
47
+ local ____self_1 = adopt(_G, result)
48
+ ____self_1["then"](
49
+ ____self_1,
50
+ fulfilled,
51
+ rejected(_G, errorHandler)
52
+ )
45
53
  end
46
54
  end
47
55
  asyncCoroutine = coroutine.create(generator)
@@ -1,19 +1,19 @@
1
1
  function __TS__CloneDescriptor(____bindingPattern0)
2
+ local value
3
+ local writable
4
+ local set
5
+ local get
6
+ local configurable
2
7
  local enumerable
3
8
  enumerable = ____bindingPattern0.enumerable
4
- local configurable
5
9
  configurable = ____bindingPattern0.configurable
6
- local get
7
10
  get = ____bindingPattern0.get
8
- local set
9
11
  set = ____bindingPattern0.set
10
- local writable
11
12
  writable = ____bindingPattern0.writable
12
- local value
13
13
  value = ____bindingPattern0.value
14
14
  local descriptor = {enumerable = enumerable == true, configurable = configurable == true}
15
- local hasGetterOrSetter = (get ~= nil) or (set ~= nil)
16
- local hasValueOrWritableAttribute = (writable ~= nil) or (value ~= nil)
15
+ local hasGetterOrSetter = get ~= nil or set ~= nil
16
+ local hasValueOrWritableAttribute = writable ~= nil or value ~= nil
17
17
  if hasGetterOrSetter and hasValueOrWritableAttribute then
18
18
  error("Invalid property descriptor. Cannot both specify accessors and a value or writable attribute.", 0)
19
19
  end
@@ -12,7 +12,7 @@ function __TS__Decorate(decorators, target, key, desc)
12
12
  local value = rawget(target, key)
13
13
  local descriptor = __TS__ObjectGetOwnPropertyDescriptor(target, key) or ({configurable = true, writable = true, value = value})
14
14
  local desc = decorator(_G, target, key, descriptor) or descriptor
15
- local isSimpleValue = (((desc.configurable == true) and (desc.writable == true)) and (not desc.get)) and (not desc.set)
15
+ local isSimpleValue = desc.configurable == true and desc.writable == true and not desc.get and not desc.set
16
16
  if isSimpleValue then
17
17
  rawset(target, key, desc.value)
18
18
  else
@@ -1,9 +1,7 @@
1
1
  function __TS__DelegatedYield(iterable)
2
2
  if type(iterable) == "string" then
3
3
  for index = 0, #iterable - 1 do
4
- coroutine.yield(
5
- __TS__StringAccess(iterable, index)
6
- )
4
+ coroutine.yield(__TS__StringAccess(iterable, index))
7
5
  end
8
6
  elseif iterable.____coroutine ~= nil then
9
7
  local co = iterable.____coroutine
@@ -4,16 +4,16 @@ function __TS__Delete(target, key)
4
4
  if descriptor then
5
5
  if not descriptor.configurable then
6
6
  error(
7
- ((("Cannot delete property " .. tostring(key)) .. " of ") .. tostring(target)) .. ".",
7
+ __TS__New(
8
+ TypeError,
9
+ ((("Cannot delete property " .. tostring(key)) .. " of ") .. tostring(target)) .. "."
10
+ ),
8
11
  0
9
12
  )
10
13
  end
11
14
  descriptors[key] = nil
12
15
  return true
13
16
  end
14
- if target[key] ~= nil then
15
- target[key] = nil
16
- return true
17
- end
18
- return false
17
+ target[key] = nil
18
+ return true
19
19
  end