Skip to content

Commit b4bfeba

Browse files
committed
Merge remote-tracking branch 'upstream/master' into eslint
2 parents 1d05790 + 3ad73bb commit b4bfeba

File tree

120 files changed

+4455
-1891
lines changed

Some content is hidden

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

120 files changed

+4455
-1891
lines changed

CHANGELOG.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

3-
## Unreleased
3+
## 0.32.0
4+
5+
- **Deprecated:** The `noHoisting` option has been removed, hoisting will always be done.
46

57
- TypeScript has been updated to 3.8. See [release notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html) for details.
68

@@ -52,6 +54,55 @@
5254

5355
This change simplifies our codebase and opens a path to object accessors implementation
5456

57+
- Errors reported during transpilation now are created as TypeScript diagnostics instead of being thrown as JavaScript errors. This makes TypeScriptToLua always try to generate valid code (even in presence of errors) and allows multiple errors to be reported in a single file:
58+
59+
```ts
60+
for (var x in []) {
61+
}
62+
```
63+
64+
```shell
65+
$ tstl file.ts
66+
file.ts:1:1 - error TSTL: Iterating over arrays with 'for ... in' is not allowed.
67+
file.ts:1:6 - error TSTL: `var` declarations are not supported. Use `let` or `const` instead.
68+
69+
$ cat file.lua
70+
for x in pairs({}) do
71+
end
72+
```
73+
74+
- Added `tstl.luaPlugins` option, allowing to specify plugins in a `tsconfig.json` file:
75+
76+
```json
77+
{
78+
"tstl": {
79+
"luaPlugins": [{ "name": "./plugin.ts" }]
80+
}
81+
}
82+
```
83+
84+
- Added support for all valid TS `for ... of` loop variable patterns.
85+
86+
- Fixed a bug where spread expressions in array literals were not correctly translated:
87+
88+
```diff
89+
- [1, ...[2, 3], 4] // --> { 1, 2, 4 }
90+
+ [1, ...[2, 3], 4] // --> { 1, 2, 3, 4 }
91+
92+
- ((...values) => values)(1, ...[2, 3], 4) // --> { 1, 2, 4 }
93+
+ ((...values) => values)(1, ...[2, 3], 4) // --> { 1, 2, 3, 4 }
94+
```
95+
96+
- Fixed Lua error when left hand side of `instanceof` was not a table type.
97+
98+
- Fixed `sourcemapTraceback` function returning a value different from the standard Lua result in 5.1.
99+
100+
- Fixed missing LuaLib dependency for Error LuaLib function.
101+
102+
- Fixed several issues with exported identifiers breaking `for ... in` loops and some default class code.
103+
104+
- Fixed overflowing numbers transforming to undefined Infinity, instead they are now transformed to `math.huge`.
105+
55106
## 0.31.0
56107
57108
- **Breaking:** The old annotation syntax (`/* !varArg */`) **no longer works**, the only currently supported syntax is:

package-lock.json

Lines changed: 7 additions & 7 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": "0.31.0",
3+
"version": "0.32.0",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
55
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
66
"license": "MIT",

src/CompilerOptions.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ts from "typescript";
2+
import * as diagnosticFactories from "./transpilation/diagnostics";
23

34
type KnownKeys<T> = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends {
45
[K in keyof T]: infer U;
@@ -17,6 +18,11 @@ export interface TransformerImport {
1718
[option: string]: any;
1819
}
1920

21+
export interface LuaPluginImport {
22+
name: string;
23+
import?: string;
24+
}
25+
2026
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2127
noImplicitSelf?: boolean;
2228
noHeader?: boolean;
@@ -25,6 +31,7 @@ export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2531
luaTarget?: LuaTarget;
2632
luaLibImport?: LuaLibImportKind;
2733
sourceMapTraceback?: boolean;
34+
luaPlugins?: LuaPluginImport[];
2835
plugins?: Array<ts.PluginImport | TransformerImport>;
2936
[option: string]: any;
3037
};
@@ -47,37 +54,12 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
4754
const diagnostics: ts.Diagnostic[] = [];
4855

4956
if (options.luaBundle && !options.luaBundleEntry) {
50-
diagnostics.push(configErrorDiagnostic(`'luaBundleEntry' is required when 'luaBundle' is enabled.`));
57+
diagnostics.push(diagnosticFactories.luaBundleEntryIsRequired());
5158
}
5259

5360
if (options.luaBundle && options.luaLibImport === LuaLibImportKind.Inline) {
54-
diagnostics.push(
55-
configWarningDiagnostic(
56-
`Using 'luaBundle' with 'luaLibImport: "inline"' might generate duplicate code. ` +
57-
`It is recommended to use 'luaLibImport: "require"'`
58-
)
59-
);
61+
diagnostics.push(diagnosticFactories.usingLuaBundleWithInlineMightGenerateDuplicateCode());
6062
}
6163

6264
return diagnostics;
6365
}
64-
65-
const configErrorDiagnostic = (message: string): ts.Diagnostic => ({
66-
file: undefined,
67-
start: undefined,
68-
length: undefined,
69-
category: ts.DiagnosticCategory.Error,
70-
code: 0,
71-
source: "typescript-to-lua",
72-
messageText: message,
73-
});
74-
75-
const configWarningDiagnostic = (message: string): ts.Diagnostic => ({
76-
file: undefined,
77-
start: undefined,
78-
length: undefined,
79-
category: ts.DiagnosticCategory.Warning,
80-
code: 0,
81-
source: "typescript-to-lua",
82-
messageText: message,
83-
});

src/LuaPrinter.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ts from "typescript";
44
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
55
import * as lua from "./LuaAST";
66
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
7-
import { isValidLuaIdentifier, luaKeywords } from "./transformation/utils/safe-names";
7+
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
88
import { EmitHost } from "./transpilation";
99
import { trimExtension } from "./utils";
1010

@@ -659,11 +659,7 @@ export class LuaPrinter {
659659
const value = this.printExpression(expression.value);
660660

661661
if (expression.key) {
662-
if (
663-
lua.isStringLiteral(expression.key) &&
664-
isValidLuaIdentifier(expression.key.value) &&
665-
!luaKeywords.has(expression.key.value)
666-
) {
662+
if (lua.isStringLiteral(expression.key) && isValidLuaIdentifier(expression.key.value)) {
667663
chunks.push(expression.key.value, " = ", value);
668664
} else {
669665
chunks.push("[", this.printExpression(expression.key), "] = ", value);
@@ -760,11 +756,7 @@ export class LuaPrinter {
760756
const chunks: SourceChunk[] = [];
761757

762758
chunks.push(this.printExpressionInParenthesesIfNeeded(expression.table));
763-
if (
764-
lua.isStringLiteral(expression.index) &&
765-
isValidLuaIdentifier(expression.index.value) &&
766-
!luaKeywords.has(expression.index.value)
767-
) {
759+
if (lua.isStringLiteral(expression.index) && isValidLuaIdentifier(expression.index.value)) {
768760
chunks.push(".", this.createSourceNode(expression.index, expression.index.value));
769761
} else {
770762
chunks.push("[", this.printExpression(expression.index), "]");

src/cli/diagnostics.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import * as ts from "typescript";
2+
import { createSerialDiagnosticFactory, createDiagnosticFactoryWithCode } from "../utils";
23

3-
export const tstlOptionsAreMovingToTheTstlObject = (tstl: Record<string, any>): ts.Diagnostic => ({
4-
file: undefined,
5-
start: undefined,
6-
length: undefined,
4+
export const tstlOptionsAreMovingToTheTstlObject = createSerialDiagnosticFactory((tstl: Record<string, any>) => ({
75
category: ts.DiagnosticCategory.Warning,
8-
code: 0,
9-
source: "typescript-to-lua",
106
messageText:
117
'TSTL options are moving to the "tstl" object. Adjust your tsconfig to look like\n' +
128
`"tstl": ${JSON.stringify(tstl, undefined, 4)}`,
13-
});
9+
}));
1410

1511
export const watchErrorSummary = (errorCount: number): ts.Diagnostic => ({
1612
file: undefined,
@@ -24,16 +20,8 @@ export const watchErrorSummary = (errorCount: number): ts.Diagnostic => ({
2420
: `Found ${errorCount} errors. Watching for file changes.`,
2521
});
2622

27-
const createCommandLineError = <TArgs extends any[]>(code: number, getMessage: (...args: TArgs) => string) => (
28-
...args: TArgs
29-
): ts.Diagnostic => ({
30-
file: undefined,
31-
start: undefined,
32-
length: undefined,
33-
category: ts.DiagnosticCategory.Error,
34-
code,
35-
messageText: getMessage(...args),
36-
});
23+
const createCommandLineError = <TArgs extends any[]>(code: number, getMessage: (...args: TArgs) => string) =>
24+
createDiagnosticFactoryWithCode(code, (...args: TArgs) => ({ messageText: getMessage(...args) }));
3725

3826
export const unknownCompilerOption = createCommandLineError(
3927
5023,

src/cli/parse.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ interface CommandLineOptionOfEnum extends CommandLineOptionBase {
1717
choices: string[];
1818
}
1919

20-
interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
21-
type: "boolean";
20+
interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
21+
type: "boolean" | "string" | "object";
2222
}
2323

24-
interface CommandLineOptionOfString extends CommandLineOptionBase {
25-
type: "string";
26-
}
27-
28-
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean | CommandLineOptionOfString;
24+
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;
2925

3026
export const optionDeclarations: CommandLineOption[] = [
3127
{
@@ -66,6 +62,11 @@ export const optionDeclarations: CommandLineOption[] = [
6662
description: "Applies the source map to show source TS files and lines in error tracebacks.",
6763
type: "boolean",
6864
},
65+
{
66+
name: "luaPlugins",
67+
description: "List of TypeScriptToLua plugins.",
68+
type: "object",
69+
},
6970
];
7071

7172
export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine {
@@ -170,8 +171,9 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
170171
if (value === null) return { value };
171172

172173
switch (option.type) {
174+
case "boolean":
173175
case "string":
174-
case "boolean": {
176+
case "object": {
175177
if (typeof value !== option.type) {
176178
return {
177179
value: undefined,

src/cli/report.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as ts from "typescript";
22

3+
export const prepareDiagnosticForFormatting = (diagnostic: ts.Diagnostic) =>
4+
diagnostic.source === "typescript-to-lua" ? { ...diagnostic, code: "TL" as any } : diagnostic;
5+
36
export function createDiagnosticReporter(pretty: boolean, system = ts.sys): ts.DiagnosticReporter {
47
const reporter = ts.createDiagnosticReporter(system, pretty);
5-
return diagnostic => {
6-
if (diagnostic.source === "typescript-to-lua") {
7-
diagnostic = { ...diagnostic, code: `TL${diagnostic.code}` as any };
8-
}
9-
10-
reporter(diagnostic);
11-
};
8+
return diagnostic => reporter(prepareDiagnosticForFormatting(diagnostic));
129
}

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ export * from "./LuaAST";
66
export { LuaLibFeature } from "./LuaLib";
77
export * from "./LuaPrinter";
88
export * from "./transformation/context";
9-
export { TranspileError } from "./transformation/utils/errors";
109
export * from "./transpilation";

src/transformation/builtins/array.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
4-
import { UnsupportedProperty } from "../utils/errors";
4+
import { unsupportedProperty } from "../utils/diagnostics";
55
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
66
import { PropertyCallExpression, transformArguments } from "../visitors/call";
77

88
export function transformArrayPrototypeCall(
99
context: TransformationContext,
1010
node: PropertyCallExpression
11-
): lua.CallExpression {
11+
): lua.CallExpression | undefined {
1212
const expression = node.expression;
1313
const signature = context.checker.getResolvedSignature(node);
1414
const params = transformArguments(context, node.arguments, signature);
@@ -79,7 +79,7 @@ export function transformArrayPrototypeCall(
7979
case "flatMap":
8080
return transformLuaLibFunction(context, LuaLibFeature.ArrayFlatMap, node, caller, ...params);
8181
default:
82-
throw UnsupportedProperty("array", expressionName, node);
82+
context.diagnostics.push(unsupportedProperty(expression.name, "array", expressionName));
8383
}
8484
}
8585

0 commit comments

Comments
 (0)