Skip to content

Commit 4e6a846

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

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

lib/index.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* @typedef {import('estree').Comment} EstreeComment
44
*
55
* @typedef State
6+
* Info passed around.
67
* @property {Array<EstreeComment>} comments
8+
* Comments.
79
* @property {number} index
10+
* Index of comment.
811
*
912
* @typedef Fields
1013
* @property {boolean} leading
@@ -16,9 +19,16 @@ const own = {}.hasOwnProperty
1619
/**
1720
* Attach semistandard estree comment nodes to the tree.
1821
*
19-
* @param {EstreeNode} tree
20-
* @param {Array<EstreeComment>} [comments]
22+
* @template {EstreeNode} Tree
23+
* Node type.
24+
* @param {Tree} tree
25+
* Tree to attach to.
26+
* @param {Array<EstreeComment> | null | undefined} [comments]
27+
* List of comments.
28+
* @returns {Tree}
29+
* Given tree.
2130
*/
31+
// To do: next major: don’t return given `tree`.
2232
export function attachComments(tree, comments) {
2333
const list = (comments || []).concat().sort(compare)
2434
if (list.length > 0) walk(tree, {comments: list, index: 0})
@@ -29,7 +39,11 @@ export function attachComments(tree, comments) {
2939
* Attach semistandard estree comment nodes to the tree.
3040
*
3141
* @param {EstreeNode} node
42+
* Node.
3243
* @param {State} state
44+
* Info passed around.
45+
* @returns {void}
46+
* Nothing.
3347
*/
3448
function walk(node, state) {
3549
// Done, we can quit.
@@ -47,7 +61,7 @@ function walk(node, state) {
4761
// Find all children of `node`
4862
for (key in node) {
4963
if (own.call(node, key)) {
50-
/** @type {EstreeNode|Array<EstreeNode>} */
64+
/** @type {EstreeNode | Array<EstreeNode>} */
5165
// @ts-expect-error: indexable.
5266
const value = node[key]
5367

@@ -96,9 +110,15 @@ function walk(node, state) {
96110

97111
/**
98112
* @param {State} state
113+
* Info passed around.
99114
* @param {EstreeNode} node
115+
* Node.
100116
* @param {boolean} compareEnd
117+
* Whether to compare on the end (default is on start).
101118
* @param {Fields} fields
119+
* Fields.
120+
* @returns {Array<EstreeComment>}
121+
* Slice from `state.comments`.
102122
*/
103123
function slice(state, node, compareEnd, fields) {
104124
/** @type {Array<EstreeComment>} */
@@ -115,10 +135,16 @@ function slice(state, node, compareEnd, fields) {
115135
}
116136

117137
/**
118-
* @param {EstreeNode|EstreeComment} left
119-
* @param {EstreeNode|EstreeComment} right
120-
* @param {boolean} [compareEnd]
138+
* Sort two nodes (or comments).
139+
*
140+
* @param {EstreeNode | EstreeComment} left
141+
* A node.
142+
* @param {EstreeNode | EstreeComment} right
143+
* The other node.
144+
* @param {boolean | undefined} [compareEnd=false]
145+
* Compare on `end` of `right`, default is to compare on `start`.
121146
* @returns {number}
147+
* Sorting.
122148
*/
123149
function compare(left, right, compareEnd) {
124150
const field = compareEnd ? 'end' : 'start'

0 commit comments

Comments
 (0)