Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unified-lint-rule

Package Overview
Dependencies
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unified-lint-rule - npm Package Compare versions

Comparing version 2.1.2 to 3.0.0

index.d.ts.map

16

index.d.ts

@@ -1,9 +0,7 @@

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>
export { lintRule } from "./lib/index.js";
export type Node = import('unist').Node;
export type Label = import('./lib/index.js').Label;
export type Meta = import('./lib/index.js').Meta;
export type Severity = import('./lib/index.js').Severity;
export type Rule<Tree extends import("unist").Node = import("unist").Node, Options extends unknown = unknown> = import('./lib/index.js').Rule<Tree, Options>;
//# sourceMappingURL=index.d.ts.map
/**
* unified helper to help make lint rules.
*
* ## What is this?
*
* This package is a helper that makes it a bit easier to create linting rules.
*
* ## When should I use this?
*
* You can use this package when you want to make custom lint rules.
*
* ## Use
*
* ```js
* import {lintRule} from 'unified-lint-rule'
*
* const remarkLintFileExtension = lintRule(
* 'remark-lint:file-extension',
* function (tree, file, options) {
* const ext = file.extname
* const option = options || 'md'
*
* if (ext && ext.slice(1) !== option) {
* file.message('Incorrect extension: use `' + option + '`')
* }
* }
* )
*
* export default remarkLintFileExtension
* ```
*
* ## API
*
* ### `lintRule(meta, rule)`
*
* Create a plugin.
*
* ###### Parameters
*
* * `meta` ([`Meta`][api-meta] or `string`)
* — info or origin
* * `rule` ([`Rule`][api-rule])
* — rule
*
* ###### Returns
*
* Plugin ([`Plugin` from `unified`][github-unified-plugin]).
*
* ### `Label`
*
* Severity label (TypeScript type);
* `'off'`: `0`, `'on'` and `warn`: `1`, `'error'`: `2`.
*
* ###### Type
*
* ```ts
* type Label = 'error' | 'on' | 'off' | 'warn'
* ```
*
* ### `Meta`
*
* Rule metadata (TypeScript type).
*
* ###### Fields
*
* * `origin` (`string`)
* — name of the lint rule
* * `url` (`string`, optional)
* — link to documentation
*
* ### `Rule`
*
* Rule (TypeScript type).
*
* ###### Parameters
*
* * `tree` ([`Node` from `unist`][github-unist-node])
* — tree
* * `file` ([`VFile`][github-vfile])
* — file
* * `options` (`any`, optional)
* — parameter
*
* ###### Returns
*
* Nothing (`Promise<undefined>` or `undefined`).
*
* ### `Severity`
*
* Severity number (TypeScript type);
* `0`: `'off'`, `1`: `'on'` and `warn`, `2`: `'error'`.
*
* ###### Type
*
* ```ts
* type Severity = 0 | 1 | 2
* ```
*
* [api-label]: #label
* [api-meta]: #meta
* [api-rule]: #rule
* [api-severity]: #severity
* [api-lint-rule]: #lintrulemeta-rule
* [github-unist-node]: https://github.com/syntax-tree/unist#node
* [github-unified-plugin]: https://github.com/unifiedjs/unified#plugin
* [github-vfile]: https://github.com/vfile/vfile
*/
/**
* @typedef {import('unist').Node} Node

@@ -6,3 +114,5 @@ */

/**
* @typedef {import('./lib/index.js').RuleMeta} RuleMeta
* @typedef {import('./lib/index.js').Label} Label
* @typedef {import('./lib/index.js').Meta} Meta
* @typedef {import('./lib/index.js').Severity} Severity
*/

@@ -9,0 +119,0 @@

/**
* @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>}
* Node kind.
* @template {any} [Option=unknown]
* Parameter kind.
* @param {Meta | string} meta
* Info.
* @param {Rule<Tree, Option>} rule
* Rule.
* @returns
* Plugin.
*/
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<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
export function lintRule<Tree extends import("unist").Node = import("unist").Node, Option extends unknown = unknown>(meta: Meta | string, rule: Rule<Tree, Option>): {
(config?: Label | Severity | Option | [level: Label | Severity, option?: Option] | undefined): ((tree: Tree, file: VFile, next: import("unified").TransformCallback<Tree>) => undefined) | undefined;
readonly name: string;
};
export type Node = import('unist').Node;
export type VFile = import('vfile').VFile;
/**
* Numeric severity (`0`: `'off'`, `1`: `'on'`, `2`: `'error'`).
* Severity label;
* `'off'`: `0`, `'on'` and `warn`: `1`, `'error'`: `2`.
*/
export type Severity = 0 | 1 | 2
export type Label = 'error' | 'on' | 'off' | 'warn';
/**
* Severity label (`'off'`: `0`, `'on'`: `1`, `'error'`: `2`).
* Rule metadata.
*/
export type Label = 'warn' | 'on' | 'off' | 'error'
export type Meta = {
/**
* Name of the lint rule.
*/
origin: string;
/**
* Link to documentation (optional).
*/
url?: string | null | undefined;
};
/**
* Severity number;
* `0`: `'off'`, `1`: `'on'` and `warn`, `2`: `'error'`.
*/
export type Severity = 0 | 1 | 2;
/**
* Parsed severty and options.
*/
export type SeverityTuple = [Severity, ...Array<unknown>]
export type SeverityTuple = [severity: Severity, ...parameters: Array<unknown>];
/**
* Rule metadata.
* Rule.
*/
export type RuleMeta = {
/**
* Name of the lint rule.
*/
origin: string
/**
* Link to documentation.
*/
url?: string | null | undefined
}
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
export type Rule<Tree extends import("unist").Node = import("unist").Node, Option extends unknown = unknown> = (tree: Tree, file: VFile, option: Option) => Promise<undefined | void> | undefined | void;
//# sourceMappingURL=index.d.ts.map

@@ -7,10 +7,7 @@ /**

/**
* @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 {'error' | 'on' | 'off' | 'warn'} Label
* Severity label;
* `'off'`: `0`, `'on'` and `warn`: `1`, `'error'`: `2`.
*
* @typedef RuleMeta
* @typedef Meta
* Rule metadata.

@@ -20,3 +17,10 @@ * @property {string} origin

* @property {string | null | undefined} [url]
* Link to documentation.
* Link to documentation (optional).
*
* @typedef {0 | 1 | 2} Severity
* Severity number;
* `0`: `'off'`, `1`: `'on'` and `warn`, `2`: `'error'`.
*
* @typedef {[severity: Severity, ...parameters: Array<unknown>]} SeverityTuple
* Parsed severty and options.
*/

@@ -26,8 +30,15 @@

* @template {Node} [Tree=Node]
* @template {any} [Options=unknown]
* Node kind (optional).
* @template {any} [Option=unknown]
* Parameter kind (optional).
* @callback Rule
* Rule.
* @param {Tree} tree
* Tree.
* @param {VFile} file
* @param {Options} options
* @returns {Promise<Tree | undefined | void> | Tree | undefined | void}
* File.
* @param {Option} option
* Parameter.
* @returns {Promise<undefined | void> | undefined | void}
* Nothing.
*/

@@ -39,6 +50,11 @@

* @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>}
* Node kind.
* @template {any} [Option=unknown]
* Parameter kind.
* @param {Meta | string} meta
* Info.
* @param {Rule<Tree, Option>} rule
* Rule.
* @returns
* Plugin.
*/

@@ -49,4 +65,3 @@ export function lintRule(meta, rule) {

const parts = id.split(':')
// Possibly useful if externalised later.
/* c8 ignore next */
/* c8 ignore next -- Possibly useful if externalised later. */
const source = parts[1] ? parts[0] : undefined

@@ -57,22 +72,36 @@ const ruleId = parts[1]

// @ts-expect-error: to do: fix.
return plugin
/** @type {import('unified').Plugin<[unknown] | Array<void>>} */
/**
* @param {[level: Label | Severity, option?: Option] | Label | Option | Severity} [config]
* Config.
* @returns
* Transform, if on.
*/
function plugin(config) {
const [severity, options] = coerce(ruleId, config)
const fatal = severity === 2
if (!severity) return
const fatal = severity === 2
return (tree, file, next) => {
/**
* @param {Tree} tree
* Tree.
* @param {VFile} file
* File.
* @param {import('unified').TransformCallback<Tree>} next
* Next.
* @returns {undefined}
* Nothing.
*/
return function (tree, file, next) {
let index = file.messages.length - 1
wrap(rule, (error) => {
wrap(rule, function (error) {
const messages = file.messages
// Add the error, if not already properly added.
// Only happens for incorrect plugins.
/* c8 ignore next 6 */
/* c8 ignore next 8 -- add the error,
* if not already properly added.
* Only happens for incorrect plugins. */
// @ts-expect-error: errors could be `messages`.

@@ -86,3 +115,3 @@ if (error && !messages.includes(error)) {

while (++index < messages.length) {
Object.assign(messages[index], {ruleId, source, fatal, url})
Object.assign(messages[index], {fatal, ruleId, source, url})
}

@@ -100,7 +129,13 @@

* @param {string} name
* Rule name.
* @param {unknown} config
* Configuration.
* @returns {SeverityTuple}
* Severity and options.
*/
function coerce(name, config) {
if (!Array.isArray(config)) return [1, config]
if (!Array.isArray(config)) {
return [1, config]
}
/** @type {Array<unknown>} */

@@ -110,4 +145,4 @@ const [severity, ...options] = config

case false:
case 'off':
case 0: {
case 0:
case 'off': {
return [0, ...options]

@@ -117,10 +152,10 @@ }

case true:
case 1:
case 'on':
case 'warn':
case 1: {
case 'warn': {
return [1, ...options]
}
case 'error':
case 2: {
case 2:
case 'error': {
return [2, ...options]

@@ -130,3 +165,6 @@ }

default: {
if (typeof severity !== 'number') return [1, config]
if (typeof severity !== 'number') {
return [1, config]
}
throw new Error(

@@ -133,0 +171,0 @@ 'Incorrect severity `' +

{
"name": "unified-lint-rule",
"version": "2.1.2",
"version": "3.0.0",
"description": "unified plugin to make it a bit easier to create linting rules",
"license": "MIT",
"keywords": [
"lint",
"plugin",
"remark-lint",
"rule",
"unified",
"unified-plugin",
"plugin",
"lint",
"rule"
"unified-plugin"
],
"repository": {
"type": "git",
"url": "https://github.com/remarkjs/remark-lint",
"directory": "packages/unified-lint-rule"
},
"repository": "https://github.com/remarkjs/remark-lint/tree/main/packages/unified-lint-rule",
"bugs": "https://github.com/remarkjs/remark-lint/issues",

@@ -25,27 +22,32 @@ "funding": {

"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
"Titus Wormer <tituswormer@gmail.com>"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts",
"index.js"
"index.d.ts.map",
"index.js",
"lib/"
],
"dependencies": {
"@types/unist": "^2.0.0",
"@types/unist": "^3.0.0",
"trough": "^2.0.0",
"unified": "^10.0.0",
"vfile": "^5.0.0"
"unified": "^11.0.0",
"vfile": "^6.0.0"
},
"scripts": {},
"xo": false,
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
"ignoreCatch": true,
"strict": true
},
"xo": {
"prettier": true,
"rules": {
"capitalized-comments": "off"
}
}
}

@@ -0,36 +1,35 @@

<!--This file is generated-->
# unified-lint-rule
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[![Sponsors][badge-funding-sponsors-image]][badge-funding-url]
[![Backers][badge-funding-backers-image]][badge-funding-url]
[![Chat][badge-chat-image]][badge-chat-url]
**[unified][]** plugin to help make lint rules.
**[unified][github-unified]** helper to help make lint rules.
See the [monorepo readme][mono] for more info on remark lint.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`lintRule(origin|meta, rule)`](#lintruleoriginmeta-rule)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`lintRule(meta, rule)`](#lintrulemeta-rule)
* [`Label`](#label)
* [`Meta`](#meta)
* [`Rule`](#rule)
* [`Severity`](#severity)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a [unified][] plugin that makes it a bit easier to create
linting rules.
This package is a helper that makes it a bit easier to create linting rules.
**unified** is a project that transforms content with abstract syntax trees
(ASTs).
This is a plugin that make it easier to inspect trees.
## When should I use this?

@@ -42,4 +41,5 @@

This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npm-install]:

@@ -50,13 +50,13 @@ ```sh

In Deno with [`esm.sh`][esmsh]:
In Deno with [`esm.sh`][esm-sh]:
```js
import {lintRule} from 'https://esm.sh/unified-lint-rule@2'
import {lintRule} from 'https://esm.sh/unified-lint-rule@3'
```
In browsers with [`esm.sh`][esmsh]:
In browsers with [`esm.sh`][esm-sh]:
```html
<script type="module">
import {lintRule} from 'https://esm.sh/unified-lint-rule@2?bundle'
import {lintRule} from 'https://esm.sh/unified-lint-rule@3?bundle'
</script>

@@ -72,4 +72,5 @@ ```

'remark-lint:file-extension',
(tree, file, option = 'md') => {
var ext = file.extname
function (tree, file, options) {
const ext = file.extname
const option = options || 'md'

@@ -87,6 +88,11 @@ if (ext && ext.slice(1) !== option) {

This package exports the following identifier: `lintRule`.
This package exports the identifier
[`lintRule`][api-lint-rule].
It exports the [TypeScript][typescript] types
[`Label`][api-label],
[`Meta`][api-meta], and
[`Severity`][api-severity].
There is no default export.
### `lintRule(origin|meta, rule)`
### `lintRule(meta, rule)`

@@ -97,34 +103,79 @@ Create a plugin.

* `origin` (`string`)
— treated as a `meta` of `{origin}`
* `meta` (`Object`)
— rule metadata
* `meta.origin` (`string`)
— message origin, either a rule name (`'file-extension'`) or both
a rule source and name joined with `:` (`'remark-lint:file-extension'`)
* `meta.url` (`string`, optional)
— URL to documentation for messages
* `rule` (`Function`, optional)
— your code, like a transform function, except that an extra `option` is
passed
* `meta` ([`Meta`][api-meta] or `string`)
— info or origin
* `rule` ([`Rule`][api-rule])
— rule
###### Returns
A unified plugin that handles all kinds of options (see [Configure][configure]
in the monorepo readme for how them).
Plugin ([`Plugin` from `unified`][github-unified-plugin]).
### `Label`
Severity label (TypeScript type);
`'off'`: `0`, `'on'` and `warn`: `1`, `'error'`: `2`.
###### Type
```ts
type Label = 'error' | 'on' | 'off' | 'warn'
```
### `Meta`
Rule metadata (TypeScript type).
###### Fields
* `origin` (`string`)
— name of the lint rule
* `url` (`string`, optional)
— link to documentation
### `Rule`
Rule (TypeScript type).
###### Parameters
* `tree` ([`Node` from `unist`][github-unist-node])
— tree
* `file` ([`VFile`][github-vfile])
— file
* `options` (`any`, optional)
— parameter
###### Returns
Nothing (`Promise<undefined>` or `undefined`).
### `Severity`
Severity number (TypeScript type);
`0`: `'off'`, `1`: `'on'` and `warn`, `2`: `'error'`.
###### Type
```ts
type Severity = 0 | 1 | 2
```
## Compatibility
Projects maintained by the unified collective are compatible with all maintained
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
`unified-lint-rule@3`,
compatible with Node.js 16.
## Contribute
See [`contributing.md`][contributing] in [`remarkjs/.github`][health] for ways
See [`contributing.md`][github-dotfiles-contributing] in [`remarkjs/.github`][github-dotfiles-health] for ways
to get started.
See [`support.md`][support] for ways to get help.
See [`support.md`][github-dotfiles-support] for ways to get help.
This project has a [code of conduct][coc].
This project has a [code of conduct][github-dotfiles-coc].
By interacting with this repository, organization, or community you agree to

@@ -135,52 +186,66 @@ abide by its terms.

[MIT][license] © [Titus Wormer][author]
[MIT][file-license] © [Titus Wormer][author]
[build-badge]: https://github.com/remarkjs/remark-lint/workflows/main/badge.svg
[api-label]: #label
[build]: https://github.com/remarkjs/remark-lint/actions
[api-lint-rule]: #lintrulemeta-rule
[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg
[api-meta]: #meta
[coverage]: https://codecov.io/github/remarkjs/remark-lint
[api-rule]: #rule
[downloads-badge]: https://img.shields.io/npm/dm/unified-lint-rule.svg
[api-severity]: #severity
[downloads]: https://www.npmjs.com/package/unified-lint-rule
[author]: https://wooorm.com
[size-badge]: https://img.shields.io/bundlephobia/minzip/unified-lint-rule.svg
[badge-build-image]: https://github.com/remarkjs/remark-lint/workflows/main/badge.svg
[size]: https://bundlephobia.com/result?p=unified-lint-rule
[badge-build-url]: https://github.com/remarkjs/remark-lint/actions
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[badge-chat-image]: https://img.shields.io/badge/chat-discussions-success.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[badge-chat-url]: https://github.com/remarkjs/remark/discussions
[collective]: https://opencollective.com/unified
[badge-coverage-image]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[badge-coverage-url]: https://codecov.io/github/remarkjs/remark-lint
[chat]: https://github.com/remarkjs/remark/discussions
[badge-downloads-image]: https://img.shields.io/npm/dm/unified-lint-rule.svg
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[badge-downloads-url]: https://www.npmjs.com/package/unified-lint-rule
[esmsh]: https://esm.sh
[badge-funding-backers-image]: https://opencollective.com/unified/backers/badge.svg
[npm]: https://docs.npmjs.com/cli/install
[badge-funding-sponsors-image]: https://opencollective.com/unified/sponsors/badge.svg
[health]: https://github.com/remarkjs/.github
[badge-funding-url]: https://opencollective.com/unified
[contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
[badge-size-image]: https://img.shields.io/bundlejs/size/unified-lint-rule
[support]: https://github.com/remarkjs/.github/blob/main/support.md
[badge-size-url]: https://bundlejs.com/?q=unified-lint-rule
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
[esm-sh]: https://esm.sh
[license]: https://github.com/remarkjs/remark-lint/blob/main/license
[file-license]: https://github.com/remarkjs/remark-lint/blob/main/license
[author]: https://wooorm.com
[github-dotfiles-coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
[unified]: https://github.com/unifiedjs/unified
[github-dotfiles-contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
[mono]: https://github.com/remarkjs/remark-lint
[github-dotfiles-health]: https://github.com/remarkjs/.github
[configure]: https://github.com/remarkjs/remark-lint#configure
[github-dotfiles-support]: https://github.com/remarkjs/.github/blob/main/support.md
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-unified]: https://github.com/unifiedjs/unified
[github-unified-plugin]: https://github.com/unifiedjs/unified#plugin
[github-unist-node]: https://github.com/syntax-tree/unist#node
[github-vfile]: https://github.com/vfile/vfile
[npm-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc