1
1
/**
2
2
* @import {Options} from 'unist-util-inspect'
3
3
* @import {Node} from 'unist'
4
+ * @import {State} from './types.js'
4
5
*/
5
6
6
- /**
7
- * @typedef State
8
- * Info passed around.
9
- * @property {boolean } showPositions
10
- * Whether to include positional information.
11
- */
7
+ import { color as colorDefault } from '#conditional-color'
12
8
13
- import { color } from '#conditional-color'
9
+ /** @type {Options } */
10
+ const emptyOptions = { }
11
+
12
+ // To do: next major (?): use `Object.hasOwn`.
13
+ const own = { } . hasOwnProperty
14
14
15
15
/**
16
- * Inspect a node, with color in Node, without color in browsers .
16
+ * Inspect a node, without color.
17
17
*
18
- * @param tree
18
+ * @param { unknown } tree
19
19
* Tree to inspect.
20
- * @param options
21
- * Configuration (optional) .
22
- * @returns
20
+ * @param { Options | null | undefined } [ options]
21
+ * Configuration.
22
+ * @returns { string }
23
23
* Pretty printed `tree`.
24
24
*/
25
- /* c8 ignore next */
26
- export const inspect = color ? inspectColor : inspectNoColor
27
-
28
- // To do: next major (?): use `Object.hasOwn`.
29
- const own = { } . hasOwnProperty
30
-
31
- const bold = ansiColor ( 1 , 22 )
32
- const dim = ansiColor ( 2 , 22 )
33
- const yellow = ansiColor ( 33 , 39 )
34
- const green = ansiColor ( 32 , 39 )
25
+ export function inspect ( tree , options ) {
26
+ const settings = options || emptyOptions
27
+ const color =
28
+ typeof settings . color === 'boolean' ? settings . color : colorDefault
29
+ const showPositions =
30
+ typeof settings . showPositions === 'boolean' ? settings . showPositions : true
31
+ /** @type {State } */
32
+ const state = {
33
+ bold : color ? ansiColor ( 1 , 22 ) : identity ,
34
+ dim : color ? ansiColor ( 2 , 22 ) : identity ,
35
+ green : color ? ansiColor ( 32 , 39 ) : identity ,
36
+ showPositions,
37
+ yellow : color ? ansiColor ( 33 , 39 ) : identity
38
+ }
35
39
36
- // ANSI color regex.
37
- /* eslint-disable no-control-regex */
38
- const colorExpression =
39
- / (?: (?: \u001B \[ ) | \u009B ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001B [ A - M ] / g
40
- /* eslint-enable no-control-regex */
40
+ return inspectValue ( tree , state )
41
+ }
41
42
43
+ // To do: remove.
42
44
/**
43
45
* Inspect a node, without color.
44
46
*
47
+ * @deprecated
48
+ * Use `inspect` instead, with `color: false`.
45
49
* @param {unknown } tree
46
50
* Tree to inspect.
47
- * @param {Options | null | undefined } [options]
51
+ * @param {Omit< Options, 'color'> | null | undefined } [options]
48
52
* Configuration.
49
53
* @returns {string }
50
54
* Pretty printed `tree`.
51
55
*/
52
56
export function inspectNoColor ( tree , options ) {
53
- return inspectColor ( tree , options ) . replace ( colorExpression , '' )
57
+ return inspect ( tree , { ... options , color : false } )
54
58
}
55
59
60
+ // To do: remove.
56
61
/**
57
62
* Inspects a node, using color.
58
63
*
64
+ * @deprecated
65
+ * Use `inspect` instead, with `color: true`.
59
66
* @param {unknown } tree
60
67
* Tree to inspect.
61
- * @param {Options | null | undefined } [options]
68
+ * @param {Omit< Options, 'color'> | null | undefined } [options]
62
69
* Configuration (optional).
63
70
* @returns {string }
64
71
* Pretty printed `tree`.
65
72
*/
66
73
export function inspectColor ( tree , options ) {
67
- /** @type {State } */
68
- const state = {
69
- showPositions :
70
- ! options ||
71
- options . showPositions === null ||
72
- options . showPositions === undefined
73
- ? true
74
- : options . showPositions
75
- }
76
-
77
- return inspectValue ( tree , state )
74
+ return inspect ( tree , { ...options , color : true } )
78
75
}
79
76
80
77
/**
@@ -129,15 +126,16 @@ function inspectNodes(nodes, state) {
129
126
130
127
while ( ++ index < nodes . length ) {
131
128
result . push (
132
- dim (
129
+ state . dim (
133
130
( index < nodes . length - 1 ? '├' : '└' ) +
134
131
'─' +
135
132
String ( index ) . padEnd ( size )
136
133
) +
137
134
' ' +
138
135
indent (
139
136
inspectValue ( nodes [ index ] , state ) ,
140
- ( index < nodes . length - 1 ? dim ( '│' ) : ' ' ) + ' ' . repeat ( size + 2 ) ,
137
+ ( index < nodes . length - 1 ? state . dim ( '│' ) : ' ' ) +
138
+ ' ' . repeat ( size + 2 ) ,
141
139
true
142
140
)
143
141
)
@@ -202,14 +200,17 @@ function inspectFields(object, state) {
202
200
}
203
201
204
202
result . push (
205
- key + dim ( ':' ) + ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) + formatted
203
+ key +
204
+ state . dim ( ':' ) +
205
+ ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) +
206
+ formatted
206
207
)
207
208
}
208
209
209
210
return indent (
210
211
result . join ( '\n' ) ,
211
212
( isArrayUnknown ( object . children ) && object . children . length > 0
212
- ? dim ( '│' )
213
+ ? state . dim ( '│' )
213
214
: ' ' ) + ' '
214
215
)
215
216
}
@@ -250,7 +251,7 @@ function inspectTree(node, state) {
250
251
* Formatted node.
251
252
*/
252
253
function formatNode ( node , state ) {
253
- const result = [ bold ( node . type ) ]
254
+ const result = [ state . bold ( node . type ) ]
254
255
// Cast as record to allow indexing.
255
256
const map = /** @type {Record<string, unknown> } */ (
256
257
/** @type {unknown } */ ( node )
@@ -263,13 +264,17 @@ function formatNode(node, state) {
263
264
}
264
265
265
266
if ( isArrayUnknown ( map . children ) ) {
266
- result . push ( dim ( '[' ) , yellow ( String ( map . children . length ) ) , dim ( ']' ) )
267
+ result . push (
268
+ state . dim ( '[' ) ,
269
+ state . yellow ( String ( map . children . length ) ) ,
270
+ state . dim ( ']' )
271
+ )
267
272
} else if ( typeof map . value === 'string' ) {
268
- result . push ( ' ' , green ( inspectNonTree ( map . value ) ) )
273
+ result . push ( ' ' , state . green ( inspectNonTree ( map . value ) ) )
269
274
}
270
275
271
276
if ( position ) {
272
- result . push ( ' ' , dim ( '(' ) , position , dim ( ')' ) )
277
+ result . push ( ' ' , state . dim ( '(' ) , position , state . dim ( ')' ) )
273
278
}
274
279
275
280
return result . join ( '' )
@@ -394,3 +399,12 @@ function isNode(value) {
394
399
function isArrayUnknown ( node ) {
395
400
return Array . isArray ( node )
396
401
}
402
+
403
+ /**
404
+ * @template T
405
+ * @param {T } value
406
+ * @returns {T }
407
+ */
408
+ function identity ( value ) {
409
+ return value
410
+ }
0 commit comments