Skip to content

Commit

Permalink
use method directly
Browse files Browse the repository at this point in the history
  • Loading branch information
scripthunter7 committed Sep 19, 2024
1 parent 98f5bb4 commit 4d40d40
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/helpers/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
}[keyof T];
type FilteredConversionMethods<ExcludedMethods extends keyof typeof RuleConverter = never> = {
[K in keyof typeof RuleConverter]: typeof RuleConverter[K] extends (...args: any[]) => any ? K : never
}[Exclude<keyof typeof RuleConverter, ExcludedMethods>];

/**
* Extracts method names from {@link RuleConverter} class
* Conversion method type
*/
type ConversionMethodNames = Exclude<MethodNames<typeof RuleConverter>, 'convertToAbp'>;
type ConversionMethod = (typeof RuleConverter)[FilteredConversionMethods<'convertToAbp'>];

/**
* Checks if an array of rules is an array of scriptlet rules
Expand All @@ -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);

Expand All @@ -60,7 +62,7 @@ const convertScriptletTo = (
return [getRuleText(ruleNode)];
}

const conversionResult = RuleConverter[method](ruleNode);
const conversionResult = method(ruleNode);

if (!conversionResult.isConverted) {
return [getRuleText(ruleNode)];
Expand All @@ -78,7 +80,7 @@ const convertScriptletTo = (
* @deprecated
*/
export const convertUboScriptletToAdg = (rule: string | ScriptletInjectionRule): string[] => {
return convertScriptletTo(rule, 'convertToAdg');
return convertScriptletTo(rule, RuleConverter.convertToAdg);
};

/**
Expand All @@ -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);
};

/**
Expand All @@ -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 [];
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 4d40d40

Please sign in to comment.