From 4d40d409f29705bf7ba3fcd30bce79047160feb5 Mon Sep 17 00:00:00 2001 From: scripthunter7 Date: Thu, 19 Sep 2024 12:17:08 +0200 Subject: [PATCH] use method directly --- src/helpers/converter.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/helpers/converter.ts b/src/helpers/converter.ts index dce247a9e..776490c4d 100644 --- a/src/helpers/converter.ts +++ b/src/helpers/converter.ts @@ -14,16 +14,18 @@ import validator from './validator'; import { getRuleNode, getRuleText } from './rule-helpers'; /** - * Extracts method names from a class + * Helper type to get methods of RuleConverter that are not excluded + * + * @template ExcludedMethods Methods to exclude */ -type MethodNames = { - [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never; -}[keyof T]; +type FilteredConversionMethods = { + [K in keyof typeof RuleConverter]: typeof RuleConverter[K] extends (...args: any[]) => any ? K : never +}[Exclude]; /** - * Extracts method names from {@link RuleConverter} class + * Conversion method type */ -type ConversionMethodNames = Exclude, 'convertToAbp'>; +type ConversionMethod = (typeof RuleConverter)[FilteredConversionMethods<'convertToAbp'>]; /** * Checks if an array of rules is an array of scriptlet rules @@ -49,7 +51,7 @@ const isArrayOfScriptletRules = (rules: AnyRule[]): rules is ScriptletInjectionR */ const convertScriptletTo = ( rule: string | ScriptletInjectionRule, - method: ConversionMethodNames, + method: ConversionMethod, ): string[] => { const ruleNode = getRuleNode(rule); @@ -60,7 +62,7 @@ const convertScriptletTo = ( return [getRuleText(ruleNode)]; } - const conversionResult = RuleConverter[method](ruleNode); + const conversionResult = method(ruleNode); if (!conversionResult.isConverted) { return [getRuleText(ruleNode)]; @@ -78,7 +80,7 @@ const convertScriptletTo = ( * @deprecated */ export const convertUboScriptletToAdg = (rule: string | ScriptletInjectionRule): string[] => { - return convertScriptletTo(rule, 'convertToAdg'); + return convertScriptletTo(rule, RuleConverter.convertToAdg); }; /** @@ -88,7 +90,7 @@ export const convertUboScriptletToAdg = (rule: string | ScriptletInjectionRule): * @returns array of AdGuard scriptlet rules, one or few items depends on Abp-rule */ export const convertAbpSnippetToAdg = (rule: string | ScriptletInjectionRule): string[] => { - return convertScriptletTo(rule, 'convertToAdg'); + return convertScriptletTo(rule, RuleConverter.convertToAdg); }; /** @@ -102,7 +104,7 @@ export const convertAbpSnippetToAdg = (rule: string | ScriptletInjectionRule): s */ export const convertScriptletToAdg = (rule: string | ScriptletInjectionRule): string[] => { try { - return convertScriptletTo(rule, 'convertToAdg'); + return convertScriptletTo(rule, RuleConverter.convertToAdg); } catch (e) { return []; } @@ -117,7 +119,7 @@ export const convertScriptletToAdg = (rule: string | ScriptletInjectionRule): st */ export const convertAdgScriptletToUbo = (rule: string | ScriptletInjectionRule): string | undefined => { try { - return convertScriptletTo(rule, 'convertToUbo')[0]; + return convertScriptletTo(rule, RuleConverter.convertToUbo)[0]; } catch (e) { return undefined; }