Skip to content

Commit 521f102

Browse files
committed
2 parents 14b2bc1 + 37b632a commit 521f102

43 files changed

Lines changed: 467 additions & 94 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 1.8.0
4+
5+
- Added support for the [tsconfig.json paths](https://www.typescriptlang.org/tsconfig#paths) configuration option.
6+
- Fixed spreading lua iterables & iterators translating to incorrect lua.
7+
- You can now write things like `[...pairs(obj)]`.
8+
- Fixed a bug in module resolution resolving the wrong lua files when having the same file names in nested directories.
9+
- Fixed a bug causing temporary variables for nested destructuring in loop variables to be outside the loop, instead of inside.
10+
- Fixed import expressions not actually requiring their import.
11+
- Fixed `super` calls not being source-mapped correctly.
12+
313
## 1.7.0
414

515
- Added support for `LuaMap` and `LuaSet` language extensions that translate to very low level Lua table operations. See [our docs](https://typescripttolua.github.io/docs/advanced/language-extensions/#luamap-and-luaset) for more information.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-to-lua",
3-
"version": "1.7.1",
3+
"version": "1.8.2",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
55
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
66
"homepage": "https://typescripttolua.github.io/",

src/CompilerOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,9 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
8787
diagnostics.push(diagnosticFactories.unsupportedJsxEmit());
8888
}
8989

90+
if (options.paths && !options.baseUrl) {
91+
diagnostics.push(diagnosticFactories.pathsWithoutBaseUrl());
92+
}
93+
9094
return diagnostics;
9195
}

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export enum LuaLibFeature {
4444
InstanceOf = "InstanceOf",
4545
InstanceOfObject = "InstanceOfObject",
4646
Iterator = "Iterator",
47+
LuaIteratorSpread = "LuaIteratorSpread",
4748
Map = "Map",
4849
MathAtan2 = "MathAtan2",
4950
MathSign = "MathSign",

src/lualib/LuaIteratorSpread.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function __TS__LuaIteratorSpread<TKey, TValue, TState>(
2+
this: (this: void, state: TState, key: TKey) => LuaMultiReturn<[TKey, TValue]>,
3+
state: TState,
4+
firstKey: TKey
5+
): LuaMultiReturn<Array<[TKey, TValue]>> {
6+
const results = [];
7+
let [key, value] = this(state, firstKey);
8+
while (key) {
9+
results.push([key, value]);
10+
[key, value] = this(state, key);
11+
}
12+
return $multi(...results);
13+
}

src/transformation/builtins/index.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import { TransformationContext } from "../context";
44
import { createNaN } from "../utils/lua-ast";
55
import { importLuaLibFeature, LuaLibFeature } from "../utils/lualib";
66
import { getIdentifierSymbolId } from "../utils/symbols";
7-
import {
8-
isStandardLibraryType,
9-
isStandardLibraryDeclaration,
10-
isStringType,
11-
isArrayType,
12-
isFunctionType,
13-
} from "../utils/typescript";
7+
import { isStandardLibraryType, isStringType, isArrayType, isFunctionType } from "../utils/typescript";
148
import { getCalledExpression } from "../visitors/call";
159
import { transformArrayConstructorCall, transformArrayProperty, transformArrayPrototypeCall } from "./array";
1610
import { transformConsoleCall } from "./console";
@@ -85,9 +79,7 @@ function tryTransformBuiltinGlobalMethodCall(
8579
calledMethod: ts.PropertyAccessExpression
8680
) {
8781
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
88-
if (!isStandardLibraryType(context, ownerType, undefined)) return;
89-
90-
const ownerSymbol = ownerType.symbol;
82+
const ownerSymbol = tryGetStandardLibrarySymbolOfType(context, ownerType);
9183
if (!ownerSymbol || ownerSymbol.parent) return;
9284

9385
let result: lua.Expression | undefined;
@@ -129,10 +121,9 @@ function tryTransformBuiltinPropertyCall(
129121
node: ts.CallExpression,
130122
calledMethod: ts.PropertyAccessExpression
131123
) {
132-
const signatureDeclaration = context.checker.getResolvedSignature(node)?.declaration;
133-
if (!signatureDeclaration || !isStandardLibraryDeclaration(context, signatureDeclaration)) return;
134-
135-
const callSymbol = context.checker.getTypeAtLocation(signatureDeclaration).symbol;
124+
const functionType = context.checker.getTypeAtLocation(node.expression);
125+
const callSymbol = tryGetStandardLibrarySymbolOfType(context, functionType);
126+
if (!callSymbol) return;
136127
const ownerSymbol = callSymbol.parent;
137128
if (!ownerSymbol || ownerSymbol.parent) return;
138129

@@ -217,3 +208,16 @@ export function checkForLuaLibType(context: TransformationContext, type: ts.Type
217208
importLuaLibFeature(context, LuaLibFeature.Error);
218209
}
219210
}
211+
212+
function tryGetStandardLibrarySymbolOfType(context: TransformationContext, type: ts.Type): ts.Symbol | undefined {
213+
if (type.isUnionOrIntersection()) {
214+
for (const subType of type.types) {
215+
const symbol = tryGetStandardLibrarySymbolOfType(context, subType);
216+
if (symbol) return symbol;
217+
}
218+
} else if (isStandardLibraryType(context, type, undefined)) {
219+
return type.symbol;
220+
}
221+
222+
return undefined;
223+
}

src/transformation/utils/language-extensions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ export enum IterableExtensionKind {
9898
PairsKey = "PairsKey",
9999
}
100100

101+
export function isLuaIterable(context: TransformationContext, type: ts.Type): boolean {
102+
return getPropertyValue(context, type, "__tstlIterable") !== undefined;
103+
}
104+
101105
export function getIterableExtensionTypeForType(
102106
context: TransformationContext,
103107
type: ts.Type

src/transformation/visitors/call.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function transformPropertyCall(
183183
if (calledMethod.expression.kind === ts.SyntaxKind.SuperKeyword) {
184184
// Super calls take the format of super.call(self,...)
185185
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
186-
return lua.createCallExpression(context.transformExpression(node.expression), parameters);
186+
return lua.createCallExpression(context.transformExpression(node.expression), parameters, node);
187187
}
188188

189189
const signatureDeclaration = signature?.getDeclaration();
@@ -207,7 +207,7 @@ function transformElementCall(context: TransformationContext, node: ts.CallExpre
207207
} else {
208208
// No context
209209
const [expression, parameters] = transformCallAndArguments(context, node.expression, node.arguments, signature);
210-
return lua.createCallExpression(expression, parameters);
210+
return lua.createCallExpression(expression, parameters, node);
211211
}
212212
}
213213

@@ -257,7 +257,8 @@ export const transformCallExpression: FunctionVisitor<ts.CallExpression> = (node
257257
context.transformExpression(ts.factory.createSuper()),
258258
lua.createStringLiteral("____constructor")
259259
),
260-
parameters
260+
parameters,
261+
node
261262
);
262263
}
263264

src/transformation/visitors/language-extensions/iterable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function transformForOfMultiIterableStatement(
1717
luaIterator: lua.Expression,
1818
invalidMultiUseDiagnostic: (node: ts.Node) => ts.Diagnostic
1919
): lua.Statement {
20+
context.pushPrecedingStatements();
2021
let identifiers: lua.Identifier[] = [];
2122

2223
if (ts.isVariableDeclarationList(statement.initializer)) {
@@ -51,6 +52,8 @@ function transformForOfMultiIterableStatement(
5152
identifiers.push(lua.createAnonymousIdentifier());
5253
}
5354

55+
block.statements.unshift(...context.popPrecedingStatements());
56+
5457
return lua.createForInStatement(block, identifiers, [luaIterator], statement);
5558
}
5659

0 commit comments

Comments
 (0)