Strip inline comment from an empty unquoted value#672
Open
eeshsaxena wants to merge 1 commit into
Open
Conversation
For a line like ``KEY= # comment`` the comment became the value because the whitespace before ``#`` is consumed by the equal-sign match, so parse_unquoted_value()'s ``\s+#`` strip never matched. Thread a "preceded by whitespace" flag so an empty value followed by a comment resolves to "" while a value that legitimately starts with ``#`` (e.g. ``KEY=#value``) is preserved. Fixes theskumar#600
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 #600.
For a line where the key has an empty value followed by an inline comment, the comment text became the value:
parse_unquoted_value()strips inline comments withre.sub(r"\s+#.*", "", part), which requires whitespace before the#. But_equal_sign(=[^\S\r\n]*) consumes the whitespace between=and the value, so for an empty value the remaining text starts at#with no leading whitespace, and the strip never matches.The whitespace was there in the source, though — it's just been consumed. This threads a
preceded_by_whitespaceflag (derived from whether the_equal_signmatch captured any whitespace) so that:KEY= # comment/KEY= #c→""(empty value, comment stripped)KEY=#value→"#value"(no separating whitespace, so#is part of the value — unchanged)KEY=v # c→"v",KEY=v#c→"v#c"— all unchangedNew parser test cases cover
a= #c,a= # comment, anda=#b. Full suite:tests/test_parser.py48 passed;tests/test_main.pyunaffected.ruffclean.