typescript-to-lua 1.4.0 → 1.4.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.
package/dist/LuaLib.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare enum LuaLibFeature {
8
8
  ArrayForEach = "ArrayForEach",
9
9
  ArrayFind = "ArrayFind",
10
10
  ArrayFindIndex = "ArrayFindIndex",
11
+ ArrayFrom = "ArrayFrom",
11
12
  ArrayIncludes = "ArrayIncludes",
12
13
  ArrayIndexOf = "ArrayIndexOf",
13
14
  ArrayIsArray = "ArrayIsArray",
package/dist/LuaLib.js CHANGED
@@ -12,6 +12,7 @@ var LuaLibFeature;
12
12
  LuaLibFeature["ArrayForEach"] = "ArrayForEach";
13
13
  LuaLibFeature["ArrayFind"] = "ArrayFind";
14
14
  LuaLibFeature["ArrayFindIndex"] = "ArrayFindIndex";
15
+ LuaLibFeature["ArrayFrom"] = "ArrayFrom";
15
16
  LuaLibFeature["ArrayIncludes"] = "ArrayIncludes";
16
17
  LuaLibFeature["ArrayIndexOf"] = "ArrayIndexOf";
17
18
  LuaLibFeature["ArrayIsArray"] = "ArrayIsArray";
@@ -129,28 +129,28 @@ class LuaPrinter {
129
129
  }
130
130
  printFile(file) {
131
131
  var _a;
132
- let header = file.trivia;
132
+ let sourceChunks = [file.trivia];
133
133
  if (!this.options.noHeader) {
134
- header += exports.tstlHeader;
134
+ sourceChunks.push(exports.tstlHeader);
135
135
  }
136
- let statements = file.statements;
137
136
  const luaLibImport = (_a = this.options.luaLibImport) !== null && _a !== void 0 ? _a : CompilerOptions_1.LuaLibImportKind.Require;
138
137
  if (luaLibImport === CompilerOptions_1.LuaLibImportKind.Require && file.luaLibFeatures.size > 0) {
139
138
  // Import lualib features
140
- const importStatements = (0, LuaLib_1.loadImportedLualibFeatures)(file.luaLibFeatures, this.emitHost);
141
- statements = importStatements.concat(statements);
139
+ sourceChunks = this.printStatementArray((0, LuaLib_1.loadImportedLualibFeatures)(file.luaLibFeatures, this.emitHost));
142
140
  }
143
141
  else if (luaLibImport === CompilerOptions_1.LuaLibImportKind.Inline && file.luaLibFeatures.size > 0) {
144
142
  // Inline lualib features
145
- header += "-- Lua Library inline imports\n";
146
- header += (0, LuaLib_1.loadInlineLualibFeatures)(file.luaLibFeatures, this.emitHost);
143
+ sourceChunks.push("-- Lua Library inline imports\n");
144
+ sourceChunks.push((0, LuaLib_1.loadInlineLualibFeatures)(file.luaLibFeatures, this.emitHost));
147
145
  }
148
146
  if (this.options.sourceMapTraceback && !(0, CompilerOptions_1.isBundleEnabled)(this.options)) {
149
147
  // In bundle mode the traceback is being generated for the entire file in getBundleResult
150
148
  // Otherwise, traceback is being generated locally
151
- header += `${LuaPrinter.sourceMapTracebackPlaceholder}\n`;
149
+ sourceChunks.push(`${LuaPrinter.sourceMapTracebackPlaceholder}\n`);
152
150
  }
153
- return this.concatNodes(header, ...this.printStatementArray(statements));
151
+ // Print reest of the statements in file
152
+ sourceChunks.push(...this.printStatementArray(file.statements));
153
+ return this.concatNodes(...sourceChunks);
154
154
  }
155
155
  pushIndent() {
156
156
  this.currentIndent += " ";
package/dist/index.d.ts CHANGED
@@ -8,4 +8,4 @@ export { LuaLibFeature } from "./LuaLib";
8
8
  export * from "./LuaPrinter";
9
9
  export * from "./transformation/context";
10
10
  export * from "./transpilation";
11
- export { ProcessedFile } from "./transpilation/utils";
11
+ export { EmitHost, EmitFile, ProcessedFile } from "./transpilation/utils";
@@ -0,0 +1,32 @@
1
+ local __TS__ArrayFrom
2
+ do
3
+ local function arrayLikeStep(self, index)
4
+ index = index + 1
5
+ if index > self.length then
6
+ return
7
+ end
8
+ return index, self[index]
9
+ end
10
+ local function arrayLikeIterator(arr)
11
+ if type(arr.length) == "number" then
12
+ return arrayLikeStep, arr, 0
13
+ end
14
+ return __TS__Iterator(arr)
15
+ end
16
+ function __TS__ArrayFrom(arrayLike, mapFn, thisArg)
17
+ local result = {}
18
+ if mapFn == nil then
19
+ for ____, v in arrayLikeIterator(arrayLike) do
20
+ __TS__ArrayPush(result, v)
21
+ end
22
+ else
23
+ for i, v in arrayLikeIterator(arrayLike) do
24
+ __TS__ArrayPush(
25
+ result,
26
+ mapFn(thisArg, v, i - 1)
27
+ )
28
+ end
29
+ end
30
+ return result
31
+ end
32
+ end
@@ -115,6 +115,88 @@ local function __TS__ArrayFindIndex(arr, callbackFn)
115
115
  return -1
116
116
  end
117
117
 
118
+ local function __TS__ArrayPush(arr, ...)
119
+ local items = {...}
120
+ for ____, item in ipairs(items) do
121
+ arr[#arr + 1] = item
122
+ end
123
+ return #arr
124
+ end
125
+
126
+ local __TS__Iterator
127
+ do
128
+ local function iteratorGeneratorStep(self)
129
+ local co = self.____coroutine
130
+ local status, value = coroutine.resume(co)
131
+ if not status then
132
+ error(value, 0)
133
+ end
134
+ if coroutine.status(co) == "dead" then
135
+ return
136
+ end
137
+ return true, value
138
+ end
139
+ local function iteratorIteratorStep(self)
140
+ local result = self:next()
141
+ if result.done then
142
+ return
143
+ end
144
+ return true, result.value
145
+ end
146
+ local function iteratorStringStep(self, index)
147
+ index = index + 1
148
+ if index > #self then
149
+ return
150
+ end
151
+ return index, string.sub(self, index, index)
152
+ end
153
+ function __TS__Iterator(iterable)
154
+ if type(iterable) == "string" then
155
+ return iteratorStringStep, iterable, 0
156
+ elseif iterable.____coroutine ~= nil then
157
+ return iteratorGeneratorStep, iterable
158
+ elseif iterable[Symbol.iterator] then
159
+ local iterator = iterable[Symbol.iterator](iterable)
160
+ return iteratorIteratorStep, iterator
161
+ else
162
+ return ipairs(iterable)
163
+ end
164
+ end
165
+ end
166
+
167
+ local __TS__ArrayFrom
168
+ do
169
+ local function arrayLikeStep(self, index)
170
+ index = index + 1
171
+ if index > self.length then
172
+ return
173
+ end
174
+ return index, self[index]
175
+ end
176
+ local function arrayLikeIterator(arr)
177
+ if type(arr.length) == "number" then
178
+ return arrayLikeStep, arr, 0
179
+ end
180
+ return __TS__Iterator(arr)
181
+ end
182
+ function __TS__ArrayFrom(arrayLike, mapFn, thisArg)
183
+ local result = {}
184
+ if mapFn == nil then
185
+ for ____, v in arrayLikeIterator(arrayLike) do
186
+ __TS__ArrayPush(result, v)
187
+ end
188
+ else
189
+ for i, v in arrayLikeIterator(arrayLike) do
190
+ __TS__ArrayPush(
191
+ result,
192
+ mapFn(thisArg, v, i - 1)
193
+ )
194
+ end
195
+ end
196
+ return result
197
+ end
198
+ end
199
+
118
200
  local function __TS__ArrayIncludes(self, searchElement, fromIndex)
119
201
  if fromIndex == nil then
120
202
  fromIndex = 0
@@ -194,14 +276,6 @@ local function __TS__ArrayMap(arr, callbackfn)
194
276
  return newArray
195
277
  end
196
278
 
197
- local function __TS__ArrayPush(arr, ...)
198
- local items = {...}
199
- for ____, item in ipairs(items) do
200
- arr[#arr + 1] = item
201
- end
202
- return #arr
203
- end
204
-
205
279
  local function __TS__ArrayReduce(arr, callbackFn, ...)
206
280
  local len = #arr
207
281
  local k = 0
@@ -1158,47 +1232,6 @@ local function __TS__InstanceOfObject(value)
1158
1232
  return valueType == "table" or valueType == "function"
1159
1233
  end
1160
1234
 
1161
- local __TS__Iterator
1162
- do
1163
- local function iteratorGeneratorStep(self)
1164
- local co = self.____coroutine
1165
- local status, value = coroutine.resume(co)
1166
- if not status then
1167
- error(value, 0)
1168
- end
1169
- if coroutine.status(co) == "dead" then
1170
- return
1171
- end
1172
- return true, value
1173
- end
1174
- local function iteratorIteratorStep(self)
1175
- local result = self:next()
1176
- if result.done then
1177
- return
1178
- end
1179
- return true, result.value
1180
- end
1181
- local function iteratorStringStep(self, index)
1182
- index = index + 1
1183
- if index > #self then
1184
- return
1185
- end
1186
- return index, string.sub(self, index, index)
1187
- end
1188
- function __TS__Iterator(iterable)
1189
- if type(iterable) == "string" then
1190
- return iteratorStringStep, iterable, 0
1191
- elseif iterable.____coroutine ~= nil then
1192
- return iteratorGeneratorStep, iterable
1193
- elseif iterable[Symbol.iterator] then
1194
- local iterator = iterable[Symbol.iterator](iterable)
1195
- return iteratorIteratorStep, iterator
1196
- else
1197
- return ipairs(iterable)
1198
- end
1199
- end
1200
- end
1201
-
1202
1235
  local Map
1203
1236
  do
1204
1237
  Map = __TS__Class()
@@ -2372,6 +2405,7 @@ return {
2372
2405
  __TS__ArrayForEach = __TS__ArrayForEach,
2373
2406
  __TS__ArrayFind = __TS__ArrayFind,
2374
2407
  __TS__ArrayFindIndex = __TS__ArrayFindIndex,
2408
+ __TS__ArrayFrom = __TS__ArrayFrom,
2375
2409
  __TS__ArrayIncludes = __TS__ArrayIncludes,
2376
2410
  __TS__ArrayIndexOf = __TS__ArrayIndexOf,
2377
2411
  __TS__ArrayIsArray = __TS__ArrayIsArray,
@@ -40,6 +40,15 @@
40
40
  "__TS__ArrayFindIndex"
41
41
  ]
42
42
  },
43
+ "ArrayFrom": {
44
+ "exports": [
45
+ "__TS__ArrayFrom"
46
+ ],
47
+ "dependencies": [
48
+ "ArrayPush",
49
+ "Iterator"
50
+ ]
51
+ },
43
52
  "ArrayIncludes": {
44
53
  "exports": [
45
54
  "__TS__ArrayIncludes"
@@ -2,6 +2,6 @@ import * as ts from "typescript";
2
2
  import * as lua from "../../LuaAST";
3
3
  import { TransformationContext } from "../context";
4
4
  import { PropertyCallExpression } from "../visitors/call";
5
- export declare function transformArrayConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.CallExpression | undefined;
5
+ export declare function transformArrayConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined;
6
6
  export declare function transformArrayPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.CallExpression | undefined;
7
7
  export declare function transformArrayProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.UnaryExpression | undefined;
@@ -6,14 +6,19 @@ const diagnostics_1 = require("../utils/diagnostics");
6
6
  const lualib_1 = require("../utils/lualib");
7
7
  const call_1 = require("../visitors/call");
8
8
  const typescript_1 = require("../utils/typescript");
9
+ const lua_ast_1 = require("../utils/lua-ast");
9
10
  function transformArrayConstructorCall(context, node) {
10
11
  const expression = node.expression;
11
12
  const signature = context.checker.getResolvedSignature(node);
12
13
  const params = (0, call_1.transformArguments)(context, node.arguments, signature);
13
14
  const expressionName = expression.name.text;
14
15
  switch (expressionName) {
16
+ case "from":
17
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayFrom, node, ...params);
15
18
  case "isArray":
16
19
  return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayIsArray, node, ...params);
20
+ case "of":
21
+ return (0, lua_ast_1.wrapInTable)(...params);
17
22
  default:
18
23
  context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "Array", expressionName));
19
24
  }
@@ -3,7 +3,7 @@ import { EmitHost } from "..";
3
3
  import { CompilerOptions } from "../CompilerOptions";
4
4
  import { Printer } from "../LuaPrinter";
5
5
  import { Visitors } from "../transformation/context";
6
- import { ProcessedFile } from "./utils";
6
+ import { EmitFile, ProcessedFile } from "./utils";
7
7
  export interface Plugin {
8
8
  /**
9
9
  * An augmentation to the map of visitors that transform TypeScript AST to Lua AST.
@@ -22,8 +22,15 @@ export interface Plugin {
22
22
  */
23
23
  beforeTransform?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost) => ts.Diagnostic[] | void;
24
24
  /**
25
- * This function is called after TypeScriptToLua has translated the input program to Lua.
25
+ * This function is called after translating the input program to Lua, but before resolving dependencies or bundling.
26
26
  */
27
27
  afterPrint?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost, result: ProcessedFile[]) => ts.Diagnostic[] | void;
28
+ /**
29
+ * This function is called after translating the input program to Lua, after resolving dependencies and after bundling.
30
+ */
31
+ beforeEmit?: (program: ts.Program, options: CompilerOptions, emitHost: EmitHost, result: EmitFile[]) => ts.Diagnostic[] | void;
28
32
  }
29
- export declare function getPlugins(program: ts.Program, diagnostics: ts.Diagnostic[], customPlugins: Plugin[]): Plugin[];
33
+ export declare function getPlugins(program: ts.Program): {
34
+ diagnostics: ts.Diagnostic[];
35
+ plugins: Plugin[];
36
+ };
@@ -2,8 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPlugins = void 0;
4
4
  const utils_1 = require("./utils");
5
- function getPlugins(program, diagnostics, customPlugins) {
5
+ function getPlugins(program) {
6
6
  var _a;
7
+ const diagnostics = [];
7
8
  const pluginsFromOptions = [];
8
9
  const options = program.getCompilerOptions();
9
10
  for (const [index, pluginOption] of ((_a = options.luaPlugins) !== null && _a !== void 0 ? _a : []).entries()) {
@@ -16,7 +17,10 @@ function getPlugins(program, diagnostics, customPlugins) {
16
17
  const plugin = typeof factory === "function" ? factory(pluginOption) : factory;
17
18
  pluginsFromOptions.push(plugin);
18
19
  }
19
- return [...customPlugins, ...pluginsFromOptions];
20
+ if (options.tstlVerbose) {
21
+ console.log(`Loaded ${pluginsFromOptions.length} plugins`);
22
+ }
23
+ return { diagnostics, plugins: pluginsFromOptions };
20
24
  }
21
25
  exports.getPlugins = getPlugins;
22
26
  //# sourceMappingURL=plugins.js.map
@@ -11,4 +11,4 @@ export interface TranspileResult {
11
11
  diagnostics: ts.Diagnostic[];
12
12
  transpiledFiles: ProcessedFile[];
13
13
  }
14
- export declare function getProgramTranspileResult(emitHost: EmitHost, writeFileResult: ts.WriteFileCallback, { program, sourceFiles: targetSourceFiles, customTransformers, plugins: customPlugins }: TranspileOptions): TranspileResult;
14
+ export declare function getProgramTranspileResult(emitHost: EmitHost, writeFileResult: ts.WriteFileCallback, { program, sourceFiles: targetSourceFiles, customTransformers, plugins }: TranspileOptions): TranspileResult;
@@ -7,9 +7,8 @@ const CompilerOptions_1 = require("../CompilerOptions");
7
7
  const LuaPrinter_1 = require("../LuaPrinter");
8
8
  const transformation_1 = require("../transformation");
9
9
  const utils_1 = require("../utils");
10
- const plugins_1 = require("./plugins");
11
10
  const transformers_1 = require("./transformers");
12
- function getProgramTranspileResult(emitHost, writeFileResult, { program, sourceFiles: targetSourceFiles, customTransformers = {}, plugins: customPlugins = [] }) {
11
+ function getProgramTranspileResult(emitHost, writeFileResult, { program, sourceFiles: targetSourceFiles, customTransformers = {}, plugins = [] }) {
13
12
  var _a, _b;
14
13
  const options = program.getCompilerOptions();
15
14
  if (options.tstlVerbose) {
@@ -40,10 +39,6 @@ function getProgramTranspileResult(emitHost, writeFileResult, { program, sourceF
40
39
  return { diagnostics: preEmitDiagnostics, transpiledFiles };
41
40
  }
42
41
  }
43
- const plugins = (0, plugins_1.getPlugins)(program, diagnostics, customPlugins);
44
- if (options.tstlVerbose) {
45
- console.log(`Successfully loaded ${plugins.length} plugins`);
46
- }
47
42
  for (const plugin of plugins) {
48
43
  if (plugin.beforeTransform) {
49
44
  const pluginDiagnostics = (_a = plugin.beforeTransform(program, options, emitHost)) !== null && _a !== void 0 ? _a : [];
@@ -7,6 +7,7 @@ const CompilerOptions_1 = require("../CompilerOptions");
7
7
  const LuaLib_1 = require("../LuaLib");
8
8
  const utils_1 = require("../utils");
9
9
  const bundle_1 = require("./bundle");
10
+ const plugins_1 = require("./plugins");
10
11
  const resolve_1 = require("./resolve");
11
12
  const transpile_1 = require("./transpile");
12
13
  class Transpiler {
@@ -14,18 +15,27 @@ class Transpiler {
14
15
  this.emitHost = emitHost;
15
16
  }
16
17
  emit(emitOptions) {
17
- var _a;
18
+ var _a, _b;
18
19
  const { program, writeFile = this.emitHost.writeFile } = emitOptions;
19
- const verbose = program.getCompilerOptions().tstlVerbose;
20
- const { diagnostics, transpiledFiles: freshFiles } = (0, transpile_1.getProgramTranspileResult)(this.emitHost, writeFile, emitOptions);
20
+ const options = program.getCompilerOptions();
21
+ const { diagnostics: getPluginsDiagnostics, plugins } = (0, plugins_1.getPlugins)(program);
22
+ const { diagnostics, transpiledFiles: freshFiles } = (0, transpile_1.getProgramTranspileResult)(this.emitHost, writeFile, {
23
+ ...emitOptions,
24
+ plugins,
25
+ });
21
26
  const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles);
22
- if (verbose) {
27
+ if (options.tstlVerbose) {
23
28
  console.log("Emitting output");
24
29
  }
25
- const options = program.getCompilerOptions();
26
- const emitBOM = (_a = options.emitBOM) !== null && _a !== void 0 ? _a : false;
30
+ for (const plugin of plugins) {
31
+ if (plugin.beforeEmit) {
32
+ const beforeEmitPluginDiagnostics = (_a = plugin.beforeEmit(program, options, this.emitHost, emitPlan)) !== null && _a !== void 0 ? _a : [];
33
+ diagnostics.push(...beforeEmitPluginDiagnostics);
34
+ }
35
+ }
36
+ const emitBOM = (_b = options.emitBOM) !== null && _b !== void 0 ? _b : false;
27
37
  for (const { outputPath, code, sourceMap, sourceFiles } of emitPlan) {
28
- if (verbose) {
38
+ if (options.tstlVerbose) {
29
39
  console.log(`Emitting ${(0, utils_1.normalizeSlashes)(outputPath)}`);
30
40
  }
31
41
  writeFile(outputPath, code, emitBOM, undefined, sourceFiles);
@@ -33,10 +43,10 @@ class Transpiler {
33
43
  writeFile(outputPath + ".map", sourceMap, emitBOM, undefined, sourceFiles);
34
44
  }
35
45
  }
36
- if (verbose) {
46
+ if (options.tstlVerbose) {
37
47
  console.log("Emit finished!");
38
48
  }
39
- return { diagnostics, emitSkipped: emitPlan.length === 0 };
49
+ return { diagnostics: getPluginsDiagnostics.concat(diagnostics), emitSkipped: emitPlan.length === 0 };
40
50
  }
41
51
  getEmitPlan(program, diagnostics, files) {
42
52
  const options = program.getCompilerOptions();
@@ -1,3 +1,4 @@
1
+ import { SourceNode } from "source-map";
1
2
  import * as ts from "typescript";
2
3
  import * as lua from "../LuaAST";
3
4
  export interface EmitHost {
@@ -15,6 +16,7 @@ interface BaseFile {
15
16
  export interface ProcessedFile extends BaseFile {
16
17
  fileName: string;
17
18
  luaAst?: lua.File;
19
+ sourceMapNode?: SourceNode;
18
20
  }
19
21
  export interface EmitFile extends BaseFile {
20
22
  outputPath: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
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/",