Skip to content

Commit

Permalink
use SpecificKey in validateForSpecificSyntax()
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit f4fc93e
Merge: b0ffee4 7ff682c
Author: Slava Leleka <[email protected]>
Date:   Thu Aug 17 19:55:09 2023 +0300

    Merge branch 'master' into fix/AG-24028-use-SpecificKey-enum

commit b0ffee4
Author: Slava Leleka <[email protected]>
Date:   Thu Aug 17 19:06:36 2023 +0300

    use SpecificKey in validateForSpecificSyntax()
  • Loading branch information
slavaleleka committed Aug 17, 2023
1 parent 7ff682c commit a130373
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
9 changes: 5 additions & 4 deletions packages/agtree/src/compatibility-tables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

export { getModifiersData } from './modifiers-data';

export type {
ModifierData,
ModifierDataMap,
SpecificPlatformModifierData,
export {
type ModifierData,
type ModifierDataMap,
type SpecificPlatformModifierData,
SpecificKey,
} from './types';
19 changes: 10 additions & 9 deletions packages/agtree/src/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type ModifierDataMap,
type SpecificPlatformModifierData,
getModifiersData,
SpecificKey,
} from '../compatibility-tables';
import { type Modifier } from '../parser/common';
import { AdblockSyntax } from '../utils/adblockers';
Expand Down Expand Up @@ -145,42 +146,42 @@ const validateForSpecificSyntax = (
}

// e.g. 'object-subrequest'
if (specificBlockerData.removed) {
if (specificBlockerData[SpecificKey.Removed]) {
return getInvalidValidationResult(`${INVALID_ERROR_PREFIX.REMOVED}: '${modifierName}'`);
}

if (specificBlockerData.deprecated) {
if (!specificBlockerData.deprecation_message) {
if (specificBlockerData[SpecificKey.Deprecated]) {
if (!specificBlockerData[SpecificKey.DeprecationMessage]) {
throw new Error('Deprecation notice is required for deprecated modifier');
}
return {
ok: true,
warn: specificBlockerData.deprecation_message,
warn: specificBlockerData[SpecificKey.DeprecationMessage],
};
}

if (specificBlockerData.block_only && isException) {
if (specificBlockerData[SpecificKey.BlockOnly] && isException) {
return getInvalidValidationResult(`${INVALID_ERROR_PREFIX.BLOCK_ONLY}: '${modifierName}'`);
}

if (specificBlockerData.exception_only && !isException) {
if (specificBlockerData[SpecificKey.ExceptionOnly] && !isException) {
return getInvalidValidationResult(`${INVALID_ERROR_PREFIX.EXCEPTION_ONLY}: '${modifierName}'`);
}

// e.g. '~domain=example.com'
if (!specificBlockerData.negatable && modifier.exception) {
if (!specificBlockerData[SpecificKey.Negatable] && modifier.exception) {
return getInvalidValidationResult(`${INVALID_ERROR_PREFIX.NOT_NEGATABLE}: '${modifierName}'`);
}

// e.g. 'domain'
if (specificBlockerData.assignable) {
if (specificBlockerData[SpecificKey.Assignable]) {
/**
* Some assignable modifiers can be used without a value,
* e.g. '@@||example.com^$cookie'.
*/
if (!modifier.value
// value should be specified if it is not optional
&& !specificBlockerData.value_optional) {
&& !specificBlockerData[SpecificKey.ValueOptional]) {
return getInvalidValidationResult(`${INVALID_ERROR_PREFIX.VALUE_REQUIRED}: '${modifierName}'`);
}
/**
Expand Down

0 comments on commit a130373

Please sign in to comment.