Skip to content

Commit

Permalink
add extra syntax property to Agent interface. AG-23891
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 55985b3
Author: Slava Leleka <[email protected]>
Date:   Fri Jul 28 14:07:17 2023 +0300

    update ABP_NAME_MARKERS

commit e5c1632
Author: Slava Leleka <[email protected]>
Date:   Fri Jul 28 14:03:44 2023 +0300

    arrays -> sets

commit 9849512
Author: Slava Leleka <[email protected]>
Date:   Fri Jul 28 13:06:47 2023 +0300

    Revert "improve check-compatibility-tables"

    This reverts commit 691c0c5.

commit 33dab50
Author: Slava Leleka <[email protected]>
Date:   Fri Jul 28 12:55:27 2023 +0300

    fix linter

commit 6963034
Author: Slava Leleka <[email protected]>
Date:   Fri Jul 28 12:49:31 2023 +0300

    add extra syntax property to Agent interface

commit a7d6b21
Merge: 0045b38 9b26e0b
Author: Slava Leleka <[email protected]>
Date:   Mon Jul 24 16:35:23 2023 +0300

    Merge branch 'master' of ssh://bit.adguard.com:7999/adguard-filters/tsurlfilter

commit 0045b38
Author: Slava Leleka <[email protected]>
Date:   Wed Jul 19 20:58:46 2023 +0300

    Revert "add simple value_format for app modifier"

    This reverts commit a5b3f70.

commit 691c0c5
Author: Slava Leleka <[email protected]>
Date:   Wed Jul 19 20:54:16 2023 +0300

    improve check-compatibility-tables

commit a5b3f70
Author: Slava Leleka <[email protected]>
Date:   Wed Jul 19 20:53:21 2023 +0300

    add simple value_format for app modifier
  • Loading branch information
slavaleleka committed Jul 28, 2023
1 parent 9b26e0b commit 5c9457a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
60 changes: 55 additions & 5 deletions packages/agtree/src/parser/comment/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,52 @@ import { locRange } from '../../utils/location';
import { EMPTY, SPACE } from '../../utils/constants';
import { StringUtils } from '../../utils/string';
import {
Agent,
Location,
Value,
type Agent,
type Location,
type Value,
defaultLocation,
} from '../common';
import { AdblockSyntaxError } from '../errors/adblock-syntax-error';
import { AdblockSyntax } from '../../utils/adblockers';

const ADG_NAME_MARKERS = new Set([
'adguard',
'adg',
]);

const UBO_NAME_MARKERS = new Set([
'ublock',
'ublock origin',
'ubo',
]);

const ABP_NAME_MARKERS = new Set([
'adblock',
'adblock plus',
'adblockplus',
'abp',
]);

/**
* Returns the adblock syntax based on the adblock name
* parsed from the agent type comment.
* Needed for modifiers validation of network rules by AGLint.
*
* @param name Adblock name.
*
* @returns Adblock syntax.
*/
const getAdblockSyntax = (name: string): AdblockSyntax => {
let syntax = AdblockSyntax.Common;
if (ADG_NAME_MARKERS.has(name.toLowerCase())) {
syntax = AdblockSyntax.Adg;
} else if (UBO_NAME_MARKERS.has(name.toLowerCase())) {
syntax = AdblockSyntax.Ubo;
} else if (ABP_NAME_MARKERS.has(name.toLowerCase())) {
syntax = AdblockSyntax.Abp;
}
return syntax;
};

/**
* `AgentParser` is responsible for parsing single adblock agent elements.
Expand Down Expand Up @@ -52,6 +92,8 @@ export class AgentParser {
// Prepare variables for name and version
let name: Value | null = null;
let version: Value | null = null;
// default value for the syntax
let syntax: AdblockSyntax = AdblockSyntax.Common;

// Get agent parts by splitting it by spaces. The last part may be a version.
// Example: "Adblock Plus 2.0"
Expand All @@ -71,11 +113,13 @@ export class AgentParser {
);
}

const parsedNamePart = raw.substring(nameStartIndex, nameEndIndex);

// Save name
name = {
type: 'Value',
loc: locRange(loc, nameStartIndex, nameEndIndex),
value: raw.substring(nameStartIndex, nameEndIndex),
value: parsedNamePart,
};

// Save version
Expand All @@ -84,6 +128,9 @@ export class AgentParser {
loc: locRange(loc, offset, partEnd),
value: part,
};

// Save syntax
syntax = getAdblockSyntax(parsedNamePart);
} else {
nameEndIndex = partEnd;
}
Expand All @@ -94,11 +141,13 @@ export class AgentParser {

// If we didn't find a version, the whole string is the name
if (name === null) {
const parsedNamePart = raw.substring(nameStartIndex, nameEndIndex);
name = {
type: 'Value',
loc: locRange(loc, nameStartIndex, nameEndIndex),
value: raw.substring(nameStartIndex, nameEndIndex),
value: parsedNamePart,
};
syntax = getAdblockSyntax(parsedNamePart);
}

// Agent name cannot be empty
Expand All @@ -114,6 +163,7 @@ export class AgentParser {
loc: locRange(loc, 0, raw.length),
adblock: name,
version,
syntax,
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/agtree/src/parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@ export interface Agent extends Node {
* Adblock version (if specified).
*/
version: Value | null;

/**
* Needed for network rules modifier validation.
*/
syntax: AdblockSyntax;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/agtree/test/parser/comment/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AgentCommentRuleParser } from '../../../src/parser/comment/agent-rule';
import { AdblockSyntax } from '../../../src/utils/adblockers';
import { EMPTY, SPACE } from '../../../src/utils/constants';

describe('AgentCommentRuleParser', () => {
Expand Down Expand Up @@ -80,6 +81,7 @@ describe('AgentCommentRuleParser', () => {
value: 'AdBlock',
},
version: null,
syntax: AdblockSyntax.Abp,
},
],
});
Expand Down Expand Up @@ -132,6 +134,7 @@ describe('AgentCommentRuleParser', () => {
value: 'AdGuard',
},
version: null,
syntax: AdblockSyntax.Adg,
},
],
});
Expand Down Expand Up @@ -184,6 +187,7 @@ describe('AgentCommentRuleParser', () => {
value: 'uBlock',
},
version: null,
syntax: AdblockSyntax.Ubo,
},
],
});
Expand Down Expand Up @@ -237,6 +241,7 @@ describe('AgentCommentRuleParser', () => {
value: 'uBlock Origin',
},
version: null,
syntax: AdblockSyntax.Ubo,
},
],
},
Expand Down Expand Up @@ -306,6 +311,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '2.0',
},
syntax: AdblockSyntax.Abp,
},
],
},
Expand Down Expand Up @@ -374,6 +380,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.0.0',
},
syntax: AdblockSyntax.Ubo,
},
],
});
Expand Down Expand Up @@ -441,6 +448,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '2.0',
},
syntax: AdblockSyntax.Abp,
},
{
type: 'Agent',
Expand Down Expand Up @@ -473,6 +481,7 @@ describe('AgentCommentRuleParser', () => {
value: 'AdGuard',
},
version: null,
syntax: AdblockSyntax.Adg,
},
],
});
Expand Down Expand Up @@ -541,6 +550,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '2.0',
},
syntax: AdblockSyntax.Abp,
},
{
type: 'Agent',
Expand Down Expand Up @@ -588,6 +598,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.0.1.10',
},
syntax: AdblockSyntax.Adg,
},
],
},
Expand Down Expand Up @@ -658,6 +669,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '3.1',
},
syntax: AdblockSyntax.Abp,
},
{
type: 'Agent',
Expand Down Expand Up @@ -705,6 +717,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.4',
},
syntax: AdblockSyntax.Adg,
},
{
type: 'Agent',
Expand Down Expand Up @@ -752,6 +765,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.0.15.0',
},
syntax: AdblockSyntax.Ubo,
},
],
});
Expand Down Expand Up @@ -821,6 +835,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '3.1',
},
syntax: AdblockSyntax.Abp,
},
{
type: 'Agent',
Expand Down Expand Up @@ -868,6 +883,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.4',
},
syntax: AdblockSyntax.Adg,
},
{
type: 'Agent',
Expand Down Expand Up @@ -915,6 +931,7 @@ describe('AgentCommentRuleParser', () => {
},
value: '1.0.15.0',
},
syntax: AdblockSyntax.Ubo,
},
],
});
Expand Down

0 comments on commit 5c9457a

Please sign in to comment.