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

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-12-28 20:28:44 +08:00
import * as ts 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 ts.PropertyDeclaration,
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
}
2018-03-23 17:57:54 +08:00
const getter = ts.createGetAccessor(
undefined,
undefined,
property.name,
undefined,
property.type,
ts.createBlock([
ts.createReturn(
ts.createCall(ts.createIdentifier(type + 'PropertyGet'), undefined, [
2017-12-28 20:28:44 +08:00
ts.createThis(),
2020-05-16 20:40:49 +08:00
ts.createLiteral((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
2018-03-23 17:57:54 +08:00
const setter = ts.createSetAccessor(
undefined,
undefined,
property.name,
2020-05-16 20:40:49 +08:00
[ts.createParameter(undefined, undefined, undefined, 'value', undefined, property.type)],
2018-03-23 17:57:54 +08:00
ts.createBlock([
ts.createStatement(
ts.createCall(ts.createIdentifier(type + 'PropertySet'), undefined, [
2017-12-28 20:28:44 +08:00
ts.createThis(),
ts.createLiteral((property.name as any).text),
2020-05-16 20:40:49 +08:00
ts.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];
}