Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-check lintRule() implementation #290

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/unified-lint-rule/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand All @@ -23,10 +23,4 @@ export function lintRule<Tree extends Node = Node, Options = unknown>(
Tree
>

export type Rule<Tree extends Node = Node, Options = unknown> = (
node: Tree,
file: VFile,
options: Options
) => Promise<Tree | undefined | void> | Tree | undefined | void

export {Severity, Label} from './lib/index.js'
export {Label, Rule, Severity} from './lib/index.js'
33 changes: 19 additions & 14 deletions packages/unified-lint-rule/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
*
* @typedef {0|1|2} Severity
* @typedef {'warn'|'on'|'off'|'error'} Label
* @typedef {[Severity, ...Array<unknown>]} 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<Tree, Options>} rule
*/
export function lintRule(meta, rule) {
const id = typeof meta === 'string' ? meta : meta.origin
Expand All @@ -36,7 +41,7 @@ export function lintRule(meta, rule) {

return plugin

/** @type {import('unified').Plugin<[unknown]|Array<void>>} */
/** @type {import('unified').Plugin<Array<void>|[Options|[boolean|Label|Severity, (Options|undefined)?]]>} */
function plugin(config) {
const [severity, options] = coerce(ruleId, config)

Expand Down Expand Up @@ -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<unknown>} */
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 +
Expand Down