typescript-to-lua 1.7.2 → 1.8.2

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.
@@ -39,6 +39,9 @@ function validateOptions(options) {
39
39
  if (options.jsx && options.jsx !== typescript_1.JsxEmit.React) {
40
40
  diagnostics.push(diagnosticFactories.unsupportedJsxEmit());
41
41
  }
42
+ if (options.paths && !options.baseUrl) {
43
+ diagnostics.push(diagnosticFactories.pathsWithoutBaseUrl());
44
+ }
42
45
  return diagnostics;
43
46
  }
44
47
  exports.validateOptions = validateOptions;
package/dist/LuaLib.d.ts CHANGED
@@ -42,6 +42,7 @@ export declare enum LuaLibFeature {
42
42
  InstanceOf = "InstanceOf",
43
43
  InstanceOfObject = "InstanceOfObject",
44
44
  Iterator = "Iterator",
45
+ LuaIteratorSpread = "LuaIteratorSpread",
45
46
  Map = "Map",
46
47
  MathAtan2 = "MathAtan2",
47
48
  MathSign = "MathSign",
package/dist/LuaLib.js CHANGED
@@ -46,6 +46,7 @@ var LuaLibFeature;
46
46
  LuaLibFeature["InstanceOf"] = "InstanceOf";
47
47
  LuaLibFeature["InstanceOfObject"] = "InstanceOfObject";
48
48
  LuaLibFeature["Iterator"] = "Iterator";
49
+ LuaLibFeature["LuaIteratorSpread"] = "LuaIteratorSpread";
49
50
  LuaLibFeature["Map"] = "Map";
50
51
  LuaLibFeature["MathAtan2"] = "MathAtan2";
51
52
  LuaLibFeature["MathSign"] = "MathSign";
@@ -0,0 +1,9 @@
1
+ local function __TS__LuaIteratorSpread(self, state, firstKey)
2
+ local results = {}
3
+ local key, value = self(state, firstKey)
4
+ while key do
5
+ results[#results + 1] = {key, value}
6
+ key, value = self(state, key)
7
+ end
8
+ return __TS__Unpack(results)
9
+ end
@@ -1207,6 +1207,16 @@ local function __TS__InstanceOfObject(value)
1207
1207
  return valueType == "table" or valueType == "function"
1208
1208
  end
1209
1209
 
1210
+ local function __TS__LuaIteratorSpread(self, state, firstKey)
1211
+ local results = {}
1212
+ local key, value = self(state, firstKey)
1213
+ while key do
1214
+ results[#results + 1] = {key, value}
1215
+ key, value = self(state, key)
1216
+ end
1217
+ return __TS__Unpack(results)
1218
+ end
1219
+
1210
1220
  local Map
1211
1221
  do
1212
1222
  Map = __TS__Class()
@@ -2448,6 +2458,7 @@ return {
2448
2458
  __TS__InstanceOf = __TS__InstanceOf,
2449
2459
  __TS__InstanceOfObject = __TS__InstanceOfObject,
2450
2460
  __TS__Iterator = __TS__Iterator,
2461
+ __TS__LuaIteratorSpread = __TS__LuaIteratorSpread,
2451
2462
  Map = Map,
2452
2463
  __TS__MathAtan2 = __TS__MathAtan2,
2453
2464
  __TS__MathSign = __TS__MathSign,
@@ -264,6 +264,14 @@
264
264
  "Symbol"
265
265
  ]
266
266
  },
267
+ "LuaIteratorSpread": {
268
+ "exports": [
269
+ "__TS__LuaIteratorSpread"
270
+ ],
271
+ "dependencies": [
272
+ "Unpack"
273
+ ]
274
+ },
267
275
  "Map": {
268
276
  "exports": [
269
277
  "Map"
@@ -67,9 +67,7 @@ function transformBuiltinCallExpression(context, node) {
67
67
  exports.transformBuiltinCallExpression = transformBuiltinCallExpression;
68
68
  function tryTransformBuiltinGlobalMethodCall(context, node, calledMethod) {
69
69
  const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
70
- if (!(0, typescript_1.isStandardLibraryType)(context, ownerType, undefined))
71
- return;
72
- const ownerSymbol = ownerType.symbol;
70
+ const ownerSymbol = tryGetStandardLibrarySymbolOfType(context, ownerType);
73
71
  if (!ownerSymbol || ownerSymbol.parent)
74
72
  return;
75
73
  let result;
@@ -106,11 +104,10 @@ function tryTransformBuiltinGlobalMethodCall(context, node, calledMethod) {
106
104
  return result;
107
105
  }
108
106
  function tryTransformBuiltinPropertyCall(context, node, calledMethod) {
109
- var _a;
110
- const signatureDeclaration = (_a = context.checker.getResolvedSignature(node)) === null || _a === void 0 ? void 0 : _a.declaration;
111
- if (!signatureDeclaration || !(0, typescript_1.isStandardLibraryDeclaration)(context, signatureDeclaration))
107
+ const functionType = context.checker.getTypeAtLocation(node.expression);
108
+ const callSymbol = tryGetStandardLibrarySymbolOfType(context, functionType);
109
+ if (!callSymbol)
112
110
  return;
113
- const callSymbol = context.checker.getTypeAtLocation(signatureDeclaration).symbol;
114
111
  const ownerSymbol = callSymbol.parent;
115
112
  if (!ownerSymbol || ownerSymbol.parent)
116
113
  return;
@@ -187,4 +184,17 @@ function checkForLuaLibType(context, type) {
187
184
  }
188
185
  }
189
186
  exports.checkForLuaLibType = checkForLuaLibType;
187
+ function tryGetStandardLibrarySymbolOfType(context, type) {
188
+ if (type.isUnionOrIntersection()) {
189
+ for (const subType of type.types) {
190
+ const symbol = tryGetStandardLibrarySymbolOfType(context, subType);
191
+ if (symbol)
192
+ return symbol;
193
+ }
194
+ }
195
+ else if ((0, typescript_1.isStandardLibraryType)(context, type, undefined)) {
196
+ return type.symbol;
197
+ }
198
+ return undefined;
199
+ }
190
200
  //# sourceMappingURL=index.js.map
@@ -60,5 +60,6 @@ export declare enum IterableExtensionKind {
60
60
  Pairs = "Pairs",
61
61
  PairsKey = "PairsKey"
62
62
  }
63
+ export declare function isLuaIterable(context: TransformationContext, type: ts.Type): boolean;
63
64
  export declare function getIterableExtensionTypeForType(context: TransformationContext, type: ts.Type): IterableExtensionKind | undefined;
64
65
  export declare function getIterableExtensionKindForNode(context: TransformationContext, node: ts.Node): IterableExtensionKind | undefined;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getIterableExtensionKindForNode = exports.getIterableExtensionTypeForType = exports.IterableExtensionKind = exports.getExtensionKindForSymbol = exports.getExtensionKindForNode = exports.getExtensionKindForType = exports.ExtensionKind = void 0;
3
+ exports.getIterableExtensionKindForNode = exports.getIterableExtensionTypeForType = exports.isLuaIterable = exports.IterableExtensionKind = exports.getExtensionKindForSymbol = exports.getExtensionKindForNode = exports.getExtensionKindForType = exports.ExtensionKind = void 0;
4
4
  const ts = require("typescript");
5
5
  var ExtensionKind;
6
6
  (function (ExtensionKind) {
@@ -96,6 +96,10 @@ var IterableExtensionKind;
96
96
  IterableExtensionKind["Pairs"] = "Pairs";
97
97
  IterableExtensionKind["PairsKey"] = "PairsKey";
98
98
  })(IterableExtensionKind = exports.IterableExtensionKind || (exports.IterableExtensionKind = {}));
99
+ function isLuaIterable(context, type) {
100
+ return getPropertyValue(context, type, "__tstlIterable") !== undefined;
101
+ }
102
+ exports.isLuaIterable = isLuaIterable;
99
103
  function getIterableExtensionTypeForType(context, type) {
100
104
  const value = getPropertyValue(context, type, "__tstlIterable");
101
105
  if (value && value in IterableExtensionKind) {
@@ -11,7 +11,9 @@ function isMultiReturnType(type) {
11
11
  }
12
12
  exports.isMultiReturnType = isMultiReturnType;
13
13
  function canBeMultiReturnType(type) {
14
- return isMultiReturnType(type) || (type.isUnion() && type.types.some(t => canBeMultiReturnType(t)));
14
+ return ((type.flags & ts.TypeFlags.Any) !== 0 ||
15
+ isMultiReturnType(type) ||
16
+ (type.isUnion() && type.types.some(t => canBeMultiReturnType(t))));
15
17
  }
16
18
  exports.canBeMultiReturnType = canBeMultiReturnType;
17
19
  function isMultiFunctionCall(context, expression) {
@@ -12,28 +12,33 @@ const diagnostics_1 = require("../utils/diagnostics");
12
12
  const typescript_1 = require("../utils/typescript");
13
13
  function transformExpressionsInReturn(context, node, insideTryCatch) {
14
14
  const expressionType = context.checker.getTypeAtLocation(node);
15
- if (ts.isCallExpression(node)) {
15
+ // skip type assertions
16
+ // don't skip parenthesis as it may arise confusion with lua behavior (where parenthesis are significant)
17
+ const innerNode = ts.skipOuterExpressions(node, ts.OuterExpressionKinds.Assertions);
18
+ if (ts.isCallExpression(innerNode)) {
16
19
  // $multi(...)
17
- if ((0, multi_1.isMultiFunctionCall)(context, node)) {
20
+ if ((0, multi_1.isMultiFunctionCall)(context, innerNode)) {
18
21
  // Don't allow $multi to be implicitly cast to something other than LuaMultiReturn
19
22
  const type = context.checker.getContextualType(node);
20
23
  if (type && !(0, multi_1.canBeMultiReturnType)(type)) {
21
- context.diagnostics.push((0, diagnostics_1.invalidMultiFunctionReturnType)(node));
24
+ context.diagnostics.push((0, diagnostics_1.invalidMultiFunctionReturnType)(innerNode));
22
25
  }
23
- let returnValues = (0, call_1.transformArguments)(context, node.arguments);
26
+ let returnValues = (0, call_1.transformArguments)(context, innerNode.arguments);
24
27
  if (insideTryCatch) {
25
28
  returnValues = [(0, lua_ast_1.wrapInTable)(...returnValues)]; // Wrap results when returning inside try/catch
26
29
  }
27
30
  return returnValues;
28
31
  }
29
32
  // Force-wrap LuaMultiReturn when returning inside try/catch
30
- if (insideTryCatch && (0, multi_1.returnsMultiType)(context, node) && !(0, multi_1.shouldMultiReturnCallBeWrapped)(context, node)) {
33
+ if (insideTryCatch &&
34
+ (0, multi_1.returnsMultiType)(context, innerNode) &&
35
+ !(0, multi_1.shouldMultiReturnCallBeWrapped)(context, innerNode)) {
31
36
  return [(0, lua_ast_1.wrapInTable)(context.transformExpression(node))];
32
37
  }
33
38
  }
34
- else if ((0, multi_1.isInMultiReturnFunction)(context, node) && (0, multi_1.isMultiReturnType)(expressionType)) {
39
+ else if ((0, multi_1.isInMultiReturnFunction)(context, innerNode) && (0, multi_1.isMultiReturnType)(expressionType)) {
35
40
  // Unpack objects typed as LuaMultiReturn
36
- return [(0, lua_ast_1.createUnpackCall)(context, context.transformExpression(node), node)];
41
+ return [(0, lua_ast_1.createUnpackCall)(context, context.transformExpression(innerNode), innerNode)];
37
42
  }
38
43
  return [context.transformExpression(node)];
39
44
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformSpreadElement = exports.isOptimizedVarArgSpread = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
+ const language_extensions_1 = require("../utils/language-extensions");
6
7
  const lua_ast_1 = require("../utils/lua-ast");
7
8
  const lualib_1 = require("../utils/lualib");
8
9
  const scope_1 = require("../utils/scope");
@@ -60,6 +61,9 @@ const transformSpreadElement = (node, context) => {
60
61
  if ((0, multi_1.isMultiReturnCall)(context, tsInnerExpression))
61
62
  return innerExpression;
62
63
  const type = context.checker.getTypeAtLocation(node.expression); // not ts-inner expression, in case of casts
64
+ if ((0, language_extensions_1.isLuaIterable)(context, type)) {
65
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.LuaIteratorSpread, node, innerExpression);
66
+ }
63
67
  if ((0, typescript_1.isArrayType)(context, type)) {
64
68
  return (0, lua_ast_1.createUnpackCall)(context, innerExpression, node);
65
69
  }
@@ -32,3 +32,6 @@ export declare const cannotBundleLibrary: (() => ts.Diagnostic) & {
32
32
  export declare const unsupportedJsxEmit: (() => ts.Diagnostic) & {
33
33
  code: number;
34
34
  };
35
+ export declare const pathsWithoutBaseUrl: (() => ts.Diagnostic) & {
36
+ code: number;
37
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unsupportedJsxEmit = exports.cannotBundleLibrary = exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = exports.luaBundleEntryIsRequired = exports.couldNotFindBundleEntryPoint = exports.transformerShouldBeATsTransformerFactory = exports.shouldHaveAExport = exports.couldNotResolveFrom = exports.toLoadItShouldBeTranspiled = exports.couldNotReadDependency = exports.couldNotResolveRequire = void 0;
3
+ exports.pathsWithoutBaseUrl = exports.unsupportedJsxEmit = exports.cannotBundleLibrary = exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = exports.luaBundleEntryIsRequired = exports.couldNotFindBundleEntryPoint = exports.transformerShouldBeATsTransformerFactory = exports.shouldHaveAExport = exports.couldNotResolveFrom = exports.toLoadItShouldBeTranspiled = exports.couldNotReadDependency = exports.couldNotResolveRequire = void 0;
4
4
  const ts = require("typescript");
5
5
  const utils_1 = require("../utils");
6
6
  const createDiagnosticFactory = (getMessage, category = ts.DiagnosticCategory.Error) => (0, utils_1.createSerialDiagnosticFactory)((...args) => ({ messageText: getMessage(...args), category }));
@@ -19,4 +19,5 @@ exports.usingLuaBundleWithInlineMightGenerateDuplicateCode = (0, utils_1.createS
19
19
  }));
20
20
  exports.cannotBundleLibrary = createDiagnosticFactory(() => 'Cannot bundle probjects with"buildmode": "library". Projects including the library can still bundle (which will include external library files).');
21
21
  exports.unsupportedJsxEmit = createDiagnosticFactory(() => 'JSX is only supported with "react" jsx option.');
22
+ exports.pathsWithoutBaseUrl = createDiagnosticFactory(() => "When configuring 'paths' in tsconfig.json, the option 'baseUrl' must also be provided.");
22
23
  //# sourceMappingURL=diagnostics.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.7.2",
3
+ "version": "1.8.2",
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/",