Skip to content

Commit d67bcc2

Browse files
committed
update syntax highlighting for exception scriptlet rules. #132 AG-31622
Squashed commit of the following: commit 316513f Author: scripthunter7 <d.tota@adguard.com> Date: Tue Apr 2 19:08:31 2024 +0200 small improvements commit bc76fc7 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 20:03:24 2024 +0300 update changelog commit f6bed55 Merge: 963e15d 32bd831 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 18:31:07 2024 +0300 Merge branch 'fix/AG-31622' of ssh://bit.int.agrd.dev:7999/adguard-filters/vscode-adblock-syntax into fix/AG-31622 commit 963e15d Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 18:27:32 2024 +0300 fix linter commit 32bd831 Author: scripthunter7 <d.tota@adguard.com> Date: Tue Apr 2 17:27:06 2024 +0200 fix linter commit af7f0a6 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 18:20:20 2024 +0300 update changelog commit aee8fe9 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 18:15:01 2024 +0300 add tests commit 31e2e88 Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 18:10:31 2024 +0300 fix scriptlet match commit 4514d5a Author: Slava Leleka <v.leleka@adguard.com> Date: Tue Apr 2 17:55:14 2024 +0300 update vscode-oniguruma commit 2a6e823 Author: scripthunter7 <d.tota@adguard.com> Date: Tue Apr 2 15:58:59 2024 +0200 update test rules commit 4cdb98a Author: scripthunter7 <d.tota@adguard.com> Date: Tue Apr 2 15:58:45 2024 +0200 update syntax highlighting Co-authored-by: Slava <slavaleleka@users.noreply.github.com>
1 parent b4a53be commit d67bcc2

7 files changed

Lines changed: 193 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog], and this project adheres to [Semantic Versioning][semver].
66

7+
## 1.1.8 - 2024-04-03
8+
9+
### Fixed
10+
11+
- Highlighting for `#@%#//scriptlet()` like rules: [#132]
12+
13+
[#132]: https://github.com/AdguardTeam/VscodeAdblockSyntax/issues/132
14+
715

816
## 1.1.7 - 2024-01-10
917

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
"tsx": "^4.7.0",
158158
"typescript": "^4.9.5",
159159
"unified": "^11.0.4",
160-
"vscode-oniguruma": "^1.7.0",
160+
"vscode-oniguruma": "^2.0.1",
161161
"vscode-textmate": "^9.0.0",
162162
"yaml": "^2.2.2"
163163
}

syntaxes/adblock.yaml-tmlanguage

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,57 @@ repository:
213213
- include: "#contentAttributes"
214214
scriptletRules:
215215
patterns:
216-
- match: "^(\\[.+?\\])?(.*?)(#@?%#)(\\/\\/scriptlet)(\\()(.+)(\\)\\s*)$"
216+
- include: "#exceptionScriptletRules"
217+
- include: "#blockingScriptletRules"
218+
exceptionScriptletRules:
219+
patterns:
220+
- match: |-
221+
(?x)
222+
^ # Start of the line
223+
\s* # Optional leading whitespace
224+
(\[.+?\])? # Group 1. AdGuard modifier list
225+
(.*)? # Group 2. Domain list
226+
(\#@%\#) # Group 3. Cosmetic rule marker
227+
(\/\/scriptlet) # Group 4. Scriptlet marker
228+
(\() # Group 5. Opening parenthesis
229+
(.*)? # Group 6. Arguments
230+
(\)) # Group 7. Closing parenthesis
231+
\s* # Optional trailing whitespace
232+
$ # End of the line
233+
captures:
234+
"1":
235+
patterns:
236+
- include: "#cosmeticRulesOptions"
237+
"2":
238+
patterns:
239+
- include: "#domainListCommaSeparated"
240+
"3":
241+
name: keyword.control.adblock
242+
"4":
243+
name: entity.name.function.adblock
244+
"5":
245+
name: punctuation.section.adblock
246+
"6":
247+
patterns:
248+
- include: "#emptyScriptletFunction"
249+
- include: "#scriptletFunction"
250+
"7":
251+
name: punctuation.section.adblock
252+
blockingScriptletRules:
253+
patterns:
254+
- match: |-
255+
(?x)
256+
^ # Start of the line
257+
\s* # Optional leading whitespace
258+
(\[.+?\])? # Group 1. AdGuard modifier list
259+
(.*)? # Group 2. Domain list
260+
(\#%\#) # Group 3. Cosmetic rule marker
261+
(\/\/scriptlet) # Group 4. Scriptlet marker
262+
(\() # Group 5. Opening parenthesis
263+
(.*\S.*) # Group 6. Arguments. Note: we look for a parameter that contain at least one non-whitespace character
264+
(\)) # Group 7. Closing parenthesis
265+
\s* # Optional trailing whitespace
266+
$ # End of the line
217267
captures:
218268
"1":
219269
patterns:
@@ -568,6 +618,10 @@ repository:
568618
name: punctuation.section.adblock
569619
- name: invalid.illegal.adblock
570620
match: ".*"
621+
emptyScriptletFunction:
622+
patterns:
623+
- match: \s*\z
624+
name: entity.name.section.adblock.empty-scriptlet
571625
scriptletFunction:
572626
patterns:
573627
- match: ((['|"])(.*?)(?<!\\)(\2))(,\s*)?

test/grammar/comments/comment.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ describe('test comments', () => {
4848
'#$?#.selector:has(> .selector) { display: none; }',
4949
'#@$?#.selector:has(> .selector) { display: none; }',
5050
'#%#//scriptlet(\'adg-scriptlet0\', \'arg0\', \'arg1\', \'arg2\')',
51+
"#@%#//scriptlet('adg-scriptlet0')",
52+
'#@%#//scriptlet()',
5153
'#@%#//scriptlet(\'adg-scriptlet0\', \'arg0\', \'arg1\', \'arg2\')',
5254
'$$div',
5355
'$@$div',
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @file Tests for scriptlets rules
3+
*/
4+
5+
import { type AdblockTokenizer, getAdblockTokenizer } from '../common/get-adblock-tokenizer';
6+
import { expectTokens } from '../common/token-expectation';
7+
8+
let tokenize: AdblockTokenizer;
9+
10+
// Before running any tests, we should load the grammar and get the tokenizer
11+
beforeAll(async () => {
12+
tokenize = await getAdblockTokenizer();
13+
});
14+
15+
describe('scriptlet rules', () => {
16+
describe('valid', () => {
17+
test('blocking', () => {
18+
// single quoted arguments
19+
expectTokens(
20+
tokenize,
21+
"#%#//scriptlet('foo', 'bar')",
22+
[
23+
{ fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
24+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
25+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
26+
{ fragment: "'foo'", scopes: ['text.adblock', 'string.quoted.adblock'] },
27+
{ fragment: ', ', scopes: ['text.adblock', 'keyword.operator.adblock'] },
28+
{ fragment: "'bar'", scopes: ['text.adblock', 'string.quoted.adblock'] },
29+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
30+
],
31+
);
32+
33+
// double quoted arguments
34+
expectTokens(
35+
tokenize,
36+
'#%#//scriptlet("foo", "bar")',
37+
[
38+
{ fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
39+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
40+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
41+
{ fragment: '"foo"', scopes: ['text.adblock', 'string.quoted.adblock'] },
42+
{ fragment: ', ', scopes: ['text.adblock', 'keyword.operator.adblock'] },
43+
{ fragment: '"bar"', scopes: ['text.adblock', 'string.quoted.adblock'] },
44+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
45+
],
46+
);
47+
48+
// should handle if the argument contain a different quote type
49+
expectTokens(
50+
tokenize,
51+
'#%#//scriptlet("foo\'bar")',
52+
[
53+
{ fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
54+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
55+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
56+
{ fragment: '"foo\'bar"', scopes: ['text.adblock', 'string.quoted.adblock'] },
57+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
58+
],
59+
);
60+
});
61+
62+
test('exception for specific scriptlet', () => {
63+
expectTokens(
64+
tokenize,
65+
"#@%#//scriptlet('foo')",
66+
[
67+
{ fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
68+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
69+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
70+
{ fragment: "'foo'", scopes: ['text.adblock', 'string.quoted.adblock'] },
71+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
72+
],
73+
);
74+
});
75+
76+
test('exception for all scriptlets', () => {
77+
expectTokens(
78+
tokenize,
79+
'#@%#//scriptlet()',
80+
[
81+
{ fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
82+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
83+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
84+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
85+
],
86+
);
87+
88+
expectTokens(
89+
tokenize,
90+
'#@%#//scriptlet( )',
91+
[
92+
{ fragment: '#@%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
93+
{ fragment: '//scriptlet', scopes: ['text.adblock', 'entity.name.function.adblock'] },
94+
{ fragment: '(', scopes: ['text.adblock', 'punctuation.section.adblock'] },
95+
{ fragment: ' ', scopes: ['text.adblock', 'entity.name.section.adblock.empty-scriptlet'] },
96+
{ fragment: ')', scopes: ['text.adblock', 'punctuation.section.adblock'] },
97+
],
98+
);
99+
});
100+
});
101+
102+
describe('invalid', () => {
103+
test('blocking', () => {
104+
expectTokens(
105+
tokenize,
106+
// blocking scriptlet rule cannot be used without any arguments
107+
'#%#//scriptlet()',
108+
[
109+
{ fragment: '#%#', scopes: ['text.adblock', 'keyword.control.adblock'] },
110+
{ fragment: '//scriptlet()', scopes: ['text.adblock', 'invalid.illegal'] },
111+
],
112+
);
113+
});
114+
});
115+
});

test/static/rules/test_rules.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,11 @@ example.com##.ads
440440
! https://github.com/AdguardTeam/VscodeAdblockSyntax/issues/119
441441
#@#+js(ubo-scriptlet0, arg0, arg1, arg2)
442442
example.com#@$#abp-snippet0 arg0 arg1 arg2
443+
444+
! https://github.com/AdguardTeam/VscodeAdblockSyntax/issues/132
445+
! empty blocking rule should be invalid
446+
#%#//scriptlet()
447+
#%#//scriptlet( )
448+
! empty exception rule should be valid
449+
#@%#//scriptlet()
450+
#@%#//scriptlet( )

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5593,10 +5593,10 @@ vfile@^6.0.0:
55935593
unist-util-stringify-position "^4.0.0"
55945594
vfile-message "^4.0.0"
55955595

5596-
vscode-oniguruma@^1.7.0:
5597-
version "1.7.0"
5598-
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz#439bfad8fe71abd7798338d1cd3dc53a8beea94b"
5599-
integrity sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA==
5596+
vscode-oniguruma@^2.0.1:
5597+
version "2.0.1"
5598+
resolved "https://registry.yarnpkg.com/vscode-oniguruma/-/vscode-oniguruma-2.0.1.tgz#1196a343635ff8d42eb880e325b5f4e70731c02f"
5599+
integrity sha512-poJU8iHIWnC3vgphJnrLZyI3YdqRlR27xzqDmpPXYzA93R4Gk8z7T6oqDzDoHjoikA2aS82crdXFkjELCdJsjQ==
56005600

56015601
vscode-textmate@^9.0.0:
56025602
version "9.0.0"

0 commit comments

Comments
 (0)