typescript-to-lua 1.6.0 → 1.6.1

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.
@@ -93,9 +93,8 @@ do
93
93
  return self:entries()
94
94
  end
95
95
  function Map.prototype.entries(self)
96
- local ____temp_0 = self
97
- local items = ____temp_0.items
98
- local nextKey = ____temp_0.nextKey
96
+ local items = self.items
97
+ local nextKey = self.nextKey
99
98
  local key = self.firstKey
100
99
  return {
101
100
  [Symbol.iterator] = function(self)
@@ -123,9 +122,8 @@ do
123
122
  }
124
123
  end
125
124
  function Map.prototype.values(self)
126
- local ____temp_1 = self
127
- local items = ____temp_1.items
128
- local nextKey = ____temp_1.nextKey
125
+ local items = self.items
126
+ local nextKey = self.nextKey
129
127
  local key = self.firstKey
130
128
  return {
131
129
  [Symbol.iterator] = function(self)
@@ -1280,9 +1280,8 @@ do
1280
1280
  return self:entries()
1281
1281
  end
1282
1282
  function Map.prototype.entries(self)
1283
- local ____temp_0 = self
1284
- local items = ____temp_0.items
1285
- local nextKey = ____temp_0.nextKey
1283
+ local items = self.items
1284
+ local nextKey = self.nextKey
1286
1285
  local key = self.firstKey
1287
1286
  return {
1288
1287
  [Symbol.iterator] = function(self)
@@ -1310,9 +1309,8 @@ do
1310
1309
  }
1311
1310
  end
1312
1311
  function Map.prototype.values(self)
1313
- local ____temp_1 = self
1314
- local items = ____temp_1.items
1315
- local nextKey = ____temp_1.nextKey
1312
+ local items = self.items
1313
+ local nextKey = self.nextKey
1316
1314
  local key = self.firstKey
1317
1315
  return {
1318
1316
  [Symbol.iterator] = function(self)
@@ -59,8 +59,13 @@ function getDeclarationContextType({ program }, signatureDeclaration) {
59
59
  }
60
60
  // When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
61
61
  const options = program.getCompilerOptions();
62
- if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) {
63
- return ContextType.Void;
62
+ if (options.noImplicitSelf) {
63
+ const sourceFile = program.getSourceFile(signatureDeclaration.getSourceFile().fileName);
64
+ if (sourceFile !== undefined &&
65
+ !program.isSourceFileDefaultLibrary(sourceFile) &&
66
+ !program.isSourceFileFromExternalLibrary(sourceFile)) {
67
+ return ContextType.Void;
68
+ }
64
69
  }
65
70
  // Walk up to find @noSelf or @noSelfInFile
66
71
  if (hasNoSelfAncestor(signatureDeclaration)) {
@@ -72,17 +72,14 @@ function transformAssignmentWithRightPrecedingStatements(context, lhs, right, ri
72
72
  }
73
73
  exports.transformAssignmentWithRightPrecedingStatements = transformAssignmentWithRightPrecedingStatements;
74
74
  function transformDestructuredAssignmentExpression(context, expression) {
75
- const rootIdentifier = context.createTempNameForNode(expression.right);
76
75
  let [rightPrecedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
77
76
  context.addPrecedingStatements(rightPrecedingStatements);
78
77
  if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
79
78
  right = (0, lua_ast_1.wrapInTable)(right);
80
79
  }
81
- const statements = [
82
- lua.createVariableDeclarationStatement(rootIdentifier, right),
83
- ...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier, rightPrecedingStatements.length > 0),
84
- ];
85
- return { statements, result: rootIdentifier };
80
+ const rightExpr = (0, expression_list_1.moveToPrecedingTemp)(context, right, expression.right);
81
+ const statements = (0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rightExpr, rightPrecedingStatements.length > 0);
82
+ return { statements, result: rightExpr };
86
83
  }
87
84
  function transformAssignmentExpression(context, expression) {
88
85
  // Validate assignment
@@ -99,15 +96,12 @@ function transformAssignmentExpression(context, expression) {
99
96
  return result;
100
97
  }
101
98
  if (ts.isPropertyAccessExpression(expression.left) || ts.isElementAccessExpression(expression.left)) {
102
- const tempVar = context.createTempNameForNode(expression.right);
103
99
  const [precedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
104
100
  const left = transformAssignmentLeftHandSideExpression(context, expression.left, precedingStatements.length > 0);
105
- context.addPrecedingStatements([
106
- ...precedingStatements,
107
- lua.createVariableDeclarationStatement(tempVar, right, expression.right),
108
- lua.createAssignmentStatement(left, lua.cloneIdentifier(tempVar), expression.left),
109
- ]);
110
- return lua.cloneIdentifier(tempVar);
101
+ context.addPrecedingStatements(precedingStatements);
102
+ const rightExpr = (0, expression_list_1.moveToPrecedingTemp)(context, right, expression.right);
103
+ context.addPrecedingStatements(lua.createAssignmentStatement(left, rightExpr, expression.left));
104
+ return rightExpr;
111
105
  }
112
106
  else {
113
107
  // Simple assignment
@@ -158,16 +152,8 @@ function transformAssignmentStatement(context, expression) {
158
152
  const left = expression.left.elements.map(e => transformAssignmentLeftHandSideExpression(context, e));
159
153
  return [lua.createAssignmentStatement(left, right, expression)];
160
154
  }
161
- let [rightPrecedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
162
- context.addPrecedingStatements(rightPrecedingStatements);
163
- if ((0, multi_1.isMultiReturnCall)(context, expression.right)) {
164
- right = (0, lua_ast_1.wrapInTable)(right);
165
- }
166
- const rootIdentifier = context.createTempNameForNode(expression.left);
167
- return [
168
- lua.createVariableDeclarationStatement(rootIdentifier, right),
169
- ...(0, destructuring_assignments_1.transformDestructuringAssignment)(context, expression, rootIdentifier, rightPrecedingStatements.length > 0),
170
- ];
155
+ const { statements } = transformDestructuredAssignmentExpression(context, expression);
156
+ return statements;
171
157
  }
172
158
  else {
173
159
  const [precedingStatements, right] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.right));
@@ -184,10 +184,9 @@ const transformCallExpression = (node, context) => {
184
184
  const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
185
185
  return lua.createCallExpression(lua.createTableIndexExpression(context.transformExpression(ts.factory.createSuper()), lua.createStringLiteral("____constructor")), parameters);
186
186
  }
187
- const signatureDeclaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
188
187
  let callPath;
189
188
  let parameters;
190
- const isContextualCall = !signatureDeclaration || (0, function_context_1.getDeclarationContextType)(context, signatureDeclaration) !== function_context_1.ContextType.Void;
189
+ const isContextualCall = isContextualCallExpression(context, signature);
191
190
  if (!isContextualCall) {
192
191
  [callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
193
192
  }
@@ -195,9 +194,7 @@ const transformCallExpression = (node, context) => {
195
194
  // if is optionalContinuation, context will be handled by transformOptionalChain.
196
195
  const useGlobalContext = !context.isStrict && optionalContinuation === undefined;
197
196
  const callContext = useGlobalContext ? ts.factory.createIdentifier("_G") : ts.factory.createNull();
198
- [callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature,
199
- // Only pass context if noImplicitSelf is not configured
200
- context.options.noImplicitSelf ? undefined : callContext);
197
+ [callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature, callContext);
201
198
  }
202
199
  const callExpression = lua.createCallExpression(callPath, parameters, node);
203
200
  if (optionalContinuation && isContextualCall) {
@@ -206,6 +203,13 @@ const transformCallExpression = (node, context) => {
206
203
  return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(callExpression) : callExpression;
207
204
  };
208
205
  exports.transformCallExpression = transformCallExpression;
206
+ function isContextualCallExpression(context, signature) {
207
+ const declaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
208
+ if (!declaration) {
209
+ return !context.options.noImplicitSelf;
210
+ }
211
+ return (0, function_context_1.getDeclarationContextType)(context, declaration) !== function_context_1.ContextType.Void;
212
+ }
209
213
  function getCalledExpression(node) {
210
214
  function unwrapExpression(expression) {
211
215
  expression = ts.skipOuterExpressions(expression);
@@ -12,7 +12,10 @@ const optional_chaining_1 = require("./optional-chaining");
12
12
  function shouldMoveToTemp(context, expression, tsOriginal) {
13
13
  return (!lua.isLiteral(expression) &&
14
14
  !(lua.isIdentifier(expression) && expression.symbolId === context_1.tempSymbolId) && // Treat generated temps as consts
15
- !(tsOriginal && ((0, typescript_1.isConstIdentifier)(context, tsOriginal) || (0, optional_chaining_1.isOptionalContinuation)(tsOriginal))));
15
+ !(tsOriginal &&
16
+ ((0, typescript_1.isConstIdentifier)(context, tsOriginal) ||
17
+ (0, optional_chaining_1.isOptionalContinuation)(tsOriginal) ||
18
+ tsOriginal.kind === ts.SyntaxKind.ThisKeyword)));
16
19
  }
17
20
  exports.shouldMoveToTemp = shouldMoveToTemp;
18
21
  // Cache an expression in a preceding statement and return the temp identifier
@@ -2,7 +2,7 @@ import * as ts from "typescript";
2
2
  import * as lua from "../../LuaAST";
3
3
  import { FunctionVisitor, TransformationContext } from "../context";
4
4
  export declare function transformArrayBindingElement(context: TransformationContext, name: ts.ArrayBindingElement): lua.Identifier;
5
- export declare function transformBindingPattern(context: TransformationContext, pattern: ts.BindingPattern, table: lua.Identifier, propertyAccessStack?: ts.PropertyName[]): lua.Statement[];
5
+ export declare function transformBindingPattern(context: TransformationContext, pattern: ts.BindingPattern, table: lua.Expression, propertyAccessStack?: ts.PropertyName[]): lua.Statement[];
6
6
  export declare function transformBindingVariableDeclaration(context: TransformationContext, bindingPattern: ts.BindingPattern, initializer?: ts.Expression): lua.Statement[];
7
7
  export declare function transformVariableDeclaration(context: TransformationContext, statement: ts.VariableDeclaration): lua.Statement[];
8
8
  export declare function checkVariableDeclarationList(context: TransformationContext, node: ts.VariableDeclarationList): void;
@@ -14,6 +14,7 @@ const function_1 = require("./function");
14
14
  const identifier_1 = require("./identifier");
15
15
  const multi_1 = require("./language-extensions/multi");
16
16
  const literal_1 = require("./literal");
17
+ const expression_list_1 = require("./expression-list");
17
18
  function transformArrayBindingElement(context, name) {
18
19
  if (ts.isOmittedExpression(name)) {
19
20
  return lua.createAnonymousIdentifier(name);
@@ -117,22 +118,18 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
117
118
  const isComplexBindingElement = (e) => ts.isBindingElement(e) && (!ts.isIdentifier(e.name) || e.dotDotDotToken);
118
119
  if (ts.isObjectBindingPattern(bindingPattern) || bindingPattern.elements.some(isComplexBindingElement)) {
119
120
  let table;
120
- if (initializer !== undefined && ts.isIdentifier(initializer)) {
121
- table = (0, identifier_1.transformIdentifier)(context, initializer);
122
- }
123
- else {
121
+ if (initializer) {
124
122
  // Contain the expression in a temporary variable
125
- if (initializer) {
126
- table = context.createTempNameForNode(initializer);
127
- let expression = context.transformExpression(initializer);
128
- if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
129
- expression = (0, lua_ast_1.wrapInTable)(expression);
130
- }
131
- statements.push(lua.createVariableDeclarationStatement(table, expression));
132
- }
133
- else {
134
- table = lua.createAnonymousIdentifier();
123
+ let expression = context.transformExpression(initializer);
124
+ if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
125
+ expression = (0, lua_ast_1.wrapInTable)(expression);
135
126
  }
127
+ const [moveStatements, movedExpr] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, expression_list_1.moveToPrecedingTemp)(context, expression, initializer));
128
+ statements.push(...moveStatements);
129
+ table = movedExpr;
130
+ }
131
+ else {
132
+ table = lua.createAnonymousIdentifier();
136
133
  }
137
134
  statements.push(...transformBindingPattern(context, bindingPattern, table));
138
135
  return statements;
@@ -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, unless it is included from node_modules
119
- if (required === "lualib_bundle" && !isNodeModulesFile(file.fileName)) {
118
+ // Do no resolve lualib - always use the lualib of the application entry point, not the lualib from external packages
119
+ if (required === "lualib_bundle") {
120
120
  dependencies.push({ fileName: "lualib_bundle", code: "" });
121
121
  continue;
122
122
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
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/",