Skip to content

Commit 475ea66

Browse files
committed
Refactor code-style
1 parent 58b4a4e commit 475ea66

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

lib/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
2-
* @typedef {import('mdast').Parents} Parents
32
* @typedef {import('mdast').Nodes} Nodes
4-
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('mdast').Parents} Parents
54
*/
65

76
/**
@@ -45,7 +44,7 @@ import {visit} from 'unist-util-visit'
4544
* Comment name to look for.
4645
* @param {Handler} handler
4746
* Handle a section.
48-
* @returns {void}
47+
* @returns {undefined}
4948
* Nothing.
5049
*/
5150
export function zone(node, name, handler) {
@@ -56,10 +55,12 @@ export function zone(node, name, handler) {
5655
/** @type {Parents | undefined} */
5756
let scope
5857

59-
visit(node, (node, index, parent) => {
58+
visit(node, function (node, index, parent) {
6059
const info = commentMarker(node)
6160
const match =
62-
info && info.name === name ? info.attributes.match(/(start|end)\b/) : null
61+
info && info.name === name
62+
? info.attributes.match(/(start|end)\b/)
63+
: undefined
6364
const type = match ? match[0] : undefined
6465

6566
if (parent && index !== undefined && type) {
@@ -99,8 +100,12 @@ export function zone(node, name, handler) {
99100
if (node) result.push(node)
100101
}
101102

102-
// @ts-expect-error: Assume the correct children are passed.
103-
scope.children.splice(start, index - start + 1, ...result)
103+
scope.children.splice(
104+
start,
105+
index - start + 1,
106+
// @ts-expect-error: Assume the correct children are passed.
107+
...result
108+
)
104109
}
105110

106111
marker = undefined

readme.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ console.log(String(file))
9494

9595
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
9696
function myPluginThatReplacesFoo() {
97-
return (tree) => {
98-
zone(tree, 'foo', (start, nodes, end) => [
99-
start,
100-
{type: 'paragraph', children: [{type: 'text', value: 'Bar.'}]},
101-
end
102-
])
97+
return function (tree) {
98+
zone(tree, 'foo', function (start, nodes, end) {
99+
return [
100+
start,
101+
{type: 'paragraph', children: [{type: 'text', value: 'Bar.'}]},
102+
end
103+
]
104+
})
103105
}
104106
}
105107
```
@@ -170,7 +172,7 @@ Extra info (TypeScript type).
170172
— parent of the section
171173
* `start` (`number`)
172174
— index of `start` in `parent`
173-
* `end` (`number` or `null`)
175+
* `end` (`number`)
174176
— index of `end` in `parent`
175177

176178
## Types

test/fixtures/nodes/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* @typedef {import('mdast').Root} Root
3-
* @typedef {import('../../../index.js').Handler} Handler
43
*/
54

65
import assert from 'node:assert/strict'

test/fixtures/replace-children/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default function assertion(tree) {
1111
zone(tree, 'foo', function (start, _, end) {
1212
return [
1313
start,
14-
/** @type {import('mdast').Heading} */ ({
14+
{
1515
type: 'heading',
1616
depth: 2,
1717
children: [{type: 'text', value: 'Bar'}]
18-
}),
18+
},
1919
end
2020
]
2121
})

test/fixtures/replace/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import {zone} from '../../../index.js'
88
* @param {Root} tree
99
*/
1010
export default function assertion(tree) {
11-
zone(tree, 'foo', () => [
12-
{
13-
type: 'heading',
14-
depth: 2,
15-
children: [{type: 'text', value: 'Bar'}]
16-
}
17-
])
11+
zone(tree, 'foo', function () {
12+
return [
13+
{
14+
type: 'heading',
15+
depth: 2,
16+
children: [{type: 'text', value: 'Bar'}]
17+
}
18+
]
19+
})
1820
}

test/index.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
import assert from 'node:assert/strict'
66
import fs from 'node:fs/promises'
77
import test from 'node:test'
8+
import {isHidden} from 'is-hidden'
89
import {fromMarkdown} from 'mdast-util-from-markdown'
910
import {toMarkdown} from 'mdast-util-to-markdown'
10-
import {isHidden} from 'is-hidden'
11-
import * as mod from '../index.js'
1211

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

2017
const root = new URL('fixtures/', import.meta.url)
2118
const folders = await fs.readdir(root)
@@ -26,26 +23,30 @@ test('zone', async () => {
2623

2724
if (isHidden(folder)) continue
2825

29-
/** @type {string | undefined} */
30-
let expected
26+
await t.test('should work on `' + folder + '`', async function () {
27+
/** @type {string | undefined} */
28+
let expected
3129

32-
try {
33-
expected = String(await fs.readFile(new URL(folder + '/output.md', root)))
34-
} catch {}
30+
try {
31+
expected = String(
32+
await fs.readFile(new URL(folder + '/output.md', root))
33+
)
34+
} catch {}
3535

36-
/** @type {{default: (tree: Root) => void}} */
37-
const mod = await import(new URL(folder + '/index.js', root).href)
38-
const check = mod.default
39-
// To do: remove cast when `from-markdown` is released.
40-
const tree = /** @type {Root} */ (
41-
fromMarkdown(await fs.readFile(new URL(folder + '/input.md', root)))
42-
)
36+
/** @type {{default: (tree: Root) => undefined}} */
37+
const mod = await import(new URL(folder + '/index.js', root).href)
38+
const check = mod.default
39+
// To do: remove cast when `from-markdown` is released.
40+
const tree = /** @type {Root} */ (
41+
fromMarkdown(await fs.readFile(new URL(folder + '/input.md', root)))
42+
)
4343

44-
check(tree)
44+
check(tree)
4545

46-
// @ts-expect-error: remove cast when `to-markdown` is released.
47-
const result = toMarkdown(tree)
46+
// @ts-expect-error: remove cast when `to-markdown` is released.
47+
const result = toMarkdown(tree)
4848

49-
assert.equal(result, expected, folder)
49+
assert.equal(result, expected)
50+
})
5051
}
5152
})

0 commit comments

Comments
 (0)