Skip to content

Commit 7f69fe6

Browse files
committed
Refactor code-style
1 parent eb2535e commit 7f69fe6

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
2-
* @typedef {import('mdast').Root} Root
32
* @typedef {import('mdast').Content} Content
43
* @typedef {import('mdast').Heading} Heading
4+
* @typedef {import('mdast').Root} Root
55
*/
66

7+
// To do: when `mdast` is released, use `Nodes`.
78
/**
89
* @typedef {Content | Root} Node
910
*/
@@ -34,7 +35,7 @@ export function normalizeHeadings(tree) {
3435
/** @type {Heading | undefined} */
3536
let title
3637

37-
visit(tree, 'heading', (node) => {
38+
visit(tree, 'heading', function (node) {
3839
all.push(node)
3940

4041
if (!first) {

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@
8282
"strict": true
8383
},
8484
"xo": {
85+
"overrides": [
86+
{
87+
"files": [
88+
"test/**/*.js"
89+
],
90+
"rules": {
91+
"no-await-in-loop": "off"
92+
}
93+
}
94+
],
8595
"prettier": true
8696
}
8797
}

test/index.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,40 @@ import test from 'node:test'
88
import {fromMarkdown} from 'mdast-util-from-markdown'
99
import {removePosition} from 'unist-util-remove-position'
1010
import {normalizeHeadings} from '../index.js'
11-
import * as mod from '../index.js'
1211

13-
test('normalizeHeadings', async () => {
14-
assert.deepEqual(
15-
Object.keys(mod).sort(),
16-
['normalizeHeadings'],
17-
'should expose the public api'
18-
)
12+
test('normalizeHeadings', async function (t) {
13+
await t.test('should expose the public api', async function () {
14+
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
15+
'normalizeHeadings'
16+
])
17+
})
1918

20-
await check('no-headings', 'No-op if there is no headings')
21-
await check('no-titles', 'No-op if there is no top-level headings')
22-
await check('one-title', 'No-op if there is a single top-level heading')
23-
await check('two-titles', 'Makes the second header one level deeper')
24-
await check('more-titles', 'Shifts all other headings one level deeper')
25-
await check('hierarchy', 'There is no depth level 7')
26-
})
27-
/**
28-
* @param {string} test
29-
* @param {string} message
30-
* @returns {Promise<void>}
31-
*/
32-
async function check(test, message) {
33-
const input = await fs.readFile(
34-
new URL('fixture/' + test + '.in', import.meta.url)
35-
)
36-
const output = await fs.readFile(
37-
new URL('fixture/' + test + '.out', import.meta.url)
38-
)
39-
const actual = fromMarkdown(input)
40-
const expected = fromMarkdown(output)
41-
normalizeHeadings(actual)
19+
/** @type {Record<string, string>} */
20+
const cases = {
21+
'no-headings': 'should be a no-op if there are no headings',
22+
'no-titles': 'should be a no-op if there are no top-level headings',
23+
'one-title': 'should be a no-op if there is a single top-level heading',
24+
'two-titles': 'should make the second header one level deeper',
25+
'more-titles': 'should shift all other headings one level deeper',
26+
hierarchy: 'should be create a depth level 7'
27+
}
4228

43-
assert.deepEqual(
44-
removePosition(actual, true),
45-
removePosition(expected, true),
46-
message
47-
)
48-
}
29+
for (const [name, message] of Object.entries(cases)) {
30+
await t.test(message, async function () {
31+
const input = await fs.readFile(
32+
new URL('fixture/' + name + '.in', import.meta.url)
33+
)
34+
const output = await fs.readFile(
35+
new URL('fixture/' + name + '.out', import.meta.url)
36+
)
37+
const actual = fromMarkdown(input)
38+
const expected = fromMarkdown(output)
39+
normalizeHeadings(actual)
40+
41+
assert.deepEqual(
42+
removePosition(actual, true),
43+
removePosition(expected, true)
44+
)
45+
})
46+
}
47+
})

0 commit comments

Comments
 (0)