Skip to content

Commit bc3ccfc

Browse files
committed
Refactor code-style
1 parent 9adea97 commit bc3ccfc

File tree

4 files changed

+329
-279
lines changed

4 files changed

+329
-279
lines changed

lib/index.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
11
/**
2-
* @typedef {import('unist').Parent} UnistParent
32
* @typedef {import('mdast').Content} Content
43
* @typedef {import('mdast').Heading} Heading
54
* @typedef {import('mdast').Root} Root
5+
* @typedef {import('unist').Parent} UnistParent
66
*/
77

8+
// To do: use `Nodes`, `Parents` from `mdast` when released.
89
/**
910
* @typedef {Content | Root} Node
1011
* @typedef {Extract<Node, UnistParent>} Parent
1112
*/
1213

1314
/**
14-
* @callback TestFunction
15-
* Check if a node matches.
16-
* @param {string} value
17-
* Plain-text heading.
18-
* @param {Heading} node
19-
* Heading node.
20-
* @returns {boolean | null | undefined}
21-
* Whether this is the heading that is searched for.
22-
*
23-
* @typedef {string | RegExp | TestFunction} Test
24-
* Test for a heading.
25-
*
26-
* When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
27-
* when `RegExp`, wrapped in `(value) => expression.test(value)`
28-
*
29-
* @typedef Options
30-
* Configuration.
31-
* @property {Test} test
32-
* Test for a heading.
33-
* @property {boolean | null | undefined} [ignoreFinalDefinitions=false]
34-
* Ignore final definitions otherwise in the section.
35-
*
36-
* @typedef Info
37-
* Extra info.
38-
* @property {Parent} parent
39-
* Parent of the section.
40-
* @property {number} start
41-
* Index of `start` in `parent`.
42-
* @property {number | null} end
43-
* Index of `end` in `parent`.
44-
*
4515
* @callback Handler
4616
* Callback called when a section is found.
4717
* @param {Heading} start
@@ -58,8 +28,40 @@
5828
* If nothing is returned, nothing will be changed.
5929
* If an array of nodes (can include `null` and `undefined`) is returned, the
6030
* original section will be replaced by those nodes.
31+
*
32+
* @typedef Info
33+
* Extra info.
34+
* @property {Parent} parent
35+
* Parent of the section.
36+
* @property {number} start
37+
* Index of `start` in `parent`.
38+
* @property {number | null} end
39+
* Index of `end` in `parent`.
40+
*
41+
* @typedef Options
42+
* Configuration.
43+
* @property {Test} test
44+
* Test for a heading.
45+
* @property {boolean | null | undefined} [ignoreFinalDefinitions=false]
46+
* Ignore final definitions otherwise in the section (default: `false`).
47+
*
48+
* @typedef {RegExp | TestFunction | string} Test
49+
* Test for a heading.
50+
*
51+
* When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
52+
* when `RegExp`, wrapped in `(value) => expression.test(value)`
53+
*
54+
* @callback TestFunction
55+
* Check if a node matches.
56+
* @param {string} value
57+
* Plain-text heading.
58+
* @param {Heading} node
59+
* Heading node.
60+
* @returns {boolean | null | undefined | void}
61+
* Whether this is the heading that is searched for.
6162
*/
6263

64+
import {ok as assert} from 'devlop'
6365
import {toString} from 'mdast-util-to-string'
6466

6567
// To do: next major: remove `null` in API output.
@@ -78,16 +80,17 @@ import {toString} from 'mdast-util-to-string'
7880
*
7981
* @param {Node} tree
8082
* Tree to change.
81-
* @param {Test | Options} options
83+
* @param {Options | Test} options
8284
* Configuration.
8385
* @param {Handler} handler
8486
* Handle a section.
85-
* @returns {void}
87+
* @returns {undefined}
8688
* Nothing.
8789
*/
8890
// eslint-disable-next-line complexity
8991
export function headingRange(tree, options, handler) {
9092
let test = options
93+
// To do: smarter types to allow siblings of headings.
9194
/** @type {Array<Node>} */
9295
const children = 'children' in tree ? tree.children : []
9396
let ignoreFinalDefinitions = false
@@ -154,12 +157,11 @@ export function headingRange(tree, options, handler) {
154157
}
155158
}
156159

157-
/** @type {Heading} */
158-
// @ts-expect-error `start` points to a heading.
159160
const head = children[start]
160-
/** @type {Parent} */
161-
// @ts-expect-error always a parent, or we don’t come here.
161+
assert(head.type === 'heading')
162+
162163
const parent = tree
164+
assert('children' in parent, 'parent is a parent')
163165

164166
const nodes = handler(head, children.slice(start + 1, end), children[end], {
165167
parent,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@types/mdast": "^3.0.0",
3838
"@types/unist": "^2.0.0",
39+
"devlop": "^1.1.0",
3940
"mdast-util-to-string": "^3.0.0"
4041
},
4142
"devDependencies": {

readme.md

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

9898
/** @type {import('unified').Plugin<[], import('mdast').Root>} */
9999
function myPluginThatReplacesFoo() {
100-
return (tree) => {
101-
headingRange(tree, 'foo', (start, nodes, end) => [
102-
start,
103-
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
104-
end
105-
])
100+
return function (tree) {
101+
headingRange(tree, 'foo', function (start, nodes, end) {
102+
return [
103+
start,
104+
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
105+
end
106+
]
107+
})
106108
}
107109
}
108110
```
@@ -146,7 +148,7 @@ excluded.
146148

147149
###### Returns
148150

149-
Nothing (`void`).
151+
Nothing (`undefined`).
150152

151153
### `Handler`
152154

0 commit comments

Comments
 (0)