typescript-to-lua 1.31.4 → 1.32.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.
@@ -8,6 +8,7 @@ export declare function createExportsIdentifier(): lua.Identifier;
8
8
  export declare function addToNumericExpression(expression: lua.Expression, change: number): lua.Expression;
9
9
  export declare function getNumberLiteralValue(expression?: lua.Expression): number | undefined;
10
10
  export declare function createUnpackCall(context: TransformationContext, expression: lua.Expression, tsOriginal?: ts.Node): lua.Expression;
11
+ export declare function createBoundedUnpackCall(context: TransformationContext, expression: lua.Expression, maxUnpackItem: number, tsOriginal?: ts.Node): lua.Expression;
11
12
  export declare function isUnpackCall(node: lua.Node): boolean;
12
13
  export declare function wrapInTable(...expressions: lua.Expression[]): lua.TableExpression;
13
14
  export declare function wrapInToStringForConcat(expression: lua.Expression): lua.Expression;
@@ -7,6 +7,7 @@ exports.createExportsIdentifier = createExportsIdentifier;
7
7
  exports.addToNumericExpression = addToNumericExpression;
8
8
  exports.getNumberLiteralValue = getNumberLiteralValue;
9
9
  exports.createUnpackCall = createUnpackCall;
10
+ exports.createBoundedUnpackCall = createBoundedUnpackCall;
10
11
  exports.isUnpackCall = isUnpackCall;
11
12
  exports.wrapInTable = wrapInTable;
12
13
  exports.wrapInToStringForConcat = wrapInToStringForConcat;
@@ -72,6 +73,21 @@ function createUnpackCall(context, expression, tsOriginal) {
72
73
  : lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("unpack"));
73
74
  return lua.setNodeFlags(lua.createCallExpression(unpack, [expression], tsOriginal), lua.NodeFlags.TableUnpackCall);
74
75
  }
76
+ function createBoundedUnpackCall(context, expression, maxUnpackItem, tsOriginal) {
77
+ if (context.luaTarget === CompilerOptions_1.LuaTarget.Universal) {
78
+ return (0, lualib_1.transformLuaLibFunction)(context, LuaLib_1.LuaLibFeature.Unpack, tsOriginal, expression);
79
+ }
80
+ // Lua 5.0 does not support this signature, so don't add the arguments there
81
+ const extraArgs = context.luaTarget === CompilerOptions_1.LuaTarget.Lua50
82
+ ? []
83
+ : [lua.createNumericLiteral(1), lua.createNumericLiteral(maxUnpackItem)];
84
+ const unpack = context.luaTarget === CompilerOptions_1.LuaTarget.Lua50 ||
85
+ context.luaTarget === CompilerOptions_1.LuaTarget.Lua51 ||
86
+ context.luaTarget === CompilerOptions_1.LuaTarget.LuaJIT
87
+ ? lua.createIdentifier("unpack")
88
+ : lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("unpack"));
89
+ return lua.setNodeFlags(lua.createCallExpression(unpack, [expression, ...extraArgs], tsOriginal), lua.NodeFlags.TableUnpackCall);
90
+ }
75
91
  function isUnpackCall(node) {
76
92
  return lua.isCallExpression(node) && (node.flags & lua.NodeFlags.TableUnpackCall) !== 0;
77
93
  }
@@ -83,7 +83,7 @@ function transformPropertyAccessExpressionWithCapture(context, node, thisValueCa
83
83
  const isOptionalLeft = (0, optional_chaining_1.isOptionalContinuation)(node.expression);
84
84
  let property = node.name.text;
85
85
  const symbol = context.checker.getSymbolAtLocation(node.name);
86
- const customName = (0, identifier_1.getCustomNameFromSymbol)(symbol);
86
+ const customName = (0, identifier_1.getCustomNameFromSymbol)(context, symbol);
87
87
  if (customName) {
88
88
  property = customName;
89
89
  }
@@ -165,7 +165,7 @@ function transformAssignmentStatement(context, expression) {
165
165
  else {
166
166
  right = context.transformExpression(expression.right);
167
167
  if (!(0, multi_1.isMultiReturnCall)(context, expression.right) && (0, typescript_2.isArrayType)(context, rightType)) {
168
- right = (0, lua_ast_1.createUnpackCall)(context, right, expression.right);
168
+ right = (0, lua_ast_1.createBoundedUnpackCall)(context, right, expression.left.elements.length, expression.right);
169
169
  }
170
170
  }
171
171
  const left = expression.left.elements.map(e => transformAssignmentLeftHandSideExpression(context, e));
@@ -94,7 +94,7 @@ function transformContextualCallExpression(context, node, args) {
94
94
  const table = context.transformExpression(left.expression);
95
95
  let name = left.name.text;
96
96
  const symbol = context.checker.getSymbolAtLocation(left);
97
- const customName = (0, identifier_1.getCustomNameFromSymbol)(symbol);
97
+ const customName = (0, identifier_1.getCustomNameFromSymbol)(context, symbol);
98
98
  if (customName) {
99
99
  name = customName;
100
100
  }
@@ -2,6 +2,6 @@ import * as ts from "typescript";
2
2
  import * as lua from "../../LuaAST";
3
3
  import { FunctionVisitor, TransformationContext } from "../context";
4
4
  export declare function transformIdentifier(context: TransformationContext, identifier: ts.Identifier): lua.Identifier;
5
- export declare function getCustomNameFromSymbol(symbol?: ts.Symbol): undefined | string;
5
+ export declare function getCustomNameFromSymbol(context: TransformationContext, symbol?: ts.Symbol): undefined | string;
6
6
  export declare function transformIdentifierWithSymbol(context: TransformationContext, node: ts.Identifier, symbol: ts.Symbol | undefined): lua.Expression;
7
7
  export declare const transformIdentifierExpression: FunctionVisitor<ts.Identifier>;
@@ -22,7 +22,7 @@ const annotations_1 = require("../utils/annotations");
22
22
  function transformIdentifier(context, identifier) {
23
23
  return transformNonValueIdentifier(context, identifier, context.checker.getSymbolAtLocation(identifier));
24
24
  }
25
- function getCustomNameFromSymbol(symbol) {
25
+ function getCustomNameFromSymbol(context, symbol) {
26
26
  let retVal;
27
27
  if (symbol) {
28
28
  const declarations = symbol.getDeclarations();
@@ -35,6 +35,17 @@ function getCustomNameFromSymbol(symbol) {
35
35
  customNameAnnotation = foundAnnotation;
36
36
  break;
37
37
  }
38
+ // If the symbol is an imported value, check the original declaration
39
+ // beware of declaration.propertyName, this is the import name alias and should not be renamed!
40
+ if (ts.isImportSpecifier(declaration) && !declaration.propertyName) {
41
+ const importedType = context.checker.getTypeAtLocation(declaration);
42
+ if (importedType.symbol) {
43
+ const importedCustomName = getCustomNameFromSymbol(context, importedType.symbol);
44
+ if (importedCustomName) {
45
+ return importedCustomName;
46
+ }
47
+ }
48
+ }
38
49
  }
39
50
  if (customNameAnnotation) {
40
51
  retVal = customNameAnnotation.args[0];
@@ -73,7 +84,7 @@ function transformNonValueIdentifier(context, identifier, symbol) {
73
84
  }
74
85
  }
75
86
  let text = (0, safe_names_1.hasUnsafeIdentifierName)(context, identifier, symbol) ? (0, safe_names_1.createSafeName)(identifier.text) : identifier.text;
76
- const customName = getCustomNameFromSymbol(symbol);
87
+ const customName = getCustomNameFromSymbol(context, symbol);
77
88
  if (customName)
78
89
  text = customName;
79
90
  const symbolId = (0, symbols_1.getIdentifierSymbolId)(context, identifier, symbol);
@@ -35,9 +35,14 @@ function shouldBeImported(context, importNode) {
35
35
  return context.resolver.isReferencedAliasDeclaration(importNode);
36
36
  }
37
37
  function transformImportSpecifier(context, importSpecifier, moduleTableName) {
38
- var _a;
38
+ var _a, _b;
39
+ const type = context.checker.getTypeAtLocation(importSpecifier.name);
39
40
  const leftIdentifier = (0, identifier_1.transformIdentifier)(context, importSpecifier.name);
40
- const propertyName = (0, literal_1.transformPropertyName)(context, (_a = importSpecifier.propertyName) !== null && _a !== void 0 ? _a : importSpecifier.name);
41
+ // If imported value has a customName annotation use that, otherwise use regular property
42
+ const customName = (0, identifier_1.getCustomNameFromSymbol)(context, type.getSymbol());
43
+ const propertyName = customName
44
+ ? lua.createStringLiteral(customName, (_a = importSpecifier.propertyName) !== null && _a !== void 0 ? _a : importSpecifier.name)
45
+ : (0, literal_1.transformPropertyName)(context, (_b = importSpecifier.propertyName) !== null && _b !== void 0 ? _b : importSpecifier.name);
41
46
  return lua.createVariableDeclarationStatement(leftIdentifier, lua.createTableIndexExpression(moduleTableName, propertyName), importSpecifier);
42
47
  }
43
48
  const transformImportDeclaration = (statement, context) => {
@@ -153,8 +153,8 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
153
153
  statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, values, initializer));
154
154
  }
155
155
  else {
156
- // local vars = this.transpileDestructingAssignmentValue(node.initializer);
157
- const unpackedInitializer = (0, lua_ast_1.createUnpackCall)(context, context.transformExpression(initializer), initializer);
156
+ // use unpack(thing, 1, #bindingItems) to unpack
157
+ const unpackedInitializer = (0, lua_ast_1.createBoundedUnpackCall)(context, context.transformExpression(initializer), bindingPattern.elements.length, initializer);
158
158
  statements.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, vars, unpackedInitializer, initializer));
159
159
  }
160
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.31.4",
3
+ "version": "1.32.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/",