Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dont print escape backslash on strings containing escaped backslashes #401

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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