Skip to content

Commit f1839a0

Browse files
Perryvwlolleko
authored andcommitted
Fixed bug with default values for constructor parameters
1 parent 1c344a2 commit f1839a0

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
@@ -1748,29 +1748,30 @@ export abstract class LuaTranspiler {
17481748

17491749
public transpileConstructor(node: ts.ConstructorDeclaration,
17501750
className: string): string {
1751-
const extraInstanceFields = [];
1751+
// Check for field declarations in constructor
1752+
const constructorFieldsDeclarations = node.parameters.filter(p => p.modifiers !== undefined);
17521753

1753-
const parameters = ["self"];
1754-
node.parameters.forEach(param => {
1755-
// If param has decorators, add extra instance field
1756-
if (param.modifiers !== undefined) {
1757-
extraInstanceFields.push(this.transpileIdentifier(param.name as ts.Identifier));
1758-
}
1759-
// Add to parameter list
1760-
parameters.push(this.transpileIdentifier(param.name as ts.Identifier));
1761-
});
1762-
1763-
let result = this.indent + `function ${className}.constructor(${parameters.join(",")})\n`;
1754+
const [paramNames, spreadIdentifier] = this.transpileParameters(node.parameters);
17641755

1765-
// Add in instance field declarations
1766-
for (const f of extraInstanceFields) {
1767-
result += this.indent + ` self.${f} = ${f}\n`;
1768-
}
1756+
let result = this.indent + `function ${className}.constructor(${["self"].concat(paramNames).join(",")})\n`;
17691757

17701758
// Transpile constructor body
17711759
this.pushIndent();
17721760
this.classStack.push(className);
1773-
result += this.transpileBlock(node.body);
1761+
1762+
// Add in instance field declarations
1763+
for (const declaration of constructorFieldsDeclarations) {
1764+
const declarationName = this.transpileIdentifier(declaration.name as ts.Identifier);
1765+
if (declaration.initializer) {
1766+
const value = this.transpileExpression(declaration.initializer);
1767+
result += this.indent + `self.${declarationName} = ${declarationName} or ${value}\n`;
1768+
} else {
1769+
result += this.indent + `self.${declarationName} = ${declarationName}\n`;
1770+
}
1771+
}
1772+
1773+
result += this.transpileFunctionBody(node.parameters, node.body, spreadIdentifier);
1774+
17741775
this.classStack.pop();
17751776
this.popIndent();
17761777

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)