Skip to content

Commit 28ec229

Browse files
committed
Add quote preservation to fuzzy matching suggestions in no-invalid-scriptlets rule
- Modified changeScriptlet fix to preserve original quote type from scriptlet name - Added actualQuote variable to capture quote type using QuoteUtils.getStringQuoteType - Updated fixer.replaceWithText to apply original quote type to suggested scriptlet name using QuoteUtils.setStringQuoteType - Ensures suggested scriptlet replacements maintain consistent quote style with original code
1 parent 3ed095a commit 28ec229

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

docs/rules/no-invalid-scriptlets.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,43 @@ should be reported as:
364364
1:3 Scriptlet 'debug-on-property-read' has too many parameters, expected maximum 1, got 2
365365
```
366366

367+
### Almost correct scriptlet name, but misspelled
368+
369+
The following code
370+
371+
```adblock
372+
#%#//scriptlet("pardot1.0")
373+
```
374+
375+
with the following rule config:
376+
377+
```json
378+
[
379+
{
380+
"fuzzyThreshold": 0.6
381+
}
382+
]
383+
```
384+
385+
should be reported as:
386+
387+
```shell
388+
1:3 Unknown scriptlet 'pardot1.0' for 'Any AdGuard product'
389+
```
390+
391+
and the following suggestions should be offered:
392+
393+
- Change scriptlet to 'pardot-1.0'
394+
395+
```diff
396+
===================================================================
397+
--- original
398+
+++ fixed
399+
@@ -1,1 +1,1 @@
400+
-#%#//scriptlet("pardot1.0")
401+
+#%#//scriptlet("pardot-1.0")
402+
```
403+
367404
## Version
368405

369406
This rule was added in AGLint version 4.0.0

src/rules/no-invalid-scriptlets.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export default defineRule({
120120
name: 'Too many parameters specified',
121121
code: '#%#//scriptlet("debug-on-property-read", "foo", "bar")',
122122
},
123+
{
124+
name: 'Almost correct scriptlet name, but misspelled',
125+
code: '#%#//scriptlet("pardot1.0")',
126+
},
123127
],
124128
version: '4.0.0',
125129
},
@@ -247,6 +251,8 @@ export default defineRule({
247251
},
248252
);
249253

254+
const actualQuote = QuoteUtils.getStringQuoteType(node.value);
255+
250256
context.report({
251257
messageId: 'unknownScriptlet',
252258
data: {
@@ -260,10 +266,10 @@ export default defineRule({
260266
suggestedScriptlet: match,
261267
},
262268
fix(fixer) {
263-
return fixer.replaceWithText([
264-
node.start!,
265-
node.end!,
266-
], match);
269+
return fixer.replaceWithText(
270+
[node.start!, node.end!],
271+
QuoteUtils.setStringQuoteType(match, actualQuote),
272+
);
267273
},
268274
node: scriptletRuleBody,
269275
})),

0 commit comments

Comments
 (0)