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

68 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(),
ts.createLiteral((property.name as any).text)
2018-03-23 17:57:54 +08:00
])
2017-12-28 20:28:44 +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,
[
ts.createParameter(
2017-12-28 20:28:44 +08:00
undefined,
2018-03-23 17:57:54 +08:00
undefined,
undefined,
'value',
undefined,
property.type
)
],
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),
ts.createIdentifier('value')
2018-03-23 17:57:54 +08:00
])
2017-12-28 20:28:44 +08:00
)
2018-03-23 17:57:54 +08:00
])
);
2017-12-28 20:28:44 +08:00
return [getter, setter];
}