@@ -12,7 +12,13 @@ import { WildcardDomainsWithTld } from './wildcard-domain-processor';
12
12
import { DOMAIN_MODIFIERS } from './domain-extractor' ;
13
13
import { utils } from './utils' ;
14
14
15
- const expandWildcardInNetworkRuleAst = (
15
+ /**
16
+ * Expands wildcards in a network rule AST.
17
+ * @param ast - The network rule AST to process.
18
+ * @param wildcardDomains - A map of wildcard domains to their non-wildcard equivalents.
19
+ * @returns The updated network rule AST with expanded wildcards, or null if no valid domains are left.
20
+ */
21
+ const expandNetworkRulesWildcard = (
16
22
ast : NetworkRule ,
17
23
wildcardDomains : WildcardDomainsWithTld ,
18
24
) : NetworkRule | null => {
@@ -21,10 +27,8 @@ const expandWildcardInNetworkRuleAst = (
21
27
}
22
28
23
29
const modifiers = ast . modifiers . children ;
24
-
25
30
const newPermittedDomains = new Map ( ) ;
26
31
const newRestrictedDomains = new Map ( ) ;
27
-
28
32
let hadWildcard = false ;
29
33
const newModifiers = [ ] ;
30
34
@@ -37,7 +41,7 @@ const expandWildcardInNetworkRuleAst = (
37
41
const domainList = DomainListParser . parse ( modifier . value . value , agtree . PIPE_MODIFIER_SEPARATOR ) ;
38
42
39
43
for ( const domain of domainList . children ) {
40
- if ( utils . isDomainWithTldWildcard ( domain . value ) ) {
44
+ if ( utils . isWildcardDomain ( domain . value ) ) {
41
45
hadWildcard = true ;
42
46
const nonWildcardDomains = wildcardDomains [ domain . value ] ;
43
47
for ( const nonWildcardDomain of nonWildcardDomains ) {
@@ -100,15 +104,23 @@ const expandWildcardInNetworkRuleAst = (
100
104
return newAst ;
101
105
} ;
102
106
103
- const expandWildcardCosmeticRuleAst = ( ast : CosmeticRule , wildcardDomains : WildcardDomainsWithTld ) : any => {
107
+ /**
108
+ * Expands wildcards in a cosmetic rule AST.
109
+ * @param ast - The cosmetic rule AST to process.
110
+ * @param wildcardDomains - A map of wildcard domains to their non-wildcard equivalents.
111
+ * @returns The updated cosmetic rule AST with expanded wildcards, or null if no valid domains are left.
112
+ */
113
+ const expandCosmeticRulesWildcard = (
114
+ ast : CosmeticRule ,
115
+ wildcardDomains : WildcardDomainsWithTld ,
116
+ ) : AnyRule | null => {
104
117
const domains = ast . domains . children ;
105
-
106
118
const newPermittedDomains = new Map ( ) ;
107
119
const newRestrictedDomains = new Map ( ) ;
108
-
109
120
let hadWildcard = false ;
121
+
110
122
for ( const domain of domains ) {
111
- if ( utils . isDomainWithTldWildcard ( domain . value ) ) {
123
+ if ( utils . isWildcardDomain ( domain . value ) ) {
112
124
hadWildcard = true ;
113
125
const nonWildcardDomains = wildcardDomains [ domain . value ] ;
114
126
nonWildcardDomains . forEach ( ( d ) => {
@@ -122,18 +134,18 @@ const expandWildcardCosmeticRuleAst = (ast: CosmeticRule, wildcardDomains: Wildc
122
134
newPermittedDomains . set ( d , newDomain ) ;
123
135
}
124
136
} ) ;
137
+ continue ;
138
+ }
139
+
140
+ if ( domain . exception ) {
141
+ newRestrictedDomains . set ( domain . value , domain ) ;
125
142
} else {
126
- // eslint-disable-next-line
127
- if ( domain . exception ) {
128
- newRestrictedDomains . set ( domain . value , domain ) ;
129
- } else {
130
- newPermittedDomains . set ( domain . value , domain ) ;
131
- }
143
+ newPermittedDomains . set ( domain . value , domain ) ;
132
144
}
133
145
}
134
146
135
147
if ( ! hadWildcard ) {
136
- return ast ;
148
+ return ast as AnyRule ;
137
149
}
138
150
139
151
const newDomains = [ ] ;
@@ -158,22 +170,35 @@ const expandWildcardCosmeticRuleAst = (ast: CosmeticRule, wildcardDomains: Wildc
158
170
const newAst = structuredClone ( ast ) ;
159
171
newAst . domains . children = newDomains ;
160
172
161
- return newAst ;
173
+ return newAst as AnyRule ;
162
174
} ;
163
175
176
+ /**
177
+ * Expands wildcards in an AST based on its category.
178
+ * @param ast - The AST to process.
179
+ * @param wildcardDomains - A map of wildcard domains to their non-wildcard equivalents.
180
+ * @returns The updated AST with expanded wildcards, or null if no valid domains are left.
181
+ * @throws Will throw an error if the AST category is unsupported.
182
+ */
164
183
const expandWildcardDomainsInAst = ( ast : AnyRule , wildcardDomains : WildcardDomainsWithTld ) : AnyRule | null => {
165
184
switch ( ast . category ) {
166
185
case 'Network' :
167
- return expandWildcardInNetworkRuleAst ( ast , wildcardDomains ) ;
186
+ return expandNetworkRulesWildcard ( ast as NetworkRule , wildcardDomains ) ;
168
187
case 'Cosmetic' :
169
- return expandWildcardCosmeticRuleAst ( ast , wildcardDomains ) ;
188
+ return expandCosmeticRulesWildcard ( ast as CosmeticRule , wildcardDomains ) ;
170
189
case 'Comment' :
171
190
return ast ;
172
191
default :
173
192
throw new Error ( `Unsupported rule category: ${ ast . category } ` ) ;
174
193
}
175
194
} ;
176
195
196
+ /**
197
+ * Expands wildcards in a rule string.
198
+ * @param rule - The rule string to process.
199
+ * @param wildcardDomains - A map of wildcard domains to their non-wildcard equivalents.
200
+ * @returns The updated rule string with expanded wildcards, or null if no valid domains are left.
201
+ */
177
202
export function expandWildcardsInRule ( rule : string , wildcardDomains : WildcardDomainsWithTld ) : string | null {
178
203
const ast = RuleParser . parse ( rule ) ;
179
204
@@ -189,6 +214,12 @@ export function expandWildcardsInRule(rule: string, wildcardDomains: WildcardDom
189
214
return RuleParser . generate ( astWithExpandedWildcardDomain ) ;
190
215
}
191
216
217
+ /**
218
+ * Patches a filter content by expanding wildcards in all rules.
219
+ * @param filterContent - The filter content to patch.
220
+ * @param wildcardDomains - A map of wildcard domains to their non-wildcard equivalents.
221
+ * @returns The patched filter content with expanded wildcards.
222
+ */
192
223
function patchWildcards ( filterContent : string , wildcardDomains : WildcardDomainsWithTld ) : string {
193
224
const rules = filterContent . split ( / \r ? \n / ) ;
194
225
const newRules = [ ] ;
@@ -203,6 +234,11 @@ function patchWildcards(filterContent: string, wildcardDomains: WildcardDomainsW
203
234
204
235
const WILDCARD_DOMAINS_FILE = 'wildcard_domains.json' ;
205
236
237
+ /**
238
+ * Patches platform filter files by expanding wildcards in all rules.
239
+ * @param platformsDir - The directory containing the platform filter files.
240
+ * @returns A promise that resolves when the patching is complete.
241
+ */
206
242
export async function patchPlatforms ( platformsDir : string ) : Promise < void > {
207
243
const filters = await findFilterFiles ( path . resolve ( __dirname , platformsDir ) , / f i l t e r s \/ \d + ( _ o p t i m i z e d ) ? \. t x t / ) ;
208
244
0 commit comments