typescript-to-lua 1.20.1 → 1.21.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.
@@ -4,6 +4,7 @@ export declare enum AnnotationKind {
4
4
  CompileMembersOnly = "compileMembersOnly",
5
5
  NoResolution = "noResolution",
6
6
  NoSelf = "noSelf",
7
+ CustomName = "customName",
7
8
  NoSelfInFile = "noSelfInFile"
8
9
  }
9
10
  export interface Annotation {
@@ -8,6 +8,7 @@ var AnnotationKind;
8
8
  AnnotationKind["CompileMembersOnly"] = "compileMembersOnly";
9
9
  AnnotationKind["NoResolution"] = "noResolution";
10
10
  AnnotationKind["NoSelf"] = "noSelf";
11
+ AnnotationKind["CustomName"] = "customName";
11
12
  AnnotationKind["NoSelfInFile"] = "noSelfInFile";
12
13
  })(AnnotationKind || (exports.AnnotationKind = AnnotationKind = {}));
13
14
  const annotationValues = new Map(Object.values(AnnotationKind).map(k => [k.toLowerCase(), k]));
@@ -16,6 +16,10 @@ interface FunctionDefinitionInfo {
16
16
  referencedSymbols: Map<lua.SymbolId, ts.Node[]>;
17
17
  definition?: lua.VariableDeclarationStatement | lua.AssignmentStatement;
18
18
  }
19
+ export declare enum LoopContinued {
20
+ WithGoto = 0,
21
+ WithRepeatBreak = 1
22
+ }
19
23
  export interface Scope {
20
24
  type: ScopeType;
21
25
  id: number;
@@ -24,7 +28,7 @@ export interface Scope {
24
28
  variableDeclarations?: lua.VariableDeclarationStatement[];
25
29
  functionDefinitions?: Map<lua.SymbolId, FunctionDefinitionInfo>;
26
30
  importStatements?: lua.Statement[];
27
- loopContinued?: boolean;
31
+ loopContinued?: LoopContinued;
28
32
  functionReturned?: boolean;
29
33
  }
30
34
  export interface HoistingResult {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.addScopeVariableDeclaration = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.ScopeType = void 0;
3
+ exports.performHoisting = exports.separateHoistedStatements = exports.isFunctionScopeWithDefinition = exports.hasReferencedSymbol = exports.hasReferencedUndefinedLocalFunction = exports.addScopeVariableDeclaration = exports.findScope = exports.peekScope = exports.markSymbolAsReferencedInCurrentScopes = exports.walkScopesUp = exports.LoopContinued = exports.ScopeType = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
6
  const utils_1 = require("../../utils");
@@ -18,6 +18,11 @@ var ScopeType;
18
18
  ScopeType[ScopeType["Catch"] = 128] = "Catch";
19
19
  ScopeType[ScopeType["LoopInitializer"] = 256] = "LoopInitializer";
20
20
  })(ScopeType || (exports.ScopeType = ScopeType = {}));
21
+ var LoopContinued;
22
+ (function (LoopContinued) {
23
+ LoopContinued[LoopContinued["WithGoto"] = 0] = "WithGoto";
24
+ LoopContinued[LoopContinued["WithRepeatBreak"] = 1] = "WithRepeatBreak";
25
+ })(LoopContinued || (exports.LoopContinued = LoopContinued = {}));
21
26
  function* walkScopesUp(context) {
22
27
  const scopeStack = context.scopeStack;
23
28
  for (let i = scopeStack.length - 1; i >= 0; --i) {
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.transformContinueStatement = exports.transformBreakStatement = void 0;
4
4
  const CompilerOptions_1 = require("../../CompilerOptions");
5
5
  const lua = require("../../LuaAST");
6
- const diagnostics_1 = require("../utils/diagnostics");
7
6
  const scope_1 = require("../utils/scope");
8
7
  const transformBreakStatement = (breakStatement, context) => {
9
8
  void context;
@@ -12,16 +11,25 @@ const transformBreakStatement = (breakStatement, context) => {
12
11
  exports.transformBreakStatement = transformBreakStatement;
13
12
  const transformContinueStatement = (statement, context) => {
14
13
  var _a;
15
- if (context.luaTarget === CompilerOptions_1.LuaTarget.Universal ||
16
- context.luaTarget === CompilerOptions_1.LuaTarget.Lua50 ||
17
- context.luaTarget === CompilerOptions_1.LuaTarget.Lua51) {
18
- context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "Continue statement", context.luaTarget));
19
- }
20
14
  const scope = (0, scope_1.findScope)(context, scope_1.ScopeType.Loop);
15
+ const continuedWith = context.luaTarget === CompilerOptions_1.LuaTarget.Universal ||
16
+ context.luaTarget === CompilerOptions_1.LuaTarget.Lua50 ||
17
+ context.luaTarget === CompilerOptions_1.LuaTarget.Lua51
18
+ ? scope_1.LoopContinued.WithRepeatBreak
19
+ : scope_1.LoopContinued.WithGoto;
21
20
  if (scope) {
22
- scope.loopContinued = true;
21
+ scope.loopContinued = continuedWith;
22
+ }
23
+ const label = `__continue${(_a = scope === null || scope === void 0 ? void 0 : scope.id) !== null && _a !== void 0 ? _a : ""}`;
24
+ switch (continuedWith) {
25
+ case scope_1.LoopContinued.WithGoto:
26
+ return lua.createGotoStatement(label, statement);
27
+ case scope_1.LoopContinued.WithRepeatBreak:
28
+ return [
29
+ lua.createAssignmentStatement(lua.createIdentifier(label), lua.createBooleanLiteral(true), statement),
30
+ lua.createBreakStatement(statement),
31
+ ];
23
32
  }
24
- return lua.createGotoStatement(`__continue${(_a = scope === null || scope === void 0 ? void 0 : scope.id) !== null && _a !== void 0 ? _a : ""}`, statement);
25
33
  };
26
34
  exports.transformContinueStatement = transformContinueStatement;
27
35
  //# sourceMappingURL=break-continue.js.map
@@ -17,6 +17,7 @@ const preceding_statements_1 = require("../utils/preceding-statements");
17
17
  const optional_chaining_1 = require("./optional-chaining");
18
18
  const import_1 = require("./modules/import");
19
19
  const call_extension_1 = require("./language-extensions/call-extension");
20
+ const identifier_1 = require("./identifier");
20
21
  function validateArguments(context, params, signature) {
21
22
  if (!signature || signature.parameters.length < params.length) {
22
23
  return;
@@ -89,7 +90,13 @@ function transformContextualCallExpression(context, node, args, signature) {
89
90
  argPrecedingStatements.length === 0) {
90
91
  // table:name()
91
92
  const table = context.transformExpression(left.expression);
92
- return lua.createMethodCallExpression(table, lua.createIdentifier(left.name.text, left.name), transformedArguments, node);
93
+ let name = left.name.text;
94
+ const symbol = context.checker.getSymbolAtLocation(left);
95
+ const customName = (0, identifier_1.getCustomNameFromSymbol)(symbol);
96
+ if (customName) {
97
+ name = customName;
98
+ }
99
+ return lua.createMethodCallExpression(table, lua.createIdentifier(name, left.name), transformedArguments, node);
93
100
  }
94
101
  else if (ts.isElementAccessExpression(left) || ts.isPropertyAccessExpression(left)) {
95
102
  if ((0, typescript_1.isExpressionWithEvaluationEffect)(left.expression)) {
@@ -17,6 +17,7 @@ const setup_1 = require("./setup");
17
17
  const CompilerOptions_1 = require("../../../CompilerOptions");
18
18
  const preceding_statements_1 = require("../../utils/preceding-statements");
19
19
  const decorators_2 = require("./decorators");
20
+ const typescript_1 = require("../../utils/typescript");
20
21
  const transformClassDeclaration = (declaration, context) => {
21
22
  // If declaration is a default export, transform to export variable assignment instead
22
23
  if ((0, export_1.hasDefaultExportModifier)(declaration)) {
@@ -180,7 +181,14 @@ const transformSuperExpression = (expression, context) => {
180
181
  // Use "className.____super" if the base is not a simple identifier
181
182
  baseClassName = lua.createTableIndexExpression(className, lua.createStringLiteral("____super"), expression);
182
183
  }
183
- return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype"));
184
+ const f = (0, typescript_1.findFirstNodeAbove)(expression, ts.isFunctionLike);
185
+ if (f && ts.canHaveModifiers(f) && (0, utils_1.isStaticNode)(f)) {
186
+ // In static method, don't add prototype to super call
187
+ return baseClassName;
188
+ }
189
+ else {
190
+ return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype"));
191
+ }
184
192
  };
185
193
  exports.transformSuperExpression = transformSuperExpression;
186
194
  //# sourceMappingURL=index.js.map
@@ -2,5 +2,6 @@ import * as ts from "typescript";
2
2
  import * as lua from "../../LuaAST";
3
3
  import { FunctionVisitor, TransformationContext } from "../context";
4
4
  export declare function transformIdentifier(context: TransformationContext, identifier: ts.Identifier): lua.Identifier;
5
+ export declare function getCustomNameFromSymbol(symbol?: ts.Symbol): undefined | string;
5
6
  export declare function transformIdentifierWithSymbol(context: TransformationContext, node: ts.Identifier, symbol: ts.Symbol | undefined): lua.Expression;
6
7
  export declare const transformIdentifierExpression: FunctionVisitor<ts.Identifier>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformIdentifierExpression = exports.transformIdentifierWithSymbol = exports.transformIdentifier = void 0;
3
+ exports.transformIdentifierExpression = exports.transformIdentifierWithSymbol = exports.getCustomNameFromSymbol = exports.transformIdentifier = void 0;
4
4
  const ts = require("typescript");
5
5
  const lua = require("../../LuaAST");
6
6
  const builtins_1 = require("../builtins");
@@ -15,10 +15,33 @@ const typescript_1 = require("../utils/typescript");
15
15
  const language_extensions_1 = require("../utils/language-extensions");
16
16
  const call_extension_1 = require("./language-extensions/call-extension");
17
17
  const identifier_1 = require("./language-extensions/identifier");
18
+ const annotations_1 = require("../utils/annotations");
18
19
  function transformIdentifier(context, identifier) {
19
20
  return transformNonValueIdentifier(context, identifier, context.checker.getSymbolAtLocation(identifier));
20
21
  }
21
22
  exports.transformIdentifier = transformIdentifier;
23
+ function getCustomNameFromSymbol(symbol) {
24
+ let retVal;
25
+ if (symbol) {
26
+ const declarations = symbol.getDeclarations();
27
+ if (declarations) {
28
+ let customNameAnnotation = undefined;
29
+ for (const declaration of declarations) {
30
+ const nodeAnnotations = (0, annotations_1.getNodeAnnotations)(declaration);
31
+ const foundAnnotation = nodeAnnotations.get(annotations_1.AnnotationKind.CustomName);
32
+ if (foundAnnotation) {
33
+ customNameAnnotation = foundAnnotation;
34
+ break;
35
+ }
36
+ }
37
+ if (customNameAnnotation) {
38
+ retVal = customNameAnnotation.args[0];
39
+ }
40
+ }
41
+ }
42
+ return retVal;
43
+ }
44
+ exports.getCustomNameFromSymbol = getCustomNameFromSymbol;
22
45
  function transformNonValueIdentifier(context, identifier, symbol) {
23
46
  if ((0, optional_chaining_1.isOptionalContinuation)(identifier)) {
24
47
  const result = lua.createIdentifier(identifier.text, undefined, context_1.tempSymbolId);
@@ -48,9 +71,10 @@ function transformNonValueIdentifier(context, identifier, symbol) {
48
71
  return (0, promise_1.createPromiseIdentifier)(identifier);
49
72
  }
50
73
  }
51
- const text = (0, safe_names_1.hasUnsafeIdentifierName)(context, identifier, symbol)
52
- ? (0, safe_names_1.createSafeName)(identifier.text)
53
- : identifier.text;
74
+ let text = (0, safe_names_1.hasUnsafeIdentifierName)(context, identifier, symbol) ? (0, safe_names_1.createSafeName)(identifier.text) : identifier.text;
75
+ const customName = getCustomNameFromSymbol(symbol);
76
+ if (customName)
77
+ text = customName;
54
78
  const symbolId = (0, symbols_1.getIdentifierSymbolId)(context, identifier, symbol);
55
79
  return lua.createIdentifier(text, identifier, symbolId, identifier.text);
56
80
  }
@@ -16,13 +16,22 @@ function transformLoopBody(context, loop) {
16
16
  const body = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, loop.statement));
17
17
  const scope = context.popScope();
18
18
  const scopeId = scope.id;
19
- if (!scope.loopContinued) {
20
- return body;
19
+ switch (scope.loopContinued) {
20
+ case undefined:
21
+ return body;
22
+ case scope_1.LoopContinued.WithGoto:
23
+ return [lua.createDoStatement(body), lua.createLabelStatement(`__continue${scopeId}`)];
24
+ case scope_1.LoopContinued.WithRepeatBreak:
25
+ const identifier = lua.createIdentifier(`__continue${scopeId}`);
26
+ const literalTrue = lua.createBooleanLiteral(true);
27
+ return [
28
+ lua.createDoStatement([
29
+ lua.createVariableDeclarationStatement(identifier),
30
+ lua.createRepeatStatement(lua.createBlock([...body, lua.createAssignmentStatement(identifier, literalTrue)]), literalTrue),
31
+ lua.createIfStatement(lua.createUnaryExpression(identifier, lua.SyntaxKind.NotOperator), lua.createBlock([lua.createBreakStatement()])),
32
+ ]),
33
+ ];
21
34
  }
22
- const baseResult = [lua.createDoStatement(body)];
23
- const continueLabel = lua.createLabelStatement(`__continue${scopeId}`);
24
- baseResult.push(continueLabel);
25
- return baseResult;
26
35
  }
27
36
  exports.transformLoopBody = transformLoopBody;
28
37
  function getVariableDeclarationBinding(context, node) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.20.1",
3
+ "version": "1.21.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/",