Escape literal backslashes when inspecting strings - #170
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.