unified-lint-rule
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -1,34 +0,9 @@ | ||
import type {Node} from 'unist' | ||
import type {VFile} from 'vfile' | ||
import type {Plugin} from 'unified' | ||
import type {Label, Severity} from './lib/index.js' | ||
export interface RuleMeta { | ||
/** | ||
* Name of the lint rule | ||
*/ | ||
origin: string | ||
/** | ||
* Link to documentation | ||
*/ | ||
url?: string | undefined | ||
} | ||
export function lintRule<Tree extends Node = Node, Settings = unknown>( | ||
name: string | RuleMeta, | ||
rule: Rule<Tree, Settings> | ||
): Plugin< | ||
| void[] | ||
| [Settings | Label | Severity] | ||
| [boolean | Label | Severity, Settings], | ||
Tree | ||
> | ||
export type Rule<Tree extends Node = Node, Settings = unknown> = ( | ||
node: Tree, | ||
file: VFile, | ||
settings: Settings | ||
) => Promise<Tree | undefined | void> | Tree | undefined | void | ||
export {Severity, Label} from './lib/index.js' | ||
export {lintRule} from './lib/index.js' | ||
export type Node = import('unist').Node | ||
export type RuleMeta = import('./lib/index.js').RuleMeta | ||
export type Rule< | ||
Tree extends import('unist').Node< | ||
import('unist').Data | ||
> = import('unist').Node<import('unist').Data>, | ||
Options extends unknown = unknown | ||
> = import('./lib/index.js').Rule<Tree, Options> |
14
index.js
@@ -0,1 +1,15 @@ | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
*/ | ||
/** | ||
* @typedef {import('./lib/index.js').RuleMeta} RuleMeta | ||
*/ | ||
/** | ||
* @template {Node} [Tree=Node] | ||
* @template {any} [Options=unknown] | ||
* @typedef {import('./lib/index.js').Rule<Tree, Options>} Rule | ||
*/ | ||
export {lintRule} from './lib/index.js' |
/** | ||
* @param {string|RuleMeta} meta | ||
* @param {Rule} rule | ||
* @template {Node} [Tree=Node] | ||
* @template {any} [Options=unknown] | ||
* @param {string | RuleMeta} meta | ||
* @param {Rule<Tree, Options>} rule | ||
* @returns {import('unified').Plugin<Array<void> | [Options | [boolean | Label | Severity, (Options | undefined)?]], Tree>} | ||
*/ | ||
export function lintRule( | ||
export function lintRule< | ||
Tree extends import('unist').Node< | ||
import('unist').Data | ||
> = import('unist').Node<import('unist').Data>, | ||
Options extends unknown = unknown | ||
>( | ||
meta: string | RuleMeta, | ||
rule: Rule | ||
): { | ||
(raw: unknown): | ||
| void | ||
| import('unified').Transformer< | ||
import('unist').Node<import('unist').Data>, | ||
import('unist').Node<import('unist').Data> | ||
> | ||
readonly name: string | ||
} | ||
rule: Rule<Tree, Options> | ||
): import('unified').Plugin< | ||
void[] | [Options | [boolean | Label | Severity, (Options | undefined)?]], | ||
Tree, | ||
Tree | ||
> | ||
export type Node = import('unist').Node | ||
export type VFile = import('vfile').VFile | ||
/** | ||
* Numeric severity (`0`: `'off'`, `1`: `'on'`, `2`: `'error'`). | ||
*/ | ||
export type Severity = 0 | 1 | 2 | ||
/** | ||
* Severity label (`'off'`: `0`, `'on'`: `1`, `'error'`: `2`). | ||
*/ | ||
export type Label = 'warn' | 'on' | 'off' | 'error' | ||
/** | ||
* Parsed severty and options. | ||
*/ | ||
export type SeverityTuple = [Severity, ...Array<unknown>] | ||
/** | ||
* Rule metadata. | ||
*/ | ||
export type RuleMeta = { | ||
/** | ||
* name of the lint rule | ||
* Name of the lint rule. | ||
*/ | ||
origin: string | ||
/** | ||
* link to documentation | ||
* Link to documentation. | ||
*/ | ||
url?: string | undefined | ||
url?: string | null | undefined | ||
} | ||
export type Rule = (tree: Node, file: VFile, options: unknown) => void | ||
export type Rule< | ||
Tree extends import('unist').Node< | ||
import('unist').Data | ||
> = import('unist').Node<import('unist').Data>, | ||
Options extends unknown = unknown | ||
> = ( | ||
tree: Tree, | ||
file: VFile, | ||
options: Options | ||
) => Promise<Tree | undefined | void> | Tree | undefined | void |
124
lib/index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('vfile').VFile} VFile | ||
* | ||
* @typedef {0|1|2} Severity | ||
* @typedef {'warn'|'on'|'off'|'error'} Label | ||
*/ | ||
/** | ||
* @typedef {0 | 1 | 2} Severity | ||
* Numeric severity (`0`: `'off'`, `1`: `'on'`, `2`: `'error'`). | ||
* @typedef {'warn' | 'on' | 'off' | 'error'} Label | ||
* Severity label (`'off'`: `0`, `'on'`: `1`, `'error'`: `2`). | ||
* @typedef {[Severity, ...Array<unknown>]} SeverityTuple | ||
* Parsed severty and options. | ||
* | ||
* @typedef RuleMeta | ||
* @property {string} origin name of the lint rule | ||
* @property {string} [url] link to documentation | ||
* | ||
* Rule metadata. | ||
* @property {string} origin | ||
* Name of the lint rule. | ||
* @property {string | null | undefined} [url] | ||
* Link to documentation. | ||
*/ | ||
/** | ||
* @template {Node} [Tree=Node] | ||
* @template {any} [Options=unknown] | ||
* @callback Rule | ||
* @param {Node} tree | ||
* @param {Tree} tree | ||
* @param {VFile} file | ||
* @param {unknown} options | ||
* @returns {void} | ||
* @param {Options} options | ||
* @returns {Promise<Tree | undefined | void> | Tree | undefined | void} | ||
*/ | ||
@@ -22,7 +34,8 @@ | ||
const primitives = new Set(['string', 'number', 'boolean']) | ||
/** | ||
* @param {string|RuleMeta} meta | ||
* @param {Rule} rule | ||
* @template {Node} [Tree=Node] | ||
* @template {any} [Options=unknown] | ||
* @param {string | RuleMeta} meta | ||
* @param {Rule<Tree, Options>} rule | ||
* @returns {import('unified').Plugin<Array<void> | [Options | [boolean | Label | Severity, (Options | undefined)?]], Tree>} | ||
*/ | ||
@@ -40,7 +53,8 @@ export function lintRule(meta, rule) { | ||
// @ts-expect-error: to do: fix. | ||
return plugin | ||
/** @type {import('unified').Plugin<[unknown]|Array<void>>} */ | ||
function plugin(raw) { | ||
const [severity, options] = coerce(ruleId, raw) | ||
/** @type {import('unified').Plugin<[unknown] | Array<void>>} */ | ||
function plugin(config) { | ||
const [severity, options] = coerce(ruleId, config) | ||
@@ -81,58 +95,40 @@ if (!severity) return | ||
* @param {string} name | ||
* @param {unknown} value | ||
* @param {unknown} config | ||
* @returns {SeverityTuple} | ||
*/ | ||
function coerce(name, value) { | ||
function coerce(name, config) { | ||
if (!Array.isArray(config)) return [1, config] | ||
/** @type {Array<unknown>} */ | ||
let result | ||
const [severity, ...options] = config | ||
switch (severity) { | ||
case false: | ||
case 'off': | ||
case 0: { | ||
return [0, ...options] | ||
} | ||
if (typeof value === 'boolean') { | ||
result = [value] | ||
} else if (value === null || value === undefined) { | ||
result = [1] | ||
} else if ( | ||
Array.isArray(value) && | ||
// `isArray(unknown)` is turned into `Array<any>`: | ||
// type-coverage:ignore-next-line | ||
primitives.has(typeof value[0]) | ||
) { | ||
// `isArray(unknown)` is turned into `Array<any>`: | ||
// type-coverage:ignore-next-line | ||
result = [...value] | ||
} else { | ||
result = [1, value] | ||
} | ||
case true: | ||
case 'on': | ||
case 'warn': | ||
case 1: { | ||
return [1, ...options] | ||
} | ||
let level = result[0] | ||
case 'error': | ||
case 2: { | ||
return [2, ...options] | ||
} | ||
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] | ||
default: { | ||
if (typeof severity !== 'number') return [1, config] | ||
throw new Error( | ||
'Incorrect severity `' + | ||
severity + | ||
'` for `' + | ||
name + | ||
'`, ' + | ||
'expected 0, 1, or 2' | ||
) | ||
} | ||
} | ||
if (typeof level !== 'number' || level < 0 || level > 2) { | ||
throw new Error( | ||
'Incorrect severity `' + | ||
level + | ||
'` for `' + | ||
name + | ||
'`, ' + | ||
'expected 0, 1, or 2' | ||
) | ||
} | ||
result[0] = level | ||
// @ts-expect-error: it’s now a valid tuple. | ||
return result | ||
} |
{ | ||
"name": "unified-lint-rule", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "unified plugin to make it a bit easier to create linting rules", | ||
@@ -42,5 +42,3 @@ "license": "MIT", | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"lib/**/*.d.ts\" && tsc && type-coverage" | ||
}, | ||
"scripts": {}, | ||
"xo": false, | ||
@@ -47,0 +45,0 @@ "typeCoverage": { |
@@ -49,13 +49,13 @@ # unified-lint-rule | ||
In Deno with [Skypack][]: | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {lintRule} from 'https://cdn.skypack.dev/unified-lint-rule@2?dts' | ||
import {lintRule} from 'https://esm.sh/unified-lint-rule@2' | ||
``` | ||
In browsers with [Skypack][]: | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {lintRule} from 'https://cdn.skypack.dev/unified-lint-rule@2?min' | ||
import {lintRule} from 'https://esm.sh/unified-lint-rule@2?bundle' | ||
</script> | ||
@@ -161,3 +161,3 @@ ``` | ||
[skypack]: https://www.skypack.dev | ||
[esmsh]: https://esm.sh | ||
@@ -164,0 +164,0 @@ [npm]: https://docs.npmjs.com/cli/install |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11317
191
1