Skip to content

Commit 1c7e0ab

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent aaa3c62 commit 1c7e0ab

File tree

3 files changed

+142
-167
lines changed

3 files changed

+142
-167
lines changed

Diff for: lib/index.js

+63-35
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
11
/**
2-
* @typedef {import('unist').Parent} Parent
3-
* @typedef {import('mdast').Root|import('mdast').Content} Node
2+
* @typedef {import('unist').Parent} UnistParent
3+
* @typedef {import('mdast').Content} Content
44
* @typedef {import('mdast').Heading} Heading
5+
* @typedef {import('mdast').Root} Root
6+
*/
7+
8+
/**
9+
* @typedef {Content | Root} Node
10+
* @typedef {Extract<Node, UnistParent>} Parent
511
*
6-
* @typedef {(value: string, node: Heading) => boolean} TestFunction
7-
* Function called for each heading with its content and `node` itself check
8-
* if it’s the one to look for.
12+
* @callback TestFunction
13+
* Function called with headings to check if it’s the one to look for.
14+
* @param {string} value
15+
* Plain-text heading.
16+
* @param {Heading} node
17+
* Heading node.
18+
* @returns {boolean}
19+
* Whether this is the heading that is searched for.
920
*
10-
* @typedef {string|RegExp|TestFunction} Test
21+
* @typedef {string | RegExp | TestFunction} Test
22+
* A test.
1123
*
1224
* @typedef Options
1325
* Configuration (optional).
1426
* @property {Test} test
1527
* Heading to look for.
28+
*
1629
* When `string`, wrapped in `new RegExp('^(' + value + ')$', 'i')`;
17-
* when `RegExp`, wrapped in `function (value) {expression.test(value)}`
18-
* @property {boolean} [ignoreFinalDefinitions=false]
30+
* when `RegExp`, wrapped in `(value) => expression.test(value)`
31+
* @property {boolean | null | undefined} [ignoreFinalDefinitions=false]
1932
* Ignore final definitions otherwise in the section.
2033
*
2134
* @typedef ZoneInfo
2235
* Extra info.
23-
* @property {Parent|null} parent
36+
* @property {Parent | null} parent
2437
* Parent of the range.
2538
* @property {number} start
2639
* index of `start` in `parent`
27-
* @property {number} end
40+
* @property {number | null} end
2841
* index of `end` in `parent`
2942
*
3043
* @callback Handler
3144
* Callback called when a range is found.
32-
* @param {Heading|undefined} start
33-
* Start of range.
45+
* @param {Heading} start
46+
* Start of range (a heading node matching `test`).
3447
* @param {Array<Node>} between
3548
* Nodes between `start` and `end`.
36-
* @param {Node|undefined} end
49+
* @param {Node | undefined} end
3750
* End of range, if any.
3851
* @param {ZoneInfo} scope
3952
* Extra info.
53+
* @returns {Array<Node | null | undefined> | null | undefined | void}
54+
* Results.
55+
*
56+
* If nothing is returned, nothing will be changed.
57+
* If an array of nodes (can include `null` and `undefined`) is returned, the
58+
* original section will be replaced by those nodes.
4059
*/
4160

4261
import {toString} from 'mdast-util-to-string'
@@ -47,24 +66,29 @@ import {toString} from 'mdast-util-to-string'
4766
*
4867
* A “section” is a heading that passes `test`, until the next heading of the
4968
* same or lower depth, or the end of the document.
69+
*
5070
* If `ignoreFinalDefinitions: true`, final definitions “in” the section are
5171
* excluded.
5272
*
5373
* @param {Node} tree
54-
* @param {Test|Options} options
74+
* Tree to change.
75+
* @param {Test | Options} options
76+
* Configuration.
5577
* @param {Handler} handler
78+
* Handle a range.
79+
* @returns {void}
80+
* Nothing.
5681
*/
5782
// eslint-disable-next-line complexity
5883
export function headingRange(tree, options, handler) {
5984
let test = options
6085
/** @type {Array<Node>} */
6186
const children = 'children' in tree ? tree.children : []
62-
/** @type {boolean|undefined} */
63-
let ignoreFinalDefinitions
87+
let ignoreFinalDefinitions = false
6488

6589
// Object, not regex.
6690
if (test && typeof test === 'object' && !('exec' in test)) {
67-
ignoreFinalDefinitions = test.ignoreFinalDefinitions
91+
ignoreFinalDefinitions = test.ignoreFinalDefinitions === true
6892
test = test.test
6993
}
7094

@@ -87,11 +111,11 @@ export function headingRange(tree, options, handler) {
87111
}
88112

89113
let index = -1
90-
/** @type {number|undefined} */
114+
/** @type {number | undefined} */
91115
let start
92-
/** @type {number|undefined} */
116+
/** @type {number | undefined} */
93117
let end
94-
/** @type {number|undefined} */
118+
/** @type {number| undefined} */
95119
let depth
96120

97121
// Find the range.
@@ -124,14 +148,18 @@ export function headingRange(tree, options, handler) {
124148
}
125149
}
126150

127-
/** @type {Array<Node>} */
128-
const nodes = handler(
129-
// @ts-expect-error `start` points to a heading.
130-
children[start],
131-
children.slice(start + 1, end),
132-
children[end],
133-
{parent: tree, start, end: children[end] ? end : null}
134-
)
151+
/** @type {Heading} */
152+
// @ts-expect-error `start` points to a heading.
153+
const head = children[start]
154+
/** @type {Parent} */
155+
// @ts-expect-error always a parent, or we don’t come here.
156+
const parent = tree
157+
158+
const nodes = handler(head, children.slice(start + 1, end), children[end], {
159+
parent,
160+
start,
161+
end: children[end] ? end : null
162+
})
135163

136164
if (nodes) {
137165
// Ensure no empty nodes are inserted.
@@ -141,7 +169,8 @@ export function headingRange(tree, options, handler) {
141169
let index = -1
142170

143171
while (++index < nodes.length) {
144-
if (nodes[index]) result.push(nodes[index])
172+
const node = nodes[index]
173+
if (node) result.push(node)
145174
}
146175

147176
children.splice(start, end - start + 1, ...result)
@@ -151,17 +180,16 @@ export function headingRange(tree, options, handler) {
151180

152181
/**
153182
* Wrap an expression into an assertion function.
183+
*
154184
* @param {RegExp} expression
155-
* @returns {(value: string) => boolean}
185+
* Test expression.
186+
* @returns {TestFunction}
187+
* Test function.
156188
*/
157189
function wrapExpression(expression) {
158190
return assertion
159191

160-
/**
161-
* Assert `value` matches the bound `expression`.
162-
* @param {string} value
163-
* @returns {boolean}
164-
*/
192+
/** @type {TestFunction} */
165193
function assertion(value) {
166194
return expression.test(value)
167195
}

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
"devDependencies": {
4242
"@types/tape": "^4.0.0",
4343
"c8": "^7.0.0",
44+
"mdast-util-from-markdown": "^1.0.0",
45+
"mdast-util-to-markdown": "^1.0.0",
4446
"prettier": "^2.0.0",
45-
"remark": "^14.0.0",
4647
"remark-cli": "^11.0.0",
4748
"remark-preset-wooorm": "^9.0.0",
4849
"tape": "^5.0.0",

0 commit comments

Comments
 (0)