Preserve line breaks across a parse(stringify()) round-trip - #251
Open
mahirhir wants to merge 1 commit into
Open
Preserve line breaks across a parse(stringify()) round-trip#251mahirhir wants to merge 1 commit into
mahirhir wants to merge 1 commit into
Conversation
stringify escapes a line break to the two characters \n and wraps the value in double quotes, but parse stripped the quotes without reversing that escape, so a value containing a line break did not survive a round trip through the library's own functions. removeQuotes now unescapes \n back to a line break for double-quoted values, matching dotenv's double-quote semantics. Single-quoted values are left raw. Adds a round-trip regression test.
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.
stringifyescapes a line break to the two characters\nand wraps the value in double quotes, butparsestrips the quotes without reversing that escape. A value that contains a line break therefore does not survive a round-trip through the library's own functions:The two existing tests show the intent. "line breaks inside quotes should be preserved on parse" feeds
parsean actual line break inside quotes, and "...on stringify" checks thatstringifyemits the escaped\nform. They were only ever checked in isolation, so composing them silently drops the line break.parseis the inverse ofstringify, so it should reverse whatstringifyproduces. This makesremoveQuotesunescape\nback to a line break for double-quoted values, which matches dotenv's double-quote semantics where\ndenotes a line break. Single-quoted values are left raw, as in dotenv.All existing tests still pass unchanged. I added one that runs
parse(stringify(value))for a value with a line break; reverting the one-line change inremoveQuotesmakes only that new test fail, with the literal backslash-n shown above.