typescript-to-lua 1.33.0 → 1.34.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.
@@ -54,6 +54,7 @@ export declare enum LuaTarget {
54
54
  Lua52 = "5.2",
55
55
  Lua53 = "5.3",
56
56
  Lua54 = "5.4",
57
+ Lua55 = "5.5",
57
58
  LuaJIT = "JIT",
58
59
  Luau = "Luau"
59
60
  }
@@ -19,6 +19,7 @@ var LuaTarget;
19
19
  LuaTarget["Lua52"] = "5.2";
20
20
  LuaTarget["Lua53"] = "5.3";
21
21
  LuaTarget["Lua54"] = "5.4";
22
+ LuaTarget["Lua55"] = "5.5";
22
23
  LuaTarget["LuaJIT"] = "JIT";
23
24
  LuaTarget["Luau"] = "Luau";
24
25
  })(LuaTarget || (exports.LuaTarget = LuaTarget = {}));
@@ -29,7 +29,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
29
29
  end
30
30
  local result = string.gsub(
31
31
  trace,
32
- "(%S+)%.lua:(%d+)",
32
+ "([^%s<]+)%.lua:(%d+)",
33
33
  function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end
34
34
  )
35
35
  local function stringReplacer(____, file, line)
@@ -2309,7 +2309,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
2309
2309
  end
2310
2310
  local result = string.gsub(
2311
2311
  trace,
2312
- "(%S+)%.lua:(%d+)",
2312
+ "([^%s<]+)%.lua:(%d+)",
2313
2313
  function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end
2314
2314
  )
2315
2315
  local function stringReplacer(____, file, line)
@@ -29,7 +29,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
29
29
  end
30
30
  local result = string.gsub(
31
31
  trace,
32
- "(%S+)%.lua:(%d+)",
32
+ "([^%s<]+)%.lua:(%d+)",
33
33
  function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end
34
34
  )
35
35
  local function stringReplacer(____, file, line)
@@ -2241,7 +2241,7 @@ local function __TS__SourceMapTraceBack(fileName, sourceMap)
2241
2241
  end
2242
2242
  local result = string.gsub(
2243
2243
  trace,
2244
- "(%S+)%.lua:(%d+)",
2244
+ "([^%s<]+)%.lua:(%d+)",
2245
2245
  function(file, line) return replacer(nil, file .. ".lua", file .. ".ts", line) end
2246
2246
  )
2247
2247
  local function stringReplacer(____, file, line)
@@ -59,6 +59,6 @@ export declare class TransformationContext {
59
59
  readonly usedLuaLibFeatures: Set<LuaLibFeature>;
60
60
  readonly scopeStack: Scope[];
61
61
  private lastScopeId;
62
- pushScope(type: ScopeType): Scope;
62
+ pushScope(type: ScopeType, node: ts.Node): Scope;
63
63
  popScope(): Scope;
64
64
  }
@@ -206,8 +206,8 @@ class TransformationContext {
206
206
  nextSymbolId() {
207
207
  return ++this.lastSymbolId;
208
208
  }
209
- pushScope(type) {
210
- const scope = { type, id: ++this.lastScopeId };
209
+ pushScope(type, node) {
210
+ const scope = { type, id: ++this.lastScopeId, node };
211
211
  this.scopeStack.push(scope);
212
212
  return scope;
213
213
  }
@@ -24,7 +24,7 @@ export declare enum LoopContinued {
24
24
  export interface Scope {
25
25
  type: ScopeType;
26
26
  id: number;
27
- node?: ts.Node;
27
+ node: ts.Node;
28
28
  referencedSymbols?: Map<lua.SymbolId, ts.Node[]>;
29
29
  variableDeclarations?: lua.VariableDeclarationStatement[];
30
30
  functionDefinitions?: Map<lua.SymbolId, FunctionDefinitionInfo>;
@@ -44,9 +44,6 @@ export declare function findScope(context: TransformationContext, scopeTypes: Sc
44
44
  export declare function addScopeVariableDeclaration(scope: Scope, declaration: lua.VariableDeclarationStatement): void;
45
45
  export declare function hasReferencedUndefinedLocalFunction(context: TransformationContext, scope: Scope): boolean;
46
46
  export declare function hasReferencedSymbol(context: TransformationContext, scope: Scope, symbol: ts.Symbol): boolean | undefined;
47
- export declare function isFunctionScopeWithDefinition(scope: Scope): scope is Scope & {
48
- node: ts.SignatureDeclaration;
49
- };
50
47
  export declare function separateHoistedStatements(context: TransformationContext, statements: lua.Statement[]): HoistingResult;
51
48
  export declare function performHoisting(context: TransformationContext, statements: lua.Statement[]): lua.Statement[];
52
49
  export {};
@@ -8,7 +8,6 @@ exports.findScope = findScope;
8
8
  exports.addScopeVariableDeclaration = addScopeVariableDeclaration;
9
9
  exports.hasReferencedUndefinedLocalFunction = hasReferencedUndefinedLocalFunction;
10
10
  exports.hasReferencedSymbol = hasReferencedSymbol;
11
- exports.isFunctionScopeWithDefinition = isFunctionScopeWithDefinition;
12
11
  exports.separateHoistedStatements = separateHoistedStatements;
13
12
  exports.performHoisting = performHoisting;
14
13
  const ts = require("typescript");
@@ -76,7 +75,7 @@ function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
76
75
  // and thus will be hoisted above the current position.
77
76
  function hasReferencedUndefinedLocalFunction(context, scope) {
78
77
  var _a;
79
- if (!scope.referencedSymbols || !scope.node) {
78
+ if (!scope.referencedSymbols) {
80
79
  return false;
81
80
  }
82
81
  for (const [symbolId, nodes] of scope.referencedSymbols) {
@@ -100,9 +99,6 @@ function hasReferencedSymbol(context, scope, symbol) {
100
99
  }
101
100
  return false;
102
101
  }
103
- function isFunctionScopeWithDefinition(scope) {
104
- return scope.node !== undefined && ts.isFunctionLike(scope.node);
105
- }
106
102
  function separateHoistedStatements(context, statements) {
107
103
  const scope = peekScope(context);
108
104
  const allHoistedStatments = [];
@@ -10,13 +10,13 @@ function transformBlockOrStatement(context, statement) {
10
10
  return context.transformStatements(ts.isBlock(statement) ? statement.statements : statement);
11
11
  }
12
12
  function transformScopeBlock(context, node, scopeType) {
13
- context.pushScope(scopeType);
13
+ context.pushScope(scopeType, node);
14
14
  const statements = (0, scope_1.performHoisting)(context, context.transformStatements(node.statements));
15
15
  const scope = context.popScope();
16
16
  return [lua.createBlock(statements, node), scope];
17
17
  }
18
18
  const transformBlock = (node, context) => {
19
- context.pushScope(scope_1.ScopeType.Block);
19
+ context.pushScope(scope_1.ScopeType.Block, node);
20
20
  const statements = (0, scope_1.performHoisting)(context, context.transformStatements(node.statements));
21
21
  context.popScope();
22
22
  return lua.createDoStatement(statements, node);
@@ -19,6 +19,7 @@ const transformContinueStatement = (statement, context) => {
19
19
  [CompilerOptions_1.LuaTarget.Lua52]: scope_1.LoopContinued.WithGoto,
20
20
  [CompilerOptions_1.LuaTarget.Lua53]: scope_1.LoopContinued.WithGoto,
21
21
  [CompilerOptions_1.LuaTarget.Lua54]: scope_1.LoopContinued.WithGoto,
22
+ [CompilerOptions_1.LuaTarget.Lua55]: scope_1.LoopContinued.WithGoto,
22
23
  [CompilerOptions_1.LuaTarget.LuaJIT]: scope_1.LoopContinued.WithGoto,
23
24
  [CompilerOptions_1.LuaTarget.Luau]: scope_1.LoopContinued.WithContinue,
24
25
  }[context.luaTarget];
@@ -13,7 +13,7 @@ const decorators_1 = require("../decorators");
13
13
  function transformAccessor(context, node, className) {
14
14
  var _a;
15
15
  const [params, dot, restParam] = (0, function_1.transformParameters)(context, node.parameters, (0, lua_ast_1.createSelfIdentifier)());
16
- const body = node.body ? (0, function_1.transformFunctionBody)(context, node.parameters, node.body, restParam)[0] : [];
16
+ const body = node.body ? (0, function_1.transformFunctionBody)(context, node.parameters, node.body, node, restParam)[0] : [];
17
17
  const accessorFunction = lua.createFunctionExpression(lua.createBlock(body), params, dot, lua.NodeFlags.Declaration);
18
18
  if ((_a = ts.getDecorators(node)) === null || _a === void 0 ? void 0 : _a.length) {
19
19
  return (0, decorators_1.createClassAccessorDecoratingExpression)(context, node, accessorFunction, className);
@@ -22,7 +22,7 @@ function transformConstructorDeclaration(context, statement, className, instance
22
22
  return undefined;
23
23
  }
24
24
  // Transform body
25
- const scope = context.pushScope(scope_1.ScopeType.Function);
25
+ const scope = context.pushScope(scope_1.ScopeType.Function, statement);
26
26
  const body = (0, function_1.transformFunctionBodyContent)(context, statement.body);
27
27
  const [params, dotsLiteral, restParamName] = (0, function_1.transformParameters)(context, statement.parameters, (0, lua_ast_1.createSelfIdentifier)());
28
28
  // Make sure default parameters are assigned before fields are initialized
@@ -44,7 +44,7 @@ const transformConditionalExpression = (expression, context) => {
44
44
  };
45
45
  exports.transformConditionalExpression = transformConditionalExpression;
46
46
  function transformIfStatement(statement, context) {
47
- context.pushScope(scope_1.ScopeType.Conditional);
47
+ context.pushScope(scope_1.ScopeType.Conditional, statement);
48
48
  // Check if we need to add diagnostic about Lua truthiness
49
49
  checkOnlyTruthyCondition(statement.expression, context);
50
50
  const condition = context.transformExpression(statement.expression);
@@ -72,7 +72,7 @@ function transformIfStatement(statement, context) {
72
72
  }
73
73
  }
74
74
  else {
75
- context.pushScope(scope_1.ScopeType.Conditional);
75
+ context.pushScope(scope_1.ScopeType.Conditional, statement);
76
76
  const elseStatements = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, statement.elseStatement));
77
77
  context.popScope();
78
78
  const elseBlock = lua.createBlock(elseStatements);
@@ -28,7 +28,7 @@ const transformEnumDeclaration = (node, context) => {
28
28
  const result = [];
29
29
  if (!membersOnly && (0, typescript_1.isFirstDeclaration)(context, node)) {
30
30
  const name = (0, identifier_1.transformIdentifier)(context, node.name);
31
- const table = lua.createBinaryExpression(lua.cloneIdentifier(name), lua.createTableExpression(), lua.SyntaxKind.OrOperator);
31
+ const table = lua.createBinaryExpression((0, export_1.addExportToIdentifier)(context, name), lua.createTableExpression(), lua.SyntaxKind.OrOperator);
32
32
  result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, name, table, node));
33
33
  }
34
34
  const enumReference = context.transformExpression(node.name);
@@ -6,7 +6,7 @@ export declare function createCallableTable(functionExpression: lua.Expression):
6
6
  export declare function isFunctionTypeWithProperties(context: TransformationContext, functionType: ts.Type): boolean;
7
7
  export declare function transformFunctionBodyContent(context: TransformationContext, body: ts.ConciseBody): lua.Statement[];
8
8
  export declare function transformFunctionBodyHeader(context: TransformationContext, bodyScope: Scope, parameters: ts.NodeArray<ts.ParameterDeclaration>, spreadIdentifier?: lua.Identifier): lua.Statement[];
9
- export declare function transformFunctionBody(context: TransformationContext, parameters: ts.NodeArray<ts.ParameterDeclaration>, body: ts.ConciseBody, spreadIdentifier?: lua.Identifier, node?: ts.FunctionLikeDeclaration): [lua.Statement[], Scope];
9
+ export declare function transformFunctionBody(context: TransformationContext, parameters: ts.NodeArray<ts.ParameterDeclaration>, body: ts.ConciseBody, node: ts.FunctionLikeDeclaration, spreadIdentifier?: lua.Identifier): [lua.Statement[], Scope];
10
10
  export declare function transformParameters(context: TransformationContext, parameters: ts.NodeArray<ts.ParameterDeclaration>, functionContext?: lua.Identifier): [lua.Identifier[], lua.DotsLiteral | undefined, lua.Identifier | undefined];
11
11
  export declare function transformFunctionToExpression(context: TransformationContext, node: ts.FunctionLikeDeclaration): [lua.Expression, Scope];
12
12
  export declare function transformFunctionLikeDeclaration(node: ts.FunctionLikeDeclaration, context: TransformationContext): lua.Expression;
@@ -120,9 +120,8 @@ function transformFunctionBodyHeader(context, bodyScope, parameters, spreadIdent
120
120
  headerStatements.push(...bindingPatternDeclarations);
121
121
  return headerStatements;
122
122
  }
123
- function transformFunctionBody(context, parameters, body, spreadIdentifier, node) {
124
- const scope = context.pushScope(scope_1.ScopeType.Function);
125
- scope.node = node;
123
+ function transformFunctionBody(context, parameters, body, node, spreadIdentifier) {
124
+ const scope = context.pushScope(scope_1.ScopeType.Function, node);
126
125
  let bodyStatements = transformFunctionBodyContent(context, body);
127
126
  if (node && (0, async_await_1.isAsyncFunction)(node)) {
128
127
  bodyStatements = [lua.createReturnStatement([(0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements)])];
@@ -191,7 +190,7 @@ function transformFunctionToExpression(context, node) {
191
190
  flags |= lua.NodeFlags.Declaration;
192
191
  }
193
192
  const [paramNames, dotsLiteral, spreadIdentifier] = transformParameters(context, node.parameters, functionContext);
194
- const [transformedBody, functionScope] = transformFunctionBody(context, node.parameters, node.body, spreadIdentifier, node);
193
+ const [transformedBody, functionScope] = transformFunctionBody(context, node.parameters, node.body, node, spreadIdentifier);
195
194
  const functionExpression = lua.createFunctionExpression(lua.createBlock(transformedBody), paramNames, dotsLiteral, flags, node);
196
195
  return [
197
196
  node.asteriskToken
@@ -9,7 +9,7 @@ const utils_1 = require("./utils");
9
9
  const scope_1 = require("../../utils/scope");
10
10
  const transformForStatement = (statement, context) => {
11
11
  const result = [];
12
- context.pushScope(scope_1.ScopeType.Loop);
12
+ context.pushScope(scope_1.ScopeType.Loop, statement);
13
13
  if (statement.initializer) {
14
14
  if (ts.isVariableDeclarationList(statement.initializer)) {
15
15
  (0, variable_declaration_1.checkVariableDeclarationList)(context, statement.initializer);
@@ -15,7 +15,7 @@ const block_1 = require("../block");
15
15
  const identifier_1 = require("../identifier");
16
16
  const variable_declaration_1 = require("../variable-declaration");
17
17
  function transformLoopBody(context, loop) {
18
- context.pushScope(scope_1.ScopeType.Loop);
18
+ context.pushScope(scope_1.ScopeType.Loop, loop);
19
19
  const body = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, loop.statement));
20
20
  const scope = context.popScope();
21
21
  const scopeId = scope.id;
@@ -60,7 +60,7 @@ function getVariableDeclarationBinding(context, node) {
60
60
  }
61
61
  function transformForInitializer(context, initializer, block) {
62
62
  const valueVariable = lua.createIdentifier("____value");
63
- context.pushScope(scope_1.ScopeType.LoopInitializer);
63
+ context.pushScope(scope_1.ScopeType.LoopInitializer, initializer);
64
64
  if (ts.isVariableDeclarationList(initializer)) {
65
65
  // Declaration of new variable
66
66
  const binding = getVariableDeclarationBinding(context, initializer);
@@ -77,7 +77,7 @@ const transformModuleDeclaration = (node, context) => {
77
77
  context.currentNamespaces = node;
78
78
  // Transform moduleblock to block and visit it
79
79
  if (moduleHasEmittedBody(node)) {
80
- context.pushScope(scope_1.ScopeType.Block);
80
+ context.pushScope(scope_1.ScopeType.Block, node);
81
81
  const statements = (0, scope_1.performHoisting)(context, context.transformStatements(ts.isModuleBlock(node.body) ? node.body.statements : node.body));
82
82
  context.popScope();
83
83
  result.push(lua.createDoStatement(statements));
@@ -27,7 +27,7 @@ const transformSourceFileNode = (node, context) => {
27
27
  }
28
28
  }
29
29
  else {
30
- context.pushScope(scope_1.ScopeType.File);
30
+ context.pushScope(scope_1.ScopeType.File, node);
31
31
  statements = (0, scope_1.performHoisting)(context, context.transformStatements(node.statements));
32
32
  context.popScope();
33
33
  if (context.isModule) {
@@ -28,7 +28,7 @@ function isOptimizedVarArgSpread(context, symbol, identifier) {
28
28
  return true;
29
29
  }
30
30
  // Scope must be a function scope associated with a real ts function
31
- if (!(0, scope_1.isFunctionScopeWithDefinition)(scope)) {
31
+ if (!ts.isFunctionLike(scope.node)) {
32
32
  return false;
33
33
  }
34
34
  // Scope cannot be an async function
@@ -48,7 +48,7 @@ const coalesceCondition = (condition, conditionPrecedingStatements, switchVariab
48
48
  return { precedingStatements: [...conditionPrecedingStatements, ...precedingStatements], result: comparison };
49
49
  };
50
50
  const transformSwitchStatement = (statement, context) => {
51
- const scope = context.pushScope(scope_1.ScopeType.Switch);
51
+ const scope = context.pushScope(scope_1.ScopeType.Switch, statement);
52
52
  // Give the switch and condition accumulator a unique name to prevent nested switches from acting up.
53
53
  const switchName = `____switch${scope.id}`;
54
54
  const conditionName = `____cond${scope.id}`;
@@ -93,7 +93,9 @@ function getBundleResult(program, files) {
93
93
  const moduleTable = createModuleTableNode(moduleTableEntries);
94
94
  // return require("<entry module path>")
95
95
  const args = options.luaTarget === CompilerOptions_1.LuaTarget.Lua50 ? "unpack(arg == nil and {} or arg)" : "...";
96
- const entryPoint = `return require(${createModulePath(entryModuleFilePath !== null && entryModuleFilePath !== void 0 ? entryModuleFilePath : entryModule, program)}, ${args})\n`;
96
+ // Avoid producing a tail-call (which removes the bundle's stack frame) by assigning the `require` result to a local and returning it.
97
+ const entryPath = createModulePath(entryModuleFilePath !== null && entryModuleFilePath !== void 0 ? entryModuleFilePath : entryModule, program);
98
+ const entryPoint = `local ____entry = require(${entryPath}, ${args})\nreturn ____entry\n`;
97
99
  const footers = [];
98
100
  if (options.sourceMapTraceback) {
99
101
  // Generates SourceMapTraceback for the entire file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.33.0",
3
+ "version": "1.34.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/",
@@ -64,8 +64,8 @@
64
64
  "javascript-stringify": "^2.0.1",
65
65
  "jest": "^29.5.0",
66
66
  "jest-circus": "^29.7.0",
67
- "lua-types": "^2.13.0",
68
- "lua-wasm-bindings": "^0.3.1",
67
+ "lua-types": "^2.14.1",
68
+ "lua-wasm-bindings": "^0.5.3",
69
69
  "prettier": "^2.8.8",
70
70
  "ts-jest": "^29.2.5",
71
71
  "ts-node": "^10.9.2",