Skip to content

Commit

Permalink
feat: dont print escape backslash on strings containing escaped backs…
Browse files Browse the repository at this point in the history
…lashes (#401)

* feat: dont print escape backslash on strings containing escaped backslashes

* fix: changing replace to work on node 14

* fix: grammar issues
  • Loading branch information
ademarsj authored Feb 17, 2023
1 parent c335167 commit 38cbc11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,21 @@ function prettifyObject ({
result += colorizer.greyMessage(stringifySafe(plain))
}
result += eol
// Avoid printing the escape character on escaped backslashes.
result = result.replace(/\\\\/gi, '\\')
} else {
// Put each object entry on its own line
Object.entries(plain).forEach(([keyName, keyValue]) => {
// custom prettifiers are already applied above, so we can skip it now
const lines = typeof customPrettifiers[keyName] === 'function'
let lines = typeof customPrettifiers[keyName] === 'function'
? keyValue
: stringifySafe(keyValue, null, 2)

if (lines === undefined) return

// Avoid printing the escape character on escaped backslashes.
lines = lines.replace(/\\\\/gi, '\\')

const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
})
Expand Down
10 changes: 10 additions & 0 deletions test/lib/utils.public.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ tap.test('prettifyObject', t => {
t.equal(str, ' foo: "bar"\n')
})

t.test('ignores escaped backslashes in string values', async t => {
const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' } })
t.equal(str, ' foo_regexp: "\\[^\\w\\s]\\"\n')
})

t.test('ignores escaped backslashes in string values (singleLine option)', async t => {
const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' }, singleLine: true })
t.equal(str, '{"foo_regexp":"\\[^\\w\\s]\\"}\n')
})

t.test('works with error props', async t => {
const err = Error('Something went wrong')
const serializedError = {
Expand Down

0 comments on commit 38cbc11

Please sign in to comment.