Files
awesome-cordova-plugins/scripts/build/transformers/methods.ts
T
Daniel Sogl 62956e429c fix: add strict types to build scripts and remove all any usage
Replace all implicit and explicit any types in build scripts with
proper TypeScript Compiler API types (Decorator, ClassDeclaration,
MethodDeclaration, Identifier, SourceFile, etc.). Add PackageJson
and InjectableClassEntry interfaces. Fix return types, null checks,
and type assertions throughout all transformer scripts.
2026-03-21 16:11:27 -07:00

68 lines
2.0 KiB
TypeScript

import { Expression, factory, Identifier, 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);
if (!decorator) return;
const decoratorName = getDecoratorName(decorator);
const 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: unknown) {
Logger.error('Error transforming method: ' + (method.name as Identifier).text);
Logger.error(e instanceof Error ? e.message : String(e));
}
}
function getMethodBlock(
method: MethodDeclaration,
decoratorName: string,
decoratorArgs: Record<string, string | number | boolean | string[]>
): 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 as string) || (method.name as Identifier).text),
convertValueToLiteral(decoratorArgs),
factory.createIdentifier('arguments'),
]);
}
}