11import { color } from './color.js'
22
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
614
715var own = { } . hasOwnProperty
816
@@ -15,12 +23,24 @@ var green = ansiColor(32, 39)
1523/* eslint-disable-next-line no-control-regex */
1624var colorExpression = / (?: (?: \u001B \[ ) | \u009B ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001B [ A - M ] / g
1725
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 , '' )
2135}
2236
23- // Inspects a node.
37+ /**
38+ * Inspects a node, using color.
39+ *
40+ * @param {unknown } tree
41+ * @param {InspectOptions } [options]
42+ * @returns {string }
43+ */
2444export function inspectColor ( tree , options ) {
2545 var positions =
2646 ! options ||
@@ -31,23 +51,39 @@ export function inspectColor(tree, options) {
3151
3252 return inspectValue ( tree )
3353
54+ /**
55+ * @param {unknown } node
56+ * @returns {string }
57+ */
3458 function inspectValue ( node ) {
3559 if ( node && typeof node === 'object' && 'length' in node ) {
60+ // @ts -ignore looks like a list of nodes.
3661 return inspectNodes ( node )
3762 }
3863
64+ // @ts -ignore looks like a single node.
3965 if ( node && node . type ) {
66+ // @ts -ignore looks like a single node.
4067 return inspectTree ( node )
4168 }
4269
4370 return inspectNonTree ( node )
4471 }
4572
73+ /**
74+ * @param {unknown } value
75+ * @returns {string }
76+ */
4677 function inspectNonTree ( value ) {
4778 return JSON . stringify ( value )
4879 }
4980
81+ /**
82+ * @param {Node[] } nodes
83+ * @returns {string }
84+ */
5085 function inspectNodes ( nodes ) {
86+ /** @type {Array.<string> } */
5187 var result = [ ]
5288 var index = - 1
5389
@@ -66,13 +102,22 @@ export function inspectColor(tree, options) {
66102 return result . join ( '\n' )
67103 }
68104
105+ /**
106+ * @param {Object.<string, unknown> } object
107+ * @returns {string }
108+ */
69109 function inspectFields ( object ) {
110+ /** @type {Array.<string> } */
70111 var result = [ ]
112+ /** @type {string } */
71113 var key
114+ /** @type {unknown } */
72115 var value
116+ /** @type {string } */
73117 var formatted
74118
75119 for ( key in object ) {
120+ /* c8 ignore next 1 */
76121 if ( ! own . call ( object , key ) ) continue
77122
78123 value = object [ key ]
@@ -95,11 +140,13 @@ export function inspectColor(tree, options) {
95140 if (
96141 value &&
97142 typeof value === 'object' &&
143+ // @ts -ignore looks like a node.
98144 value . type &&
99145 key !== 'data' &&
100146 key !== 'attributes' &&
101147 key !== 'properties'
102148 ) {
149+ // @ts -ignore looks like a node.
103150 formatted = inspectTree ( value )
104151 }
105152 // A list of nodes.
@@ -110,6 +157,7 @@ export function inspectColor(tree, options) {
110157 value [ 0 ] &&
111158 value [ 0 ] . type
112159 ) {
160+ // @ts -ignore looks like a list of nodes.
113161 formatted = '\n' + inspectNodes ( value )
114162 } else {
115163 formatted = inspectNonTree ( value )
@@ -122,20 +170,31 @@ export function inspectColor(tree, options) {
122170
123171 return indent (
124172 result . join ( '\n' ) ,
173+ // @ts -ignore looks like a parent node.
125174 ( object . children && object . children . length > 0 ? dim ( '│' ) : ' ' ) + ' '
126175 )
127176 }
128177
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 ) ]
131184 var fields = inspectFields ( node )
185+ // @ts -ignore looks like a parent.
132186 var content = inspectNodes ( node . children || [ ] )
133187 if ( fields ) result . push ( fields )
134188 if ( content ) result . push ( content )
135189 return result . join ( '\n' )
136190 }
137191
138- // Colored node formatter.
192+ /**
193+ * Colored node formatter.
194+ *
195+ * @param {Node } node
196+ * @returns {string }
197+ */
139198 function formatNode ( node ) {
140199 var result = [ bold ( node . type ) ]
141200 var kind = node . tagName || node . name
@@ -146,9 +205,10 @@ export function inspectColor(tree, options) {
146205 }
147206
148207 if ( node . children ) {
208+ // @ts -ignore looks like a parent.
149209 result . push ( dim ( '[' ) , yellow ( node . children . length ) , dim ( ']' ) )
150210 } else if ( typeof node . value === 'string' ) {
151- result . push ( ' ' , green ( inspectNonTree ( node . value , '' ) ) )
211+ result . push ( ' ' , green ( inspectNonTree ( node . value ) ) )
152212 }
153213
154214 if ( position ) {
@@ -159,6 +219,12 @@ export function inspectColor(tree, options) {
159219 }
160220}
161221
222+ /**
223+ * @param {string } value
224+ * @param {string } indentation
225+ * @param {boolean } [ignoreFirst=false]
226+ * @returns {string }
227+ */
162228function indent ( value , indentation , ignoreFirst ) {
163229 var lines = value . split ( '\n' )
164230 var index = ignoreFirst ? 0 : - 1
@@ -172,11 +238,19 @@ function indent(value, indentation, ignoreFirst) {
172238 return lines . join ( '\n' )
173239}
174240
175- // Compile a position.
241+ /**
242+ * @param {Position } value
243+ * @returns {string }
244+ */
176245function stringifyPosition ( value ) {
246+ /** @type {Position } */
247+ // @ts -ignore
177248 var position = value || { }
249+ /** @type {Array.<string> } */
178250 var result = [ ]
251+ /** @type {Array.<string> } */
179252 var positions = [ ]
253+ /** @type {Array.<string> } */
180254 var offsets = [ ]
181255
182256 point ( position . start )
@@ -187,6 +261,9 @@ function stringifyPosition(value) {
187261
188262 return result . join ( ', ' )
189263
264+ /**
265+ * @param {Point } value
266+ */
190267 function point ( value ) {
191268 if ( value ) {
192269 positions . push ( ( value . line || 1 ) + ':' + ( value . column || 1 ) )
@@ -198,10 +275,20 @@ function stringifyPosition(value) {
198275 }
199276}
200277
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+ */
202285function ansiColor ( open , close ) {
203286 return color
204287
288+ /**
289+ * @param {string } value
290+ * @returns {string }
291+ */
205292 function color ( value ) {
206293 return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
207294 }
0 commit comments