Skip to content

Commit

Permalink
Combine label and numeric severity types
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Apr 1, 2022
1 parent a9ef208 commit fd894ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
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 | Label | Severity] | [boolean | Label | Severity, Options],
Tree
>
): Plugin<void[] | [Options | Severity] | [boolean | Severity, Options], 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 {'off'|'on'|'warn'|'error'|0|1|2} Severity
*
* @typedef RuleMeta
* @property {string} origin name of the lint rule
Expand Down Expand Up @@ -40,11 +38,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 @@ -77,7 +75,7 @@ export function lintRule(meta, rule) {
*
* @param {string} name
* @param {unknown} config
* @returns {SeverityTuple}
* @returns {[Severity&number, ...Array<unknown>]}
*/
function coerce(name, config) {
/** @type {Array<unknown>} */
Expand Down

0 comments on commit fd894ea

Please sign in to comment.