Skip to content

Commit 1691f23

Browse files
TheLartiansPerryvw
authored andcommitted
Fix derived get accessor (#419)
* fix derived get accessor * update tests
1 parent 16d1568 commit 1691f23

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/LuaTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ export class LuaTransformer {
540540
createClassPrototype(),
541541
tstl.createStringLiteral("__index")
542542
);
543-
if (statement.members.some(ts.isGetAccessor)) {
543+
if (tsHelper.hasGetAccessorInClassOrAncestor(statement, this.checker)) {
544544
// className.prototype.____getters = {}
545545
const classPrototypeGetters = tstl.createTableIndexExpression(
546546
createClassPrototype(),

src/TSHelper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,18 @@ export class TSHelper {
385385
) !== undefined;
386386
}
387387

388+
public static hasGetAccessorInClassOrAncestor(
389+
classDeclaration: ts.ClassLikeDeclarationBase,
390+
checker: ts.TypeChecker
391+
): boolean
392+
{
393+
return TSHelper.findInClassOrAncestor(
394+
classDeclaration,
395+
c => c.members.some(ts.isGetAccessor),
396+
checker
397+
) !== undefined;
398+
}
399+
388400
public static getPropertyName(propertyName: ts.PropertyName): string | number | undefined {
389401
if (ts.isIdentifier(propertyName) || ts.isStringLiteral(propertyName) || ts.isNumericLiteral(propertyName)) {
390402
return propertyName.text;

test/unit/accessors.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export class AccessorTests {
66
public getAccessor(): void {
77
const code =
88
`class Foo {
9-
get foo() { return "foo"; }
9+
_foo;
10+
get foo() { return this._foo; }
11+
constructor(){ this._foo = "foo"; }
1012
}
1113
const f = new Foo();
1214
return f.foo;`;
@@ -17,7 +19,9 @@ export class AccessorTests {
1719
public getAccessorInBaseClass(): void {
1820
const code =
1921
`class Foo {
20-
get foo() { return "foo"; }
22+
_foo;
23+
get foo() { return this._foo; }
24+
constructor(){ this._foo = "foo"; }
2125
}
2226
class Bar extends Foo {}
2327
const b = new Bar();

0 commit comments

Comments
 (0)