From 47562d5a8edc59649aae8444fa22b4d0f3852763 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Mon, 29 Jul 2024 20:17:11 +0200 Subject: [PATCH 1/2] Fix multiple getters and setters --- src/transformation/visitors/class/index.ts | 20 ++++++++++--- test/unit/classes/accessors.spec.ts | 33 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 23e354460..914c62a41 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -175,7 +175,9 @@ function transformClassLikeDeclaration( for (const member of classDeclaration.members) { if (ts.isAccessor(member)) { // Accessors - const accessors = getAllAccessorDeclarations(classDeclaration); + const symbol = context.checker.getSymbolAtLocation(member.name); + if (!symbol) continue; + const accessors = getAllAccessorDeclarations(classDeclaration, symbol, context); if (accessors.firstAccessor !== member) continue; const accessorsResult = transformAccessorDeclarations(context, accessors, localClassName); @@ -227,9 +229,19 @@ function transformClassLikeDeclaration( return { statements: result, name: className }; } -function getAllAccessorDeclarations(classDeclaration: ts.ClassLikeDeclaration): AllAccessorDeclarations { - const getAccessor = classDeclaration.members.find(ts.isGetAccessor); - const setAccessor = classDeclaration.members.find(ts.isSetAccessor); +function getAllAccessorDeclarations( + classDeclaration: ts.ClassLikeDeclaration, + symbol: ts.Symbol, + context: TransformationContext +): AllAccessorDeclarations { + const getAccessor = classDeclaration.members.find( + (m): m is ts.GetAccessorDeclaration => + ts.isGetAccessor(m) && context.checker.getSymbolAtLocation(m.name) == symbol + ); + const setAccessor = classDeclaration.members.find( + (m): m is ts.SetAccessorDeclaration => + ts.isSetAccessor(m) && context.checker.getSymbolAtLocation(m.name) == symbol + ); // Get the first of the two (that is not undefined) const firstAccessor = diff --git a/test/unit/classes/accessors.spec.ts b/test/unit/classes/accessors.spec.ts index 203fc1c9b..db17fe67d 100644 --- a/test/unit/classes/accessors.spec.ts +++ b/test/unit/classes/accessors.spec.ts @@ -11,6 +11,19 @@ test("get accessor", () => { `.expectToMatchJsResult(); }); +test("multiple get accessors", () => { + util.testFunction` + class Foo { + _foo = "foo"; + get foo() { return this._foo; } + _bar = "bar"; + get bar() { return this._bar; } + } + const f = new Foo(); + return f.foo + f.bar; + `.expectToMatchJsResult(); +}); + test("get accessor in base class", () => { util.testFunction` class Foo { @@ -142,6 +155,26 @@ test("get/set accessors", () => { `.expectToMatchJsResult(); }); +test("multiple get/set accessors", () => { + util.testFunction` + class Foo { + _foo = "foo"; + get foo() { return this._foo; } + set foo(val: string) { this._foo = val; } + + _bar = "bar"; + get bar() { return this._bar; } + set bar(val: string) { this._bar = val; } + } + const f = new Foo(); + const fooOriginal = f.foo; + f.foo = "fizz"; + const barOriginal = f.bar; + f.bar = "buzz"; + return [fooOriginal, f.foo, barOriginal, f.bar]; + `.expectToMatchJsResult(); +}); + test("get/set accessors in base class", () => { util.testFunction` class Foo { From 77f428c3c86964813f8fbda4be65df21afd505c1 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Mon, 29 Jul 2024 20:20:45 +0200 Subject: [PATCH 2/2] Fix lint --- src/transformation/visitors/class/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformation/visitors/class/index.ts b/src/transformation/visitors/class/index.ts index 914c62a41..0edfce050 100644 --- a/src/transformation/visitors/class/index.ts +++ b/src/transformation/visitors/class/index.ts @@ -236,11 +236,11 @@ function getAllAccessorDeclarations( ): AllAccessorDeclarations { const getAccessor = classDeclaration.members.find( (m): m is ts.GetAccessorDeclaration => - ts.isGetAccessor(m) && context.checker.getSymbolAtLocation(m.name) == symbol + ts.isGetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol ); const setAccessor = classDeclaration.members.find( (m): m is ts.SetAccessorDeclaration => - ts.isSetAccessor(m) && context.checker.getSymbolAtLocation(m.name) == symbol + ts.isSetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol ); // Get the first of the two (that is not undefined)