awesome-cordova-plugins/scripts/build/transformers/members.ts
2021-09-27 21:04:28 +02:00

35 lines
1.1 KiB
TypeScript

import { ClassDeclaration, createConstructor, SyntaxKind } from 'typescript';
import { transformMethod } from './methods';
import { transformProperty } from './properties';
export function transformMembers(cls: ClassDeclaration) {
const propertyIndices: number[] = [];
const members = cls.members.map((member: any, index: number) => {
// only process decorated members
if (!member.decorators || !member.decorators.length) return member;
switch (member.kind) {
case SyntaxKind.MethodDeclaration:
return transformMethod(member);
case SyntaxKind.PropertyDeclaration:
propertyIndices.push(index);
return member;
case SyntaxKind.Constructor:
return createConstructor(undefined, undefined, member.parameters, member.body);
default:
return member; // in case anything gets here by accident...
}
});
propertyIndices.forEach((i: number) => {
const [getter, setter] = transformProperty(members, i) as any;
members.push(getter, setter);
});
propertyIndices.reverse().forEach(i => members.splice(i, 1));
return members;
}