Skip to content

Commit 559e365

Browse files
committed
Add fuzzy matching suggestions for unknown scriptlets in no-invalid-scriptlets rule
- Implemented fuzzy matching using fast-fuzzy library to suggest similar scriptlet names when unknown scriptlet is detected - Added configurable `fuzzyThreshold` option (default 0.6) to control minimum similarity threshold for suggestions - Added `changeScriptlet` message ID and suggestion fixes that replace unknown scriptlet with suggested alternatives - Cached scriptlet names per platform to optimize fuzzy search
1 parent 9054078 commit 559e365

5 files changed

Lines changed: 378 additions & 3 deletions

File tree

config-presets/all.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"no-invalid-domains": "error",
2525
"no-invalid-hint-params": "error",
2626
"no-invalid-modifiers": "error",
27-
"no-invalid-scriptlets": "error",
27+
"no-invalid-scriptlets": [
28+
"error",
29+
{
30+
"fuzzyThreshold": 0.6
31+
}
32+
],
2833
"no-short-rules": [
2934
"error",
3035
{

docs/rules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| [no-invalid-domains](./no-invalid-domains.md) | Disallows invalid domains || | 💡 |
1717
| [no-invalid-hint-params](./no-invalid-hint-params.md) | Checks if hints are parameterized correctly || | |
1818
| [no-invalid-modifiers](./no-invalid-modifiers.md) | Checks modifiers validity for basic (network) rules || | |
19-
| [no-invalid-scriptlets](./no-invalid-scriptlets.md) | Checks if scriptlets are valid based on compatibility tables | | | |
19+
| [no-invalid-scriptlets](./no-invalid-scriptlets.md) | Checks if scriptlets are valid based on compatibility tables | | | 💡 |
2020
| [no-short-rules](./no-short-rules.md) | Checks if a rule is too short || | |
2121
| [no-unknown-hint-platforms](./no-unknown-hint-platforms.md) | Checks if platforms in related hints are known || | |
2222
| [no-unknown-hints](./no-unknown-hints.md) | Checks if hints are known || | |

docs/rules/no-invalid-scriptlets.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,141 @@ Checks if scriptlets are valid based on compatibility tables
99

1010
Problem. Identifies parts that causes errors or confusing behavior. High priority fix.
1111

12+
## Automatic issue fixing
13+
14+
- Some reported problems can be fixed via suggestions 💡
15+
16+
## Options
17+
18+
This rule can be configured using the following options.
19+
20+
### Options overview
21+
22+
```typescript
23+
[
24+
{
25+
fuzzyThreshold: number // Minimum similarity threshold for fuzzy matching
26+
}
27+
]
28+
```
29+
30+
### Options valibot schema
31+
32+
<details>
33+
<summary>Click to expand</summary>
34+
35+
```typescript
36+
{
37+
"kind": "schema",
38+
"type": "tuple",
39+
"expects": "Array",
40+
"async": false,
41+
"items": [
42+
{
43+
"kind": "schema",
44+
"type": "strict_object",
45+
"expects": "Object",
46+
"async": false,
47+
"entries": {
48+
"fuzzyThreshold": {
49+
"kind": "schema",
50+
"type": "number",
51+
"expects": "number",
52+
"async": false,
53+
"~standard": {
54+
"version": 1,
55+
"vendor": "valibot"
56+
},
57+
"pipe": [
58+
{
59+
"kind": "schema",
60+
"type": "number",
61+
"expects": "number",
62+
"async": false,
63+
"~standard": {
64+
"version": 1,
65+
"vendor": "valibot"
66+
}
67+
},
68+
{
69+
"kind": "validation",
70+
"type": "min_value",
71+
"async": false,
72+
"expects": ">=0",
73+
"requirement": 0
74+
},
75+
{
76+
"kind": "validation",
77+
"type": "max_value",
78+
"async": false,
79+
"expects": "<=1",
80+
"requirement": 1
81+
},
82+
{
83+
"kind": "metadata",
84+
"type": "description",
85+
"description": "Minimum similarity threshold for fuzzy matching"
86+
}
87+
]
88+
}
89+
},
90+
"~standard": {
91+
"version": 1,
92+
"vendor": "valibot"
93+
}
94+
}
95+
],
96+
"~standard": {
97+
"version": 1,
98+
"vendor": "valibot"
99+
}
100+
}
101+
```
102+
103+
</details>
104+
105+
### Options JSON schema
106+
107+
<details>
108+
<summary>Click to expand</summary>
109+
110+
```typescript
111+
{
112+
"$schema": "http://json-schema.org/draft-07/schema#",
113+
"type": "array",
114+
"items": [
115+
{
116+
"type": "object",
117+
"properties": {
118+
"fuzzyThreshold": {
119+
"type": "number",
120+
"minimum": 0,
121+
"maximum": 1,
122+
"description": "Minimum similarity threshold for fuzzy matching"
123+
}
124+
},
125+
"required": [
126+
"fuzzyThreshold"
127+
],
128+
"additionalProperties": false
129+
}
130+
],
131+
"minItems": 1
132+
}
133+
```
134+
135+
</details>
136+
137+
### Default options
138+
139+
```json
140+
[
141+
{
142+
"fuzzyThreshold": 0.6
143+
}
144+
]
145+
```
146+
12147
## Correct examples
13148

14149
Examples of correct code:
@@ -21,6 +156,16 @@ The following code
21156
#%#//scriptlet("set-constant", "foo", "bar")
22157
```
23158

159+
with the following rule config:
160+
161+
```json
162+
[
163+
{
164+
"fuzzyThreshold": 0.6
165+
}
166+
]
167+
```
168+
24169
should not be reported
25170

26171
### Valid uBlock Origin scriptlet call
@@ -31,6 +176,16 @@ The following code
31176
##+js(set.js, foo, bar)
32177
```
33178

179+
with the following rule config:
180+
181+
```json
182+
[
183+
{
184+
"fuzzyThreshold": 0.6
185+
}
186+
]
187+
```
188+
34189
should not be reported
35190

36191
### Valid Adblock Plus scriptlet call
@@ -41,6 +196,16 @@ The following code
41196
#$#override-property-read foo bar
42197
```
43198

199+
with the following rule config:
200+
201+
```json
202+
[
203+
{
204+
"fuzzyThreshold": 0.6
205+
}
206+
]
207+
```
208+
44209
should not be reported
45210

46211
## Incorrect examples
@@ -55,12 +220,24 @@ The following code
55220
#%#//scriptlet("unknown-scriptlet", "foo", "bar")
56221
```
57222

223+
with the following rule config:
224+
225+
```json
226+
[
227+
{
228+
"fuzzyThreshold": 0.6
229+
}
230+
]
231+
```
232+
58233
should be reported as:
59234

60235
```shell
61236
1:3 Unknown scriptlet 'unknown-scriptlet' for 'Any AdGuard product'
62237
```
63238

239+
and the following suggestions should be offered:
240+
64241
### Unknown uBlock Origin scriptlet
65242

66243
The following code
@@ -69,12 +246,24 @@ The following code
69246
##+js(unknown-scriptlet.js, foo, bar)
70247
```
71248

249+
with the following rule config:
250+
251+
```json
252+
[
253+
{
254+
"fuzzyThreshold": 0.6
255+
}
256+
]
257+
```
258+
72259
should be reported as:
73260

74261
```shell
75262
1:2 Unknown scriptlet 'unknown-scriptlet.js' for 'Any uBlock Origin product'
76263
```
77264

265+
and the following suggestions should be offered:
266+
78267
### Unknown Adblock Plus scriptlet
79268

80269
The following code
@@ -83,12 +272,24 @@ The following code
83272
#$#unknown-scriptlet foo bar
84273
```
85274

275+
with the following rule config:
276+
277+
```json
278+
[
279+
{
280+
"fuzzyThreshold": 0.6
281+
}
282+
]
283+
```
284+
86285
should be reported as:
87286

88287
```shell
89288
1:3 Unknown scriptlet 'unknown-scriptlet' for 'Any AdBlock / Adblock Plus product'
90289
```
91290

291+
and the following suggestions should be offered:
292+
92293
### Required parameters are missing
93294

94295
The following code
@@ -97,6 +298,16 @@ The following code
97298
#%#//scriptlet("set-constant")
98299
```
99300

301+
with the following rule config:
302+
303+
```json
304+
[
305+
{
306+
"fuzzyThreshold": 0.6
307+
}
308+
]
309+
```
310+
100311
should be reported as:
101312

102313
```shell
@@ -113,6 +324,16 @@ The following code
113324
#%#//scriptlet("log-addEventListener", "foo", "bar")
114325
```
115326

327+
with the following rule config:
328+
329+
```json
330+
[
331+
{
332+
"fuzzyThreshold": 0.6
333+
}
334+
]
335+
```
336+
116337
should be reported as:
117338

118339
```shell
@@ -127,6 +348,16 @@ The following code
127348
#%#//scriptlet("debug-on-property-read", "foo", "bar")
128349
```
129350

351+
with the following rule config:
352+
353+
```json
354+
[
355+
{
356+
"fuzzyThreshold": 0.6
357+
}
358+
]
359+
```
360+
130361
should be reported as:
131362

132363
```shell

0 commit comments

Comments
 (0)