Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down
33 changes: 33 additions & 0 deletions test/unit/classes/accessors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down