Skip to content

Commit aee7cba

Browse files
committed
render with pass-through style for no color, not post-hoc stripping
1 parent b6a9fa3 commit aee7cba

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

lib/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ export const inspect = color ? inspectColor : inspectNoColor
4444

4545
const own = {}.hasOwnProperty
4646

47-
// ANSI color regex.
48-
/* eslint-disable no-control-regex */
49-
const colorExpression =
50-
/(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
51-
/* eslint-enable no-control-regex */
52-
5347
/**
5448
* Inspect a node, without color.
5549
*
@@ -61,7 +55,23 @@ const colorExpression =
6155
* Pretty printed `tree`.
6256
*/
6357
export function inspectNoColor(tree, options) {
64-
return inspectColor(tree, options).replace(colorExpression, '')
58+
/** @type {State} */
59+
const state = {
60+
style: {
61+
bold: noColor,
62+
dim: noColor,
63+
yellow: noColor,
64+
green: noColor
65+
},
66+
showPositions:
67+
!options ||
68+
options.showPositions === null ||
69+
options.showPositions === undefined
70+
? true
71+
: options.showPositions
72+
}
73+
74+
return inspectValue(tree, state)
6575
}
6676

6777
/**
@@ -403,6 +413,16 @@ function ansiColor(open, close) {
403413
}
404414
}
405415

416+
/**
417+
* Style function which does not perform colorization.
418+
*
419+
* @param {string} value
420+
* @return {string}
421+
*/
422+
function noColor(value) {
423+
return value
424+
}
425+
406426
/**
407427
* @param {unknown} value
408428
* @returns {value is Node}

0 commit comments

Comments
 (0)