Skip to content

Commit 8ddfbc0

Browse files
authored
Fix multiple getters and setters (#1567)
* Fix multiple getters and setters * Fix lint
1 parent b464bc4 commit 8ddfbc0

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ function transformClassLikeDeclaration(
175175
for (const member of classDeclaration.members) {
176176
if (ts.isAccessor(member)) {
177177
// Accessors
178-
const accessors = getAllAccessorDeclarations(classDeclaration);
178+
const symbol = context.checker.getSymbolAtLocation(member.name);
179+
if (!symbol) continue;
180+
const accessors = getAllAccessorDeclarations(classDeclaration, symbol, context);
179181
if (accessors.firstAccessor !== member) continue;
180182

181183
const accessorsResult = transformAccessorDeclarations(context, accessors, localClassName);
@@ -227,9 +229,19 @@ function transformClassLikeDeclaration(
227229
return { statements: result, name: className };
228230
}
229231

230-
function getAllAccessorDeclarations(classDeclaration: ts.ClassLikeDeclaration): AllAccessorDeclarations {
231-
const getAccessor = classDeclaration.members.find(ts.isGetAccessor);
232-
const setAccessor = classDeclaration.members.find(ts.isSetAccessor);
232+
function getAllAccessorDeclarations(
233+
classDeclaration: ts.ClassLikeDeclaration,
234+
symbol: ts.Symbol,
235+
context: TransformationContext
236+
): AllAccessorDeclarations {
237+
const getAccessor = classDeclaration.members.find(
238+
(m): m is ts.GetAccessorDeclaration =>
239+
ts.isGetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol
240+
);
241+
const setAccessor = classDeclaration.members.find(
242+
(m): m is ts.SetAccessorDeclaration =>
243+
ts.isSetAccessor(m) && context.checker.getSymbolAtLocation(m.name) === symbol
244+
);
233245

234246
// Get the first of the two (that is not undefined)
235247
const firstAccessor =

test/unit/classes/accessors.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ test("get accessor", () => {
1111
`.expectToMatchJsResult();
1212
});
1313

14+
test("multiple get accessors", () => {
15+
util.testFunction`
16+
class Foo {
17+
_foo = "foo";
18+
get foo() { return this._foo; }
19+
_bar = "bar";
20+
get bar() { return this._bar; }
21+
}
22+
const f = new Foo();
23+
return f.foo + f.bar;
24+
`.expectToMatchJsResult();
25+
});
26+
1427
test("get accessor in base class", () => {
1528
util.testFunction`
1629
class Foo {
@@ -142,6 +155,26 @@ test("get/set accessors", () => {
142155
`.expectToMatchJsResult();
143156
});
144157

158+
test("multiple get/set accessors", () => {
159+
util.testFunction`
160+
class Foo {
161+
_foo = "foo";
162+
get foo() { return this._foo; }
163+
set foo(val: string) { this._foo = val; }
164+
165+
_bar = "bar";
166+
get bar() { return this._bar; }
167+
set bar(val: string) { this._bar = val; }
168+
}
169+
const f = new Foo();
170+
const fooOriginal = f.foo;
171+
f.foo = "fizz";
172+
const barOriginal = f.bar;
173+
f.bar = "buzz";
174+
return [fooOriginal, f.foo, barOriginal, f.bar];
175+
`.expectToMatchJsResult();
176+
});
177+
145178
test("get/set accessors in base class", () => {
146179
util.testFunction`
147180
class Foo {

0 commit comments

Comments
 (0)