2017-12-28 20:28:44 +08:00
|
|
|
import * as ts from 'typescript';
|
2017-12-29 12:15:34 +08:00
|
|
|
import { Logger } from '../../logger';
|
2017-12-28 20:28:44 +08:00
|
|
|
import { convertValueToLiteral, getDecorator, getDecoratorArgs, getDecoratorName, getMethodsForDecorator } from '../helpers';
|
|
|
|
|
|
|
|
export function transformMethod(method: ts.MethodDeclaration) {
|
|
|
|
if (!method) return;
|
|
|
|
|
|
|
|
const decorator = getDecorator(method),
|
|
|
|
decoratorName = getDecoratorName(decorator),
|
|
|
|
decoratorArgs = getDecoratorArgs(decorator);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return ts.createMethod(undefined, undefined, undefined, method.name, undefined, method.typeParameters, method.parameters, method.type, ts.createBlock([
|
|
|
|
ts.createReturn(
|
|
|
|
getMethodBlock(method, decoratorName, decoratorArgs)
|
|
|
|
)
|
|
|
|
]));
|
|
|
|
} catch (e) {
|
2017-12-29 12:15:34 +08:00
|
|
|
Logger.error('Error transforming method: ' + (method.name as any).text);
|
|
|
|
Logger.error(e.message);
|
2017-12-28 20:28:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMethodBlock(method: ts.MethodDeclaration, decoratorName: string, decoratorArgs: any): ts.Expression {
|
|
|
|
const decoratorMethod = getMethodsForDecorator(decoratorName)[0];
|
|
|
|
|
|
|
|
switch (decoratorName) {
|
|
|
|
case 'CordovaCheck':
|
|
|
|
case 'InstanceCheck':
|
2017-12-28 21:54:28 +08:00
|
|
|
// TODO remove function wrapper
|
|
|
|
return ts.createImmediatelyInvokedArrowFunction([ts.createIf(
|
2017-12-28 20:28:44 +08:00
|
|
|
ts.createBinary(
|
|
|
|
ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [ts.createThis()]),
|
|
|
|
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
|
|
ts.createTrue()
|
|
|
|
),
|
|
|
|
method.body
|
|
|
|
)]);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ts.createCall(ts.createIdentifier(decoratorMethod), undefined, [
|
|
|
|
ts.createThis(),
|
|
|
|
ts.createLiteral((method.name as any).text),
|
|
|
|
convertValueToLiteral(decoratorArgs),
|
|
|
|
ts.createIdentifier('arguments')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|