awesome-cordova-plugins/scripts/build/transformers/members.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

import { ClassDeclaration, factory, SyntaxKind } from 'typescript';
2017-12-28 20:28:44 +08:00
import { transformMethod } from './methods';
import { transformProperty } from './properties';
export function transformMembers(cls: ClassDeclaration) {
2017-12-28 20:28:44 +08:00
const propertyIndices: number[] = [];
2018-03-23 17:57:54 +08:00
const members = cls.members.map((member: any, index: number) => {
2017-12-28 20:28:44 +08:00
// only process decorated members
if (!member.decorators || !member.decorators.length) return member;
switch (member.kind) {
case SyntaxKind.MethodDeclaration:
2017-12-28 20:28:44 +08:00
return transformMethod(member);
case SyntaxKind.PropertyDeclaration:
2017-12-28 20:28:44 +08:00
propertyIndices.push(index);
return member;
case SyntaxKind.Constructor:
return factory.createConstructorDeclaration(undefined, undefined, member.parameters, member.body);
2017-12-28 20:28:44 +08:00
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);
});
2021-09-28 04:09:05 +08:00
propertyIndices.reverse().forEach((i) => members.splice(i, 1));
2017-12-28 20:28:44 +08:00
return members;
}