Skip to content

Commit d81ed61

Browse files
committed
Refactor code-style
1 parent febcd25 commit d81ed61

18 files changed

+547
-480
lines changed

index.test-d.ts

Lines changed: 1 addition & 1 deletion
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 {Nodes, Parents} from 'mdast'
33
import {assert, parent} from './index.js'
44

lib/index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
/**
2-
* @typedef {import('unist-util-assert').AssertionError} AssertionError
3-
*
4-
* @typedef {import('unist').Parent} UnistParent
5-
*
6-
* @typedef {import('mdast').Root} Root
7-
* @typedef {import('mdast').Nodes} Nodes
8-
* @typedef {import('mdast').Parents} Parents
9-
* @typedef {import('mdast').Literals} Literals
10-
* @typedef {import('mdast').List} List
11-
* @typedef {import('mdast').ListItem} ListItem
12-
* @typedef {import('mdast').Heading} Heading
132
* @typedef {import('mdast').Code} Code
14-
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
153
* @typedef {import('mdast').Definition} Definition
16-
* @typedef {import('mdast').Link} Link
4+
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
5+
* @typedef {import('mdast').FootnoteReference} FootnoteReference
6+
* @typedef {import('mdast').Heading} Heading
177
* @typedef {import('mdast').Image} Image
18-
* @typedef {import('mdast').LinkReference} LinkReference
198
* @typedef {import('mdast').ImageReference} ImageReference
20-
* @typedef {import('mdast').FootnoteReference} FootnoteReference
9+
* @typedef {import('mdast').Link} Link
10+
* @typedef {import('mdast').LinkReference} LinkReference
11+
* @typedef {import('mdast').List} List
12+
* @typedef {import('mdast').ListItem} ListItem
13+
* @typedef {import('mdast').Literals} Literals
14+
* @typedef {import('mdast').Nodes} Nodes
15+
* @typedef {import('mdast').Parents} Parents
16+
* @typedef {import('mdast').Root} Root
2117
* @typedef {import('mdast').Table} Table
18+
*
19+
* @typedef {import('unist').Parent} UnistParent
20+
*
21+
* @typedef {import('unist-util-assert').AssertionError} AssertionError
22+
*
2223
*/
2324

2425
import nodeAssert from 'node:assert'
25-
import {zwitch} from 'zwitch'
2626
import {mapz} from 'mapz'
2727
import {
28+
_void,
2829
assert as unistAssert,
29-
parent as unistParent,
3030
literal as unistLiteral,
31-
wrap,
32-
_void
31+
parent as unistParent,
32+
wrap
3333
} from 'unist-util-assert'
34+
import {zwitch} from 'zwitch'
3435

3536
/**
3637
* Assert that `tree` is a valid mdast node.

test/children.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +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',
3034
children: [{type: 'bar', children: ['one']}]
3135
})
32-
},
33-
/node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/,
34-
'should throw on invalid descendants'
35-
)
36+
}, /node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/)
37+
})
3638
})

test/code.js

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

5-
test('assert(code)', () => {
6-
nodeAssert.throws(
7-
() => {
5+
test('assert(code)', async function (t) {
6+
await t.test('should throw if `code` is not a text', async function () {
7+
nodeAssert.throws(function () {
88
assert({type: 'code'})
9-
},
10-
/literal should have `value`: `{ type: 'code' }`$/,
11-
'should throw if `code` is not a text'
12-
)
13-
14-
nodeAssert.doesNotThrow(() => {
15-
assert({type: 'code', value: ''})
16-
}, 'should not throw if `code` has no extra properties')
9+
}, /literal should have `value`: `{ type: 'code' }`$/)
10+
})
11+
await t.test('', async function () {
12+
nodeAssert.doesNotThrow(function () {
13+
assert({
14+
type: 'code',
15+
value: 'should not throw if `code` has no extra properties'
16+
})
17+
})
18+
})
1719

18-
nodeAssert.throws(
19-
() => {
20+
await t.test('should throw if `lang` is not a string', async function () {
21+
nodeAssert.throws(function () {
2022
assert({type: 'code', lang: 0, value: ''})
21-
},
22-
/`lang` must be `string`: `{ type: 'code', lang: 0, value: '' }`$/,
23-
'should throw if `lang` is not a string'
24-
)
23+
}, /`lang` must be `string`: `{ type: 'code', lang: 0, value: '' }`$/)
24+
})
2525

26-
nodeAssert.throws(
27-
() => {
26+
await t.test('should throw if `meta` is not a string', async function () {
27+
nodeAssert.throws(function () {
2828
assert({type: 'code', lang: 'js', meta: 1, value: ''})
29-
},
30-
/`meta` must be `string`: `{ type: 'code', lang: 'js', meta: 1, value: '' }`$/,
31-
'should throw if `meta` is not a string'
32-
)
29+
}, /`meta` must be `string`: `{ type: 'code', lang: 'js', meta: 1, value: '' }`$/)
30+
})
3331

34-
nodeAssert.throws(
35-
() => {
36-
assert({type: 'code', meta: '', value: ''})
37-
},
38-
/code with `meta` must also have `lang`: `{ type: 'code', meta: '', value: '' }`$/,
39-
'should throw if `meta` is defined but not `lang`'
32+
await t.test(
33+
'should throw if `meta` is defined but not `lang`',
34+
async function () {
35+
nodeAssert.throws(function () {
36+
assert({type: 'code', meta: '', value: ''})
37+
}, /code with `meta` must also have `lang`: `{ type: 'code', meta: '', value: '' }`$/)
38+
}
4039
)
4140
})

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('assert', () => {
6-
nodeAssert.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+
nodeAssert.deepEqual(Object.keys(await import('../index.js')).sort(), [
7+
'_void',
8+
'assert',
9+
'literal',
10+
'parent',
11+
'wrap'
12+
])
13+
})
1114
})

test/definition.js

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

5-
test('assert(definition)', () => {
6-
nodeAssert.throws(
7-
() => {
8-
assert({type: 'definition'})
9-
},
10-
/`identifier` must be `string`: `{ type: 'definition' }`$/,
11-
'should throw if `definition` has no `identifier`'
5+
test('assert(definition)', async function (t) {
6+
await t.test(
7+
'should throw if `definition` has no `identifier`',
8+
async function () {
9+
nodeAssert.throws(function () {
10+
assert({type: 'definition'})
11+
}, /`identifier` must be `string`: `{ type: 'definition' }`$/)
12+
}
1213
)
1314

14-
nodeAssert.throws(
15-
() => {
15+
await t.test('should throw if `definition` has no `url`', async function () {
16+
nodeAssert.throws(function () {
1617
assert({type: 'definition', identifier: '1'})
17-
},
18-
/`url` must be `string`: `{ type: 'definition', identifier: '1' }`$/,
19-
'should throw if `definition` has no `url`'
20-
)
18+
}, /`url` must be `string`: `{ type: 'definition', identifier: '1' }`$/)
19+
})
2120

22-
nodeAssert.throws(
23-
() => {
24-
assert({type: 'definition', identifier: 1})
25-
},
26-
/`identifier` must be `string`: `{ type: 'definition', identifier: 1 }`$/,
27-
'should throw if `identifier` is not a `string`'
21+
await t.test(
22+
'should throw if `identifier` is not a `string`',
23+
async function () {
24+
nodeAssert.throws(function () {
25+
assert({type: 'definition', identifier: 1})
26+
}, /`identifier` must be `string`: `{ type: 'definition', identifier: 1 }`$/)
27+
}
2828
)
2929

30-
nodeAssert.throws(
31-
() => {
30+
await t.test('should throw if `url` is not a `string`', async function () {
31+
nodeAssert.throws(function () {
3232
assert({type: 'definition', url: 1})
33-
},
34-
/`identifier` must be `string`: `{ type: 'definition', url: 1 }`$/,
35-
'should throw if `url` is not a `string`'
36-
)
33+
}, /`identifier` must be `string`: `{ type: 'definition', url: 1 }`$/)
34+
})
3735

38-
nodeAssert.doesNotThrow(() => {
39-
assert({type: 'definition', identifier: '1', url: '1'})
40-
}, 'should not throw if `definition` has no other properties')
36+
await t.test(
37+
'should not throw if `definition` has no other properties',
38+
async function () {
39+
nodeAssert.doesNotThrow(function () {
40+
assert({type: 'definition', identifier: '1', url: '1'})
41+
})
42+
}
43+
)
4144

42-
nodeAssert.throws(
43-
() => {
45+
await t.test('should throw if `title` is not a `string`', async function () {
46+
nodeAssert.throws(function () {
4447
assert({type: 'definition', identifier: '1', url: '1', title: 1})
45-
},
46-
/`title` must be `string`: `{ type: 'definition', identifier: '1', url: '1', title: 1 }`$/,
47-
'should throw if `title` is not a `string`'
48-
)
48+
}, /`title` must be `string`: `{ type: 'definition', identifier: '1', url: '1', title: 1 }`$/)
49+
})
4950

50-
nodeAssert.throws(
51-
() => {
51+
await t.test('should throw if `label` is not a `string`', async function () {
52+
nodeAssert.throws(function () {
5253
assert({type: 'definition', identifier: '1', url: '1', label: 1})
53-
},
54-
/`label` must be `string`: `{ type: 'definition', identifier: '1', url: '1', label: 1 }`$/,
55-
'should throw if `label` is not a `string`'
56-
)
54+
}, /`label` must be `string`: `{ type: 'definition', identifier: '1', url: '1', label: 1 }`$/)
55+
})
5756
})

test/footnote-definition.js

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

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

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

22-
nodeAssert.throws(
23-
() => {
24-
assert({type: 'footnoteDefinition', identifier: 1, children: []})
25-
},
26-
/`footnoteDefinition` must have `identifier`: `{ type: 'footnoteDefinition', identifier: 1, children: \[] }`$/,
27-
'should throw if `identifier` is not a `string`'
24+
await t.test(
25+
'should throw if `identifier` is not a `string`',
26+
async function () {
27+
nodeAssert.throws(function () {
28+
assert({type: 'footnoteDefinition', identifier: 1, children: []})
29+
}, /`footnoteDefinition` must have `identifier`: `{ type: 'footnoteDefinition', identifier: 1, children: \[] }`$/)
30+
}
2831
)
2932

30-
nodeAssert.doesNotThrow(() => {
31-
assert({type: 'footnoteDefinition', identifier: '1', children: []})
32-
}, 'should not throw if `footnoteDefinition` has an identifier')
33+
await t.test(
34+
'should not throw if `footnoteDefinition` has an identifier',
35+
async function () {
36+
nodeAssert.doesNotThrow(function () {
37+
assert({type: 'footnoteDefinition', identifier: '1', children: []})
38+
})
39+
}
40+
)
3341

34-
nodeAssert.throws(
35-
() => {
42+
await t.test('should throw if `label` is not a `string`', async function () {
43+
nodeAssert.throws(function () {
3644
assert({
3745
type: 'footnoteDefinition',
3846
identifier: '1',
3947
label: 1,
4048
children: []
4149
})
42-
},
43-
/`label` must be `string`: `{ type: 'footnoteDefinition',\s+identifier: '1',\s+label: 1,\s+children: \[] }`$/,
44-
'should throw if `label` is not a `string`'
45-
)
50+
}, /`label` must be `string`: `{ type: 'footnoteDefinition',\s+identifier: '1',\s+label: 1,\s+children: \[] }`$/)
51+
})
4652
})

0 commit comments

Comments
 (0)