diff --git a/packages/unified-lint-rule/index.d.ts b/packages/unified-lint-rule/index.d.ts index 326df65d..a5f7ee48 100644 --- a/packages/unified-lint-rule/index.d.ts +++ b/packages/unified-lint-rule/index.d.ts @@ -1,7 +1,7 @@ import type {Node} from 'unist' import type {VFile} from 'vfile' import type {Plugin} from 'unified' -import type {Label, Severity} from './lib/index.js' +import type {Label, Rule, Severity} from './lib/index.js' export interface RuleMeta { /** @@ -23,10 +23,4 @@ export function lintRule( Tree > -export type Rule = ( - node: Tree, - file: VFile, - options: Options -) => Promise | Tree | undefined | void - -export {Severity, Label} from './lib/index.js' +export {Label, Rule, Severity} from './lib/index.js' diff --git a/packages/unified-lint-rule/lib/index.js b/packages/unified-lint-rule/lib/index.js index 4c5c2be3..310b7d78 100644 --- a/packages/unified-lint-rule/lib/index.js +++ b/packages/unified-lint-rule/lib/index.js @@ -4,24 +4,29 @@ * * @typedef {0|1|2} Severity * @typedef {'warn'|'on'|'off'|'error'} Label - * @typedef {[Severity, ...Array]} SeverityTuple * * @typedef RuleMeta * @property {string} origin name of the lint rule * @property {string} [url] link to documentation - * + */ + +/** + * @template {Node} Tree + * @template Options * @callback Rule - * @param {Node} tree + * @param {Tree} tree * @param {VFile} file - * @param {unknown} options + * @param {Options} options * @returns {void} */ import {wrap} from 'trough' /** + * @template {Node} Tree + * @template Options * @param {string|RuleMeta} meta - * @param {Rule} rule + * @param {Rule} rule */ export function lintRule(meta, rule) { const id = typeof meta === 'string' ? meta : meta.origin @@ -36,7 +41,7 @@ export function lintRule(meta, rule) { return plugin - /** @type {import('unified').Plugin<[unknown]|Array>} */ + /** @type {import('unified').Plugin|[Options|[boolean|Label|Severity, (Options|undefined)?]]>} */ function plugin(config) { const [severity, options] = coerce(ruleId, config) @@ -73,29 +78,29 @@ export function lintRule(meta, rule) { /** * Coerce a value to a severity--options tuple. * + * @template Options * @param {string} name - * @param {unknown} config - * @returns {SeverityTuple} + * @param {Options|[boolean|Label|Severity, (Options|undefined)?]} config + * @returns {[Severity, Options|undefined]} */ function coerce(name, config) { if (!Array.isArray(config)) return [1, config] - /** @type {Array} */ - const [severity, ...options] = config + const [severity, options] = config switch (severity) { case false: case 'off': case 0: - return [0, ...options] + return [0, options] case true: case 'on': case 'warn': case 1: - return [1, ...options] + return [1, options] case 'error': case 2: - return [2, ...options] + return [2, options] default: - if (typeof severity !== 'number') return [1, config] + if (typeof severity !== 'number') return [1, /** @type {never} */ (config)] throw new Error( 'Incorrect severity `' + severity +