typescript-to-lua 1.32.0 → 1.33.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.
@@ -7,7 +7,7 @@ export declare const lualibDiagnostic: ((message: string, file?: ts.SourceFile |
7
7
  };
8
8
  declare class LuaLibPlugin implements tstl.Plugin {
9
9
  visitors: {
10
- 307: (file: ts.SourceFile, context: tstl.TransformationContext) => tstl.File;
10
+ 308: (file: ts.SourceFile, context: tstl.TransformationContext) => tstl.File;
11
11
  };
12
12
  printer: tstl.Printer;
13
13
  afterPrint(program: ts.Program, options: tstl.CompilerOptions, emitHost: EmitHost, result: ProcessedFile[]): ts.Diagnostic[];
@@ -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");
@@ -42,10 +41,9 @@ function* walkScopesUp(context) {
42
41
  }
43
42
  }
44
43
  function markSymbolAsReferencedInCurrentScopes(context, symbolId, identifier) {
44
+ var _a;
45
45
  for (const scope of context.scopeStack) {
46
- if (!scope.referencedSymbols) {
47
- scope.referencedSymbols = new Map();
48
- }
46
+ (_a = scope.referencedSymbols) !== null && _a !== void 0 ? _a : (scope.referencedSymbols = new Map());
49
47
  const references = (0, utils_1.getOrUpdate)(scope.referencedSymbols, symbolId, () => []);
50
48
  references.push(identifier);
51
49
  }
@@ -65,9 +63,8 @@ function findScope(context, scopeTypes) {
65
63
  }
66
64
  }
67
65
  function addScopeVariableDeclaration(scope, declaration) {
68
- if (!scope.variableDeclarations) {
69
- scope.variableDeclarations = [];
70
- }
66
+ var _a;
67
+ (_a = scope.variableDeclarations) !== null && _a !== void 0 ? _a : (scope.variableDeclarations = []);
71
68
  scope.variableDeclarations.push(declaration);
72
69
  }
73
70
  function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
@@ -78,7 +75,7 @@ function isHoistableFunctionDeclaredInScope(symbol, scopeNode) {
78
75
  // and thus will be hoisted above the current position.
79
76
  function hasReferencedUndefinedLocalFunction(context, scope) {
80
77
  var _a;
81
- if (!scope.referencedSymbols || !scope.node) {
78
+ if (!scope.referencedSymbols) {
82
79
  return false;
83
80
  }
84
81
  for (const [symbolId, nodes] of scope.referencedSymbols) {
@@ -102,9 +99,6 @@ function hasReferencedSymbol(context, scope, symbol) {
102
99
  }
103
100
  return false;
104
101
  }
105
- function isFunctionScopeWithDefinition(scope) {
106
- return scope.node !== undefined && ts.isFunctionLike(scope.node);
107
- }
108
102
  function separateHoistedStatements(context, statements) {
109
103
  const scope = peekScope(context);
110
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);
@@ -191,10 +191,8 @@ const transformSuperExpression = (expression, context) => {
191
191
  baseClassName = (0, identifier_1.transformIdentifier)(context, extendsExpression);
192
192
  }
193
193
  }
194
- if (!baseClassName) {
195
- // Use "className.____super" if the base is not a simple identifier
196
- baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression);
197
- }
194
+ // Use "className.____super" if the base is not a simple identifier
195
+ baseClassName !== null && baseClassName !== void 0 ? baseClassName : (baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression));
198
196
  const f = (0, typescript_1.findFirstNodeAbove)(expression, ts.isFunctionLike);
199
197
  if (f && ts.canHaveModifiers(f) && (0, utils_1.isStaticNode)(f)) {
200
198
  // In static method, don't add prototype to super call
@@ -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);
@@ -49,9 +49,7 @@ const transformEnumDeclaration = (node, context) => {
49
49
  valueExpression = lua.createTableIndexExpression(enumReference, otherMemberName);
50
50
  }
51
51
  }
52
- if (!valueExpression) {
53
- valueExpression = context.transformExpression(member.initializer);
54
- }
52
+ valueExpression !== null && valueExpression !== void 0 ? valueExpression : (valueExpression = context.transformExpression(member.initializer));
55
53
  }
56
54
  else {
57
55
  valueExpression = lua.createNilLiteral();
@@ -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
@@ -234,7 +233,7 @@ function transformFunctionLikeDeclaration(node, context) {
234
233
  : functionExpression;
235
234
  }
236
235
  const transformFunctionDeclaration = (node, context) => {
237
- var _a;
236
+ var _a, _b;
238
237
  // Don't transform functions without body (overload declarations)
239
238
  if (node.body === undefined) {
240
239
  return undefined;
@@ -248,10 +247,8 @@ const transformFunctionDeclaration = (node, context) => {
248
247
  // Remember symbols referenced in this function for hoisting later
249
248
  if (name.symbolId !== undefined) {
250
249
  const scope = (0, scope_1.peekScope)(context);
251
- if (!scope.functionDefinitions) {
252
- scope.functionDefinitions = new Map();
253
- }
254
- const functionInfo = { referencedSymbols: (_a = functionScope.referencedSymbols) !== null && _a !== void 0 ? _a : new Map() };
250
+ (_a = scope.functionDefinitions) !== null && _a !== void 0 ? _a : (scope.functionDefinitions = new Map());
251
+ const functionInfo = { referencedSymbols: (_b = functionScope.referencedSymbols) !== null && _b !== void 0 ? _b : new Map() };
255
252
  scope.functionDefinitions.set(name.symbolId, functionInfo);
256
253
  }
257
254
  // Wrap functions with properties into a callable table
@@ -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);
@@ -46,10 +46,9 @@ function transformImportSpecifier(context, importSpecifier, moduleTableName) {
46
46
  return lua.createVariableDeclarationStatement(leftIdentifier, lua.createTableIndexExpression(moduleTableName, propertyName), importSpecifier);
47
47
  }
48
48
  const transformImportDeclaration = (statement, context) => {
49
+ var _a;
49
50
  const scope = (0, scope_1.peekScope)(context);
50
- if (!scope.importStatements) {
51
- scope.importStatements = [];
52
- }
51
+ (_a = scope.importStatements) !== null && _a !== void 0 ? _a : (scope.importStatements = []);
53
52
  const result = [];
54
53
  const requireCall = createModuleRequire(context, statement.moduleSpecifier);
55
54
  // import "./module";
@@ -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}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.32.0",
3
+ "version": "1.33.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/",
@@ -42,7 +42,7 @@
42
42
  "node": ">=16.10.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "typescript": "5.8.2"
45
+ "typescript": "5.9.3"
46
46
  },
47
47
  "dependencies": {
48
48
  "@typescript-to-lua/language-extensions": "1.19.0",
@@ -69,7 +69,7 @@
69
69
  "prettier": "^2.8.8",
70
70
  "ts-jest": "^29.2.5",
71
71
  "ts-node": "^10.9.2",
72
- "typescript": "5.8.2",
73
- "typescript-eslint": "^8.26.0"
72
+ "typescript": "5.9.3",
73
+ "typescript-eslint": "^8.46.3"
74
74
  }
75
75
  }