What is unified-lint-rule?
The `unified-lint-rule` package is a utility for creating linting rules for unified processors. Unified is a project that provides a suite of tools for processing content, such as Markdown and HTML, in a structured way. The `unified-lint-rule` package allows developers to define custom linting rules that can be used to enforce coding standards, catch errors, and ensure consistency in content processing.
What are unified-lint-rule's main functionalities?
Creating a Custom Lint Rule
This feature allows you to create a custom linting rule. In this example, the rule checks for the presence of the word 'foo' in text nodes and reports a message if it is found.
const rule = require('unified-lint-rule');
const visit = require('unist-util-visit');
const noFoo = rule('example/no-foo', (tree, file) => {
visit(tree, 'text', (node) => {
if (node.value.includes('foo')) {
file.message('Do not use the word "foo"', node);
}
});
});
module.exports = noFoo;
Integrating with Unified Processor
This feature demonstrates how to integrate a custom linting rule with a unified processor. The example shows how to use the `unified`, `remark-parse`, and `remark-lint` packages along with the custom `noFoo` rule to process a Markdown string and report any linting messages.
const unified = require('unified');
const markdown = require('remark-parse');
const lint = require('remark-lint');
const noFoo = require('./path-to-no-foo-rule');
unified()
.use(markdown)
.use(lint)
.use(noFoo)
.process('This is a foo test', (err, file) => {
if (err) throw err;
console.error(report(file));
});
Other packages similar to unified-lint-rule
eslint
ESLint is a widely-used linting tool for JavaScript and TypeScript. It allows developers to define and enforce coding standards and catch errors in their code. Unlike `unified-lint-rule`, which is specific to content processing with unified, ESLint is focused on JavaScript code quality and style.
stylelint
Stylelint is a powerful linting tool for CSS and other style languages. It helps developers enforce consistent conventions and avoid errors in their stylesheets. While `unified-lint-rule` is used for content processing, Stylelint is specifically designed for linting styles.
remark-lint
Remark-lint is a collection of linting rules for Markdown files, built on top of the unified ecosystem. It provides a set of predefined rules for Markdown content, similar to how `unified-lint-rule` allows for custom rule creation. Remark-lint is more focused on Markdown-specific linting.
unified-lint-rule
unified helper to help make lint rules.
Contents
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.
Install
This package is ESM only.
In Node.js (version 16+),
install with npm:
npm install unified-lint-rule
In Deno with esm.sh
:
import {lintRule} from 'https://esm.sh/unified-lint-rule@3'
In browsers with esm.sh
:
<script type="module">
import {lintRule} from 'https://esm.sh/unified-lint-rule@3?bundle'
</script>
Use
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
This package exports the identifier
lintRule
.
It exports the TypeScript types
Label
,
Meta
, and
Severity
.
There is no default export.
lintRule(meta, rule)
Create a plugin.
Parameters
meta
(Meta
or string
)
— info or originrule
(Rule
)
— rule
Returns
Plugin (Plugin
from unified
).
Label
Severity label (TypeScript type);
'off'
: 0
, 'on'
and warn
: 1
, 'error'
: 2
.
Type
type Label = 'error' | 'on' | 'off' | 'warn'
Meta
Rule metadata (TypeScript type).
Fields
origin
(string
)
— name of the lint ruleurl
(string
, optional)
— link to documentation
Rule
Rule (TypeScript type).
Parameters
Returns
Nothing (Promise<undefined>
or undefined
).
Severity
Severity number (TypeScript type);
0
: 'off'
, 1
: 'on'
and warn
, 2
: 'error'
.
Type
type Severity = 0 | 1 | 2
Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
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
in remarkjs/.github
for ways
to get started.
See support.md
for ways to get help.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer