Skip to content

Commit

Permalink
Refactor to improve internal severity coercion
Browse files Browse the repository at this point in the history
Closes GH-287.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
jablko committed Apr 4, 2022
1 parent d1e24ff commit 29bb3b7
Showing 1 changed file with 25 additions and 50 deletions.
75 changes: 25 additions & 50 deletions packages/unified-lint-rule/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import {wrap} from 'trough'

const primitives = new Set(['string', 'number', 'boolean'])

/**
* @param {string|RuleMeta} meta
* @param {Rule} rule
Expand Down Expand Up @@ -80,54 +78,31 @@ export function lintRule(meta, rule) {
* @returns {SeverityTuple}
*/
function coerce(name, config) {
if (!Array.isArray(config)) return [1, config]
/** @type {Array<unknown>} */
let result

if (config === null || config === undefined) {
result = [1]
} else if (
Array.isArray(config) &&
// `isArray(unknown)` is turned into `Array<any>`:
// type-coverage:ignore-next-line
primitives.has(typeof config[0])
) {
// `isArray(unknown)` is turned into `Array<any>`:
// type-coverage:ignore-next-line
result = [...config]
} else {
result = [1, config]
}

let level = result[0]

if (typeof level === 'boolean') {
level = level ? 1 : 0
} else if (typeof level === 'string') {
if (level === 'off') {
level = 0
} else if (level === 'on' || level === 'warn') {
level = 1
} else if (level === 'error') {
level = 2
} else {
level = 1
result = [level, result]
}
}

if (typeof level !== 'number' || level < 0 || level > 2) {
throw new Error(
'Incorrect severity `' +
level +
'` for `' +
name +
'`, ' +
'expected 0, 1, or 2'
)
const [severity, ...options] = config
switch (severity) {
case false:
case 'off':
case 0:
return [0, ...options]
case true:
case 'on':
case 'warn':
case 1:
return [1, ...options]
case 'error':
case 2:
return [2, ...options]
default:
if (typeof severity !== 'number') return [1, config]
throw new Error(
'Incorrect severity `' +
severity +
'` for `' +
name +
'`, ' +
'expected 0, 1, or 2'
)
}

result[0] = level

// @ts-expect-error: it’s now a valid tuple.
return result
}

0 comments on commit 29bb3b7

Please sign in to comment.