From 23dd9f6a4f55e044def37a2d91328b2e6b5dc82e Mon Sep 17 00:00:00 2001 From: stanislav-atr Date: Sun, 14 May 2023 20:46:57 +0300 Subject: [PATCH] improve convertScriptletToAdg, fix createOnErrorHandler calls linting --- src/helpers/converter.ts | 68 +++++++++++-------- src/scriptlets/abort-current-inline-script.js | 3 +- src/scriptlets/abort-on-property-read.js | 3 +- src/scriptlets/abort-on-stack-trace.js | 3 +- src/scriptlets/debug-current-inline-script.js | 3 +- src/scriptlets/debug-on-property-read.js | 3 +- 6 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/helpers/converter.ts b/src/helpers/converter.ts index aab7a76d..0f3fb24b 100644 --- a/src/helpers/converter.ts +++ b/src/helpers/converter.ts @@ -263,6 +263,7 @@ const isValidAdgScriptletRuleSyntax = (adgRuleText: string): boolean => { } }; +// Possible rule origins enum Origin { Ubo = 'ubo', Abp = 'abp', @@ -270,29 +271,48 @@ enum Origin { AdgInvalid = 'adgInvalid', } -const getRuleOrigin = (rule: string): Origin | undefined => { - const origins = { - [Origin.Ubo]: validator.isUboScriptletRule, - [Origin.Abp]: validator.isAbpSnippetRule, - [Origin.AdgValid]: isValidAdgScriptletRuleSyntax, - [Origin.AdgInvalid]: (r: string) => { - return validator.isAdgScriptletRule(r) && !isValidAdgScriptletRuleSyntax(r); - }, - }; - - const originNames = Object.keys(origins) as Origin[]; +// Array of origin names in the order they must be checked for rule conversion +const originNames = [Origin.Ubo, Origin.Abp, Origin.AdgValid, Origin.AdgInvalid]; + +// Functions to validate if a given string corresponds to a scriptlet rule of a particular origin. +const OriginValidator = { + [Origin.Ubo]: validator.isUboScriptletRule, + [Origin.Abp]: validator.isAbpSnippetRule, + [Origin.AdgValid]: isValidAdgScriptletRuleSyntax, + [Origin.AdgInvalid]: (r: string) => { + return validator.isAdgScriptletRule(r) && !isValidAdgScriptletRuleSyntax(r); + }, +} as const; + +// Functions to convert a given scriptlet rule from a mapped origin to an AdGuard rule +const Converter = { + ubo: convertUboScriptletToAdg, + abp: convertAbpSnippetToAdg, + adgValid: (r: string) => [r], + adgInvalid: (r: string) => { + // eslint-disable-next-line no-console + console.log(`Invalid AdGuard scriptlet rule: ${r}`); + return []; + }, +} as const; - // Return origin which was first evaluated as true - return originNames.find((originName) => origins[originName](rule)); -}; +/** + * Get rule origin name in a meaningful order + * + * @param rule - The rule string to check. + * @returns rule origin name or undefined if the rule has no valid origin + */ +const getRuleOrigin = ( + rule: string, +): Origin | undefined => originNames.find((originName) => OriginValidator[originName](rule)); /** * Converts any scriptlet rule into AdGuard syntax rule. * Comment is returned as is. * - * @param {string} rule Scriptlet rule. + * @param rule Scriptlet rule. * - * @returns {string[]} Array of AdGuard scriptlet rules: one array item for ADG and UBO or few items for ABP. + * @returns Array of AdGuard scriptlet rules: one array item for ADG and UBO or few items for ABP. * For the ADG `rule`, validates its syntax and returns an empty array if it is invalid. */ export const convertScriptletToAdg = (rule: string): string[] => { @@ -300,6 +320,7 @@ export const convertScriptletToAdg = (rule: string): string[] => { return [rule]; } + // Determine rule's origin const originName = getRuleOrigin(rule); if (!originName) { @@ -308,19 +329,8 @@ export const convertScriptletToAdg = (rule: string): string[] => { return []; } - const converters = { - ubo: convertUboScriptletToAdg, - abp: convertAbpSnippetToAdg, - adgValid: (r: string) => [r], - adgInvalid: () => { - // eslint-disable-next-line no-console - console.log(`Invalid AdGuard scriptlet rule: ${rule}`); - return []; - }, - }; - - // Call converter that corresponds with origin - return converters[originName](rule); + // Call converter of given origin + return Converter[originName](rule); }; /** diff --git a/src/scriptlets/abort-current-inline-script.js b/src/scriptlets/abort-current-inline-script.js index c3945816..c4a38345 100644 --- a/src/scriptlets/abort-current-inline-script.js +++ b/src/scriptlets/abort-current-inline-script.js @@ -203,8 +203,7 @@ export function abortCurrentInlineScript(source, property, search) { setChainPropAccess(window, property); - window.onerror = createOnErrorHandler(rid) - .bind(); + window.onerror = createOnErrorHandler(rid).bind(); } abortCurrentInlineScript.names = [ diff --git a/src/scriptlets/abort-on-property-read.js b/src/scriptlets/abort-on-property-read.js index 3f7e0101..49b40eb8 100644 --- a/src/scriptlets/abort-on-property-read.js +++ b/src/scriptlets/abort-on-property-read.js @@ -75,8 +75,7 @@ export function abortOnPropertyRead(source, property) { setChainPropAccess(window, property); - window.onerror = createOnErrorHandler(rid) - .bind(); + window.onerror = createOnErrorHandler(rid).bind(); } abortOnPropertyRead.names = [ diff --git a/src/scriptlets/abort-on-stack-trace.js b/src/scriptlets/abort-on-stack-trace.js index 248e97e9..5cb19fb6 100644 --- a/src/scriptlets/abort-on-stack-trace.js +++ b/src/scriptlets/abort-on-stack-trace.js @@ -123,8 +123,7 @@ export function abortOnStackTrace(source, property, stack) { setChainPropAccess(window, property); - window.onerror = createOnErrorHandler(rid) - .bind(); + window.onerror = createOnErrorHandler(rid).bind(); } abortOnStackTrace.names = [ diff --git a/src/scriptlets/debug-current-inline-script.js b/src/scriptlets/debug-current-inline-script.js index d2f9d492..1f9ad2f1 100644 --- a/src/scriptlets/debug-current-inline-script.js +++ b/src/scriptlets/debug-current-inline-script.js @@ -114,8 +114,7 @@ export function debugCurrentInlineScript(source, property, search) { setChainPropAccess(window, property); - window.onerror = createOnErrorHandler(rid) - .bind(); + window.onerror = createOnErrorHandler(rid).bind(); } debugCurrentInlineScript.names = [ diff --git a/src/scriptlets/debug-on-property-read.js b/src/scriptlets/debug-on-property-read.js index b5bc8f61..88cc8aa6 100644 --- a/src/scriptlets/debug-on-property-read.js +++ b/src/scriptlets/debug-on-property-read.js @@ -62,8 +62,7 @@ export function debugOnPropertyRead(source, property) { setChainPropAccess(window, property); - window.onerror = createOnErrorHandler(rid) - .bind(); + window.onerror = createOnErrorHandler(rid).bind(); } debugOnPropertyRead.names = [