Skip to content

Commit

Permalink
feat: Reduce caught exceptions in prettyDom (#1321)
Browse files Browse the repository at this point in the history
Co-authored-by: David Rieman <david.rieman@wbd.com>
  • Loading branch information
2 people authored and eps1lon committed Jul 5, 2024
1 parent 67dee41 commit 05c66d9
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/pretty-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import {getDocument} from './helpers'
import {getConfig} from './config'

const shouldHighlight = () => {
let colors
try {
colors = JSON.parse(process?.env?.COLORS)
} catch (e) {
// If this throws, process?.env?.COLORS wasn't parsable. Since we only
// care about `true` or `false`, we can safely ignore the error.
if (typeof process === 'undefined') {
// Don't colorize in non-node environments (e.g. Browsers)
return false
}

if (typeof colors === 'boolean') {
// If `colors` is set explicitly (both `true` and `false`), use that value.
return colors
} else {
// Try to safely parse env COLORS: We will default behavior if any step fails.
try {
const colors = process.env?.COLORS
if (colors) {
const b = JSON.parse(colors)
if (typeof b === 'boolean') {
return b
}
}
} catch {
// If `colors` is not set, colorize if we're in node.
return (
typeof process !== 'undefined' &&
process.versions !== undefined &&
process.versions.node !== undefined
)
return process.versions !== undefined && process.versions.node !== undefined
}

// In all other cases, whether COLORS was a weird type, or the attempt threw:
// Fall back to colorizing if we are running in node.
return !!process?.versions?.node
}

const {DOMCollection} = prettyFormat.plugins
Expand Down

0 comments on commit 05c66d9

Please sign in to comment.