1
1
/**
2
- * @typedef {import('estree').BaseNode } EstreeNode
3
- * @typedef {import('estree').Comment } EstreeComment
2
+ * @typedef {import('estree').Comment } Comment
3
+ * @typedef {import('estree').Node } Nodes
4
+ */
5
+
6
+ /**
7
+ * @typedef Fields
8
+ * Fields.
9
+ * @property {boolean } leading
10
+ * Whether it’s leading.
11
+ * @property {boolean } trailing
12
+ * Whether it’s trailing.
4
13
*
5
14
* @typedef State
6
15
* Info passed around.
7
- * @property {Array<EstreeComment > } comments
16
+ * @property {Array<Comment > } comments
8
17
* Comments.
9
18
* @property {number } index
10
19
* Index of comment.
11
- *
12
- * @typedef Fields
13
- * @property {boolean } leading
14
- * @property {boolean } trailing
15
20
*/
16
21
17
22
const own = { } . hasOwnProperty
18
23
24
+ /** @type {Array<Comment> } */
25
+ const emptyComments = [ ]
26
+
19
27
/**
20
28
* Attach semistandard estree comment nodes to the tree.
21
29
*
@@ -37,30 +45,30 @@ const own = {}.hasOwnProperty
37
45
* The algorithm supports `loc` fields (line/column), `range` fields (offsets),
38
46
* and direct `start` / `end` fields.
39
47
*
40
- * @template {EstreeNode } Tree
48
+ * @template {Nodes } Tree
41
49
* Node type.
42
50
* @param {Tree } tree
43
51
* Tree to attach to.
44
- * @param {Array<EstreeComment > | null | undefined } [comments]
45
- * List of comments.
52
+ * @param {Array<Comment > | null | undefined } [comments]
53
+ * List of comments (optional) .
46
54
* @returns {Tree }
47
55
* Given tree.
48
56
*/
49
57
// To do: next major: don’t return given `tree`.
50
58
export function attachComments ( tree , comments ) {
51
- const list = ( comments || [ ] ) . concat ( ) . sort ( compare )
59
+ const list = comments ? [ ... comments ] . sort ( compare ) : emptyComments
52
60
if ( list . length > 0 ) walk ( tree , { comments : list , index : 0 } )
53
61
return tree
54
62
}
55
63
56
64
/**
57
65
* Attach semistandard estree comment nodes to the tree.
58
66
*
59
- * @param {EstreeNode } node
67
+ * @param {Nodes } node
60
68
* Node.
61
69
* @param {State } state
62
70
* Info passed around.
63
- * @returns {void }
71
+ * @returns {undefined }
64
72
* Nothing.
65
73
*/
66
74
function walk ( node , state ) {
@@ -69,17 +77,17 @@ function walk(node, state) {
69
77
return
70
78
}
71
79
72
- /** @type {Array<EstreeNode > } */
80
+ /** @type {Array<Nodes > } */
73
81
const children = [ ]
74
- /** @type {Array<EstreeComment > } */
82
+ /** @type {Array<Comment > } */
75
83
const comments = [ ]
76
84
/** @type {string } */
77
85
let key
78
86
79
87
// Find all children of `node`
80
88
for ( key in node ) {
81
89
if ( own . call ( node , key ) ) {
82
- /** @type {EstreeNode | Array<EstreeNode> } */
90
+ /** @type {Array<Nodes> | Nodes } */
83
91
// @ts -expect-error: indexable.
84
92
const value = node [ key ]
85
93
@@ -129,17 +137,17 @@ function walk(node, state) {
129
137
/**
130
138
* @param {State } state
131
139
* Info passed around.
132
- * @param {EstreeNode } node
140
+ * @param {Nodes } node
133
141
* Node.
134
142
* @param {boolean } compareEnd
135
143
* Whether to compare on the end (default is on start).
136
144
* @param {Fields } fields
137
145
* Fields.
138
- * @returns {Array<EstreeComment > }
146
+ * @returns {Array<Comment > }
139
147
* Slice from `state.comments`.
140
148
*/
141
149
function slice ( state , node , compareEnd , fields ) {
142
- /** @type {Array<EstreeComment > } */
150
+ /** @type {Array<Comment > } */
143
151
const result = [ ]
144
152
145
153
while (
@@ -155,12 +163,13 @@ function slice(state, node, compareEnd, fields) {
155
163
/**
156
164
* Sort two nodes (or comments).
157
165
*
158
- * @param {EstreeNode | EstreeComment } left
166
+ * @param {Comment | Nodes } left
159
167
* A node.
160
- * @param {EstreeNode | EstreeComment } right
168
+ * @param {Comment | Nodes } right
161
169
* The other node.
162
170
* @param {boolean | undefined } [compareEnd=false]
163
- * Compare on `end` of `right`, default is to compare on `start`.
171
+ * Compare on `end` of `right`, default is to compare on `start` (default:
172
+ * `false`).
164
173
* @returns {number }
165
174
* Sorting.
166
175
*/
0 commit comments