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

Combine severity types #288

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
9 changes: 3 additions & 6 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 {Severity} from './lib/index.js'

export interface RuleMeta {
/**
Expand All @@ -18,15 +18,12 @@ export interface RuleMeta {
export function lintRule<Tree extends Node = Node, Options = unknown>(
name: string | RuleMeta,
rule: Rule<Tree, Options>
): Plugin<
void[] | [Options | [boolean | Label | Severity, (Options | undefined)?]],
Tree
>
): Plugin<void[] | [Options | [Severity, (Options | undefined)?]], 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 {Severity} from './lib/index.js'
12 changes: 5 additions & 7 deletions packages/unified-lint-rule/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* @typedef {import('unist').Node} Node
* @typedef {import('vfile').VFile} VFile
*
* @typedef {0|1|2} Severity
* @typedef {'warn'|'on'|'off'|'error'} Label
* @typedef {[Severity, ...Array<unknown>]} SeverityTuple
* @typedef {boolean|'off'|'on'|'warn'|'error'|0|1|2} Severity
*
* @typedef RuleMeta
* @property {string} origin name of the lint rule
Expand Down Expand Up @@ -38,11 +36,11 @@ export function lintRule(meta, rule) {

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

if (!severity) return
if (!number) return

const fatal = severity === 2
const fatal = number === 2

return (tree, file, next) => {
let index = file.messages.length - 1
Expand Down Expand Up @@ -75,7 +73,7 @@ export function lintRule(meta, rule) {
*
* @param {string} name
* @param {unknown} config
* @returns {SeverityTuple}
* @returns {[Severity&number, ...Array<unknown>]}
*/
function coerce(name, config) {
if (!Array.isArray(config)) return [1, config]
Expand Down