typescript-to-lua 1.4.3 → 1.6.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 (45) hide show
  1. package/dist/CompilerOptions.d.ts +1 -0
  2. package/dist/cli/parse.js +5 -0
  3. package/dist/lualib/Await.lua +21 -34
  4. package/dist/lualib/SourceMapTraceBack.lua +14 -1
  5. package/dist/lualib/lualib_bundle.lua +35 -35
  6. package/dist/lualib-build/plugin.d.ts +1 -1
  7. package/dist/transformation/builtins/array.d.ts +2 -3
  8. package/dist/transformation/builtins/array.js +8 -10
  9. package/dist/transformation/builtins/console.d.ts +2 -2
  10. package/dist/transformation/builtins/console.js +8 -9
  11. package/dist/transformation/builtins/function.d.ts +1 -2
  12. package/dist/transformation/builtins/function.js +5 -6
  13. package/dist/transformation/builtins/index.js +22 -23
  14. package/dist/transformation/builtins/math.d.ts +1 -2
  15. package/dist/transformation/builtins/math.js +3 -4
  16. package/dist/transformation/builtins/number.d.ts +3 -3
  17. package/dist/transformation/builtins/number.js +10 -12
  18. package/dist/transformation/builtins/object.d.ts +2 -3
  19. package/dist/transformation/builtins/object.js +16 -17
  20. package/dist/transformation/builtins/promise.d.ts +1 -2
  21. package/dist/transformation/builtins/promise.js +5 -6
  22. package/dist/transformation/builtins/string.d.ts +2 -3
  23. package/dist/transformation/builtins/string.js +7 -9
  24. package/dist/transformation/builtins/symbol.d.ts +2 -2
  25. package/dist/transformation/builtins/symbol.js +6 -7
  26. package/dist/transformation/context/context.d.ts +2 -2
  27. package/dist/transformation/context/context.js +1 -1
  28. package/dist/transformation/utils/export.d.ts +1 -1
  29. package/dist/transformation/utils/function-context.js +1 -1
  30. package/dist/transformation/utils/lua-ast.d.ts +1 -1
  31. package/dist/transformation/utils/lua-ast.js +83 -1
  32. package/dist/transformation/utils/typescript/index.js +2 -2
  33. package/dist/transformation/visitors/async-await.d.ts +1 -1
  34. package/dist/transformation/visitors/async-await.js +7 -14
  35. package/dist/transformation/visitors/binary-expression/compound.d.ts +4 -1
  36. package/dist/transformation/visitors/binary-expression/compound.js +10 -19
  37. package/dist/transformation/visitors/call.d.ts +1 -3
  38. package/dist/transformation/visitors/call.js +25 -14
  39. package/dist/transformation/visitors/errors.js +52 -11
  40. package/dist/transformation/visitors/function.js +1 -1
  41. package/dist/transformation/visitors/return.js +5 -4
  42. package/dist/transpilation/index.js +9 -4
  43. package/dist/transpilation/resolve.js +2 -2
  44. package/dist/transpilation/transpiler.js +3 -2
  45. package/package.json +9 -9
@@ -5,21 +5,58 @@ const __1 = require("../..");
5
5
  const lua = require("../../LuaAST");
6
6
  const diagnostics_1 = require("../utils/diagnostics");
7
7
  const lua_ast_1 = require("../utils/lua-ast");
8
+ const lualib_1 = require("../utils/lualib");
8
9
  const scope_1 = require("../utils/scope");
9
10
  const typescript_1 = require("../utils/typescript");
11
+ const async_await_1 = require("./async-await");
10
12
  const block_1 = require("./block");
11
13
  const identifier_1 = require("./identifier");
12
14
  const multi_1 = require("./language-extensions/multi");
13
15
  const return_1 = require("./return");
14
- const transformTryStatement = (statement, context) => {
15
- var _a;
16
- const [tryBlock, tryScope] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
17
- if (context.options.luaTarget === __1.LuaTarget.Lua51 &&
18
- (0, typescript_1.isInAsyncFunction)(statement) &&
19
- !context.options.lua51AllowTryCatchInAsyncAwait) {
16
+ const transformAsyncTry = (statement, context) => {
17
+ const [tryBlock] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
18
+ if (context.options.luaTarget === __1.LuaTarget.Lua51 && !context.options.lua51AllowTryCatchInAsyncAwait) {
20
19
  context.diagnostics.push((0, diagnostics_1.unsupportedForTargetButOverrideAvailable)(statement, "try/catch inside async functions", __1.LuaTarget.Lua51, "lua51AllowTryCatchInAsyncAwait"));
21
20
  return tryBlock.statements;
22
21
  }
22
+ // __TS__AsyncAwaiter(<catch block>)
23
+ const awaiter = (0, async_await_1.wrapInAsyncAwaiter)(context, tryBlock.statements, false);
24
+ const awaiterIdentifier = lua.createIdentifier("____try");
25
+ const awaiterDefinition = lua.createVariableDeclarationStatement(awaiterIdentifier, awaiter);
26
+ // local ____try = __TS__AsyncAwaiter(<catch block>)
27
+ const result = [awaiterDefinition];
28
+ if (statement.finallyBlock) {
29
+ const awaiterFinally = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("finally"));
30
+ const finallyFunction = lua.createFunctionExpression(lua.createBlock(context.transformStatements(statement.finallyBlock.statements)));
31
+ const finallyCall = lua.createCallExpression(awaiterFinally, [awaiterIdentifier, finallyFunction], statement.finallyBlock);
32
+ // ____try.finally(<finally function>)
33
+ result.push(lua.createExpressionStatement(finallyCall));
34
+ }
35
+ if (statement.catchClause) {
36
+ // ____try.catch(<catch function>)
37
+ const [catchFunction] = transformCatchClause(context, statement.catchClause);
38
+ if (catchFunction.params) {
39
+ catchFunction.params.unshift(lua.createAnonymousIdentifier());
40
+ }
41
+ const awaiterCatch = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("catch"));
42
+ const catchCall = lua.createCallExpression(awaiterCatch, [awaiterIdentifier, catchFunction]);
43
+ // await ____try.catch(<catch function>)
44
+ const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, catchCall);
45
+ result.push(lua.createExpressionStatement(promiseAwait, statement));
46
+ }
47
+ else {
48
+ // await ____try
49
+ const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, awaiterIdentifier);
50
+ result.push(lua.createExpressionStatement(promiseAwait, statement));
51
+ }
52
+ return result;
53
+ };
54
+ const transformTryStatement = (statement, context) => {
55
+ var _a;
56
+ if ((0, typescript_1.isInAsyncFunction)(statement)) {
57
+ return transformAsyncTry(statement, context);
58
+ }
59
+ const [tryBlock, tryScope] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
23
60
  if (context.options.luaTarget === __1.LuaTarget.Lua51 && (0, typescript_1.isInGeneratorFunction)(statement)) {
24
61
  context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "try/catch inside generator functions", __1.LuaTarget.Lua51));
25
62
  return tryBlock.statements;
@@ -33,11 +70,7 @@ const transformTryStatement = (statement, context) => {
33
70
  const tryCall = lua.createCallExpression(pCall, [lua.createFunctionExpression(tryBlock)]);
34
71
  if (statement.catchClause && statement.catchClause.block.statements.length > 0) {
35
72
  // try with catch
36
- const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, statement.catchClause.block, scope_1.ScopeType.Catch);
37
- const catchParameter = statement.catchClause.variableDeclaration
38
- ? (0, identifier_1.transformIdentifier)(context, statement.catchClause.variableDeclaration.name)
39
- : undefined;
40
- const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []);
73
+ const [catchFunction, catchScope] = transformCatchClause(context, statement.catchClause);
41
74
  const catchIdentifier = lua.createIdentifier("____catch");
42
75
  result.push(lua.createVariableDeclarationStatement(catchIdentifier, catchFunction));
43
76
  const hasReturn = (_a = tryScope.functionReturned) !== null && _a !== void 0 ? _a : catchScope.functionReturned;
@@ -96,4 +129,12 @@ const transformThrowStatement = (statement, context) => {
96
129
  return lua.createExpressionStatement(lua.createCallExpression(lua.createIdentifier("error"), parameters), statement);
97
130
  };
98
131
  exports.transformThrowStatement = transformThrowStatement;
132
+ function transformCatchClause(context, catchClause) {
133
+ const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, catchClause.block, scope_1.ScopeType.Catch);
134
+ const catchParameter = catchClause.variableDeclaration
135
+ ? (0, identifier_1.transformIdentifier)(context, catchClause.variableDeclaration.name)
136
+ : undefined;
137
+ const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []);
138
+ return [catchFunction, catchScope];
139
+ }
99
140
  //# sourceMappingURL=errors.js.map
@@ -114,7 +114,7 @@ function transformFunctionBody(context, parameters, body, spreadIdentifier, node
114
114
  scope.node = node;
115
115
  let bodyStatements = transformFunctionBodyContent(context, body);
116
116
  if (node && (0, async_await_1.isAsyncFunction)(node)) {
117
- bodyStatements = (0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements);
117
+ bodyStatements = [lua.createReturnStatement([(0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements)])];
118
118
  }
119
119
  const headerStatements = transformFunctionBodyHeader(context, scope, parameters, spreadIdentifier);
120
120
  (0, scope_1.popScope)(context);
@@ -61,14 +61,15 @@ const transformReturnStatement = (statement, context) => {
61
61
  exports.transformReturnStatement = transformReturnStatement;
62
62
  function createReturnStatement(context, values, node) {
63
63
  const results = [...values];
64
+ if ((0, typescript_1.isInAsyncFunction)(node)) {
65
+ return lua.createReturnStatement([
66
+ lua.createCallExpression(lua.createIdentifier("____awaiter_resolve"), [lua.createNilLiteral(), ...values]),
67
+ ]);
68
+ }
64
69
  if (isInTryCatch(context)) {
65
70
  // Bubble up explicit return flag and check if we're inside a try/catch block
66
71
  results.unshift(lua.createBooleanLiteral(true));
67
72
  }
68
- else if ((0, typescript_1.isInAsyncFunction)(node)) {
69
- // Add nil error handler in async function and not in try
70
- results.unshift(lua.createNilLiteral());
71
- }
72
73
  return lua.createReturnStatement(results, node);
73
74
  }
74
75
  exports.createReturnStatement = createReturnStatement;
@@ -19,6 +19,7 @@ const fs = require("fs");
19
19
  const path = require("path");
20
20
  const ts = require("typescript");
21
21
  const tsconfig_1 = require("../cli/tsconfig");
22
+ const utils_1 = require("../utils");
22
23
  const output_collector_1 = require("./output-collector");
23
24
  const transpiler_1 = require("./transpiler");
24
25
  __exportStar(require("./transpile"), exports);
@@ -44,8 +45,12 @@ exports.transpileProject = transpileProject;
44
45
  const libCache = {};
45
46
  /** @internal */
46
47
  function createVirtualProgram(input, options = {}) {
48
+ const normalizedFiles = {};
49
+ for (const [path, file] of Object.entries(input)) {
50
+ normalizedFiles[(0, utils_1.normalizeSlashes)(path)] = file;
51
+ }
47
52
  const compilerHost = {
48
- fileExists: fileName => fileName in input || ts.sys.fileExists(fileName),
53
+ fileExists: fileName => fileName in normalizedFiles || ts.sys.fileExists(fileName),
49
54
  getCanonicalFileName: fileName => fileName,
50
55
  getCurrentDirectory: () => "",
51
56
  getDefaultLibFileName: ts.getDefaultLibFileName,
@@ -54,8 +59,8 @@ function createVirtualProgram(input, options = {}) {
54
59
  useCaseSensitiveFileNames: () => false,
55
60
  writeFile() { },
56
61
  getSourceFile(fileName) {
57
- if (fileName in input) {
58
- return ts.createSourceFile(fileName, input[fileName], ts.ScriptTarget.Latest, false);
62
+ if (fileName in normalizedFiles) {
63
+ return ts.createSourceFile(fileName, normalizedFiles[fileName], ts.ScriptTarget.Latest, false);
59
64
  }
60
65
  let filePath;
61
66
  if (fileName.startsWith("lib.")) {
@@ -75,7 +80,7 @@ function createVirtualProgram(input, options = {}) {
75
80
  }
76
81
  },
77
82
  };
78
- return ts.createProgram(Object.keys(input), options, compilerHost);
83
+ return ts.createProgram(Object.keys(normalizedFiles), options, compilerHost);
79
84
  }
80
85
  exports.createVirtualProgram = createVirtualProgram;
81
86
  function transpileVirtualProject(files, options = {}) {
@@ -115,8 +115,8 @@ function resolveFileDependencies(file, context) {
115
115
  const dependencies = [];
116
116
  const diagnostics = [];
117
117
  for (const required of findRequiredPaths(file.code)) {
118
- // Do no resolve lualib
119
- if (required === "lualib_bundle") {
118
+ // Do no resolve lualib, unless it is included from node_modules
119
+ if (required === "lualib_bundle" && !isNodeModulesFile(file.fileName)) {
120
120
  dependencies.push({ fileName: "lualib_bundle", code: "" });
121
121
  continue;
122
122
  }
@@ -16,9 +16,10 @@ class Transpiler {
16
16
  }
17
17
  emit(emitOptions) {
18
18
  var _a, _b;
19
- const { program, writeFile = this.emitHost.writeFile } = emitOptions;
19
+ const { program, writeFile = this.emitHost.writeFile, plugins: optionsPlugins = [] } = emitOptions;
20
20
  const options = program.getCompilerOptions();
21
- const { diagnostics: getPluginsDiagnostics, plugins } = (0, plugins_1.getPlugins)(program);
21
+ const { diagnostics: getPluginsDiagnostics, plugins: configPlugins } = (0, plugins_1.getPlugins)(program);
22
+ const plugins = [...optionsPlugins, ...configPlugins];
22
23
  const { diagnostics, transpiledFiles: freshFiles } = (0, transpile_1.getProgramTranspileResult)(this.emitHost, writeFile, {
23
24
  ...emitOptions,
24
25
  plugins,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.4.3",
3
+ "version": "1.6.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/",
@@ -43,7 +43,7 @@
43
43
  "node": ">=12.13.0"
44
44
  },
45
45
  "peerDependencies": {
46
- "typescript": "~4.6.2"
46
+ "typescript": "~4.7.3"
47
47
  },
48
48
  "dependencies": {
49
49
  "enhanced-resolve": "^5.8.2",
@@ -53,14 +53,14 @@
53
53
  "devDependencies": {
54
54
  "@types/fs-extra": "^8.1.0",
55
55
  "@types/glob": "^7.1.1",
56
- "@types/jest": "^27.4.1",
56
+ "@types/jest": "^27.5.2",
57
57
  "@types/node": "^13.7.7",
58
58
  "@types/resolve": "1.14.0",
59
- "@typescript-eslint/eslint-plugin": "^5.13.0",
60
- "@typescript-eslint/parser": "^5.13.0",
61
- "eslint": "^8.10.0",
62
- "eslint-plugin-import": "^2.25.4",
63
- "eslint-plugin-jest": "^26.1.1",
59
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
60
+ "@typescript-eslint/parser": "^5.27.0",
61
+ "eslint": "^8.17.0",
62
+ "eslint-plugin-import": "^2.26.0",
63
+ "eslint-plugin-jest": "^26.4.6",
64
64
  "fs-extra": "^8.1.0",
65
65
  "javascript-stringify": "^2.0.1",
66
66
  "jest": "^27.3.0",
@@ -70,6 +70,6 @@
70
70
  "prettier": "^2.3.2",
71
71
  "ts-jest": "^27.1.3",
72
72
  "ts-node": "^10.3.0",
73
- "typescript": "~4.6.2"
73
+ "typescript": "~4.7.3"
74
74
  }
75
75
  }