Skip to content

Escape literal backslashes when inspecting strings - #170

Open
afonsojanu wants to merge 1 commit into
chaijs:mainfrom
afonsojanu:fix/inspect-string-backslash-escaping
Open

Escape literal backslashes when inspecting strings#170
afonsojanu wants to merge 1 commit into
chaijs:mainfrom
afonsojanu:fix/inspect-string-backslash-escaping

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #83.

stringEscapeChars is the character class that decides which characters in a string get escaped before being wrapped in quotes for display. It never included a backslash itself, even though escapeCharacters right below it already has a mapping ready for it. So a real backslash just passed through unescaped, and inspect('\n') (an actual newline) came out looking exactly the same as inspect('\n') (a literal backslash followed by n) - both render as '\n'. That's exactly the confusing behavior reported in the issue, coming through chai's assertion messages.

Added the backslash to the character class so it goes through the same escape() path as everything else already does. Added two tests: one for a plain backslash inside a string, and one that checks the newline vs backslash-n case from the issue directly, confirming inspect() now produces different output for the two.

Ran the full suite locally (484 passing) and eslint (clean) before opening this.

The character class stringEscapeChars checks against never included a
backslash itself, even though escapeCharacters already has a mapping
for it (backslash to double-backslash). So a real backslash in a
string just passed straight through untouched, which meant inspect('\n')
and inspect('\\n') rendered identically as '\n' - a newline and the
two literal characters backslash-n looked the same in any assertion
message or debug output built on top of loupe.

Adding a backslash to the character class fixes this: it now gets
escaped like every other special character, so the two cases are
finally distinguishable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backslash in string doesn't get escaped

1 participant