@adguard/aglint
Version:
Universal adblock filter list linter.
50 lines (47 loc) • 2.02 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 { RuleCategory, CosmeticRuleType } from '@adguard/agtree';
import { SEVERITY } from '../severity.js';
import { isUndefined } from '../../utils/type-guards.js';
import { CssTreeParsingContext } from '../helpers/css-tree-types.js';
import { validateDeclaration } from '../helpers/css-validator.js';
/**
* Rule that checks if CSS declarations are valid
*/
const NoInvalidCssDeclaration = {
meta: {
severity: SEVERITY.error,
},
events: {
onRule: (context) => {
const ast = context.getActualAdblockRuleAst();
if (ast.category !== RuleCategory.Cosmetic || ast.type !== CosmeticRuleType.CssInjectionRule) {
return;
}
const rawDeclarationList = ast.body.declarationList;
if (ast.body.remove !== true && !isUndefined(rawDeclarationList)) {
const declarationListNode = context.getCssNode(rawDeclarationList, CssTreeParsingContext.DeclarationList);
if (!declarationListNode) {
return;
}
declarationListNode.children.forEach((declarationNode) => {
validateDeclaration(declarationNode).forEach((error) => {
context.report({
message: error.message,
// Note: DO NOT use `declarationNode` CSS node here, we should use the AGTree node instead.
// Error locations are relative to the AGTree node.
node: rawDeclarationList,
relativeNodeStartOffset: error.start,
relativeNodeEndOffset: error.end,
});
});
});
}
},
},
};
export { NoInvalidCssDeclaration };