Files
awesome-cordova-plugins/scripts/build/transformers/methods.ts
T
Daniel Sogl 120e0f6d23 refactor: replace build dependencies with native Node.js APIs
Replace fs-extra/rimraf with native node:fs, lodash with
structuredClone/spread, ts-node with tsx, minimist with node:util
parseArgs, winston with console logger, async-promise-queue with
native Promise concurrency. Use promisify for child_process.exec.
Normalize all imports to use node: protocol. Extract Jest config to
jest.config.ts and replace ts-jest with @swc/jest.
2026-03-21 15:16:55 -07:00

62 lines
1.9 KiB
TypeScript

import { Expression, factory, MethodDeclaration, SyntaxKind } from 'typescript';
import { Logger } from '../../logger';
import {
convertValueToLiteral,
getDecorator,
getDecoratorArgs,
getDecoratorName,
getMethodsForDecorator,
} from '../helpers';
export function transformMethod(method: MethodDeclaration) {
if (!method) return;
const decorator = getDecorator(method),
decoratorName = getDecoratorName(decorator),
decoratorArgs = getDecoratorArgs(decorator);
try {
return factory.createMethodDeclaration(
undefined,
undefined,
method.name,
undefined,
method.typeParameters,
method.parameters,
method.type,
factory.createBlock([factory.createReturnStatement(getMethodBlock(method, decoratorName, decoratorArgs))])
);
} catch (e) {
Logger.error('Error transforming method: ' + (method.name as any).text);
Logger.error(e.message);
}
}
function getMethodBlock(method: MethodDeclaration, decoratorName: string, decoratorArgs: any): Expression {
const decoratorMethod = getMethodsForDecorator(decoratorName)[0];
switch (decoratorName) {
case 'CordovaCheck':
case 'InstanceCheck':
return factory.createImmediatelyInvokedArrowFunction([
factory.createIfStatement(
factory.createBinaryExpression(
factory.createCallExpression(factory.createIdentifier(decoratorMethod), undefined, [factory.createThis()]),
SyntaxKind.EqualsEqualsEqualsToken,
factory.createTrue()
),
method.body
),
]);
default:
return factory.createCallExpression(factory.createIdentifier(decoratorMethod), undefined, [
factory.createThis(),
factory.createStringLiteral(decoratorArgs?.methodName || (method.name as any).text),
convertValueToLiteral(decoratorArgs),
factory.createIdentifier('arguments'),
]);
}
}