3
3
* @typedef {import('estree').Comment } EstreeComment
4
4
*
5
5
* @typedef State
6
+ * Info passed around.
6
7
* @property {Array<EstreeComment> } comments
8
+ * Comments.
7
9
* @property {number } index
10
+ * Index of comment.
8
11
*
9
12
* @typedef Fields
10
13
* @property {boolean } leading
@@ -16,9 +19,16 @@ const own = {}.hasOwnProperty
16
19
/**
17
20
* Attach semistandard estree comment nodes to the tree.
18
21
*
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.
21
30
*/
31
+ // To do: next major: don’t return given `tree`.
22
32
export function attachComments ( tree , comments ) {
23
33
const list = ( comments || [ ] ) . concat ( ) . sort ( compare )
24
34
if ( list . length > 0 ) walk ( tree , { comments : list , index : 0 } )
@@ -29,7 +39,11 @@ export function attachComments(tree, comments) {
29
39
* Attach semistandard estree comment nodes to the tree.
30
40
*
31
41
* @param {EstreeNode } node
42
+ * Node.
32
43
* @param {State } state
44
+ * Info passed around.
45
+ * @returns {void }
46
+ * Nothing.
33
47
*/
34
48
function walk ( node , state ) {
35
49
// Done, we can quit.
@@ -47,7 +61,7 @@ function walk(node, state) {
47
61
// Find all children of `node`
48
62
for ( key in node ) {
49
63
if ( own . call ( node , key ) ) {
50
- /** @type {EstreeNode| Array<EstreeNode> } */
64
+ /** @type {EstreeNode | Array<EstreeNode> } */
51
65
// @ts -expect-error: indexable.
52
66
const value = node [ key ]
53
67
@@ -96,9 +110,15 @@ function walk(node, state) {
96
110
97
111
/**
98
112
* @param {State } state
113
+ * Info passed around.
99
114
* @param {EstreeNode } node
115
+ * Node.
100
116
* @param {boolean } compareEnd
117
+ * Whether to compare on the end (default is on start).
101
118
* @param {Fields } fields
119
+ * Fields.
120
+ * @returns {Array<EstreeComment> }
121
+ * Slice from `state.comments`.
102
122
*/
103
123
function slice ( state , node , compareEnd , fields ) {
104
124
/** @type {Array<EstreeComment> } */
@@ -115,10 +135,16 @@ function slice(state, node, compareEnd, fields) {
115
135
}
116
136
117
137
/**
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`.
121
146
* @returns {number }
147
+ * Sorting.
122
148
*/
123
149
function compare ( left , right , compareEnd ) {
124
150
const field = compareEnd ? 'end' : 'start'
0 commit comments