Skip to content

Commit dd52940

Browse files
committed
Use ESM
1 parent 2b4b2b1 commit dd52940

File tree

14 files changed

+157
-116
lines changed

14 files changed

+157
-116
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
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-
hast-util-assert.js
7-
hast-util-assert.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
hast-util-assert.js
3-
hast-util-assert.min.js
4-
*.json
52
*.md

index.js

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,124 @@
1-
'use strict'
2-
3-
var assert = require('assert')
4-
var zwitch = require('zwitch')
5-
var mapz = require('mapz')
6-
var unist = require('unist-util-assert')
7-
8-
var hast = zwitch('type')
9-
10-
exports = unist.wrap(hast)
11-
module.exports = exports
12-
13-
exports.parent = unist.wrap(parent)
14-
exports.text = unist.text
15-
exports.void = unist.void
16-
exports.wrap = unist.wrap
17-
exports.all = mapz(exports, {key: 'children', indices: false})
18-
19-
// Core interface.
20-
hast.invalid = unknown
21-
hast.unknown = unknown
22-
23-
// Per-type handling.
24-
hast.handlers = {
25-
root: unist.wrap(root),
26-
element: unist.wrap(element),
27-
doctype: unist.wrap(doctype),
28-
comment: exports.text,
29-
text: exports.text
1+
import nodeAssert from 'assert'
2+
import {zwitch} from 'zwitch'
3+
import {mapz} from 'mapz'
4+
import {
5+
assert as unistAssert,
6+
parent as unistParent,
7+
literal as unistLiteral,
8+
wrap,
9+
_void
10+
} from 'unist-util-assert'
11+
12+
/**
13+
* Assert that `node` is a valid hast node.
14+
* If `node` is a parent, all children will be asserted too.
15+
*
16+
* @param {unknown} [node]
17+
* @param {Parent} [parent]
18+
* @returns {asserts node is Node}
19+
*/
20+
export function assert(node, parent) {
21+
return wrap(hast)(node, parent)
3022
}
3123

24+
/**
25+
* Assert that `node` is a valid hast parent.
26+
*
27+
* @param {unknown} [node]
28+
* @param {Parent} [parent]
29+
* @returns {asserts node is Parent}
30+
*/
31+
export function parent(node, parent) {
32+
return wrap(assertParent)(node, parent)
33+
}
34+
35+
/**
36+
* Assert that `node` is a valid hast literal.
37+
*
38+
* @param {unknown} [node]
39+
* @param {Parent} [parent]
40+
* @returns {asserts node is Literal}
41+
*/
42+
export function literal(node, parent) {
43+
return wrap(assertLiteral)(node, parent)
44+
}
45+
46+
export {_void, wrap}
47+
48+
var hast = zwitch('type', {
49+
// Core interface.
50+
unknown,
51+
invalid: unknown,
52+
53+
// Per-type handling.
54+
handlers: {
55+
root: wrap(assertRoot),
56+
element: wrap(assertElement),
57+
doctype: wrap(assertDoctype),
58+
comment: literal,
59+
text: literal
60+
}
61+
})
62+
63+
var all = mapz(hast, {key: 'children', indices: false})
64+
3265
function unknown(node, ancestor) {
33-
unist(node, ancestor)
66+
unistAssert(node, ancestor)
3467
}
3568

36-
function parent(node) {
37-
unist.parent(node)
38-
exports.all(node)
69+
function assertParent(node) {
70+
unistParent(node)
71+
all(node)
3972
}
4073

41-
function root(node, ancestor) {
42-
parent(node)
74+
function assertLiteral(node) {
75+
unistLiteral(node)
76+
nodeAssert.strictEqual(
77+
typeof node.value,
78+
'string',
79+
'literal should have a string `value`'
80+
)
81+
}
4382

44-
assert.strictEqual(ancestor, undefined, '`root` should not have a parent')
83+
function assertRoot(node, ancestor) {
84+
assertParent(node)
85+
nodeAssert.strictEqual(ancestor, undefined, '`root` should not have a parent')
4586
}
4687

47-
function element(node) {
48-
parent(node)
88+
function assertElement(node) {
89+
assertParent(node)
4990

50-
assert.strictEqual(
91+
nodeAssert.strictEqual(
5192
typeof node.tagName,
5293
'string',
5394
'`element` should have a `tagName`'
5495
)
55-
assert.notStrictEqual(
96+
nodeAssert.notStrictEqual(
5697
node.tagName,
5798
'',
5899
'`element.tagName` should not be empty'
59100
)
60101
}
61102

62-
function doctype(node) {
63-
unist.void(node)
103+
function assertDoctype(node) {
104+
_void(node)
64105

65-
assert.strictEqual(
106+
nodeAssert.strictEqual(
66107
typeof node.name,
67108
'string',
68109
'`doctype` should have a `name`'
69110
)
70111

71112
if (node.public !== null && node.public !== undefined) {
72-
assert.strictEqual(
113+
nodeAssert.strictEqual(
73114
typeof node.public,
74115
'string',
75116
'`doctype.public` should be `string`'
76117
)
77118
}
78119

79120
if (node.system !== null && node.system !== undefined) {
80-
assert.strictEqual(
121+
nodeAssert.strictEqual(
81122
typeof node.system,
82123
'string',
83124
'`doctype.system` should be `string`'

package.json

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,30 @@
2323
"contributors": [
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
2629
"files": [
2730
"index.js"
2831
],
2932
"dependencies": {
30-
"mapz": "^1.0.0",
31-
"unist-util-assert": "^2.0.0",
32-
"zwitch": "^1.0.0"
33+
"mapz": "^2.0.0",
34+
"unist-util-assert": "^3.0.0",
35+
"zwitch": "^2.0.0"
3336
},
3437
"devDependencies": {
35-
"browserify": "^17.0.0",
36-
"nyc": "^15.0.0",
38+
"c8": "^7.0.0",
3739
"prettier": "^2.0.0",
3840
"remark-cli": "^9.0.0",
3941
"remark-preset-wooorm": "^8.0.0",
4042
"tape": "^5.0.0",
41-
"tinyify": "^3.0.0",
42-
"xo": "^0.38.0"
43+
"xo": "^0.39.0"
4344
},
4445
"scripts": {
4546
"format": "remark . -qfo && prettier . --write --loglevel warn && xo --fix",
46-
"build-bundle": "browserify . -s hastUtilAssert -o hast-util-assert.js",
47-
"build-mangle": "browserify . -s hastUtilAssert -o hast-util-assert.min.js -p tinyify",
48-
"build": "npm run build-bundle && npm run build-mangle",
49-
"test-api": "node test",
50-
"test-coverage": "nyc --reporter lcov tape test",
51-
"test": "npm run format && npm run build && npm run test-coverage"
47+
"test-api": "node test/index.js",
48+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
49+
"test": "npm run format && npm run test-coverage"
5250
},
5351
"prettier": {
5452
"tabWidth": 2,
@@ -60,16 +58,10 @@
6058
},
6159
"xo": {
6260
"prettier": true,
63-
"esnext": false,
64-
"ignores": [
65-
"hast-util-assert.js"
66-
]
67-
},
68-
"nyc": {
69-
"check-coverage": true,
70-
"lines": 100,
71-
"functions": 100,
72-
"branches": 100
61+
"rules": {
62+
"no-var": "off",
63+
"prefer-arrow-callback": "off"
64+
}
7365
},
7466
"remarkConfig": {
7567
"plugins": [

readme.md

Lines changed: 10 additions & 3 deletions
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
@@ -21,7 +24,7 @@ npm install hast-util-assert
2124
## Use
2225

2326
```js
24-
var assert = require('hast-util-assert')
27+
import {assert} from 'hast-util-assert'
2528

2629
assert({type: 'root', children: []})
2730
assert({type: 'element', tagName: 'a', properties: {}, children: []})
@@ -36,14 +39,18 @@ assert({type: 'element', properties: {}, children: []})
3639

3740
## API
3841

42+
This package exports the following identifiers: `assert`, `parent`, `literal`,
43+
`_void`, `wrap`.
44+
There is no default export.
45+
3946
### `assert(tree)`
4047

4148
Assert that the given `tree` is a valid [**hast**][hast] [*tree*][tree].
4249
If `tree` is a [*parent*][parent], all [*children*][child] will be asserted as
4350
well.
4451

45-
The `assert.parent`, `assert.text`, `assert.void`, and `assert.wrap`
46-
methods from [`unist-util-assert`][unist-util-assert] are also included.
52+
The `parent`, `literal`, `_void`, and `_wrap` methods from
53+
[`unist-util-assert`][unist-util-assert] are also included.
4754

4855
## Security
4956

test/children.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var assert = require('..')
1+
import test from 'tape'
2+
import {assert} from '../index.js'
53

64
test('children', function (t) {
75
t.throws(

test/comment.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var assert = require('..')
1+
import test from 'tape'
2+
import {assert} from '../index.js'
53

64
test('assert(comment)', function (t) {
75
t.throws(
86
function () {
97
assert({type: 'comment'})
108
},
11-
/text should have `value`: `{ type: 'comment' }`$/,
9+
/literal should have `value`: `{ type: 'comment' }`$/,
1210
'should throw if a `comment` doesn’t have a value'
1311
)
1412

test/doctype.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var assert = require('..')
1+
import test from 'tape'
2+
import {assert} from '../index.js'
53

64
test('assert(doctype)', function (t) {
75
t.throws(

test/element.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var assert = require('..')
1+
import test from 'tape'
2+
import {assert} from '../index.js'
53

64
test('assert(element)', function (t) {
75
t.throws(

test/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./node')
5-
require('./children')
6-
require('./root')
7-
require('./element')
8-
require('./doctype')
9-
require('./text')
10-
require('./comment')
2+
import './node.js'
3+
import './parent.js'
4+
import './children.js'
5+
import './root.js'
6+
import './element.js'
7+
import './doctype.js'
8+
import './text.js'
9+
import './comment.js'
1110
/* eslint-enable import/no-unassigned-import */

test/node.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var assert = require('..')
1+
import test from 'tape'
2+
import {assert} from '../index.js'
53

64
test('node', function (t) {
75
t.throws(

test/parent.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import test from 'tape'
2+
import {parent} from '../index.js'
3+
4+
test('parent', function (t) {
5+
t.throws(
6+
function () {
7+
parent()
8+
},
9+
/node should be an object: `undefined`$/,
10+
'should throw if not given a node'
11+
)
12+
13+
t.throws(
14+
function () {
15+
parent({type: 'x'})
16+
},
17+
/parent should have `children`/,
18+
'should throw if not given a parent'
19+
)
20+
21+
t.end()
22+
})

0 commit comments

Comments
 (0)