11/**
22 * @import {Options} from 'unist-util-inspect'
33 * @import {Node} from 'unist'
4+ * @import {State} from './types.js'
45 */
56
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'
128
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
1414
1515/**
16- * Inspect a node, with color in Node, without color in browsers .
16+ * Inspect a node, without color.
1717 *
18- * @param tree
18+ * @param { unknown } tree
1919 * Tree to inspect.
20- * @param options
21- * Configuration (optional) .
22- * @returns
20+ * @param { Options | null | undefined } [ options]
21+ * Configuration.
22+ * @returns { string }
2323 * Pretty printed `tree`.
2424 */
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+ }
3539
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+ }
4142
43+ // To do: remove.
4244/**
4345 * Inspect a node, without color.
4446 *
47+ * @deprecated
48+ * Use `inspect` instead, with `color: false`.
4549 * @param {unknown } tree
4650 * Tree to inspect.
47- * @param {Options | null | undefined } [options]
51+ * @param {Omit< Options, 'color'> | null | undefined } [options]
4852 * Configuration.
4953 * @returns {string }
5054 * Pretty printed `tree`.
5155 */
5256export function inspectNoColor ( tree , options ) {
53- return inspectColor ( tree , options ) . replace ( colorExpression , '' )
57+ return inspect ( tree , { ... options , color : false } )
5458}
5559
60+ // To do: remove.
5661/**
5762 * Inspects a node, using color.
5863 *
64+ * @deprecated
65+ * Use `inspect` instead, with `color: true`.
5966 * @param {unknown } tree
6067 * Tree to inspect.
61- * @param {Options | null | undefined } [options]
68+ * @param {Omit< Options, 'color'> | null | undefined } [options]
6269 * Configuration (optional).
6370 * @returns {string }
6471 * Pretty printed `tree`.
6572 */
6673export 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 } )
7875}
7976
8077/**
@@ -129,15 +126,16 @@ function inspectNodes(nodes, state) {
129126
130127 while ( ++ index < nodes . length ) {
131128 result . push (
132- dim (
129+ state . dim (
133130 ( index < nodes . length - 1 ? '├' : '└' ) +
134131 '─' +
135132 String ( index ) . padEnd ( size )
136133 ) +
137134 ' ' +
138135 indent (
139136 inspectValue ( nodes [ index ] , state ) ,
140- ( index < nodes . length - 1 ? dim ( '│' ) : ' ' ) + ' ' . repeat ( size + 2 ) ,
137+ ( index < nodes . length - 1 ? state . dim ( '│' ) : ' ' ) +
138+ ' ' . repeat ( size + 2 ) ,
141139 true
142140 )
143141 )
@@ -202,14 +200,17 @@ function inspectFields(object, state) {
202200 }
203201
204202 result . push (
205- key + dim ( ':' ) + ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) + formatted
203+ key +
204+ state . dim ( ':' ) +
205+ ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) +
206+ formatted
206207 )
207208 }
208209
209210 return indent (
210211 result . join ( '\n' ) ,
211212 ( isArrayUnknown ( object . children ) && object . children . length > 0
212- ? dim ( '│' )
213+ ? state . dim ( '│' )
213214 : ' ' ) + ' '
214215 )
215216}
@@ -250,7 +251,7 @@ function inspectTree(node, state) {
250251 * Formatted node.
251252 */
252253function formatNode ( node , state ) {
253- const result = [ bold ( node . type ) ]
254+ const result = [ state . bold ( node . type ) ]
254255 // Cast as record to allow indexing.
255256 const map = /** @type {Record<string, unknown> } */ (
256257 /** @type {unknown } */ ( node )
@@ -263,13 +264,17 @@ function formatNode(node, state) {
263264 }
264265
265266 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+ )
267272 } else if ( typeof map . value === 'string' ) {
268- result . push ( ' ' , green ( inspectNonTree ( map . value ) ) )
273+ result . push ( ' ' , state . green ( inspectNonTree ( map . value ) ) )
269274 }
270275
271276 if ( position ) {
272- result . push ( ' ' , dim ( '(' ) , position , dim ( ')' ) )
277+ result . push ( ' ' , state . dim ( '(' ) , position , state . dim ( ')' ) )
273278 }
274279
275280 return result . join ( '' )
@@ -394,3 +399,12 @@ function isNode(value) {
394399function isArrayUnknown ( node ) {
395400 return Array . isArray ( node )
396401}
402+
403+ /**
404+ * @template T
405+ * @param {T } value
406+ * @returns {T }
407+ */
408+ function identity ( value ) {
409+ return value
410+ }
0 commit comments