Skip to content

Commit 4788dfa

Browse files
committed
Refactor code-style
1 parent 905df84 commit 4788dfa

File tree

12 files changed

+155
-175
lines changed

12 files changed

+155
-175
lines changed

index.test-d.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import {expectType, expectNotType} from 'tsd'
2-
import type {Parent as UnistParent} from 'unist'
3-
import type {Root, Content} from 'hast'
1+
import type {Nodes, Parents} from 'hast'
2+
import {expectNotType, expectType} from 'tsd'
43
import {assert, parent} from './index.js'
54

6-
type Node = Root | Content
7-
type Parent = Extract<Node, UnistParent>
8-
95
const emptyNode = {type: 'doctype'}
106
const literalNode = {type: 'text', value: 'a'}
117
const parentNode = {type: 'element', children: [emptyNode, literalNode]}
128

13-
expectNotType<Node>(emptyNode)
14-
expectNotType<Node>(literalNode)
15-
expectNotType<Node>(parentNode)
9+
expectNotType<Nodes>(emptyNode)
10+
expectNotType<Nodes>(literalNode)
11+
expectNotType<Nodes>(parentNode)
1612

1713
assert(emptyNode)
18-
expectType<Node>(emptyNode)
14+
expectType<Nodes>(emptyNode)
1915

20-
expectNotType<Parent>(parentNode)
16+
expectNotType<Parents>(parentNode)
2117
parent(parentNode)
22-
expectType<Parent>(parentNode)
18+
expectType<Parents>(parentNode)

lib/index.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
/**
2-
* @typedef {import('unist-util-assert').AssertionError} AssertionError
2+
* @typedef {import('hast').Literals} Literals
3+
* @typedef {import('hast').Element} Element
4+
* @typedef {import('hast').Nodes} Nodes
5+
* @typedef {import('hast').Parents} Parents
6+
* @typedef {import('hast').Root} Root
37
*
48
* @typedef {import('unist').Node} UnistNode
59
* @typedef {import('unist').Parent} UnistParent
6-
* @typedef {import('unist').Literal} UnistLiteral
7-
* @typedef {import('hast').Root} Root
8-
* @typedef {import('hast').Content} Content
9-
* @typedef {import('hast').Element} Element
10-
*/
11-
12-
/**
13-
* @typedef {Root | Content} Node
14-
* @typedef {Extract<Node, UnistParent>} Parent
15-
* @typedef {Extract<Node, UnistLiteral>} Literal
10+
*
11+
* @typedef {import('unist-util-assert').AssertionError} AssertionError
1612
*/
1713

1814
import nodeAssert from 'node:assert'
19-
import {zwitch} from 'zwitch'
2015
import {mapz} from 'mapz'
2116
import {
17+
_void,
2218
assert as unistAssert,
23-
parent as unistParent,
2419
literal as unistLiteral,
25-
wrap,
26-
_void
20+
parent as unistParent,
21+
wrap
2722
} from 'unist-util-assert'
23+
import {zwitch} from 'zwitch'
2824

2925
/**
3026
* Assert that `tree` is a valid hast node.
@@ -37,7 +33,7 @@ import {
3733
* Thing to assert.
3834
* @param {UnistParent | null | undefined} [parent]
3935
* Optional, valid parent.
40-
* @returns {asserts tree is Node}
36+
* @returns {asserts tree is Nodes}
4137
* Nothing.
4238
* @throws {AssertionError}
4339
* When `tree` (or its descendants) is not a hast node.
@@ -57,7 +53,7 @@ export function assert(tree, parent) {
5753
* Thing to assert.
5854
* @param {UnistParent | null | undefined} [parent]
5955
* Optional, valid parent.
60-
* @returns {asserts tree is Parent}
56+
* @returns {asserts tree is Parents}
6157
* Nothing.
6258
* @throws {AssertionError}
6359
* When `tree` is not a parent or its descendants are not nodes.
@@ -75,7 +71,7 @@ export function parent(tree, parent) {
7571
* Thing to assert.
7672
* @param {UnistParent | null | undefined} [parent]
7773
* Optional, valid parent.
78-
* @returns {asserts node is Literal}
74+
* @returns {asserts node is Literals}
7975
* Nothing.
8076
* @throws {AssertionError}
8177
* When `node` is not a hast literal.
@@ -125,7 +121,7 @@ function unknown(node, parent) {
125121
*
126122
* @param {unknown} [tree]
127123
* Thing to assert.
128-
* @returns {asserts tree is Parent}
124+
* @returns {asserts tree is Parents}
129125
* Nothing.
130126
* @throws {AssertionError}
131127
* When `tree` is not a parent or its descendants are not nodes.
@@ -142,7 +138,7 @@ function assertParent(tree) {
142138
*
143139
* @param {unknown} [node]
144140
* Thing to assert.
145-
* @returns {asserts node is Literal}
141+
* @returns {asserts node is Literals}
146142
* Nothing.
147143
* @throws {AssertionError}
148144
* When `node` is not a hast literal.
@@ -192,18 +188,11 @@ function assertRoot(tree, parent) {
192188
function assertElement(tree) {
193189
assertParent(tree)
194190

195-
nodeAssert.strictEqual(
196-
// @ts-expect-error: hush.
197-
typeof tree.tagName,
198-
'string',
191+
nodeAssert.ok(
192+
'tagName' in tree && typeof tree.tagName === 'string',
199193
'`element` should have a `tagName`'
200194
)
201-
nodeAssert.notStrictEqual(
202-
// @ts-expect-error: hush.
203-
tree.tagName,
204-
'',
205-
'`element.tagName` should not be empty'
206-
)
195+
nodeAssert.ok(tree.tagName !== '', '`element.tagName` should not be empty')
207196
}
208197

209198
export {_void, wrap} from 'unist-util-assert'

test/children.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,37 @@ import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
33
import {assert} from '../index.js'
44

5-
test('children', () => {
6-
nodeAssert.throws(
7-
() => {
8-
assert({type: 'paragraph', children: {alpha: 'bravo'}})
9-
},
10-
/`children` should be an array: `{ type: 'paragraph', children: { alpha: 'bravo' } }`$/,
11-
'should throw if given a non-node child in children'
5+
test('children', async function (t) {
6+
await t.test(
7+
'should throw if given a non-node child in children',
8+
async function () {
9+
nodeAssert.throws(function () {
10+
assert({type: 'paragraph', children: {alpha: 'bravo'}})
11+
}, /`children` should be an array: `{ type: 'paragraph', children: { alpha: 'bravo' } }`$/)
12+
}
1213
)
1314

14-
nodeAssert.throws(
15-
() => {
16-
assert({type: 'paragraph', children: ['one']})
17-
},
18-
/node should be an object: `'one'` in `{ type: 'paragraph', children: \[ 'one' ] }`$/,
19-
'should throw if given a non-node child in children'
15+
await t.test(
16+
'should throw if given a non-node child in children',
17+
async function () {
18+
nodeAssert.throws(function () {
19+
assert({type: 'paragraph', children: ['one']})
20+
}, /node should be an object: `'one'` in `{ type: 'paragraph', children: \[ 'one' ] }`$/)
21+
}
2022
)
2123

22-
nodeAssert.doesNotThrow(() => {
23-
assert({type: 'paragraph', children: [{type: 'text', value: 'alpha'}]})
24-
}, 'should not throw on vald children')
24+
await t.test('should not throw on vald children', async function () {
25+
nodeAssert.doesNotThrow(function () {
26+
assert({type: 'paragraph', children: [{type: 'text', value: 'alpha'}]})
27+
})
28+
})
2529

26-
nodeAssert.throws(
27-
() => {
30+
await t.test('should throw on invalid descendants', async function () {
31+
nodeAssert.throws(function () {
2832
assert({
2933
type: 'paragraph',
30-
children: [
31-
{
32-
type: 'bar',
33-
children: ['one']
34-
}
35-
]
34+
children: [{type: 'bar', children: ['one']}]
3635
})
37-
},
38-
/node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/,
39-
'should throw on invalid descendants'
40-
)
36+
}, /node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/)
37+
})
4138
})

test/comment.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
33
import {assert} from '../index.js'
44

5-
test('assert(comment)', () => {
6-
nodeAssert.throws(
7-
() => {
8-
assert({type: 'comment'})
9-
},
10-
/literal should have `value`: `{ type: 'comment' }`$/,
11-
'should throw if a `comment` doesn’t have a value'
5+
test('assert(comment)', async function (t) {
6+
await t.test(
7+
'should throw if a `comment` doesn’t have a value',
8+
async function () {
9+
nodeAssert.throws(function () {
10+
assert({type: 'comment'})
11+
}, /literal should have `value`: `{ type: 'comment' }`$/)
12+
}
1213
)
1314

14-
nodeAssert.doesNotThrow(() => {
15-
assert({type: 'comment', value: 'Alpha'})
16-
}, 'should allow `value`')
15+
await t.test('should allow `value`', async function () {
16+
nodeAssert.doesNotThrow(function () {
17+
assert({type: 'comment', value: 'Alpha'})
18+
})
19+
})
1720
})

test/core.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
3-
import * as mod from '../index.js'
43

5-
test('api', () => {
6-
nodeAssert.deepEqual(
7-
Object.keys(mod).sort(),
8-
['_void', 'assert', 'literal', 'parent', 'wrap'],
9-
'should expose the public api'
10-
)
4+
test('api', async function (t) {
5+
await t.test('should expose the public api', async function () {
6+
nodeAssert.deepEqual(Object.keys(await import('../index.js')).sort(), [
7+
'_void',
8+
'assert',
9+
'literal',
10+
'parent',
11+
'wrap'
12+
])
13+
})
1114
})

test/doctype.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
33
import {assert} from '../index.js'
44

5-
test('assert(doctype)', () => {
6-
nodeAssert.doesNotThrow(() => {
7-
assert({type: 'doctype'})
8-
}, 'should allow doctypes')
5+
test('assert(doctype)', async function (t) {
6+
await t.test('should allow doctypes', async function () {
7+
nodeAssert.doesNotThrow(function () {
8+
assert({type: 'doctype'})
9+
})
10+
})
911
})

test/element.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
33
import {assert} from '../index.js'
44

5-
test('assert(element)', () => {
6-
nodeAssert.throws(
7-
() => {
8-
assert({type: 'element'})
9-
},
10-
/parent should have `children`: `{ type: 'element' }`$/,
11-
'should throw if a `element` is not a parent'
5+
test('assert(element)', async function (t) {
6+
await t.test(
7+
'should throw if a `element` is not a parent',
8+
async function () {
9+
nodeAssert.throws(function () {
10+
assert({type: 'element'})
11+
}, /parent should have `children`: `{ type: 'element' }`$/)
12+
}
1213
)
1314

14-
nodeAssert.throws(
15-
() => {
16-
assert({type: 'element', children: []})
17-
},
18-
/`element` should have a `tagName`: `{ type: 'element', children: \[] }`$/,
19-
'should throw if a `element` has no `tagName`'
15+
await t.test(
16+
'should throw if a `element` has no `tagName`',
17+
async function () {
18+
nodeAssert.throws(function () {
19+
assert({type: 'element', children: []})
20+
}, /`element` should have a `tagName`: `{ type: 'element', children: \[] }`$/)
21+
}
2022
)
2123

22-
nodeAssert.throws(
23-
() => {
24-
assert({type: 'element', tagName: '', children: []})
25-
},
26-
/`element.tagName` should not be empty: `{ type: 'element', tagName: '', children: \[] }`$/,
27-
'should throw if a `element` has an empty `tagName`'
24+
await t.test(
25+
'should throw if a `element` has an empty `tagName`',
26+
async function () {
27+
nodeAssert.throws(function () {
28+
assert({type: 'element', tagName: '', children: []})
29+
}, /`element.tagName` should not be empty: `{ type: 'element', tagName: '', children: \[] }`$/)
30+
}
2831
)
2932
})

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint-disable import/no-unassigned-import */
2+
import './children.js'
3+
import './comment.js'
24
import './core.js'
5+
import './doctype.js'
6+
import './element.js'
37
import './node.js'
48
import './parent.js'
5-
import './children.js'
69
import './root.js'
7-
import './element.js'
8-
import './doctype.js'
910
import './text.js'
10-
import './comment.js'
1111
/* eslint-enable import/no-unassigned-import */

test/node.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,28 @@ import nodeAssert from 'node:assert/strict'
22
import test from 'node:test'
33
import {assert} from '../index.js'
44

5-
test('node', () => {
6-
nodeAssert.throws(
7-
() => {
5+
test('node', async function (t) {
6+
await t.test('should throw if not given a node (#1)', async function () {
7+
nodeAssert.throws(function () {
88
assert()
9-
},
10-
/node should be an object: `undefined`$/,
11-
'should throw if not given a node (#1)'
12-
)
9+
}, /node should be an object: `undefined`$/)
10+
})
1311

14-
nodeAssert.throws(
15-
() => {
12+
await t.test('should throw if not given a node (#2)', async function () {
13+
nodeAssert.throws(function () {
1614
assert(null)
17-
},
18-
/node should be an object: `null`$/,
19-
'should throw if not given a node (#2)'
20-
)
15+
}, /node should be an object: `null`$/)
16+
})
2117

22-
nodeAssert.throws(
23-
() => {
18+
await t.test('should throw if given a non-node (#1)', async function () {
19+
nodeAssert.throws(function () {
2420
assert('foo')
25-
},
26-
/node should be an object: `'foo'`$/,
27-
'should throw if given a non-node (#1)'
28-
)
21+
}, /node should be an object: `'foo'`$/)
22+
})
2923

30-
nodeAssert.throws(
31-
() => {
24+
await t.test('should throw if not given a non-node (#2)', async function () {
25+
nodeAssert.throws(function () {
3226
assert(6)
33-
},
34-
/node should be an object: `6`$/,
35-
'should throw if not given a non-node (#2)'
36-
)
27+
}, /node should be an object: `6`$/)
28+
})
3729
})

0 commit comments

Comments
 (0)