Skip to content

Commit 9d61a01

Browse files
feature(plugin) collect type data from getters in class
1 parent 3fe7cdb commit 9d61a01

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/plugin/visitors/model-class.visitor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class ModelClassVisitor {
316316
}
317317

318318
private isMemberHasInlineStringEnum(
319-
member: ts.PropertyDeclaration,
319+
member: ts.PropertyDeclaration | ts.GetAccessorDeclaration,
320320
): false | { [name: string]: string } {
321321
if (!member.type || !ts.isUnionTypeNode(member.type)) {
322322
return false;
@@ -376,7 +376,7 @@ export class ModelClassVisitor {
376376
}
377377

378378
private getInlineStringEnumTypeOrUndefined(
379-
member: ts.PropertyDeclaration,
379+
member: ts.PropertyDeclaration | ts.GetAccessorDeclaration,
380380
): string {
381381
let inlineEnumName: string;
382382

@@ -386,7 +386,7 @@ export class ModelClassVisitor {
386386
const memberName = member.name.getText();
387387

388388
inlineEnumName =
389-
member.parent.name.getText() +
389+
(member.parent as ts.ClassLikeDeclaration).name.getText() +
390390
capitalizeFirstLetter(memberName) +
391391
'Enum';
392392

@@ -410,7 +410,7 @@ export class ModelClassVisitor {
410410

411411
members.forEach((member) => {
412412
if (
413-
ts.isPropertyDeclaration(member) &&
413+
(ts.isPropertyDeclaration(member) || ts.isGetAccessor(member)) &&
414414
!hasModifiers(member.modifiers, [
415415
ts.SyntaxKind.StaticKeyword,
416416
ts.SyntaxKind.PrivateKeyword,
@@ -474,7 +474,9 @@ export class ModelClassVisitor {
474474
);
475475
}
476476

477-
private hasExplicitTypeInDecorator(member: ts.PropertyDeclaration) {
477+
private hasExplicitTypeInDecorator(
478+
member: ts.PropertyDeclaration | ts.GetAccessorDeclaration,
479+
) {
478480
const fieldDecorator = member.decorators?.find(
479481
(decorator) => getDecoratorName(decorator) === Field.name,
480482
);
@@ -492,7 +494,7 @@ export class ModelClassVisitor {
492494

493495
private createDecoratorObjectLiteralExpr(
494496
f: ts.NodeFactory,
495-
node: ts.PropertyDeclaration,
497+
node: ts.PropertyDeclaration | ts.GetAccessorDeclaration,
496498
typeChecker: ts.TypeChecker,
497499
hostFilename = '',
498500
pluginOptions?: PluginOptions,

tests/plugin/model-class-visitor.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ class NotAModel {
239239
`);
240240
});
241241

242+
it('should process fields declared as getters ', () => {
243+
const source = `
244+
@ObjectType()
245+
class ObjectTypeModel {
246+
get prop(): string {}
247+
}
248+
`;
249+
250+
const actual = transpile(source, {});
251+
expect(actual).toMatchInlineSnapshot(`
252+
"\\"use strict\\";
253+
let ObjectTypeModel = class ObjectTypeModel {
254+
get prop() { }
255+
static _GRAPHQL_METADATA_FACTORY() {
256+
return { prop: { type: () => String } };
257+
}
258+
};
259+
ObjectTypeModel = __decorate([
260+
ObjectType()
261+
], ObjectTypeModel);
262+
"
263+
`);
264+
});
265+
242266
describe('Should add description from JSDoc to decorators argument', () => {
243267
it('when there are no arguments on decorator', () => {
244268
const source = `

0 commit comments

Comments
 (0)