Skip to content

Commit 2059140

Browse files
committed
Merge remote-tracking branch 'upstream/master' into destructuring-rest-patterns
2 parents 36ab803 + 0ed6fe3 commit 2059140

File tree

147 files changed

+10086
-10793
lines changed

Some content is hidden

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

147 files changed

+10086
-10793
lines changed

CHANGELOG.md

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

3+
## 0.25.0
4+
5+
- Added support for named function assignments, i.e. `const myFunc = function x(n) { ...; return x(n - 1); }`
6+
7+
- Made detection of string methods more robust.
8+
- Fixed issue regarding readonly tuple detection.
9+
- Fixed a nasty issue causing exponential complexity on chained properties/method expressions.
10+
- Improved handling of constrained generic types related to string and array detection.
11+
12+
## 0.24.0
13+
14+
- Returns in try/catch statements now properly return from the current function.
15+
- TypeScript's `globalThis` is now translated to lua's `_G`. Lualib functions were updated where relevant.
16+
17+
- Fixed issue where string/table literals were missing parentheses and caused lua syntax errors.
18+
- Various improvements/refactorings across the codebase.
19+
- Fixed syntax error in for...of loops with empty destructuring argument.
20+
- Fixed issue with `do ... while` scope.
21+
- Fixed a bug with [@combileMembersOnly](https://github.com/TypeScriptToLua/TypeScriptToLua/wiki/Compiler-Directives#compilemembersonly) where it would ignore anything before the enum name.
22+
23+
## 0.23.0
24+
25+
- Added support for OmittedExpression in array literals and array binding patterns.
26+
- Added support for [tagged template literals](https://basarat.gitbooks.io/typescript/docs/template-strings.html#tagged-templates).
27+
- Changed output lua formatting to be more debugger-friendly.
28+
- Various improvements to source maps.
29+
30+
- Fixed an issue with the interaction of super calls and exported classes.
31+
- Fixed `@noResolution` not working on named modules.
32+
- Fixed namespace merging not working due to an earlier change.
33+
34+
- Some refactoring and plumbing for the website.
35+
36+
## 0.22.0
37+
38+
- Added the [@vararg](https://github.com/TypeScriptToLua/TypeScriptToLua/wiki/Compiler-Directives#vararg) directive.
39+
- Added the [@forRange](https://github.com/TypeScriptToLua/TypeScriptToLua/wiki/Compiler-Directives#forRange) directive.
40+
- Custom ts transformers can now be loaded from tsconfig.
41+
42+
- Fixed default tstl header incorrectly showing up above lualib functions.
43+
- Some improvements to typeof expressions.
44+
45+
## 0.21.0
46+
47+
- Imports/exports that are ambient (declarations, types, interfaces, etc) or are not used in value positions no longer generate `require` statements.
48+
- For ... of loops are now translated using `ipairs`.
49+
- Added support for `array.reduce`.
50+
- Added support for `import foo = bar.baz;` statements.
51+
52+
- Fixed some issues with binding pattern parameter default values.
53+
- Fixed some issues with variable naming.
54+
- Enabled prettier on the entire codebase.
55+
356
## 0.20.0
457

558
- Added support for `string.repeat`, `string.padStart` and `string.padEnd`.

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ The real power of this transpiler is usage together with good declarations for t
6565
- [Defold Game Engine Scripting](https://github.com/dasannikov/DefoldTypeScript/blob/master/defold.d.ts)
6666
- [LÖVE 2D Game Development](https://github.com/hazzard993/love-typescript-definitions)
6767

68-
## Building & Tests
69-
70-
`npm run build` to build the project.
71-
72-
`npm run test` to run tests.
73-
74-
`npm run test-threaded` runs test in parallel, faster but less detailed output.
75-
76-
`npm run coverage` or `npm run coverage-html` to generate a coverage report.
77-
7868
## Sublime Text integration
7969

8070
This compiler works great in combination with the [Sublime Text Typescript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin) (available through the package manager as `TypeScript`).

build_lualib.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
44
import * as tstl from "./src";
5-
import { LuaLib } from "./src/LuaLib";
5+
import { loadLuaLibFeatures } from "./src/LuaLib";
66

77
const configFileName = path.resolve(__dirname, "src/lualib/tsconfig.json");
88
const { emitResult, diagnostics } = tstl.transpileProject(configFileName);
@@ -16,4 +16,4 @@ if (fs.existsSync(bundlePath)) {
1616
fs.unlinkSync(bundlePath);
1717
}
1818

19-
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature)));
19+
fs.writeFileSync(bundlePath, loadLuaLibFeatures(Object.values(tstl.LuaLibFeature), ts.sys));

package-lock.json

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

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-to-lua",
3-
"version": "0.20.0",
3+
"version": "0.25.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",
@@ -23,9 +23,9 @@
2323
"pretest": "npm run lint && ts-node --transpile-only ./build_lualib.ts",
2424
"test": "jest",
2525
"lint": "npm run lint:tslint && npm run lint:prettier",
26-
"lint:prettier": "prettier --check **/*.{js,ts,yml,json,md} || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
26+
"lint:prettier": "prettier --check \"**/*.{js,ts,yml,json,md}\" || (echo 'Run `npm run fix:prettier` to fix it.' && exit 1)",
2727
"lint:tslint": "tslint -p . && tslint -p test && tslint -p src/lualib",
28-
"fix:prettier": "prettier --check --write **/*.{js,ts,yml,json,md}",
28+
"fix:prettier": "prettier --check --write \"**/*.{js,ts,yml,json,md}\"",
2929
"release-major": "npm version major",
3030
"release-minor": "npm version minor",
3131
"release-patch": "npm version patch",
@@ -39,19 +39,21 @@
3939
"node": ">=8.5.0"
4040
},
4141
"dependencies": {
42+
"resolve": "^1.11.0",
4243
"source-map": "^0.7.3",
43-
"typescript": "^3.4.5"
44+
"typescript": "^3.5.2"
4445
},
4546
"devDependencies": {
4647
"@types/glob": "^7.1.1",
47-
"@types/jest": "^24.0.12",
48-
"@types/node": "^11.13.0",
48+
"@types/jest": "^24.0.15",
49+
"@types/node": "^11.13.14",
50+
"@types/resolve": "0.0.8",
4951
"fengari": "^0.1.4",
5052
"jest": "^24.8.0",
5153
"jest-circus": "^24.8.0",
52-
"prettier": "^1.17.0",
54+
"prettier": "^1.18.2",
5355
"ts-jest": "^24.0.2",
54-
"ts-node": "^7.0.0",
55-
"tslint": "^5.16.0"
56+
"ts-node": "^7.0.1",
57+
"tslint": "^5.17.0"
5658
}
5759
}

src/CommandLineParser.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "path";
22
import * as ts from "typescript";
33
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
4-
import * as diagnostics from "./diagnostics";
4+
import * as diagnosticFactories from "./diagnostics";
55

66
export interface ParsedCommandLine extends ts.ParsedCommandLine {
77
options: CompilerOptions;
@@ -10,7 +10,7 @@ export interface ParsedCommandLine extends ts.ParsedCommandLine {
1010
interface CommandLineOptionBase {
1111
name: string;
1212
aliases?: string[];
13-
describe: string;
13+
description: string;
1414
}
1515

1616
interface CommandLineOptionOfEnum extends CommandLineOptionBase {
@@ -23,33 +23,34 @@ interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
2323
}
2424

2525
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean;
26+
2627
const optionDeclarations: CommandLineOption[] = [
2728
{
2829
name: "luaLibImport",
29-
describe: "Specifies how js standard features missing in lua are imported.",
30+
description: "Specifies how js standard features missing in lua are imported.",
3031
type: "enum",
3132
choices: Object.values(LuaLibImportKind),
3233
},
3334
{
3435
name: "luaTarget",
3536
aliases: ["lt"],
36-
describe: "Specify Lua target version.",
37+
description: "Specify Lua target version.",
3738
type: "enum",
3839
choices: Object.values(LuaTarget),
3940
},
4041
{
4142
name: "noHeader",
42-
describe: "Specify if a header will be added to compiled files.",
43+
description: "Specify if a header will be added to compiled files.",
4344
type: "boolean",
4445
},
4546
{
4647
name: "noHoisting",
47-
describe: "Disables hoisting.",
48+
description: "Disables hoisting.",
4849
type: "boolean",
4950
},
5051
{
5152
name: "sourceMapTraceback",
52-
describe: "Applies the source map to show source TS files and lines in error tracebacks.",
53+
description: "Applies the source map to show source TS files and lines in error tracebacks.",
5354
type: "boolean",
5455
},
5556
];
@@ -78,7 +79,7 @@ export function getHelpString(): string {
7879
const valuesHint = option.type === "enum" ? option.choices.join("|") : option.type;
7980
const spacing = " ".repeat(Math.max(1, 45 - optionString.length - valuesHint.length));
8081

81-
result += `\n ${optionString} <${valuesHint}>${spacing}${option.describe}\n`;
82+
result += `\n ${optionString} <${valuesHint}>${spacing}${option.description}\n`;
8283
}
8384

8485
return result;
@@ -97,13 +98,15 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine):
9798

9899
if (parsedConfigFile.raw.tstl) {
99100
if (hasRootLevelOptions) {
100-
parsedConfigFile.errors.push(diagnostics.tstlOptionsAreMovingToTheTstlObject(parsedConfigFile.raw.tstl));
101+
parsedConfigFile.errors.push(
102+
diagnosticFactories.tstlOptionsAreMovingToTheTstlObject(parsedConfigFile.raw.tstl)
103+
);
101104
}
102105

103106
for (const key in parsedConfigFile.raw.tstl) {
104107
const option = optionDeclarations.find(option => option.name === key);
105108
if (!option) {
106-
parsedConfigFile.errors.push(diagnostics.unknownCompilerOption(key));
109+
parsedConfigFile.errors.push(diagnosticFactories.unknownCompilerOption(key));
107110
continue;
108111
}
109112

@@ -167,9 +170,11 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
167170
// Set boolean arguments without supplied value to true
168171
return { value: true, increment: 0 };
169172
}
170-
} else if (value === undefined) {
173+
}
174+
175+
if (value === undefined) {
171176
return {
172-
error: diagnostics.compilerOptionExpectsAnArgument(option.name),
177+
error: diagnosticFactories.compilerOptionExpectsAnArgument(option.name),
173178
value: undefined,
174179
increment: 0,
175180
};
@@ -191,7 +196,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
191196
if (typeof value !== "boolean") {
192197
return {
193198
value: undefined,
194-
error: diagnostics.compilerOptionRequiresAValueOfType(option.name, "boolean"),
199+
error: diagnosticFactories.compilerOptionRequiresAValueOfType(option.name, "boolean"),
195200
};
196201
}
197202

@@ -202,7 +207,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
202207
if (typeof value !== "string") {
203208
return {
204209
value: undefined,
205-
error: diagnostics.compilerOptionRequiresAValueOfType(option.name, "string"),
210+
error: diagnosticFactories.compilerOptionRequiresAValueOfType(option.name, "string"),
206211
};
207212
}
208213

@@ -211,7 +216,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
211216
const optionChoices = option.choices.join(", ");
212217
return {
213218
value: undefined,
214-
error: diagnostics.argumentForOptionMustBe(`--${option.name}`, optionChoices),
219+
error: diagnosticFactories.argumentForOptionMustBe(`--${option.name}`, optionChoices),
215220
};
216221
}
217222

src/CompilerOptions.ts

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

3-
export interface CompilerOptions extends ts.CompilerOptions {
3+
type KnownKeys<T> = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends {
4+
[_ in keyof T]: infer U;
5+
}
6+
? U
7+
: never;
8+
9+
type OmitIndexSignature<T extends Record<any, any>> = Pick<T, KnownKeys<T>>;
10+
11+
export interface TransformerImport {
12+
transform: string;
13+
import?: string;
14+
after?: boolean;
15+
afterDeclarations?: boolean;
16+
type?: "program" | "config" | "checker" | "raw" | "compilerOptions";
17+
[option: string]: any;
18+
}
19+
20+
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
421
noHeader?: boolean;
522
luaTarget?: LuaTarget;
623
luaLibImport?: LuaLibImportKind;
724
noHoisting?: boolean;
825
sourceMapTraceback?: boolean;
9-
}
26+
plugins?: Array<ts.PluginImport | TransformerImport>;
27+
[option: string]: ts.CompilerOptions[string] | Array<ts.PluginImport | TransformerImport>;
28+
};
1029

1130
export enum LuaLibImportKind {
1231
None = "none",

src/Decorator.ts

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,20 @@ export class Decorator {
44
}
55

66
public static getDecoratorKind(decoratorKindString: string): DecoratorKind | undefined {
7-
switch (decoratorKindString.toLowerCase()) {
8-
case "extension":
9-
return DecoratorKind.Extension;
10-
case "metaextension":
11-
return DecoratorKind.MetaExtension;
12-
case "customconstructor":
13-
return DecoratorKind.CustomConstructor;
14-
case "compilemembersonly":
15-
return DecoratorKind.CompileMembersOnly;
16-
case "noresolution":
17-
return DecoratorKind.NoResolution;
18-
case "pureabstract":
19-
return DecoratorKind.PureAbstract;
20-
case "phantom":
21-
return DecoratorKind.Phantom;
22-
case "tuplereturn":
23-
return DecoratorKind.TupleReturn;
24-
case "luaiterator":
25-
return DecoratorKind.LuaIterator;
26-
case "luatable":
27-
return DecoratorKind.LuaTable;
28-
case "noself":
29-
return DecoratorKind.NoSelf;
30-
case "noselfinfile":
31-
return DecoratorKind.NoSelfInFile;
32-
}
33-
34-
return undefined;
7+
return Object.values(DecoratorKind).find(
8+
decoratorKind => decoratorKind.toLowerCase() === decoratorKindString.toLowerCase()
9+
);
3510
}
3611

3712
public kind: DecoratorKind;
38-
public args: string[];
3913

40-
constructor(name: string, args: string[]) {
14+
constructor(name: string, public args: string[]) {
4115
const kind = Decorator.getDecoratorKind(name);
4216
if (kind === undefined) {
4317
throw new Error(`Failed to parse decorator '${name}'`);
4418
}
4519

4620
this.kind = kind;
47-
this.args = args;
4821
}
4922
}
5023

@@ -61,4 +34,6 @@ export enum DecoratorKind {
6134
LuaTable = "LuaTable",
6235
NoSelf = "NoSelf",
6336
NoSelfInFile = "NoSelfInFile",
37+
Vararg = "Vararg",
38+
ForRange = "ForRange",
6439
}

src/Emit.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as fs from "fs";
21
import * as path from "path";
2+
import * as ts from "typescript";
33
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
4-
import { TranspiledFile } from "./Transpile";
4+
import { TranspiledFile, EmitHost } from "./Transpile";
55

66
const trimExt = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
77
const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
@@ -12,7 +12,11 @@ export interface OutputFile {
1212
}
1313

1414
let lualibContent: string;
15-
export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: TranspiledFile[]): OutputFile[] {
15+
export function emitTranspiledFiles(
16+
options: CompilerOptions,
17+
transpiledFiles: TranspiledFile[],
18+
emitHost: EmitHost = ts.sys
19+
): OutputFile[] {
1620
let { rootDir, outDir, outFile, luaLibImport } = options;
1721

1822
const configFileName = options.configFilePath as string | undefined;
@@ -57,7 +61,12 @@ export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: T
5761

5862
if (luaLibImport === LuaLibImportKind.Require || luaLibImport === LuaLibImportKind.Always) {
5963
if (lualibContent === undefined) {
60-
lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8");
64+
const lualibBundle = emitHost.readFile(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"));
65+
if (lualibBundle !== undefined) {
66+
lualibContent = lualibBundle;
67+
} else {
68+
throw new Error("Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua");
69+
}
6170
}
6271

6372
let outPath = path.resolve(rootDir, "lualib_bundle.lua");

0 commit comments

Comments
 (0)