Skip to content

Commit c37d0e5

Browse files
committed
Use ESM
1 parent a436c39 commit c37d0e5

16 files changed

+126
-142
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-assert.js
7-
unist-util-assert.min.js
85
yarn.lock

index.js

+57-58
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
1-
'use strict'
1+
import nodeAssert from 'assert'
2+
import {inspect} from './inspect.js'
23

3-
var assert = require('assert')
4-
var array = require('x-is-array')
5-
var object = require('x-is-object')
6-
7-
var inspect
8-
9-
try {
10-
// eslint-disable-next-line no-useless-concat
11-
inspect = require('ut' + 'il').inspect
12-
} catch (_) {}
13-
14-
exports = wrap(unist)
15-
module.exports = exports
16-
17-
exports.parent = wrap(parent)
18-
exports.text = wrap(text)
19-
exports.void = wrap(empty)
20-
exports.wrap = wrap
4+
export var assert = wrap(assertNode)
5+
assert.parent = wrap(parent)
6+
assert.text = wrap(text)
7+
assert.void = wrap(empty)
218

229
// Identifier to check if a value is seen.
2310
var ID = '__unist__'
2411

2512
// List of specced properties.
26-
var defined = ['type', 'value', 'children', 'position']
13+
var defined = new Set(['type', 'value', 'children', 'position'])
2714

2815
// Wrapper around `Node` which adds the current node (and parent, if available),
2916
// to the message.
30-
function wrap(fn) {
17+
export function wrap(fn) {
3118
return wrapped
3219

3320
function wrapped(node, parent) {
@@ -50,43 +37,43 @@ function wrap(fn) {
5037
}
5138

5239
// Assert.
53-
function unist(node) {
40+
function assertNode(node) {
5441
var type
5542
var children
5643
var value
5744
var key
5845
var index
5946
var length
6047

61-
assert.ok(object(node), 'node should be an object')
48+
nodeAssert.ok(node === Object(node), 'node should be an object')
6249

6350
type = node.type
6451
children = node.children
6552
value = node.value
6653

67-
assert.ok('type' in node, 'node should have a type')
68-
assert.strictEqual(typeof type, 'string', '`type` should be a string')
69-
assert.notStrictEqual(type, '', '`type` should not be empty')
54+
nodeAssert.ok('type' in node, 'node should have a type')
55+
nodeAssert.strictEqual(typeof type, 'string', '`type` should be a string')
56+
nodeAssert.notStrictEqual(type, '', '`type` should not be empty')
7057

71-
if (value != null) {
72-
assert.strictEqual(typeof value, 'string', '`value` should be a string')
58+
if (value !== null && value !== undefined) {
59+
nodeAssert.strictEqual(typeof value, 'string', '`value` should be a string')
7360
}
7461

7562
position(node.position)
7663

7764
for (key in node) {
78-
if (!defined.includes(key)) {
65+
if (!defined.has(key)) {
7966
vanilla(key, node[key])
8067
}
8168
}
8269

83-
if (children != null) {
84-
assert.ok(array(children), '`children` should be an array')
70+
if (children !== null && children !== undefined) {
71+
nodeAssert.ok(Array.isArray(children), '`children` should be an array')
8572
index = -1
8673
length = children.length
8774

8875
while (++index < length) {
89-
exports(children[index], node)
76+
assert(children[index], node)
9077
}
9178
}
9279
}
@@ -95,50 +82,53 @@ function unist(node) {
9582
// same (deep) value.
9683
function vanilla(key, value) {
9784
try {
98-
assert.deepStrictEqual(value, JSON.parse(JSON.stringify(value)))
99-
} catch (_) {
100-
assert.fail('non-specced property `' + key + '` should be JSON')
85+
nodeAssert.deepStrictEqual(value, JSON.parse(JSON.stringify(value)))
86+
} catch {
87+
nodeAssert.fail('non-specced property `' + key + '` should be JSON')
10188
}
10289
}
10390

10491
// Stringify a value to inspect it.
10592
// Tries `JSON.stringify()`, and if that fails uses `String()` instead.
10693
function view(value) {
10794
try {
108-
/* istanbul ignore next - Browser. */
109-
return inspect ? inspect(value, {colors: false}) : JSON.stringify(value)
110-
} catch (_) {
111-
/* istanbul ignore next - Cyclical. */
95+
return inspect(value)
96+
/* c8 ignore next 3 */
97+
} catch {
11298
return String(value)
11399
}
114100
}
115101

116102
// Assert `node` is a parent node.
117103
function parent(node) {
118-
unist(node)
104+
assertNode(node)
119105

120-
assert.strictEqual('value' in node, false, 'parent should not have `value`')
121-
assert.ok('children' in node, 'parent should have `children`')
106+
nodeAssert.strictEqual(
107+
'value' in node,
108+
false,
109+
'parent should not have `value`'
110+
)
111+
nodeAssert.ok('children' in node, 'parent should have `children`')
122112
}
123113

124114
// Assert `node` is a text node.
125115
function text(node) {
126-
unist(node)
116+
assertNode(node)
127117

128-
assert.strictEqual(
118+
nodeAssert.strictEqual(
129119
'children' in node,
130120
false,
131121
'text should not have `children`'
132122
)
133-
assert.ok('value' in node, 'text should have `value`')
123+
nodeAssert.ok('value' in node, 'text should have `value`')
134124
}
135125

136126
// Assert `node` is a unist node, but neither parent nor text.
137127
function empty(node) {
138-
unist(node)
128+
assertNode(node)
139129

140-
assert.strictEqual('value' in node, false, 'void should not have `value`')
141-
assert.strictEqual(
130+
nodeAssert.strictEqual('value' in node, false, 'void should not have `value`')
131+
nodeAssert.strictEqual(
142132
'children' in node,
143133
false,
144134
'void should not have `children`'
@@ -147,8 +137,11 @@ function empty(node) {
147137

148138
// Assert `position` is a unist position.
149139
function position(position) {
150-
if (position != null) {
151-
assert.ok(object(position), '`position` should be an object')
140+
if (position !== null && position !== undefined) {
141+
nodeAssert.ok(
142+
position === Object(position),
143+
'`position` should be an object'
144+
)
152145

153146
point(position.start, 'position.start')
154147
point(position.end, 'position.end')
@@ -157,20 +150,26 @@ function position(position) {
157150

158151
// Assert `point` is a unist point.
159152
function point(point, name) {
160-
if (point != null) {
161-
assert.ok(object(point), '`' + name + '` should be an object')
153+
if (point !== null && point !== undefined) {
154+
nodeAssert.ok(point === Object(point), '`' + name + '` should be an object')
162155

163-
if (point.line != null) {
164-
assert.ok('line' in point, '`' + name + '` should have numeric `line`')
165-
assert.ok(point.line >= 1, '`' + name + '.line` should be gte `1`')
156+
if (point.line !== null && point.line !== undefined) {
157+
nodeAssert.ok(
158+
'line' in point,
159+
'`' + name + '` should have numeric `line`'
160+
)
161+
nodeAssert.ok(point.line >= 1, '`' + name + '.line` should be gte `1`')
166162
}
167163

168-
if (point.column != null) {
169-
assert.ok(
164+
if (point.column !== null && point.column !== undefined) {
165+
nodeAssert.ok(
170166
'column' in point,
171167
'`' + name + '` should have numeric `column`'
172168
)
173-
assert.ok(point.column >= 1, '`' + name + '.column` should be gte `1`')
169+
nodeAssert.ok(
170+
point.column >= 1,
171+
'`' + name + '.column` should be gte `1`'
172+
)
174173
}
175174
}
176175
}

inspect.browser.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function inspect(value) {
2+
return JSON.stringify(value)
3+
}

inspect.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {inspect as utilInspect} from 'util'
2+
3+
export function inspect(value) {
4+
return utilInspect(value, {colors: false})
5+
}

package.json

+22-28
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,38 @@
2222
"contributors": [
2323
"Titus Wormer <[email protected]> (https://wooorm.com)"
2424
],
25-
"dependencies": {
26-
"x-is-array": "^0.1.0",
27-
"x-is-object": "^0.1.0"
25+
"sideEffects": false,
26+
"type": "module",
27+
"main": "index.js",
28+
"browser": {
29+
"./inspect.js": "./inspect.browser.js"
2830
},
31+
"react-native": {
32+
"./inspect.js": "./inspect.browser.js"
33+
},
34+
"types": "types/index.d.ts",
2935
"files": [
3036
"index.js",
37+
"inspect.js",
38+
"inspect.browser.js",
3139
"types/index.d.ts"
3240
],
33-
"types": "types/index.d.ts",
41+
"dependencies": {},
3442
"devDependencies": {
35-
"browserify": "^17.0.0",
43+
"c8": "^7.0.0",
3644
"dtslint": "^4.0.0",
37-
"nyc": "^15.0.0",
3845
"prettier": "^2.0.0",
3946
"remark-cli": "^9.0.0",
4047
"remark-preset-wooorm": "^8.0.0",
4148
"tape": "^5.0.0",
42-
"tinyify": "^3.0.0",
43-
"xo": "^0.35.0"
49+
"xo": "^0.38.0"
4450
},
4551
"scripts": {
4652
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
47-
"build-bundle": "browserify . -s unistUtilAssert -o unist-util-assert.js",
48-
"build-mangle": "browserify . -s unistUtilAssert -o unist-util-assert.min.js -p tinyify",
49-
"build": "npm run build-bundle && npm run build-mangle",
50-
"test-api": "node test",
51-
"test-coverage": "nyc --reporter lcov tape test",
53+
"test-api": "node test/index.js",
54+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
5255
"test-types": "dtslint types",
53-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
56+
"test": "npm run format && npm run test-coverage && npm run test-types"
5457
},
5558
"prettier": {
5659
"tabWidth": 2,
@@ -62,24 +65,15 @@
6265
},
6366
"xo": {
6467
"prettier": true,
65-
"esnext": false,
6668
"rules": {
67-
"unicorn/prefer-optional-catch-binding": "off",
68-
"unicorn/prefer-set-has": "off",
69-
"no-eq-null": "off",
70-
"eqeqeq": "off"
69+
"import/no-mutable-exports": "off",
70+
"no-var": "off",
71+
"prefer-arrow-callback": "off"
7172
},
72-
"ignores": [
73-
"types/",
74-
"unist-util-assert.js"
73+
"ignore": [
74+
"types/"
7575
]
7676
},
77-
"nyc": {
78-
"check-coverage": true,
79-
"lines": 100,
80-
"functions": 100,
81-
"branches": 100
82-
},
8377
"remarkConfig": {
8478
"plugins": [
8579
"preset-wooorm"

readme.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
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

17-
```bash
20+
```sh
1821
npm install unist-util-assert
1922
```
2023

2124
## Use
2225

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

2629
assert({type: 'root', children: []})
2730
assert({type: 'break'})
@@ -46,6 +49,9 @@ assert({type: 'paragraph', children: ['foo']})
4649

4750
## API
4851

52+
This package exports the following identifiers: `assert`, `wrap`.
53+
There is no default export.
54+
4955
### `assert(tree)`
5056

5157
Assert that [`tree`][tree] is a valid [unist][] [node][].
@@ -70,11 +76,11 @@ Assert that `node` is a valid [unist][] [node][], but neither [parent][] nor
7076
This module can be used as a base to test subsets of [unist][] (for an example,
7177
see [`mdast-util-assert`][mdast-util-assert]).
7278
All functions that are exposed from such a module, and functions used internally
73-
to test [child][]ren, should be wrapped in `assert.wrap`, which adds an
74-
inspectable string of the respective node, and its parent when available, to
75-
the exposed error message.
79+
to test [child][]ren, should be wrapped in `wrap`, which adds an inspectable
80+
string of the respective node, and its parent when available, to the exposed
81+
error message.
7682

77-
### `assert.wrap(fn)`
83+
### `wrap(fn)`
7884

7985
Wraps `fn` (which is passed a node, and an optional parent node), so that any
8086
errors thrown inside it will contain information regarding the node (and the

test/children.js

+2-4
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/index.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./node')
5-
require('./type')
6-
require('./value')
7-
require('./children')
8-
require('./position')
9-
require('./non-defined')
10-
require('./parent')
11-
require('./text')
12-
require('./void')
2+
import './node.js'
3+
import './type.js'
4+
import './value.js'
5+
import './children.js'
6+
import './position.js'
7+
import './non-defined.js'
8+
import './parent.js'
9+
import './text.js'
10+
import './void.js'
1311
/* eslint-enable import/no-unassigned-import */

0 commit comments

Comments
 (0)