@adguard/aglint
Version:
Universal adblock filter list linter.
66 lines (63 loc) • 2.33 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 { property, lexer } from '@adguard/ecss-tree';
import { isCssTreeSyntaxError } from './css-errors.js';
import { isString } from '../../utils/type-guards.js';
import { getCssTreeStartAndEndOffsetsFromObject } from './css-loc-extractor.js';
/**
* Helper function to validate CSS declaration.
* Idea from https://github.com/csstree/validator/blob/38e7319d7b049c190f04665df77c836646f3b35e/lib/validate.js#L99-L121
*
* @param declarationNode - CSS declaration node to validate.
*
* @returns Array of syntax errors.
*/
const validateDeclaration = (declarationNode) => {
const errors = [];
const { property: property$1, value } = declarationNode;
// Ignore custom properties, like `--foo`
if (property(property$1).custom) {
return errors;
}
let possibleError;
// TODO: Improve CSSTree typing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
possibleError = lexer.checkPropertyName(property$1);
if (isCssTreeSyntaxError(possibleError)) {
errors.push({
message: possibleError.message,
start: declarationNode.loc?.start.offset,
end: declarationNode.loc?.end.offset,
});
}
else {
possibleError = lexer.matchProperty(property$1, value).error;
if (isCssTreeSyntaxError(possibleError)) {
let message = `Invalid value for '${property$1}' property`;
if ('syntax' in possibleError && isString(possibleError.syntax)) {
message += `, mismatch with syntax ${possibleError.syntax}`;
}
let start;
let end;
const possibleErrorOffsets = getCssTreeStartAndEndOffsetsFromObject(possibleError);
if (!possibleErrorOffsets) {
start = declarationNode.loc?.start.offset;
end = declarationNode.loc?.end.offset;
}
else {
[start, end] = possibleErrorOffsets;
}
errors.push({
message,
start,
end,
});
}
}
return errors;
};
export { validateDeclaration };