typescript-to-lua 1.31.0 → 1.31.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.
@@ -6,11 +6,13 @@ const lualib_1 = require("../utils/lualib");
6
6
  function usingTransformer(context) {
7
7
  return ctx => sourceFile => {
8
8
  function visit(node) {
9
- if (ts.isBlock(node)) {
9
+ if (ts.isBlock(node) || ts.isSourceFile(node)) {
10
10
  const [hasUsings, newStatements] = transformBlockWithUsing(context, node.statements, node);
11
11
  if (hasUsings) {
12
12
  // Recurse visitor into updated block to find further usings
13
- const updatedBlock = ts.factory.updateBlock(node, newStatements);
13
+ const updatedBlock = ts.isBlock(node)
14
+ ? ts.factory.updateBlock(node, newStatements)
15
+ : ts.factory.updateSourceFile(node, newStatements);
14
16
  const result = ts.visitEachChild(updatedBlock, visit, ctx);
15
17
  // Set all the synthetic node parents to something that makes sense
16
18
  const parent = [updatedBlock];
@@ -28,7 +30,8 @@ function usingTransformer(context) {
28
30
  }
29
31
  return ts.visitEachChild(node, visit, ctx);
30
32
  }
31
- return ts.visitEachChild(sourceFile, visit, ctx);
33
+ const transformedSourceFile = ts.visitEachChild(sourceFile, visit, ctx);
34
+ return visit(transformedSourceFile);
32
35
  };
33
36
  }
34
37
  function isUsingDeclarationList(node) {
@@ -68,7 +71,13 @@ function transformBlockWithUsing(context, statements, block) {
68
71
  if (isAwaitUsing) {
69
72
  call = ts.factory.createAwaitExpression(call);
70
73
  }
71
- if (ts.isBlock(block.parent) && block.parent.statements[block.parent.statements.length - 1] !== block) {
74
+ if (ts.isSourceFile(block)) {
75
+ // If block is a sourcefile, don't insert a return statement into root code
76
+ newStatements.push(ts.factory.createExpressionStatement(call));
77
+ }
78
+ else if (block.parent &&
79
+ ts.isBlock(block.parent) &&
80
+ block.parent.statements[block.parent.statements.length - 1] !== block) {
72
81
  // If this is a free-standing block in a function (not the last statement), dont return the value
73
82
  newStatements.push(ts.factory.createExpressionStatement(call));
74
83
  }
@@ -3,6 +3,7 @@ import * as lua from "../../LuaAST";
3
3
  import { TransformationContext } from "../context";
4
4
  export declare function hasDefaultExportModifier(node: ts.Node): boolean;
5
5
  export declare function hasExportModifier(node: ts.Node): boolean;
6
+ export declare function shouldBeExported(node: ts.Node): boolean;
6
7
  export declare const createDefaultExportStringLiteral: (original?: ts.Node) => lua.StringLiteral;
7
8
  export declare function getExportedSymbolDeclaration(symbol: ts.Symbol): ts.Declaration | undefined;
8
9
  export declare function getSymbolFromIdentifier(context: TransformationContext, identifier: lua.Identifier): ts.Symbol | undefined;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createDefaultExportStringLiteral = void 0;
4
4
  exports.hasDefaultExportModifier = hasDefaultExportModifier;
5
5
  exports.hasExportModifier = hasExportModifier;
6
+ exports.shouldBeExported = shouldBeExported;
6
7
  exports.getExportedSymbolDeclaration = getExportedSymbolDeclaration;
7
8
  exports.getSymbolFromIdentifier = getSymbolFromIdentifier;
8
9
  exports.getIdentifierExportScope = getIdentifierExportScope;
@@ -30,6 +31,13 @@ function hasExportModifier(node) {
30
31
  return (ts.canHaveModifiers(node) &&
31
32
  ((_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword)) === true);
32
33
  }
34
+ function shouldBeExported(node) {
35
+ if (hasExportModifier(node)) {
36
+ // Don't export if we're inside a namespace (module declaration)
37
+ return ts.findAncestor(node, ts.isModuleDeclaration) === undefined;
38
+ }
39
+ return false;
40
+ }
33
41
  const createDefaultExportStringLiteral = (original) => lua.createStringLiteral("default", original);
34
42
  exports.createDefaultExportStringLiteral = createDefaultExportStringLiteral;
35
43
  function getExportedSymbolDeclaration(symbol) {
@@ -110,7 +110,7 @@ function transformContextualCallExpression(context, node, args) {
110
110
  return lua.createCallExpression(expression, transformedArguments, node);
111
111
  }
112
112
  }
113
- else if (ts.isIdentifier(left)) {
113
+ else if (ts.isIdentifier(left) || ts.isCallExpression(left)) {
114
114
  const callContext = context.isStrict ? ts.factory.createNull() : ts.factory.createIdentifier("_G");
115
115
  let expression;
116
116
  [expression, transformedArguments] = transformCallWithArguments(context, left, transformedArguments, argPrecedingStatements, callContext);
@@ -153,7 +153,7 @@ function transformClassLikeDeclaration(classDeclaration, context, nameOverride)
153
153
  const decoratingExpression = (0, decorators_1.createClassDecoratingExpression)(context, classDeclaration, localClassName);
154
154
  const decoratingStatement = lua.createAssignmentStatement(localClassName, decoratingExpression);
155
155
  result.push(decoratingStatement);
156
- if ((0, export_1.hasExportModifier)(classDeclaration)) {
156
+ if ((0, export_1.shouldBeExported)(classDeclaration)) {
157
157
  const exportExpression = (0, export_1.hasDefaultExportModifier)(classDeclaration)
158
158
  ? (0, export_1.createDefaultExportExpression)(classDeclaration)
159
159
  : (0, export_1.createExportedIdentifier)(context, className);
@@ -6,8 +6,10 @@ const lua = require("../../../LuaAST");
6
6
  const preceding_statements_1 = require("../../utils/preceding-statements");
7
7
  const variable_declaration_1 = require("../variable-declaration");
8
8
  const utils_1 = require("./utils");
9
+ const scope_1 = require("../../utils/scope");
9
10
  const transformForStatement = (statement, context) => {
10
11
  const result = [];
12
+ context.pushScope(scope_1.ScopeType.Loop);
11
13
  if (statement.initializer) {
12
14
  if (ts.isVariableDeclarationList(statement.initializer)) {
13
15
  (0, variable_declaration_1.checkVariableDeclarationList)(context, statement.initializer);
@@ -47,6 +49,7 @@ const transformForStatement = (statement, context) => {
47
49
  }
48
50
  // while (condition) do ... end
49
51
  result.push(lua.createWhileStatement(lua.createBlock(body), condition, statement));
52
+ context.popScope();
50
53
  return lua.createDoStatement(result, statement);
51
54
  };
52
55
  exports.transformForStatement = transformForStatement;
@@ -28,10 +28,24 @@ function transformLoopBody(context, loop) {
28
28
  case scope_1.LoopContinued.WithRepeatBreak:
29
29
  const identifier = lua.createIdentifier(`__continue${scopeId}`);
30
30
  const literalTrue = lua.createBooleanLiteral(true);
31
+ // If there is a break in the body statements, do not include any code afterwards
32
+ const transformedBodyStatements = [];
33
+ let bodyBroken = false;
34
+ for (const statement of body) {
35
+ transformedBodyStatements.push(statement);
36
+ if (lua.isBreakStatement(statement)) {
37
+ bodyBroken = true;
38
+ break;
39
+ }
40
+ }
41
+ if (!bodyBroken) {
42
+ // Tell loop to continue if not broken
43
+ transformedBodyStatements.push(lua.createAssignmentStatement(identifier, literalTrue));
44
+ }
31
45
  return [
32
46
  lua.createDoStatement([
33
47
  lua.createVariableDeclarationStatement(identifier),
34
- lua.createRepeatStatement(lua.createBlock([...body, lua.createAssignmentStatement(identifier, literalTrue)]), literalTrue),
48
+ lua.createRepeatStatement(lua.createBlock(transformedBodyStatements), literalTrue),
35
49
  lua.createIfStatement(lua.createUnaryExpression(identifier, lua.SyntaxKind.NotOperator), lua.createBlock([lua.createBreakStatement()])),
36
50
  ]),
37
51
  ];
@@ -7,6 +7,8 @@ const lua = require("../../LuaAST");
7
7
  const utils_1 = require("../../utils");
8
8
  const bit_1 = require("./binary-expression/bit");
9
9
  const compound_1 = require("./binary-expression/compound");
10
+ const typescript_1 = require("../utils/typescript");
11
+ const lualib_1 = require("../utils/lualib");
10
12
  function transformUnaryExpressionStatement(context, node) {
11
13
  const expression = ts.isExpressionStatement(node) ? node.expression : node;
12
14
  if (ts.isPrefixUnaryExpression(expression) &&
@@ -38,11 +40,26 @@ const transformPrefixUnaryExpression = (expression, context) => {
38
40
  return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.PlusToken, false);
39
41
  case ts.SyntaxKind.MinusMinusToken:
40
42
  return (0, compound_1.transformCompoundAssignmentExpression)(context, expression, expression.operand, ts.factory.createNumericLiteral(1), ts.SyntaxKind.MinusToken, false);
41
- case ts.SyntaxKind.PlusToken:
42
- // TODO: Wrap with `Number`
43
- return context.transformExpression(expression.operand);
44
- case ts.SyntaxKind.MinusToken:
45
- return lua.createUnaryExpression(context.transformExpression(expression.operand), lua.SyntaxKind.NegationOperator);
43
+ case ts.SyntaxKind.PlusToken: {
44
+ const operand = context.transformExpression(expression.operand);
45
+ const type = context.checker.getTypeAtLocation(expression.operand);
46
+ if ((0, typescript_1.isNumberType)(context, type)) {
47
+ return operand;
48
+ }
49
+ else {
50
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, expression, operand);
51
+ }
52
+ }
53
+ case ts.SyntaxKind.MinusToken: {
54
+ const operand = context.transformExpression(expression.operand);
55
+ const type = context.checker.getTypeAtLocation(expression.operand);
56
+ if ((0, typescript_1.isNumberType)(context, type)) {
57
+ return lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator);
58
+ }
59
+ else {
60
+ return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Number, expression, lua.createUnaryExpression(operand, lua.SyntaxKind.NegationOperator));
61
+ }
62
+ }
46
63
  case ts.SyntaxKind.ExclamationToken:
47
64
  return lua.createUnaryExpression(context.transformExpression(expression.operand), lua.SyntaxKind.NotOperator);
48
65
  case ts.SyntaxKind.TildeToken:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.31.0",
3
+ "version": "1.31.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/",