UNPKG

@adguard/aglint

Version:

Universal adblock filter list linter.

98 lines (95 loc) 2.77 kB
/* * AGLint v3.0.2 (build date: Mon, 08 Dec 2025 08:25:49 GMT) * (c) 2025 AdGuard Software Ltd. * Released under the MIT license * https://github.com/AdguardTeam/AGLint#readme */ import { define } from 'superstruct'; /** * Represents the possible severities of a linter rule. */ const SEVERITY = Object.freeze({ /** * Rule is disabled, nothing happens. */ off: 0, /** * Rule is enabled, and throws a warning when violated. * * @example Bad practices, deprecated syntax, formatting issues, redundant rules, etc. */ warn: 1, /** * Rule is enabled, and throws an error when violated. * * @example Unknown scriptlets, unknown modifiers, etc. */ error: 2, /** * Rule is enabled, and throws a fatal error when violated. * * @example Syntax error (parsing error) */ fatal: 3, }); /** * Names of the possible severities. */ const SEVERITY_NAMES = Object.freeze(Object.keys(SEVERITY)); /** * Values of the possible severities. */ const SEVERITY_VALUES = Object.freeze(Object.values(SEVERITY)); /** * Always returns the severity value. Typically used to get the severity value from a string. * * @param value The value to get the severity value from. * * @returns The severity value. */ function getSeverity(value) { if (typeof value === 'string') { return SEVERITY[value]; } return value; } /** * Checks whether the given value is a valid severity. * * @param value The value to check. * * @returns Whether the value is a valid severity. */ function isSeverity(value) { if (typeof value === 'string') { return SEVERITY_NAMES.includes(value); } if (typeof value === 'number') { return SEVERITY_VALUES.includes(value); } return false; } /** * Superstruct type definition for the linter rule severity. * * @see {@link https://github.com/ianstormtaylor/superstruct/blob/main/src/structs/types.ts} * * @returns Defined struct. */ function severity() { // TODO: Fix possible type error // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore 2322 return define('severity', (value) => { if (typeof value === 'string') { return (SEVERITY_NAMES.includes(value) || `Expected a valid severity string (${SEVERITY_NAMES.join(', ')}), but received ${value}`); } if (typeof value === 'number') { return (SEVERITY_VALUES.includes(value) || `Expected a valid severity number, but received ${value}`); } return `Expected a string or number, but received ${typeof value}`; }); } export { SEVERITY, SEVERITY_NAMES, SEVERITY_VALUES, getSeverity, isSeverity, severity };