2021-09-28 04:04:25 +08:00
|
|
|
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) {
|
2021-09-28 03:04:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-28 04:04:25 +08:00
|
|
|
const getter = factory.createGetAccessorDeclaration(
|
2018-03-23 17:57:54 +08:00
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
property.name,
|
|
|
|
undefined,
|
|
|
|
property.type,
|
2021-09-28 04:04:25 +08:00
|
|
|
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
|
|
|
|
2021-09-28 04:04:25 +08:00
|
|
|
const setter = factory.createSetAccessorDeclaration(
|
2018-03-23 17:57:54 +08:00
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
property.name,
|
2021-09-28 04:04:25 +08:00
|
|
|
[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];
|
|
|
|
}
|