Skip to content

Commit 8aeea56

Browse files
committed
Refactor code-style
1 parent 0b25a5a commit 8aeea56

File tree

2 files changed

+189
-171
lines changed

2 files changed

+189
-171
lines changed

lib/index.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
/**
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.
413
*
514
* @typedef State
615
* Info passed around.
7-
* @property {Array<EstreeComment>} comments
16+
* @property {Array<Comment>} comments
817
* Comments.
918
* @property {number} index
1019
* Index of comment.
11-
*
12-
* @typedef Fields
13-
* @property {boolean} leading
14-
* @property {boolean} trailing
1520
*/
1621

1722
const own = {}.hasOwnProperty
1823

24+
/** @type {Array<Comment>} */
25+
const emptyComments = []
26+
1927
/**
2028
* Attach semistandard estree comment nodes to the tree.
2129
*
@@ -37,30 +45,30 @@ const own = {}.hasOwnProperty
3745
* The algorithm supports `loc` fields (line/column), `range` fields (offsets),
3846
* and direct `start` / `end` fields.
3947
*
40-
* @template {EstreeNode} Tree
48+
* @template {Nodes} Tree
4149
* Node type.
4250
* @param {Tree} tree
4351
* 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).
4654
* @returns {Tree}
4755
* Given tree.
4856
*/
4957
// To do: next major: don’t return given `tree`.
5058
export function attachComments(tree, comments) {
51-
const list = (comments || []).concat().sort(compare)
59+
const list = comments ? [...comments].sort(compare) : emptyComments
5260
if (list.length > 0) walk(tree, {comments: list, index: 0})
5361
return tree
5462
}
5563

5664
/**
5765
* Attach semistandard estree comment nodes to the tree.
5866
*
59-
* @param {EstreeNode} node
67+
* @param {Nodes} node
6068
* Node.
6169
* @param {State} state
6270
* Info passed around.
63-
* @returns {void}
71+
* @returns {undefined}
6472
* Nothing.
6573
*/
6674
function walk(node, state) {
@@ -69,17 +77,17 @@ function walk(node, state) {
6977
return
7078
}
7179

72-
/** @type {Array<EstreeNode>} */
80+
/** @type {Array<Nodes>} */
7381
const children = []
74-
/** @type {Array<EstreeComment>} */
82+
/** @type {Array<Comment>} */
7583
const comments = []
7684
/** @type {string} */
7785
let key
7886

7987
// Find all children of `node`
8088
for (key in node) {
8189
if (own.call(node, key)) {
82-
/** @type {EstreeNode | Array<EstreeNode>} */
90+
/** @type {Array<Nodes> | Nodes} */
8391
// @ts-expect-error: indexable.
8492
const value = node[key]
8593

@@ -129,17 +137,17 @@ function walk(node, state) {
129137
/**
130138
* @param {State} state
131139
* Info passed around.
132-
* @param {EstreeNode} node
140+
* @param {Nodes} node
133141
* Node.
134142
* @param {boolean} compareEnd
135143
* Whether to compare on the end (default is on start).
136144
* @param {Fields} fields
137145
* Fields.
138-
* @returns {Array<EstreeComment>}
146+
* @returns {Array<Comment>}
139147
* Slice from `state.comments`.
140148
*/
141149
function slice(state, node, compareEnd, fields) {
142-
/** @type {Array<EstreeComment>} */
150+
/** @type {Array<Comment>} */
143151
const result = []
144152

145153
while (
@@ -155,12 +163,13 @@ function slice(state, node, compareEnd, fields) {
155163
/**
156164
* Sort two nodes (or comments).
157165
*
158-
* @param {EstreeNode | EstreeComment} left
166+
* @param {Comment | Nodes} left
159167
* A node.
160-
* @param {EstreeNode | EstreeComment} right
168+
* @param {Comment | Nodes} right
161169
* The other node.
162170
* @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`).
164173
* @returns {number}
165174
* Sorting.
166175
*/

0 commit comments

Comments
 (0)