typescript-to-lua 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/LuaLib.js +1 -1
  3. package/dist/LuaPrinter.js +4 -4
  4. package/dist/lualib/Await.lua +7 -3
  5. package/dist/lualib/CloneDescriptor.lua +6 -6
  6. package/dist/lualib/Promise.lua +10 -2
  7. package/dist/lualib/lualib_bundle.lua +217 -205
  8. package/dist/transformation/builtins/function.js +5 -2
  9. package/dist/transformation/builtins/index.js +2 -2
  10. package/dist/transformation/utils/diagnostics.d.ts +3 -0
  11. package/dist/transformation/utils/diagnostics.js +2 -1
  12. package/dist/transformation/utils/language-extensions.d.ts +2 -0
  13. package/dist/transformation/utils/language-extensions.js +7 -1
  14. package/dist/transformation/utils/lua-ast.js +5 -5
  15. package/dist/transformation/utils/safe-names.d.ts +4 -2
  16. package/dist/transformation/utils/safe-names.js +11 -5
  17. package/dist/transformation/utils/scope.d.ts +2 -1
  18. package/dist/transformation/utils/scope.js +1 -0
  19. package/dist/transformation/utils/typescript/types.d.ts +3 -1
  20. package/dist/transformation/utils/typescript/types.js +40 -13
  21. package/dist/transformation/visitors/binary-expression/index.js +1 -3
  22. package/dist/transformation/visitors/call.js +1 -1
  23. package/dist/transformation/visitors/class/index.js +1 -1
  24. package/dist/transformation/visitors/conditional.js +2 -23
  25. package/dist/transformation/visitors/errors.js +11 -9
  26. package/dist/transformation/visitors/function.d.ts +2 -0
  27. package/dist/transformation/visitors/function.js +53 -4
  28. package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +5 -0
  29. package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
  30. package/dist/transformation/visitors/loops/for-of.js +4 -0
  31. package/dist/transformation/visitors/loops/utils.js +6 -1
  32. package/dist/transformation/visitors/namespace.js +1 -1
  33. package/dist/transformation/visitors/spread.js +14 -7
  34. package/dist/transformation/visitors/variable-declaration.js +10 -1
  35. package/language-extensions/index.d.ts +11 -1
  36. package/package.json +1 -1
@@ -10,6 +10,7 @@ const export_1 = require("../utils/export");
10
10
  const lua_ast_1 = require("../utils/lua-ast");
11
11
  const lualib_1 = require("../utils/lualib");
12
12
  const preceding_statements_1 = require("../utils/preceding-statements");
13
+ const function_1 = require("./function");
13
14
  const identifier_1 = require("./identifier");
14
15
  const multi_1 = require("./language-extensions/multi");
15
16
  const literal_1 = require("./literal");
@@ -183,7 +184,15 @@ function transformVariableDeclaration(context, statement) {
183
184
  // Find variable identifier
184
185
  const identifierName = (0, identifier_1.transformIdentifier)(context, statement.name);
185
186
  const value = statement.initializer && context.transformExpression(statement.initializer);
186
- return (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, identifierName, value, statement);
187
+ // Wrap functions being assigned to a type that contains additional properties in a callable table
188
+ // This catches 'const foo = function() {}; foo.bar = "FOOBAR";'
189
+ const wrappedValue = value &&
190
+ // Skip named function expressions because they will have been wrapped already
191
+ !(statement.initializer && ts.isFunctionExpression(statement.initializer) && statement.initializer.name) &&
192
+ (0, function_1.isFunctionTypeWithProperties)(context.checker.getTypeAtLocation(statement.name))
193
+ ? (0, function_1.createCallableTable)(value)
194
+ : value;
195
+ return (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, identifierName, wrappedValue, statement);
187
196
  }
188
197
  else if (ts.isArrayBindingPattern(statement.name) || ts.isObjectBindingPattern(statement.name)) {
189
198
  return transformBindingVariableDeclaration(context, statement.name, statement.initializer);
@@ -78,6 +78,16 @@ declare type LuaIterable<TValue, TState = undefined> = Iterable<TValue> &
78
78
  LuaIterator<TValue, TState> &
79
79
  LuaExtension<"__luaIterableBrand">;
80
80
 
81
+ /**
82
+ * Represents an object that can be iterated with pairs()
83
+ * For more information see: https://typescripttolua.github.io/docs/advanced/language-extensions
84
+ *
85
+ * @param TKey The type of the key returned each iteration.
86
+ * @param TValue The type of the value returned each iteration.
87
+ */
88
+ declare type LuaPairsIterable<TKey extends AnyNotNil, TValue> = Iterable<[TKey, TValue]> &
89
+ LuaExtension<"__luaPairsIterableBrand">;
90
+
81
91
  /**
82
92
  * Calls to functions with this type are translated to `left + right`.
83
93
  * For more information see: https://typescripttolua.github.io/docs/advanced/language-extensions
@@ -535,7 +545,7 @@ declare type LuaTableDeleteMethod<TKey extends AnyNotNil> = ((key: TKey) => bool
535
545
  * @param TKey The type of the keys used to access the table.
536
546
  * @param TValue The type of the values stored in the table.
537
547
  */
538
- declare interface LuaTable<TKey extends AnyNotNil = AnyNotNil, TValue = any> {
548
+ declare interface LuaTable<TKey extends AnyNotNil = AnyNotNil, TValue = any> extends LuaPairsIterable<TKey, TValue> {
539
549
  length: LuaLengthMethod<number>;
540
550
  get: LuaTableGetMethod<TKey, TValue>;
541
551
  set: LuaTableSetMethod<TKey, TValue>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.2.0",
3
+ "version": "1.3.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/",