Skip to content

Commit

Permalink
improve convertScriptletToAdg, fix createOnErrorHandler calls linting
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed May 14, 2023
1 parent 5ac964d commit 23dd9f6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
68 changes: 39 additions & 29 deletions src/helpers/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,43 +263,64 @@ const isValidAdgScriptletRuleSyntax = (adgRuleText: string): boolean => {
}
};

// Possible rule origins
enum Origin {
Ubo = 'ubo',
Abp = 'abp',
AdgValid = 'adgValid',
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[] => {
if (validator.isComment(rule)) {
return [rule];
}

// Determine rule's origin
const originName = getRuleOrigin(rule);

if (!originName) {
Expand All @@ -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);
};

/**
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/abort-current-inline-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/abort-on-property-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export function abortOnPropertyRead(source, property) {

setChainPropAccess(window, property);

window.onerror = createOnErrorHandler(rid)
.bind();
window.onerror = createOnErrorHandler(rid).bind();
}

abortOnPropertyRead.names = [
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/abort-on-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/debug-current-inline-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/debug-on-property-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export function debugOnPropertyRead(source, property) {

setChainPropAccess(window, property);

window.onerror = createOnErrorHandler(rid)
.bind();
window.onerror = createOnErrorHandler(rid).bind();
}

debugOnPropertyRead.names = [
Expand Down

0 comments on commit 23dd9f6

Please sign in to comment.