Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 20, 2021
1 parent fa9a42f commit dfbcafb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 64 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
unist-util-visit.js
unist-util-visit.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
unist-util-visit.js
unist-util-visit.min.js
*.json
*.md
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use strict'
import {visitParents, CONTINUE, SKIP, EXIT} from 'unist-util-visit-parents'

module.exports = visit
export {CONTINUE, SKIP, EXIT}

var visitParents = require('unist-util-visit-parents')

visit.CONTINUE = visitParents.CONTINUE
visit.SKIP = visitParents.SKIP
visit.EXIT = visitParents.EXIT

function visit(tree, test, visitor, reverse) {
export function visit(tree, test, visitor, reverse) {
if (typeof test === 'function' && typeof visitor !== 'function') {
reverse = visitor
visitor = test
Expand Down
43 changes: 16 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,36 @@
"Eugene Sharygin <eush77@gmail.com>",
"Richard Gibson <richard.gibson@gmail.com>"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
"types/index.d.ts",
"index.js"
],
"types": "types/index.d.ts",
"dependencies": {
"@types/unist": "^2.0.0",
"unist-util-is": "^4.0.0",
"unist-util-visit-parents": "^3.0.0"
"unist-util-is": "^5.0.0",
"unist-util-visit-parents": "^4.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark": "^13.0.0",
"remark-cli": "^9.0.0",
"remark-gfm": "^1.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"typescript": "^4.0.0",
"unified": "^9.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s unistUtilVisit -o unist-util-visit.js",
"build-mangle": "browserify . -s unistUtilVisit -o unist-util-visit.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -89,13 +79,12 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-set-has": "off"
"no-var": "off",
"prefer-arrow-callback": "off"
},
"ignores": [
"unist-util-visit.js",
"types"
"ignore": [
"types/"
]
},
"remarkConfig": {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

## Install

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

[npm][]:

```sh
Expand Down Expand Up @@ -46,6 +49,10 @@ Yields:

## API

This package exports the following identifiers: `visit`, `CONTINUE`, `SKIP`, and
`EXIT`.
There is no default export.

### `visit(tree[, test], visitor[, reverse])`

This function works exactly the same as [`unist-util-visit-parents`][vp],
Expand Down
42 changes: 20 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict'

var assert = require('assert')
var test = require('tape')
var remark = require('remark')
var gfm = require('remark-gfm')
var visit = require('.')
import assert from 'assert'
import test from 'tape'
import remark from 'remark'
import gfm from 'remark-gfm'
import {visit, CONTINUE, EXIT, SKIP} from './index.js'

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

var STOP = 5
var SKIP = 7
var SKIP_REVERSE = 6
var stopIndex = 5
var skipIndex = 7
var skipReverseIndex = 6

var texts = 6
var codes = 1
Expand Down Expand Up @@ -146,7 +144,7 @@ test('unist-util-visit', function (t) {
})

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

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

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

visit(tree, visitor)

st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
st.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

st.end()

function visitor(node) {
assert.strictEqual(node.type, types[n++], 'should be the expected type')
return n === STOP ? visit.EXIT : visit.CONTINUE
return n === stopIndex ? EXIT : CONTINUE
}
})

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

visit(tree, visitor, true)

st.equal(n, STOP, 'should visit nodes until `visit.EXIT` is given')
st.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

st.end()

Expand All @@ -197,7 +195,7 @@ test('unist-util-visit', function (t) {
reverseTypes[n++],
'should be the expected type'
)
return n === STOP ? visit.EXIT : visit.CONTINUE
return n === stopIndex ? EXIT : CONTINUE
}
})

Expand All @@ -210,7 +208,7 @@ test('unist-util-visit', function (t) {
st.equal(
count,
types.length - 1,
'should visit nodes except when `visit.SKIP` is given'
'should visit nodes except when `SKIP` is given'
)

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

if (n === SKIP) {
if (n === skipIndex) {
n++ // The one node inside it.
return visit.SKIP
return SKIP
}
}
})
Expand All @@ -235,7 +233,7 @@ test('unist-util-visit', function (t) {
st.equal(
count,
reverseTypes.length - 1,
'should visit nodes except when `visit.SKIP` is given'
'should visit nodes except when `SKIP` is given'
)

st.end()
Expand All @@ -248,9 +246,9 @@ test('unist-util-visit', function (t) {
)
count++

if (n === SKIP_REVERSE) {
if (n === skipReverseIndex) {
n++ // The one node inside it.
return visit.SKIP
return SKIP
}
}
})
Expand Down

0 comments on commit dfbcafb

Please sign in to comment.