mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-04-13 00:00:10 +08:00
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.
This commit is contained in:
+33
-38
@@ -1,5 +1,4 @@
|
||||
import { readdirSync } from 'node:fs';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { readdirSync, readFileSync } from 'node:fs';
|
||||
import { join, resolve } from 'node:path';
|
||||
import {
|
||||
ArrayLiteralExpression,
|
||||
@@ -20,35 +19,43 @@ export const ROOT = resolve(__dirname, '../../');
|
||||
export const TS_CONFIG = JSON.parse(readFileSync(resolve(ROOT, 'tsconfig.json'), 'utf-8'));
|
||||
export const COMPILER_OPTIONS = TS_CONFIG.compilerOptions;
|
||||
export const PLUGINS_ROOT = join(ROOT, 'src/@awesome-cordova-plugins/plugins/');
|
||||
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map((d) => join(PLUGINS_ROOT, d, 'index.ts'));
|
||||
export const PLUGIN_PATHS = readdirSync(PLUGINS_ROOT).map((d: string) => join(PLUGINS_ROOT, d, 'index.ts'));
|
||||
|
||||
export function getDecorator(node: Node, index = 0): Decorator {
|
||||
export function getDecorator(node: Node, index = 0): Decorator | undefined {
|
||||
const decorators = canHaveDecorators(node) ? tsGetDecorators(node) : undefined;
|
||||
if (decorators && decorators[index]) {
|
||||
return decorators[index];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function hasDecorator(decoratorName: string, node: Node): boolean {
|
||||
const decorators = canHaveDecorators(node) ? tsGetDecorators(node) : undefined;
|
||||
return decorators && decorators.length > 0 && decorators.findIndex((d) => getDecoratorName(d) === decoratorName) > -1;
|
||||
return (
|
||||
!!decorators && decorators.length > 0 && decorators.findIndex((d) => getDecoratorName(d) === decoratorName) > -1
|
||||
);
|
||||
}
|
||||
|
||||
export function getDecoratorName(decorator: any) {
|
||||
return decorator.expression.expression.text;
|
||||
export function getDecoratorName(decorator: Decorator): string {
|
||||
return (decorator.expression as unknown as { expression: { text: string } }).expression.text;
|
||||
}
|
||||
|
||||
export function getRawDecoratorArgs(decorator: any): any[] {
|
||||
if (decorator.expression.arguments.length === 0) return [];
|
||||
return decorator.expression.arguments[0].properties;
|
||||
export function getRawDecoratorArgs(
|
||||
decorator: Decorator
|
||||
): Array<{ name: { text: string }; initializer: { kind: number; text: string; elements: Array<{ text: string }> } }> {
|
||||
const expr = decorator.expression as unknown as {
|
||||
arguments: Array<{ properties: ReturnType<typeof getRawDecoratorArgs> }>;
|
||||
};
|
||||
if (expr.arguments.length === 0) return [];
|
||||
return expr.arguments[0].properties;
|
||||
}
|
||||
|
||||
export function getDecoratorArgs(decorator: any) {
|
||||
const properties: any[] = getRawDecoratorArgs(decorator);
|
||||
const args = {};
|
||||
export function getDecoratorArgs(decorator: Decorator): Record<string, string | number | boolean | string[]> {
|
||||
const properties = getRawDecoratorArgs(decorator);
|
||||
const args: Record<string, string | number | boolean | string[]> = {};
|
||||
|
||||
properties.forEach((prop) => {
|
||||
let val: number | boolean;
|
||||
let val: string | number | boolean | string[];
|
||||
|
||||
switch (prop.initializer.kind) {
|
||||
case SyntaxKind.StringLiteral:
|
||||
@@ -57,7 +64,7 @@ export function getDecoratorArgs(decorator: any) {
|
||||
break;
|
||||
|
||||
case SyntaxKind.ArrayLiteralExpression:
|
||||
val = prop.initializer.elements.map((e: any) => e.text);
|
||||
val = prop.initializer.elements.map((e) => e.text);
|
||||
break;
|
||||
|
||||
case SyntaxKind.TrueKeyword:
|
||||
@@ -84,17 +91,16 @@ export function getDecoratorArgs(decorator: any) {
|
||||
}
|
||||
|
||||
/**
|
||||
* FROM STENCIL
|
||||
* Convert a js value into typescript AST
|
||||
* @param val array, object, string, boolean, or number
|
||||
* @returns Typescript Object Literal, Array Literal, String Literal, Boolean Literal, Numeric Literal
|
||||
*/
|
||||
export function convertValueToLiteral(val: any) {
|
||||
export function convertValueToLiteral(
|
||||
val: string | number | boolean | string[] | Record<string, unknown> | unknown[]
|
||||
): Expression {
|
||||
if (Array.isArray(val)) {
|
||||
return arrayToArrayLiteral(val);
|
||||
}
|
||||
if (typeof val === 'object') {
|
||||
return objectToObjectLiteral(val);
|
||||
if (typeof val === 'object' && val !== null) {
|
||||
return objectToObjectLiteral(val as Record<string, unknown>);
|
||||
}
|
||||
if (typeof val === 'number') {
|
||||
return factory.createNumericLiteral(val);
|
||||
@@ -105,37 +111,26 @@ export function convertValueToLiteral(val: any) {
|
||||
if (typeof val === 'boolean') {
|
||||
return val ? factory.createTrue() : factory.createFalse();
|
||||
}
|
||||
throw new Error('Unexpected value type: ' + typeof val);
|
||||
}
|
||||
|
||||
/**
|
||||
* FROM STENCIL
|
||||
* Convert a js object into typescript AST
|
||||
* @param obj key value object
|
||||
* @returns Typescript Object Literal Expression
|
||||
*/
|
||||
function objectToObjectLiteral(obj: { [key: string]: any }): ObjectLiteralExpression {
|
||||
function objectToObjectLiteral(obj: Record<string, unknown>): ObjectLiteralExpression {
|
||||
const newProperties: ObjectLiteralElementLike[] = Object.keys(obj).map((key: string): ObjectLiteralElementLike => {
|
||||
return factory.createPropertyAssignment(
|
||||
factory.createStringLiteral(key),
|
||||
convertValueToLiteral(obj[key]) as Expression
|
||||
convertValueToLiteral(obj[key] as string | number | boolean | string[])
|
||||
);
|
||||
});
|
||||
|
||||
return factory.createObjectLiteralExpression(newProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* FROM STENCIL
|
||||
* Convert a js array into typescript AST
|
||||
* @param list arrayÏ
|
||||
* @returns Typescript Array Literal Expression
|
||||
*/
|
||||
function arrayToArrayLiteral(list: any[]): ArrayLiteralExpression {
|
||||
const newList: any[] = list.map(convertValueToLiteral);
|
||||
function arrayToArrayLiteral(list: unknown[]): ArrayLiteralExpression {
|
||||
const newList = list.map((item) => convertValueToLiteral(item as string | number | boolean));
|
||||
return factory.createArrayLiteralExpression(newList);
|
||||
}
|
||||
|
||||
export function getMethodsForDecorator(decoratorName: string) {
|
||||
export function getMethodsForDecorator(decoratorName: string): string[] {
|
||||
switch (decoratorName) {
|
||||
case 'CordovaProperty':
|
||||
return ['cordovaPropertyGet', 'cordovaPropertySet'];
|
||||
|
||||
Reference in New Issue
Block a user