@adguard/aglint
Version:
Universal adblock filter list linter.
109 lines (106 loc) • 3.21 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 merge from 'deepmerge';
import { optional, record, string, array, enums, boolean, object } from 'superstruct';
import { AdblockSyntax } from '@adguard/agtree';
import { linterRuleConfigSchema } from './rule.js';
/**
* Linter configuration
*/
/**
* Superstruct schema for the linter rules config object
*/
const linterRulesSchema = optional(record(string(), linterRuleConfigSchema));
/**
* Superstruct schema for the linter config object properties. It is necessary to
* separate this from the schema for the whole config object because we reuse it
* in the CLI config object.
*/
const linterConfigPropsSchema = {
root: optional(boolean()),
extends: optional(array(string())),
allowInlineConfig: optional(boolean()),
syntax: optional(array(enums([
AdblockSyntax.Common,
AdblockSyntax.Adg,
AdblockSyntax.Ubo,
AdblockSyntax.Abp,
]))),
rules: linterRulesSchema,
};
/**
* Superstruct schema for the linter rule config (used for validation)
*/
const linterConfigSchema = object(linterConfigPropsSchema);
/**
* Default linter configuration
*/
const defaultLinterConfig = {
allowInlineConfig: true,
syntax: [AdblockSyntax.Common],
};
/**
* Merges two configuration objects using deepmerge. Practically, this means that
* the `extend` object will be merged into the `initial` object.
*
* @param initial The initial config object
* @param extend The config object to extend the initial config with
* @returns The merged config object
* @example
* If you have the following config (called `initial` parameter):
* ```json
* {
* "syntax": ["Common"],
* "rules": {
* "rule1": "error",
* "rule2": "warn"
* }
* }
* ```
* And you want to extend it with the following config (called `extend` parameter):
* ```json
* {
* "syntax": ["AdGuard"],
* "rules": {
* "rule2": "off",
* },
* }
* ```
* The result will be:
* ```json
* {
* "syntax": ["AdGuard"],
* "rules": {
* "rule1": "error",
* "rule2": "off"
* }
* }
* ```
*/
function mergeConfigs(initial, extend) {
return merge(initial, extend, {
// https://github.com/TehShrike/deepmerge#options
arrayMerge: (_, sourceArray) => sourceArray,
});
}
/**
* Merges two configuration objects using deepmerge.merge().
* Practically, this means that the `extend` object will be merged into the `initial` object.
*
* It is very similar to {@link mergeConfigs|mergeConfigs()} function, but the order of parameters is reversed.
*
* @param extend The config object to extend the initial config with
* @param initial The initial config object
* @returns The merged config object
*/
function mergeConfigsReverse(extend, initial) {
return merge(extend, initial, {
// https://github.com/TehShrike/deepmerge#options
arrayMerge: (_, sourceArray) => sourceArray,
});
}
export { defaultLinterConfig, linterConfigPropsSchema, linterConfigSchema, linterRulesSchema, mergeConfigs, mergeConfigsReverse };