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 2 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
6 changes: 5 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,20 @@ function prettifyObject ({
if (Object.keys(plain).length > 0) {
result += colorizer.greyMessage(stringifySafe(plain))
}
// Avoid printing the escape character on escapting backslash
result += eol
result = result.replace(/\\\\/gi, '\\')
} else {
ademarsj marked this conversation as resolved.
Show resolved Hide resolved
// 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 escapting backslash
lines = lines.replace(/\\\\/gi, '\\')
ademarsj marked this conversation as resolved.
Show resolved Hide resolved

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 escape backslashs in string values', async t => {
ademarsj marked this conversation as resolved.
Show resolved Hide resolved
const str = prettifyObject({ input: { foo_regexp: '\\[^\\w\\s]\\' } })
t.equal(str, ' foo_regexp: "\\[^\\w\\s]\\"\n')
})

t.test('ignores escape backslashs in string values (singleLine option)', async t => {
ademarsj marked this conversation as resolved.
Show resolved Hide resolved
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