@adguard/aglint
Version:
Universal adblock filter list linter.
69 lines (66 loc) • 2.42 kB
JavaScript
/*
* AGLint v3.0.0 (build date: Wed, 21 May 2025 13:24:14 GMT)
* (c) 2025 AdGuard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/AGLint#readme
*/
import { object, array, string } from 'superstruct';
import { SEVERITY } from '../severity.js';
import { StringUtils } from '../../utils/string.js';
/**
* Fill this description with a short description of the rule.
*/
const NoExcludedRules = {
// Define the metadata of the rule
meta: {
severity: SEVERITY.error,
// TODO: Improve schema with transformation: https://github.com/AdguardTeam/AGLint/issues/217)
config: {
// Define the default config
default: {
// Define the default config values
'regexp-patterns': [],
},
// Define the schema of the config
schema: object({
'regexp-patterns': array(string()),
}),
},
},
// Define the events that the rule will listen to
events: {
onStartFilterList: (context) => {
// Each rule ONLY sees its own storage. At the beginning of the filter list,
// we just initialize the storage.
context.storage['regexp-patterns'] = [];
for (const rawPattern of context.config['regexp-patterns']) {
try {
let processedRawPattern = rawPattern;
if (StringUtils.isRegexPattern(rawPattern)) {
processedRawPattern = rawPattern.slice(1, -1);
}
context.storage['regexp-patterns'].push(new RegExp(processedRawPattern));
}
catch (error) {
// TODO: handle later
}
}
},
onRule: (context) => {
const { 'regexp-patterns': excludePatterns } = context.storage;
if (excludePatterns.length === 0) {
return;
}
const rawRuleText = context.getActualAdblockRuleRaw();
for (const pattern of excludePatterns) {
if (pattern.test(rawRuleText)) {
context.report({
message: `Rule matches an excluded pattern: ${pattern}`,
});
break;
}
}
},
},
};
export { NoExcludedRules };