Skip to content

Commit 2665a96

Browse files
committed
Add improved docs
1 parent c056978 commit 2665a96

File tree

2 files changed

+124
-75
lines changed

2 files changed

+124
-75
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function diff() {
1818
const base = file.dirname
1919
/** @type {string|undefined} */
2020
let commitRange
21-
/** @type {string[]|undefined} */
21+
/** @type {Array<string>|undefined} */
2222
let range
2323

2424
// Looks like Travis.
@@ -81,7 +81,7 @@ export default function diff() {
8181
* @param {string} root
8282
*/
8383
function tick(root) {
84-
/** @type {Record<string, [number, number][]>} */
84+
/** @type {Record<string, Array<[number, number]>>} */
8585
const diffs = {}
8686

8787
gitDiffTree(path.join(root, '.git'), {
@@ -108,7 +108,7 @@ export default function diff() {
108108
/* c8 ignore next */
109109
if (!match) return
110110

111-
/** @type {[number, number][]} */
111+
/** @type {Array<[number, number]>} */
112112
const ranges = []
113113
const start = Number.parseInt(match[3], 10) - 1
114114
let index = 0

readme.md

Lines changed: 121 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,79 @@
77
[![Backers][backers-badge]][collective]
88
[![Chat][chat-badge]][chat]
99

10-
[**unified**][unified] plugin to ignore unrelated messages.
11-
Currently works in PRs on Travis and GitHub Actions.
10+
**[unified][]** plugin to ignore unrelated messages in GitHub Actions and
11+
Travis.
1212

13-
When working with natural language, having tools that check cumbersome tasks
14-
can be very useful (think [alex][] or [retext][] plugins).
15-
However, natural language isn’t as strict as code.
16-
Integrating natural language checking in a CI often doesn’t work well due to
17-
false positives.
18-
It’s possible to add a long list of exceptions, but this soon becomes
19-
unmanageable.
13+
## Contents
2014

21-
This plugin solves that problem, when in CIs, by ignoring any messages on
22-
unchanged lines.
23-
When run outside supported CIs this plugin doesn’t do anything.
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`unified().use(unifiedDiff)`](#unifieduseunifieddiff)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Contribute](#contribute)
24+
* [License](#license)
2425

25-
## Install
26+
## What is this?
27+
28+
This package is a [unified][] plugin to ignore unrelated lint messages in a CI.
29+
30+
**unified** is a project that transforms content with abstract syntax trees
31+
(ASTs).
32+
**vfile** is the virtual file interface used in unified which manages messages.
33+
This is a unified plugin that filters messages on the vfile.
2634

27-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
28-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
35+
## When should I use this?
2936

30-
[npm][]:
37+
You can use this plugin when you are dealing with a large, existing project.
38+
39+
Using tools that check whether things follow a style guide is typically very
40+
useful.
41+
However, it can be hard to start using something in a large existing project.
42+
This plugin helps, because it ignores messages that occur in lines that are not
43+
touched by a PR in a CI.
44+
When this plugin is used outside of a supported CIs, it doesn’t do anything.
45+
46+
## Install
47+
48+
This package is [ESM only][esm].
49+
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
3150

3251
```sh
3352
npm install unified-diff
3453
```
3554

55+
In Deno with [`esm.sh`][esmsh]:
56+
57+
```js
58+
import unifiedDiff from 'https://esm.sh/unified-diff@4'
59+
```
60+
61+
In browsers with [`esm.sh`][esmsh]:
62+
63+
```html
64+
<script type="module">
65+
import unifiedDiff from 'https://esm.sh/unified-diff@4?bundle'
66+
</script>
67+
```
68+
3669
## Use
3770

38-
Say we have this `readme.md`.
39-
Note the `an an`.
71+
Say our document `example.md` contains:
4072

4173
```markdown
4274
This is an an example.
4375
```
4476

45-
Then, someone creates a PR which adds the following diff:
46-
47-
```diff
48-
diff --git a/readme.md b/readme.md
49-
index 360b225..5a96b86 100644
50-
--- a/readme.md
51-
+++ b/readme.md
52-
@@ -1 +1,3 @@
53-
This is an an example.
54-
+
55-
+Some more more text. A error.
56-
```
77+
> 👉 **Note**: `an an` is a typo.
5778
58-
We have some natural language checking in `lint.js`:
79+
…and our module `example.js` looks as follows:
5980

6081
```js
61-
import {toVFile} from 'to-vfile'
82+
import {read} from 'to-vfile'
6283
import {reporter} from 'vfile-reporter'
6384
import {unified} from 'unified'
6485
import unifiedDiff from 'unified-diff'
@@ -69,57 +90,70 @@ import retextEnglish from 'retext-english'
6990
import retextRepeatedWords from 'retext-repeated-words'
7091
import retextIndefiniteArticle from 'retext-indefinite-article'
7192

72-
toVFile.read('readme.md').then((file) => {
73-
unified()
74-
.use(remarkParse)
75-
.use(
76-
remarkRetext,
77-
unified()
78-
.use(retextEnglish)
79-
.use(retextRepeatedWords)
80-
.use(retextIndefiniteArticle)
81-
)
82-
.use(remarkStringify)
83-
.use(unifiedDiff)
84-
.process(file)
85-
.then((file) => {
86-
console.error(reporter(file))
87-
process.exit(file.messages.length > 0 ? 1 : 0)
88-
})
89-
})
93+
const file = unified()
94+
.use(remarkParse)
95+
.use(
96+
remarkRetext,
97+
unified()
98+
.use(retextEnglish)
99+
.use(retextRepeatedWords)
100+
.use(retextIndefiniteArticle)
101+
)
102+
.use(remarkStringify)
103+
.use(unifiedDiff)
104+
.process(await read('example.md'))
105+
106+
console.error(reporter(file))
107+
process.exit(file.messages.length > 0 ? 1 : 0)
90108
```
91109

92-
`lint.js` is hooked up to run on Travis in `.travis.yml` like so:
110+
…and our Travis configuration `.travis.yml` contains:
93111

94112
```yml
95-
# ...
113+
#
96114
script:
97115
- npm test
98-
- node lint.js
99-
# ...
116+
- node example.js
117+
#
118+
```
119+
120+
> 👉 **Note**: an equivalent GH Actions workflow file is also supported.
121+
122+
Then, say someone creates a PR which adds the following diff:
123+
124+
```diff
125+
diff --git a/example.md b/example.md
126+
index 360b225..5a96b86 100644
127+
--- a/example.md
128+
+++ b/example.md
129+
@@ -1 +1,3 @@
130+
This is an an example.
131+
+
132+
+Some more more text. A error.
100133
```
101134

102-
(or in an equivalent GH Actions workflow file)
135+
> 👉 **Note**: `more more` and `A` before `error` are typos.
103136
104137
When run in CI, we’ll see the following printed on **stderr**(4).
105-
Note that `an an` on L1 is not included because it’s unrelated to this PR.
106138

107139
```txt
108-
readme.md
140+
example.md
109141
3:6-3:15 warning Expected `more` once, not twice retext-repeated-words retext-repeated-words
110142
3:22-3:23 warning Use `An` before `error`, not `A` retext-indefinite-article retext-indefinite-article
111143
112144
⚠ 2 warnings
113145
```
114146

115-
As there are messages, the build exits with `1`, thus failing CI.
147+
> 👉 **Note**: `an an` on L1 is not included because it’s unrelated to this PR.
148+
149+
The build exits with `1` as there are messages, thus failing CI.
116150
The user sees this and amends the PR to the following:
117151

118152
```diff
119-
diff --git a/readme.md b/readme.md
153+
diff --git a/example.md b/example.md
120154
index 360b225..5a96b86 100644
121-
--- a/readme.md
122-
+++ b/readme.md
155+
--- a/example.md
156+
+++ b/example.md
123157
@@ -1 +1,3 @@
124158
This is an an example.
125159
+
@@ -131,11 +165,12 @@ an error, but it’s unrelated to the PR.
131165

132166
## API
133167

134-
This package exports a plugin as the default export.
168+
This package exports no identifiers.
169+
The default export is `unifiedDiff`.
135170

136-
### `unified().use(diff)`
171+
### `unified().use(unifiedDiff)`
137172

138-
Ignore messages emitted by plugins before `diff` for lines that did not change.
173+
Ignore unrelated messages in GitHub Actions and Travis.
139174

140175
There are no options.
141176
If there’s a `TRAVIS_COMMIT_RANGE`, `GITHUB_BASE_REF` and `GITHUB_HEAD_REF`, or
@@ -145,10 +180,22 @@ nothing.
145180
###### To do
146181

147182
* [ ] Add support for other CIs (ping if you want to work on this)
148-
* [ ] Add non-CI support (I’m not yet sure how though)
183+
* [ ] Add non-CI support (I’m not sure how though)
149184

150185
PRs welcome!
151186

187+
## Types
188+
189+
This package is fully typed with [TypeScript][].
190+
There are no additional exported types.
191+
192+
## Compatibility
193+
194+
Projects maintained by the unified collective are compatible with all maintained
195+
versions of Node.js.
196+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
197+
Our projects sometimes work with older versions, but this is not guaranteed.
198+
152199
## Contribute
153200

154201
See [`contributing.md`][contributing] in [`unifiedjs/.github`][health] for ways
@@ -189,20 +236,22 @@ abide by its terms.
189236

190237
[npm]: https://docs.npmjs.com/cli/install
191238

239+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
240+
241+
[esmsh]: https://esm.sh
242+
243+
[typescript]: https://www.typescriptlang.org
244+
192245
[health]: https://github.com/unifiedjs/.github
193246

194-
[contributing]: https://github.com/unifiedjs/.github/blob/HEAD/contributing.md
247+
[contributing]: https://github.com/unifiedjs/.github/blob/main/contributing.md
195248

196-
[support]: https://github.com/unifiedjs/.github/blob/HEAD/support.md
249+
[support]: https://github.com/unifiedjs/.github/blob/main/support.md
197250

198-
[coc]: https://github.com/unifiedjs/.github/blob/HEAD/code-of-conduct.md
251+
[coc]: https://github.com/unifiedjs/.github/blob/main/code-of-conduct.md
199252

200253
[license]: license
201254

202255
[author]: https://wooorm.com
203256

204257
[unified]: https://github.com/unifiedjs/unified
205-
206-
[alex]: https://github.com/wooorm/alex
207-
208-
[retext]: https://github.com/retextjs/retext/blob/HEAD/doc/plugins.md#list-of-plugins

0 commit comments

Comments
 (0)