1
1
import { color } from './color.js'
2
2
3
- export var inspect = color
4
- ? inspectColor
5
- : /* istanbul ignore next */ inspectNoColor
3
+ /**
4
+ * @typedef {import('unist').Node } Node
5
+ * @typedef {import('unist').Position } Position
6
+ * @typedef {import('unist').Point } Point
7
+ *
8
+ * @typedef {Object } InspectOptions
9
+ * @property {boolean } [showPositions=true]
10
+ */
11
+
12
+ /* c8 ignore next */
13
+ export var inspect = color ? inspectColor : inspectNoColor
6
14
7
15
var own = { } . hasOwnProperty
8
16
@@ -15,12 +23,24 @@ var green = ansiColor(32, 39)
15
23
/* eslint-disable-next-line no-control-regex */
16
24
var colorExpression = / (?: (?: \u001B \[ ) | \u009B ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001B [ A - M ] / g
17
25
18
- // Inspects a node, without using color.
19
- export function inspectNoColor ( node ) {
20
- return inspectColor ( node ) . replace ( colorExpression , '' )
26
+ /**
27
+ * Inspects a node, without using color.
28
+ *
29
+ * @param {unknown } node
30
+ * @param {InspectOptions } [options]
31
+ * @returns {string }
32
+ */
33
+ export function inspectNoColor ( node , options ) {
34
+ return inspectColor ( node , options ) . replace ( colorExpression , '' )
21
35
}
22
36
23
- // Inspects a node.
37
+ /**
38
+ * Inspects a node, using color.
39
+ *
40
+ * @param {unknown } tree
41
+ * @param {InspectOptions } [options]
42
+ * @returns {string }
43
+ */
24
44
export function inspectColor ( tree , options ) {
25
45
var positions =
26
46
! options ||
@@ -31,23 +51,39 @@ export function inspectColor(tree, options) {
31
51
32
52
return inspectValue ( tree )
33
53
54
+ /**
55
+ * @param {unknown } node
56
+ * @returns {string }
57
+ */
34
58
function inspectValue ( node ) {
35
59
if ( node && typeof node === 'object' && 'length' in node ) {
60
+ // @ts -ignore looks like a list of nodes.
36
61
return inspectNodes ( node )
37
62
}
38
63
64
+ // @ts -ignore looks like a single node.
39
65
if ( node && node . type ) {
66
+ // @ts -ignore looks like a single node.
40
67
return inspectTree ( node )
41
68
}
42
69
43
70
return inspectNonTree ( node )
44
71
}
45
72
73
+ /**
74
+ * @param {unknown } value
75
+ * @returns {string }
76
+ */
46
77
function inspectNonTree ( value ) {
47
78
return JSON . stringify ( value )
48
79
}
49
80
81
+ /**
82
+ * @param {Node[] } nodes
83
+ * @returns {string }
84
+ */
50
85
function inspectNodes ( nodes ) {
86
+ /** @type {Array.<string> } */
51
87
var result = [ ]
52
88
var index = - 1
53
89
@@ -66,13 +102,22 @@ export function inspectColor(tree, options) {
66
102
return result . join ( '\n' )
67
103
}
68
104
105
+ /**
106
+ * @param {Object.<string, unknown> } object
107
+ * @returns {string }
108
+ */
69
109
function inspectFields ( object ) {
110
+ /** @type {Array.<string> } */
70
111
var result = [ ]
112
+ /** @type {string } */
71
113
var key
114
+ /** @type {unknown } */
72
115
var value
116
+ /** @type {string } */
73
117
var formatted
74
118
75
119
for ( key in object ) {
120
+ /* c8 ignore next 1 */
76
121
if ( ! own . call ( object , key ) ) continue
77
122
78
123
value = object [ key ]
@@ -95,11 +140,13 @@ export function inspectColor(tree, options) {
95
140
if (
96
141
value &&
97
142
typeof value === 'object' &&
143
+ // @ts -ignore looks like a node.
98
144
value . type &&
99
145
key !== 'data' &&
100
146
key !== 'attributes' &&
101
147
key !== 'properties'
102
148
) {
149
+ // @ts -ignore looks like a node.
103
150
formatted = inspectTree ( value )
104
151
}
105
152
// A list of nodes.
@@ -110,6 +157,7 @@ export function inspectColor(tree, options) {
110
157
value [ 0 ] &&
111
158
value [ 0 ] . type
112
159
) {
160
+ // @ts -ignore looks like a list of nodes.
113
161
formatted = '\n' + inspectNodes ( value )
114
162
} else {
115
163
formatted = inspectNonTree ( value )
@@ -122,20 +170,31 @@ export function inspectColor(tree, options) {
122
170
123
171
return indent (
124
172
result . join ( '\n' ) ,
173
+ // @ts -ignore looks like a parent node.
125
174
( object . children && object . children . length > 0 ? dim ( '│' ) : ' ' ) + ' '
126
175
)
127
176
}
128
177
129
- function inspectTree ( node , pad ) {
130
- var result = [ formatNode ( node , pad ) ]
178
+ /**
179
+ * @param {Node } node
180
+ * @returns {string }
181
+ */
182
+ function inspectTree ( node ) {
183
+ var result = [ formatNode ( node ) ]
131
184
var fields = inspectFields ( node )
185
+ // @ts -ignore looks like a parent.
132
186
var content = inspectNodes ( node . children || [ ] )
133
187
if ( fields ) result . push ( fields )
134
188
if ( content ) result . push ( content )
135
189
return result . join ( '\n' )
136
190
}
137
191
138
- // Colored node formatter.
192
+ /**
193
+ * Colored node formatter.
194
+ *
195
+ * @param {Node } node
196
+ * @returns {string }
197
+ */
139
198
function formatNode ( node ) {
140
199
var result = [ bold ( node . type ) ]
141
200
var kind = node . tagName || node . name
@@ -146,9 +205,10 @@ export function inspectColor(tree, options) {
146
205
}
147
206
148
207
if ( node . children ) {
208
+ // @ts -ignore looks like a parent.
149
209
result . push ( dim ( '[' ) , yellow ( node . children . length ) , dim ( ']' ) )
150
210
} else if ( typeof node . value === 'string' ) {
151
- result . push ( ' ' , green ( inspectNonTree ( node . value , '' ) ) )
211
+ result . push ( ' ' , green ( inspectNonTree ( node . value ) ) )
152
212
}
153
213
154
214
if ( position ) {
@@ -159,6 +219,12 @@ export function inspectColor(tree, options) {
159
219
}
160
220
}
161
221
222
+ /**
223
+ * @param {string } value
224
+ * @param {string } indentation
225
+ * @param {boolean } [ignoreFirst=false]
226
+ * @returns {string }
227
+ */
162
228
function indent ( value , indentation , ignoreFirst ) {
163
229
var lines = value . split ( '\n' )
164
230
var index = ignoreFirst ? 0 : - 1
@@ -172,11 +238,19 @@ function indent(value, indentation, ignoreFirst) {
172
238
return lines . join ( '\n' )
173
239
}
174
240
175
- // Compile a position.
241
+ /**
242
+ * @param {Position } value
243
+ * @returns {string }
244
+ */
176
245
function stringifyPosition ( value ) {
246
+ /** @type {Position } */
247
+ // @ts -ignore
177
248
var position = value || { }
249
+ /** @type {Array.<string> } */
178
250
var result = [ ]
251
+ /** @type {Array.<string> } */
179
252
var positions = [ ]
253
+ /** @type {Array.<string> } */
180
254
var offsets = [ ]
181
255
182
256
point ( position . start )
@@ -187,6 +261,9 @@ function stringifyPosition(value) {
187
261
188
262
return result . join ( ', ' )
189
263
264
+ /**
265
+ * @param {Point } value
266
+ */
190
267
function point ( value ) {
191
268
if ( value ) {
192
269
positions . push ( ( value . line || 1 ) + ':' + ( value . column || 1 ) )
@@ -198,10 +275,20 @@ function stringifyPosition(value) {
198
275
}
199
276
}
200
277
201
- // Factory to wrap values in ANSI colours.
278
+ /**
279
+ * Factory to wrap values in ANSI colours.
280
+ *
281
+ * @param {number } open
282
+ * @param {number } close
283
+ * @returns {function(string): string }
284
+ */
202
285
function ansiColor ( open , close ) {
203
286
return color
204
287
288
+ /**
289
+ * @param {string } value
290
+ * @returns {string }
291
+ */
205
292
function color ( value ) {
206
293
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
207
294
}
0 commit comments