Skip to content

Commit 6c6af51

Browse files
committed
Fixed bug with default values for constructor parameters
1 parent 9eb5962 commit 6c6af51

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

src/Transpiler.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,29 +1889,30 @@ export abstract class LuaTranspiler {
18891889

18901890
public transpileConstructor(node: ts.ConstructorDeclaration,
18911891
className: string): string {
1892-
const extraInstanceFields = [];
1892+
// Check for field declarations in constructor
1893+
const constructorFieldsDeclarations = node.parameters.filter(p => p.modifiers !== undefined);
18931894

1894-
const parameters = ["self"];
1895-
node.parameters.forEach(param => {
1896-
// If param has decorators, add extra instance field
1897-
if (param.modifiers !== undefined) {
1898-
extraInstanceFields.push(this.transpileIdentifier(param.name as ts.Identifier));
1899-
}
1900-
// Add to parameter list
1901-
parameters.push(this.transpileIdentifier(param.name as ts.Identifier));
1902-
});
1903-
1904-
let result = this.indent + `function ${className}.constructor(${parameters.join(",")})\n`;
1895+
const [paramNames, spreadIdentifier] = this.transpileParameters(node.parameters);
19051896

1906-
// Add in instance field declarations
1907-
for (const f of extraInstanceFields) {
1908-
result += this.indent + ` self.${f} = ${f}\n`;
1909-
}
1897+
let result = this.indent + `function ${className}.constructor(${["self"].concat(paramNames).join(",")})\n`;
19101898

19111899
// Transpile constructor body
19121900
this.pushIndent();
19131901
this.classStack.push(className);
1914-
result += this.transpileBlock(node.body);
1902+
1903+
// Add in instance field declarations
1904+
for (const declaration of constructorFieldsDeclarations) {
1905+
const declarationName = this.transpileIdentifier(declaration.name as ts.Identifier);
1906+
if (declaration.initializer) {
1907+
const value = this.transpileExpression(declaration.initializer);
1908+
result += this.indent + `self.${declarationName} = ${declarationName} or ${value}\n`;
1909+
} else {
1910+
result += this.indent + `self.${declarationName} = ${declarationName}\n`;
1911+
}
1912+
}
1913+
1914+
result += this.transpileFunctionBody(node.parameters, node.body, spreadIdentifier);
1915+
19151916
this.classStack.pop();
19161917
this.popIndent();
19171918

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

0 commit comments

Comments
 (0)