Skip to content

Commit dfbcafb

Browse files
committed
Use ESM
1 parent fa9a42f commit dfbcafb

File tree

6 files changed

+46
-64
lines changed

6 files changed

+46
-64
lines changed

.gitignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
unist-util-visit.js
7-
unist-util-visit.min.js
85
yarn.lock

.prettierignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
unist-util-visit.js
3-
unist-util-visit.min.js
4-
*.json
52
*.md

index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
'use strict'
1+
import {visitParents, CONTINUE, SKIP, EXIT} from 'unist-util-visit-parents'
22

3-
module.exports = visit
3+
export {CONTINUE, SKIP, EXIT}
44

5-
var visitParents = require('unist-util-visit-parents')
6-
7-
visit.CONTINUE = visitParents.CONTINUE
8-
visit.SKIP = visitParents.SKIP
9-
visit.EXIT = visitParents.EXIT
10-
11-
function visit(tree, test, visitor, reverse) {
5+
export function visit(tree, test, visitor, reverse) {
126
if (typeof test === 'function' && typeof visitor !== 'function') {
137
reverse = visitor
148
visitor = test

package.json

+16-27
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,36 @@
3838
"Eugene Sharygin <[email protected]>",
3939
"Richard Gibson <[email protected]>"
4040
],
41+
"sideEffects": false,
42+
"type": "module",
43+
"main": "index.js",
44+
"types": "types/index.d.ts",
4145
"files": [
42-
"index.js",
43-
"types/index.d.ts"
46+
"types/index.d.ts",
47+
"index.js"
4448
],
45-
"types": "types/index.d.ts",
4649
"dependencies": {
4750
"@types/unist": "^2.0.0",
48-
"unist-util-is": "^4.0.0",
49-
"unist-util-visit-parents": "^3.0.0"
51+
"unist-util-is": "^5.0.0",
52+
"unist-util-visit-parents": "^4.0.0"
5053
},
5154
"devDependencies": {
52-
"browserify": "^17.0.0",
53-
"dtslint": "^4.0.0",
54-
"nyc": "^15.0.0",
55+
"c8": "^7.0.0",
5556
"prettier": "^2.0.0",
5657
"remark": "^13.0.0",
5758
"remark-cli": "^9.0.0",
5859
"remark-gfm": "^1.0.0",
5960
"remark-preset-wooorm": "^8.0.0",
6061
"tape": "^5.0.0",
61-
"tinyify": "^3.0.0",
6262
"typescript": "^4.0.0",
6363
"unified": "^9.0.0",
6464
"xo": "^0.38.0"
6565
},
6666
"scripts": {
6767
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
68-
"build-bundle": "browserify . -s unistUtilVisit -o unist-util-visit.js",
69-
"build-mangle": "browserify . -s unistUtilVisit -o unist-util-visit.min.js -p tinyify",
70-
"build": "npm run build-bundle && npm run build-mangle",
71-
"test-api": "node test",
72-
"test-coverage": "nyc --reporter lcov tape test.js",
73-
"test-types": "dtslint types",
74-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
75-
},
76-
"nyc": {
77-
"check-coverage": true,
78-
"lines": 100,
79-
"functions": 100,
80-
"branches": 100
68+
"test-api": "node test.js",
69+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
70+
"test": "npm run format && npm run test-coverage"
8171
},
8272
"prettier": {
8373
"tabWidth": 2,
@@ -89,13 +79,12 @@
8979
},
9080
"xo": {
9181
"prettier": true,
92-
"esnext": false,
9382
"rules": {
94-
"unicorn/prefer-set-has": "off"
83+
"no-var": "off",
84+
"prefer-arrow-callback": "off"
9585
},
96-
"ignores": [
97-
"unist-util-visit.js",
98-
"types"
86+
"ignore": [
87+
"types/"
9988
]
10089
},
10190
"remarkConfig": {

readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -46,6 +49,10 @@ Yields:
4649

4750
## API
4851

52+
This package exports the following identifiers: `visit`, `CONTINUE`, `SKIP`, and
53+
`EXIT`.
54+
There is no default export.
55+
4956
### `visit(tree[, test], visitor[, reverse])`
5057

5158
This function works exactly the same as [`unist-util-visit-parents`][vp],

test.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
'use strict'
2-
3-
var assert = require('assert')
4-
var test = require('tape')
5-
var remark = require('remark')
6-
var gfm = require('remark-gfm')
7-
var visit = require('.')
1+
import assert from 'assert'
2+
import test from 'tape'
3+
import remark from 'remark'
4+
import gfm from 'remark-gfm'
5+
import {visit, CONTINUE, EXIT, SKIP} from './index.js'
86

97
var tree = remark().parse('Some _emphasis_, **importance**, and `code`.')
108

11-
var STOP = 5
12-
var SKIP = 7
13-
var SKIP_REVERSE = 6
9+
var stopIndex = 5
10+
var skipIndex = 7
11+
var skipReverseIndex = 6
1412

1513
var texts = 6
1614
var codes = 1
@@ -146,7 +144,7 @@ test('unist-util-visit', function (t) {
146144
})
147145

148146
t.test('should accept an array of `is`-compatible tests', function (st) {
149-
var expected = ['root', 'paragraph', 'emphasis', 'strong']
147+
var expected = new Set(['root', 'paragraph', 'emphasis', 'strong'])
150148
var tests = [test, 'paragraph', {value: '.'}, ['emphasis', 'strong']]
151149
var n = 0
152150

@@ -157,7 +155,7 @@ test('unist-util-visit', function (t) {
157155
st.end()
158156

159157
function visitor(node) {
160-
var ok = expected.includes(node.type) || node.value === '.'
158+
var ok = expected.has(node.type) || node.value === '.'
161159
assert.ok(ok, 'should be a requested type: ' + node.type)
162160
n++
163161
}
@@ -172,13 +170,13 @@ test('unist-util-visit', function (t) {
172170

173171
visit(tree, visitor)
174172

175-
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
173+
st.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')
176174

177175
st.end()
178176

179177
function visitor(node) {
180178
assert.strictEqual(node.type, types[n++], 'should be the expected type')
181-
return n === STOP ? visit.EXIT : visit.CONTINUE
179+
return n === stopIndex ? EXIT : CONTINUE
182180
}
183181
})
184182

@@ -187,7 +185,7 @@ test('unist-util-visit', function (t) {
187185

188186
visit(tree, visitor, true)
189187

190-
st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
188+
st.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')
191189

192190
st.end()
193191

@@ -197,7 +195,7 @@ test('unist-util-visit', function (t) {
197195
reverseTypes[n++],
198196
'should be the expected type'
199197
)
200-
return n === STOP ? visit.EXIT : visit.CONTINUE
198+
return n === stopIndex ? EXIT : CONTINUE
201199
}
202200
})
203201

@@ -210,7 +208,7 @@ test('unist-util-visit', function (t) {
210208
st.equal(
211209
count,
212210
types.length - 1,
213-
'should visit nodes except when `visit.SKIP` is given'
211+
'should visit nodes except when `SKIP` is given'
214212
)
215213

216214
st.end()
@@ -219,9 +217,9 @@ test('unist-util-visit', function (t) {
219217
assert.strictEqual(node.type, types[n++], 'should be the expected type')
220218
count++
221219

222-
if (n === SKIP) {
220+
if (n === skipIndex) {
223221
n++ // The one node inside it.
224-
return visit.SKIP
222+
return SKIP
225223
}
226224
}
227225
})
@@ -235,7 +233,7 @@ test('unist-util-visit', function (t) {
235233
st.equal(
236234
count,
237235
reverseTypes.length - 1,
238-
'should visit nodes except when `visit.SKIP` is given'
236+
'should visit nodes except when `SKIP` is given'
239237
)
240238

241239
st.end()
@@ -248,9 +246,9 @@ test('unist-util-visit', function (t) {
248246
)
249247
count++
250248

251-
if (n === SKIP_REVERSE) {
249+
if (n === skipReverseIndex) {
252250
n++ // The one node inside it.
253-
return visit.SKIP
251+
return SKIP
254252
}
255253
}
256254
})

0 commit comments

Comments
 (0)