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

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import { factory, PropertyDeclaration } from 'typescript';
2018-03-23 17:57:54 +08:00
2017-12-28 20:28:44 +08:00
import { getDecorator, getDecoratorName } from '../helpers';
export function transformProperty(members: any[], index: number) {
const property = members[index] as PropertyDeclaration,
2017-12-28 20:28:44 +08:00
decorator = getDecorator(property),
decoratorName = getDecoratorName(decorator);
let type: 'cordova' | 'instance';
switch (decoratorName) {
case 'CordovaProperty':
type = 'cordova';
break;
case 'InstanceProperty':
type = 'instance';
break;
2018-03-23 17:57:54 +08:00
default:
return property;
2017-12-28 20:28:44 +08:00
}
const getter = factory.createGetAccessorDeclaration(
2018-03-23 17:57:54 +08:00
undefined,
undefined,
property.name,
undefined,
property.type,
factory.createBlock([
factory.createReturnStatement(
factory.createCallExpression(factory.createIdentifier(type + 'PropertyGet'), undefined, [
factory.createThis(),
factory.createStringLiteral((property.name as any).text),
2018-03-23 17:57:54 +08:00
])
2020-05-16 20:40:49 +08:00
),
2018-03-23 17:57:54 +08:00
])
);
2017-12-28 20:28:44 +08:00
const setter = factory.createSetAccessorDeclaration(
2018-03-23 17:57:54 +08:00
undefined,
undefined,
property.name,
[factory.createParameterDeclaration(undefined, undefined, undefined, 'value', undefined, property.type)],
factory.createBlock([
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier(type + 'PropertySet'), undefined, [
factory.createThis(),
factory.createStringLiteral((property.name as any).text),
factory.createIdentifier('value'),
2018-03-23 17:57:54 +08:00
])
2020-05-16 20:40:49 +08:00
),
2018-03-23 17:57:54 +08:00
])
);
2017-12-28 20:28:44 +08:00
return [getter, setter];
}