X Tutup
Skip to content

Commit 773afae

Browse files
committed
Merge branch 'master' into transformer-namespace
2 parents 743912a + 6ac127b commit 773afae

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Large projects written in lua can become hard to maintain and make it easy to ma
88
[![Coverage](https://codecov.io/gh/perryvw/typescripttolua/branch/master/graph/badge.svg)](https://codecov.io/gh/perryvw/typescripttolua)
99
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/TypescriptToLua/Lobby)
1010

11+
You can also find us on Discord: [![Discord](https://img.shields.io/discord/515854149821267971.svg)](https://discord.gg/BWAq58Y)
12+
1113
## Documentation
1214
More detailed documentation and info on writing declarations can be found [on the wiki](https://github.com/Perryvw/TypescriptToLua/wiki).
1315

package-lock.json

Lines changed: 1 addition & 1 deletion
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,7 +1,7 @@
11
{
22
"name": "typescript-to-lua",
33
"license": "MIT",
4-
"version": "0.11.0",
4+
"version": "0.11.1",
55
"repository": "https://github.com/Perryvw/TypescriptToLua",
66
"keywords": [
77
"typescript",

src/Transpiler.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export abstract class LuaTranspiler {
213213
}
214214

215215
// Inline lualib features
216-
if (this.options.luaLibImport === LuaLibImportKind.Inline) {
216+
if (this.options.luaLibImport === LuaLibImportKind.Inline && this.luaLibFeatureSet.size > 0) {
217217
result += "\n" + "-- Lua Library Imports\n";
218218
for (const feature of this.luaLibFeatureSet) {
219219
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
@@ -1789,29 +1789,30 @@ export abstract class LuaTranspiler {
17891789

17901790
public transpileConstructor(node: ts.ConstructorDeclaration,
17911791
className: string): string {
1792-
const extraInstanceFields = [];
1792+
// Check for field declarations in constructor
1793+
const constructorFieldsDeclarations = node.parameters.filter(p => p.modifiers !== undefined);
17931794

1794-
const parameters = ["self"];
1795-
node.parameters.forEach(param => {
1796-
// If param has decorators, add extra instance field
1797-
if (param.modifiers !== undefined) {
1798-
extraInstanceFields.push(this.transpileIdentifier(param.name as ts.Identifier));
1799-
}
1800-
// Add to parameter list
1801-
parameters.push(this.transpileIdentifier(param.name as ts.Identifier));
1802-
});
1803-
1804-
let result = this.indent + `function ${className}.constructor(${parameters.join(",")})\n`;
1795+
const [paramNames, spreadIdentifier] = this.transpileParameters(node.parameters);
18051796

1806-
// Add in instance field declarations
1807-
for (const f of extraInstanceFields) {
1808-
result += this.indent + ` self.${f} = ${f}\n`;
1809-
}
1797+
let result = this.indent + `function ${className}.constructor(${["self"].concat(paramNames).join(",")})\n`;
18101798

18111799
// Transpile constructor body
18121800
this.pushIndent();
18131801
this.classStack.push(className);
1814-
result += this.transpileBlock(node.body);
1802+
1803+
// Add in instance field declarations
1804+
for (const declaration of constructorFieldsDeclarations) {
1805+
const declarationName = this.transpileIdentifier(declaration.name as ts.Identifier);
1806+
if (declaration.initializer) {
1807+
const value = this.transpileExpression(declaration.initializer);
1808+
result += this.indent + `self.${declarationName} = ${declarationName} or ${value}\n`;
1809+
} else {
1810+
result += this.indent + `self.${declarationName} = ${declarationName}\n`;
1811+
}
1812+
}
1813+
1814+
result += this.transpileFunctionBody(node.parameters, node.body, spreadIdentifier);
1815+
18151816
this.classStack.pop();
18161817
this.popIndent();
18171818

test/unit/class.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class ClassTests {
4444

4545
@Test("ClassConstructorAssignment")
4646
public classConstructorAssignment(): void {
47-
// Transpile
47+
// Transpile
4848
const lua = util.transpileString(
4949
`class a { constructor(public field: number) {} }
5050
return new a(4).field;`
@@ -57,6 +57,26 @@ export class ClassTests {
5757
Expect(result).toBe(4);
5858
}
5959

60+
@Test("ClassConstructorDefaultParameter")
61+
public classConstructorDefaultParameter(): void {
62+
const result = util.transpileAndExecute(
63+
`class a { public field: number; constructor(f: number = 3) { this.field = f; } }
64+
return new a().field;`
65+
);
66+
67+
Expect(result).toBe(3);
68+
}
69+
70+
@Test("ClassConstructorAssignmentDefault")
71+
public classConstructorAssignmentParameterDefault(): void {
72+
const result = util.transpileAndExecute(
73+
`class a { constructor(public field: number = 3) { } }
74+
return new a().field;`
75+
);
76+
77+
Expect(result).toBe(3);
78+
}
79+
6080
@Test("ClassNewNoBrackets")
6181
public classNewNoBrackets(): void {
6282
// Transpile

test/unit/modules.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ export class LuaModuleTests {
2323
Expect(lua.startsWith(`require("lualib_bundle")`));
2424
}
2525

26-
@Test("lualibRequireNoUses")
27-
public lualibRequireNoUses(): void {
28-
// Transpile
29-
const lua = util.transpileString(``, { luaLibImport: LuaLibImportKind.Require, luaTarget: LuaTarget.LuaJIT });
30-
31-
// Assert
32-
Expect(lua).toBe(``);
33-
}
34-
3526
@Test("lualibRequireAlways")
3627
public lualibRequireAlways(): void {
3728
// Transpile
@@ -49,4 +40,16 @@ export class LuaModuleTests {
4940

5041
Expect(result).toBe(3);
5142
}
43+
44+
@TestCase(LuaLibImportKind.Inline)
45+
@TestCase(LuaLibImportKind.None)
46+
@TestCase(LuaLibImportKind.Require)
47+
@Test("LuaLib no uses? No code")
48+
public lualibNoUsesNoCode(impKind: LuaLibImportKind): void {
49+
// Transpile
50+
const lua = util.transpileString(``, { luaLibImport: impKind });
51+
52+
// Assert
53+
Expect(lua).toBe(``);
54+
}
5255
}

0 commit comments

Comments
 (0)
X Tutup