Skip to content

Commit dbd589c

Browse files
committed
Use ESM
1 parent 55a7878 commit dbd589c

File tree

7 files changed

+65
-58
lines changed

7 files changed

+65
-58
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
.DS_Store
2-
*.log
3-
.nyc_output/
41
coverage/
52
node_modules/
3+
.DS_Store
4+
*.log
65
yarn.lock

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
'use strict'
2-
3-
var path = require('path')
4-
var gitDiffTree = require('git-diff-tree')
5-
var findUp = require('vfile-find-up')
6-
7-
module.exports = diff
1+
import path from 'path'
2+
import gitDiffTree from 'git-diff-tree'
3+
import findUp from 'vfile-find-up'
84

95
var own = {}.hasOwnProperty
106

117
var previousRange
128

13-
function diff() {
9+
export default function diff() {
1410
var cache = {}
1511

1612
return transform
@@ -48,19 +44,22 @@ function diff() {
4844
previousRange = commitRange
4945
}
5046

47+
/* c8 ignore next 3 */
5148
if (own.call(cache, base)) {
5249
tick(cache[base])
5350
} else {
5451
findUp.one('.git', file.dirname, ongit)
5552
}
5653

5754
function ongit(error, git) {
58-
/* istanbul ignore if - Never happens */
55+
// Never happens.
56+
/* c8 ignore next 3 */
5957
if (error) {
6058
return next(error)
6159
}
6260

63-
/* istanbul ignore if - Not testable in a Git repo… */
61+
// Not testable in a Git repo…
62+
/* c8 ignore next 3 */
6463
if (!git) {
6564
return next(new Error('Not in a git repository'))
6665
}
@@ -85,7 +84,8 @@ function diff() {
8584
if (info) {
8685
fp = path.resolve(root, info.path)
8786

88-
/* istanbul ignore else - long diffs. */
87+
// Long diffs.
88+
/* c8 ignore next 3 */
8989
if (!(fp in diffs)) {
9090
diffs[fp] = []
9191
}
@@ -133,7 +133,8 @@ function parse(data) {
133133
var position
134134
var no
135135

136-
/* istanbul ignore if - Should not happen, maybe if Git returns weird diffs? */
136+
// Should not happen, maybe if Git returns weird diffs?
137+
/* c8 ignore next 3 */
137138
if (!match) {
138139
return
139140
}

package.json

+9-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"contributors": [
2626
"Titus Wormer <[email protected]> (https://wooorm.com)"
2727
],
28+
"sideEffects": false,
29+
"type": "module",
30+
"main": "index.js",
2831
"files": [
2932
"index.js"
3033
],
@@ -33,8 +36,8 @@
3336
"vfile-find-up": "^5.0.0"
3437
},
3538
"devDependencies": {
39+
"c8": "^7.0.0",
3640
"nlcst-to-string": "^2.0.0",
37-
"nyc": "^15.0.0",
3841
"prettier": "^2.0.0",
3942
"remark-cli": "^9.0.0",
4043
"remark-preset-wooorm": "^8.0.0",
@@ -43,20 +46,14 @@
4346
"tape": "^5.0.0",
4447
"to-vfile": "^6.0.0",
4548
"unist-util-visit": "^2.0.0",
46-
"xo": "^0.38.0"
49+
"xo": "^0.39.0"
4750
},
4851
"scripts": {
4952
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"test-api": "node test",
51-
"test-coverage": "nyc --reporter lcov tape test/index.js",
53+
"test-api": "node --conditions development test/index.js",
54+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
5255
"test": "npm run format && npm run test-coverage"
5356
},
54-
"nyc": {
55-
"check-coverage": true,
56-
"lines": 100,
57-
"functions": 100,
58-
"branches": 100
59-
},
6057
"prettier": {
6158
"tabWidth": 2,
6259
"useTabs": false,
@@ -67,8 +64,9 @@
6764
},
6865
"xo": {
6966
"prettier": true,
70-
"esnext": false,
7167
"rules": {
68+
"no-var": "off",
69+
"prefer-arrow-callback": "off",
7270
"unicorn/prefer-number-properties": "off",
7371
"unicorn/no-fn-reference-in-iterator": "off",
7472
"unicorn/string-content": "off"

readme.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ When run outside supported CIs this plugin doesn’t do anything.
2424

2525
## Install
2626

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.
29+
2730
[npm][]:
2831

2932
```sh
@@ -123,7 +126,9 @@ an error, but it’s unrelated to the PR.
123126

124127
## API
125128

126-
### `processor.use(diff)`
129+
This package exports a plugin as the default export.
130+
131+
### `unified().use(diff)`
127132

128133
Ignore messages emitted by plugins before `diff` for lines that did not change.
129134

@@ -132,7 +137,7 @@ If there’s a `TRAVIS_COMMIT_RANGE`, `GITHUB_BASE_REF` and `GITHUB_HEAD_REF`, o
132137
`GITHUB_SHA` environment variable, then this plugin runs, otherwise it does
133138
nothing.
134139

135-
###### TODO
140+
###### To do
136141

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

test/index.js

+29-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
2-
3-
var cp = require('child_process')
4-
var path = require('path')
5-
var promisify = require('util').promisify
6-
var test = require('tape')
7-
var rimraf = promisify(require('rimraf'))
8-
var vfile = require('to-vfile')
9-
var processor = require('./processor.js')()
1+
import cp from 'child_process'
2+
import {promises as fsPromises} from 'fs'
3+
import path from 'path'
4+
import {promisify} from 'util'
5+
import test from 'tape'
6+
import vfile from 'to-vfile'
7+
import {processor} from './processor.js'
108

119
var exec = promisify(cp.exec)
1210

@@ -45,7 +43,7 @@ test('diff() (travis)', function (t) {
4543
.then(() => exec('git config --global user.email').catch(setEmailAndName))
4644
// Add initial file.
4745
.then(() =>
48-
processor.process(vfile({path: 'example.txt', contents: stepOne}))
46+
processor().process(vfile({path: 'example.txt', contents: stepOne}))
4947
)
5048
.then((file) => {
5149
t.deepEqual(
@@ -70,7 +68,9 @@ test('diff() (travis)', function (t) {
7068
.then((result) => {
7169
final = result.stdout.trim()
7270
process.env.TRAVIS_COMMIT_RANGE = [initial, final].join('...')
73-
return processor.process(vfile({path: 'example.txt', contents: stepTwo}))
71+
return processor().process(
72+
vfile({path: 'example.txt', contents: stepTwo})
73+
)
7474
})
7575
.then((file) => {
7676
t.deepEqual(
@@ -81,7 +81,7 @@ test('diff() (travis)', function (t) {
8181
})
8282
// Again!
8383
.then(() =>
84-
processor.process(vfile({path: 'example.txt', contents: stepTwo}))
84+
processor().process(vfile({path: 'example.txt', contents: stepTwo}))
8585
)
8686
.then((file) => {
8787
t.deepEqual(
@@ -92,7 +92,7 @@ test('diff() (travis)', function (t) {
9292
})
9393
// Unstaged files.
9494
.then(() =>
95-
processor.process(vfile({path: 'missing.txt', contents: other}))
95+
processor().process(vfile({path: 'missing.txt', contents: other}))
9696
)
9797
.then((file) => {
9898
t.deepEqual(
@@ -112,7 +112,7 @@ test('diff() (travis)', function (t) {
112112

113113
process.env.TRAVIS_COMMIT_RANGE = [initial, final].join('...')
114114

115-
return processor.process(
115+
return processor().process(
116116
vfile({path: 'example.txt', contents: stepThree})
117117
)
118118
})
@@ -123,7 +123,7 @@ test('diff() (travis)', function (t) {
123123
'should deal with multiple patches'
124124
)
125125

126-
return processor.process(vfile({path: 'new.txt', contents: other}))
126+
return processor().process(vfile({path: 'new.txt', contents: other}))
127127
})
128128
.then((file) => {
129129
t.deepEqual(
@@ -132,7 +132,7 @@ test('diff() (travis)', function (t) {
132132
'should deal with new files'
133133
)
134134

135-
return processor.process(vfile({path: 'new.txt', contents: other}))
135+
return processor().process(vfile({path: 'new.txt', contents: other}))
136136
})
137137
// Restore
138138
.then(restore, restore)
@@ -143,9 +143,10 @@ test('diff() (travis)', function (t) {
143143

144144
function restore() {
145145
delete process.env.TRAVIS_COMMIT_RANGE
146-
return rimraf('.git')
147-
.then(() => rimraf('new.txt'))
148-
.then(() => rimraf('example.txt'))
146+
return fsPromises
147+
.rm('.git', {recursive: true})
148+
.then(() => fsPromises.rm('new.txt'))
149+
.then(() => fsPromises.rm('example.txt'))
149150
}
150151
})
151152

@@ -175,7 +176,9 @@ test('diff() (GitHub Actions)', function (t) {
175176
.then(() => exec('git rev-parse HEAD'))
176177
.then((result) => {
177178
process.env.GITHUB_SHA = result.stdout.trim()
178-
return processor.process(vfile({path: 'example.txt', contents: stepTwo}))
179+
return processor().process(
180+
vfile({path: 'example.txt', contents: stepTwo})
181+
)
179182
})
180183
.then((file) => {
181184
t.deepEqual(
@@ -202,7 +205,9 @@ test('diff() (GitHub Actions)', function (t) {
202205
process.env.GITHUB_SHA = result.stdout.trim()
203206
process.env.GITHUB_BASE_REF = 'refs/heads/' + main
204207
process.env.GITHUB_HEAD_REF = 'refs/heads/other-branch'
205-
return processor.process(vfile({path: 'example.txt', contents: stepFour}))
208+
return processor().process(
209+
vfile({path: 'example.txt', contents: stepFour})
210+
)
206211
})
207212
.then((file) => {
208213
t.deepEqual(
@@ -222,7 +227,9 @@ test('diff() (GitHub Actions)', function (t) {
222227
delete process.env.GITHUB_SHA
223228
delete process.env.GITHUB_BASE_REF
224229
delete process.env.GITHUB_HEAD_REF
225-
return rimraf('.git').then(() => rimraf('example.txt'))
230+
return fsPromises
231+
.rm('.git', {recursive: true})
232+
.then(() => fsPromises.rm('example.txt'))
226233
}
227234
})
228235

test/processor.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict'
1+
import retext from 'retext'
2+
import visit from 'unist-util-visit'
3+
import toString from 'nlcst-to-string'
4+
import unifiedDiff from '../index.js'
25

3-
var retext = require('retext')
4-
var visit = require('unist-util-visit')
5-
var toString = require('nlcst-to-string')
6-
var diff = require('..')
7-
8-
module.exports = retext().use(lorem).use(diff).freeze()
6+
export const processor = retext().use(lorem).use(unifiedDiff).freeze()
97

108
function lorem() {
119
return transformer

0 commit comments

Comments
 (0)