11
11
* @property {boolean } trailing
12
12
*/
13
13
14
+ const own = { } . hasOwnProperty
15
+
14
16
/**
15
17
* Attach semistandard estree comment nodes to the tree.
16
18
*
17
19
* @param {EstreeNode } tree
18
20
* @param {EstreeComment[] } [comments]
19
21
*/
20
22
export function attachComments ( tree , comments ) {
21
- var list = ( comments || [ ] ) . concat ( ) . sort ( compare )
22
- if ( list . length ) walk ( tree , { comments : list , index : 0 } )
23
+ const list = ( comments || [ ] ) . concat ( ) . sort ( compare )
24
+ if ( list . length > 0 ) walk ( tree , { comments : list , index : 0 } )
23
25
return tree
24
26
}
25
27
@@ -30,38 +32,37 @@ export function attachComments(tree, comments) {
30
32
* @param {State } state
31
33
*/
32
34
function walk ( node , state ) {
33
- /** @type {EstreeNode[] } */
34
- var children = [ ]
35
- /** @type {EstreeComment[] } */
36
- var comments = [ ]
37
- /** @type {string } */
38
- var key
39
- /** @type {EstreeNode|EstreeNode[] } */
40
- var value
41
- /** @type {number } */
42
- var index
43
-
44
35
// Done, we can quit.
45
36
if ( state . index === state . comments . length ) {
46
37
return
47
38
}
48
39
40
+ /** @type {EstreeNode[] } */
41
+ const children = [ ]
42
+ /** @type {EstreeComment[] } */
43
+ const comments = [ ]
44
+ /** @type {string } */
45
+ let key
46
+
49
47
// Find all children of `node`
50
48
for ( key in node ) {
51
- value = node [ key ]
52
-
53
- // Ignore comments.
54
- if ( value && typeof value === 'object' && key !== 'comments' ) {
55
- if ( Array . isArray ( value ) ) {
56
- index = - 1
57
-
58
- while ( ++ index < value . length ) {
59
- if ( value [ index ] && typeof value [ index ] . type === 'string' ) {
60
- children . push ( value [ index ] )
49
+ if ( own . call ( node , key ) ) {
50
+ /** @type {EstreeNode|EstreeNode[] } */
51
+ const value = node [ key ]
52
+
53
+ // Ignore comments.
54
+ if ( value && typeof value === 'object' && key !== 'comments' ) {
55
+ if ( Array . isArray ( value ) ) {
56
+ let index = - 1
57
+
58
+ while ( ++ index < value . length ) {
59
+ if ( value [ index ] && typeof value [ index ] . type === 'string' ) {
60
+ children . push ( value [ index ] )
61
+ }
61
62
}
63
+ } else if ( typeof value . type === 'string' ) {
64
+ children . push ( value )
62
65
}
63
- } else if ( typeof value . type === 'string' ) {
64
- children . push ( value )
65
66
}
66
67
}
67
68
}
@@ -72,7 +73,7 @@ function walk(node, state) {
72
73
// Initial comments.
73
74
comments . push ( ...slice ( state , node , false , { leading : true , trailing : false } ) )
74
75
75
- index = - 1
76
+ let index = - 1
76
77
77
78
while ( ++ index < children . length ) {
78
79
walk ( children [ index ] , state )
@@ -82,11 +83,11 @@ function walk(node, state) {
82
83
comments . push (
83
84
...slice ( state , node , true , {
84
85
leading : false ,
85
- trailing : Boolean ( children . length )
86
+ trailing : children . length > 0
86
87
} )
87
88
)
88
89
89
- if ( comments . length ) {
90
+ if ( comments . length > 0 ) {
90
91
// @ts -expect-error, yes, because they’re nonstandard.
91
92
node . comments = comments
92
93
}
@@ -100,7 +101,7 @@ function walk(node, state) {
100
101
*/
101
102
function slice ( state , node , compareEnd , fields ) {
102
103
/** @type {EstreeComment[] } */
103
- var result = [ ]
104
+ const result = [ ]
104
105
105
106
while (
106
107
state . comments [ state . index ] &&
@@ -119,7 +120,7 @@ function slice(state, node, compareEnd, fields) {
119
120
* @returns {number }
120
121
*/
121
122
function compare ( left , right , compareEnd ) {
122
- var field = compareEnd ? 'end' : 'start'
123
+ const field = compareEnd ? 'end' : 'start'
123
124
124
125
// Offsets.
125
126
if ( left . range && right . range ) {
@@ -141,5 +142,5 @@ function compare(left, right, compareEnd) {
141
142
return left . start - right [ field ]
142
143
}
143
144
144
- return NaN
145
+ return Number . NaN
145
146
}
0 commit comments