Skip to content

Commit d9ff3b8

Browse files
committed
Refactor code-style
1 parent e429ed8 commit d9ff3b8

15 files changed

+422
-312
lines changed

index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expectType, expectNotType} from 'tsd'
1+
import {expectNotType, expectType} from 'tsd'
22
import type {Node, Parent} from 'unist'
33
import {assert, parent} from './index.js'
44

lib/index.js

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
/**
2-
* @typedef {import('assert').AssertionError} AssertionError
3-
* Assertion error from `node:assert`.
2+
* @typedef {import('node:assert').AssertionError} AssertionError
43
*
4+
* @typedef {import('unist').Literal} Literal
55
* @typedef {import('unist').Node} Node
66
* @typedef {import('unist').Parent} Parent
7-
* @typedef {import('unist').Literal} Literal
8-
* @typedef {import('unist').Position} Position
97
* @typedef {import('unist').Point} Point
8+
* @typedef {import('unist').Position} Position
9+
*/
10+
11+
/**
12+
* @template T
13+
* @callback CustomAssertion
14+
* @param {any} [node]
15+
* @param {Parent | null | undefined} [parent]
16+
* @returns {asserts node is T}
17+
*/
18+
19+
/**
1020
* @typedef {Node & {children: never, value: never}} _Void
1121
*
1222
* @typedef SeenErrorFields
@@ -100,9 +110,9 @@ const defined = new Set(['type', 'value', 'children', 'position'])
100110
*
101111
* @template {Node} T
102112
* Node type.
103-
* @param {(node?: any, parent?: Parent | null | undefined) => asserts node is T} fn
113+
* @param {CustomAssertion<T>} fn
104114
* Custom assertion.
105-
* @returns {(node?: any, parent?: Parent | null | undefined) => asserts node is T}
115+
* @returns {CustomAssertion<T>}
106116
* Assertion.
107117
*/
108118
export function wrap(fn) {
@@ -115,7 +125,7 @@ export function wrap(fn) {
115125
* Optional, valid parent.
116126
* @throws {AssertionError}
117127
* Whether `node` is a node but neither parent nor literal.
118-
* @returns {void}
128+
* @returns {undefined}
119129
* Nothing.
120130
*/
121131
function wrapped(node, parent) {
@@ -153,43 +163,36 @@ function assertNode(node) {
153163
node && typeof node === 'object' && !Array.isArray(node),
154164
'node should be an object'
155165
)
166+
indexable(node)
156167

157168
nodeAssert.ok(own.call(node, 'type'), 'node should have a type')
158169
nodeAssert.strictEqual(
159-
// @ts-expect-error Looks like an indexed object.
160170
typeof node.type,
161171
'string',
162172
'`type` should be a string'
163173
)
164-
// @ts-expect-error Looks like an indexed object.
165174
nodeAssert.notStrictEqual(node.type, '', '`type` should not be empty')
166175

167-
// @ts-expect-error Looks like an indexed object.
168176
if (node.value !== null && node.value !== undefined) {
169177
nodeAssert.strictEqual(
170-
// @ts-expect-error Looks like an indexed object.
171178
typeof node.value,
172179
'string',
173180
'`value` should be a string'
174181
)
175182
}
176183

177-
// @ts-expect-error Looks like an indexed object.
178184
position(node.position)
179185

180186
/** @type {string} */
181187
let key
182188

183189
for (key in node) {
184190
if (!defined.has(key)) {
185-
/** @type {unknown} */
186-
// @ts-expect-error: hush.
187191
const value = node[key]
188192
vanilla(key, value)
189193
}
190194
}
191195

192-
// @ts-expect-error Looks like an indexed object.
193196
if (node.children !== null && node.children !== undefined) {
194197
/** @type {Parent} */
195198
// @ts-expect-error Looks like parent.
@@ -324,9 +327,8 @@ function position(position) {
324327
'`position` should be an object'
325328
)
326329

327-
// @ts-expect-error: indexable.
330+
indexable(position)
328331
point(position.start, 'position.start')
329-
// @ts-expect-error: indexable.
330332
point(position.end, 'position.end')
331333
}
332334
}
@@ -374,3 +376,22 @@ function point(point, label) {
374376
}
375377
}
376378
}
379+
380+
/**
381+
* TypeScript helper to check if something is indexable (any object is
382+
* indexable in JavaScript).
383+
*
384+
* @param {unknown} value
385+
* Thing to check.
386+
* @returns {asserts value is Record<string, unknown>}
387+
* Nothing.
388+
* @throws {Error}
389+
* When `value` is not an object.
390+
*/
391+
function indexable(value) {
392+
// Always called when something is an object, this is just for TS.
393+
/* c8 ignore next 3 */
394+
if (!value || typeof value !== 'object') {
395+
throw new Error('Expected object')
396+
}
397+
}

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@
8383
"strict": true
8484
},
8585
"xo": {
86+
"overrides": [
87+
{
88+
"files": [
89+
"test/**/*.js"
90+
],
91+
"rules": {
92+
"import/no-unassigned-import": "off"
93+
}
94+
}
95+
],
8696
"prettier": true
8797
}
8898
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ In browsers with [`esm.sh`][esmsh]:
6969
## Use
7070

7171
```js
72-
import {assert, parent, _void} from 'unist-util-assert'
72+
import {_void, assert, parent} from 'unist-util-assert'
7373

7474
assert({type: 'root', children: []})
7575
assert({type: 'break'})

test/children.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ 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: 'foo', children: {alpha: 'bravo'}})
9-
},
10-
/`children` should be an array: `{ type: 'foo', 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: 'foo', children: {alpha: 'bravo'}})
11+
}, /`children` should be an array: `{ type: 'foo', children: { alpha: 'bravo' } }`$/)
12+
}
1213
)
1314

14-
nodeAssert.throws(
15-
() => {
16-
assert({type: 'foo', children: ['one']})
17-
},
18-
/node should be an object: `'one'` in `{ type: 'foo', 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: 'foo', children: ['one']})
20+
}, /node should be an object: `'one'` in `{ type: 'foo', children: \[ 'one' ] }`$/)
21+
}
2022
)
2123

22-
nodeAssert.doesNotThrow(() => {
23-
assert({type: 'parent', 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: 'parent', 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: 'foo',
3034
children: [
@@ -34,8 +38,6 @@ test('children', () => {
3438
}
3539
]
3640
})
37-
},
38-
/node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/,
39-
'should throw on invalid descendants'
40-
)
41+
}, /node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/)
42+
})
4143
})

test/core.js

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

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

test/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/* eslint-disable import/no-unassigned-import */
1+
import './children.js'
22
import './core.js'
3+
import './literal.js'
34
import './node.js'
4-
import './type.js'
5-
import './value.js'
6-
import './children.js'
7-
import './position.js'
85
import './non-defined.js'
96
import './parent.js'
10-
import './literal.js'
7+
import './position.js'
8+
import './type.js'
9+
import './value.js'
1110
import './void.js'
12-
/* eslint-enable import/no-unassigned-import */

test/literal.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@ import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import {literal} from '../index.js'
44

5-
test('literal()', () => {
6-
assert.throws(
7-
() => {
5+
test('literal()', async function (t) {
6+
await t.test('should throw the same errors as `assert()`', async function () {
7+
assert.throws(function () {
88
literal({})
9-
},
10-
/node should have a type: `{}`$/,
11-
'should throw the same errors as `assert()`'
12-
)
9+
}, /node should have a type: `{}`$/)
10+
})
1311

14-
assert.throws(
15-
() => {
16-
literal({type: 'strong', children: []})
17-
},
18-
/literal should not have `children`: `{ type: 'strong', children: \[] }`$/,
19-
'should throw if the given node has `children`'
12+
await t.test(
13+
'should throw if the given node has `children`',
14+
async function () {
15+
assert.throws(function () {
16+
literal({type: 'strong', children: []})
17+
}, /literal should not have `children`: `{ type: 'strong', children: \[] }`$/)
18+
}
2019
)
2120

22-
assert.throws(
23-
() => {
24-
literal({type: 'break'})
25-
},
26-
/literal should have `value`: `{ type: 'break' }`$/,
27-
'should throw if the given node has no `value`'
21+
await t.test(
22+
'should throw if the given node has no `value`',
23+
async function () {
24+
assert.throws(function () {
25+
literal({type: 'break'})
26+
}, /literal should have `value`: `{ type: 'break' }`$/)
27+
}
2828
)
2929

30-
assert.doesNotThrow(() => {
31-
literal({type: 'text', value: 'foo'})
32-
}, 'should not throw on valid text nodes')
30+
await t.test('should not throw on valid text nodes', async function () {
31+
assert.doesNotThrow(function () {
32+
literal({type: 'text', value: 'foo'})
33+
})
34+
})
3335
})

test/node.js

+17-25
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)