Unverified Commit b8c1020b authored by James Addison's avatar James Addison Committed by GitHub
Browse files

Stability fixup: interaction between newlines and comments (#1975)

* Add test case to illustrate the issue

* Accept carriage returns as valid separators while enumerating comments

Without this acceptance, escaped multi-line statments that use carriage returns will not be counted into the 'ignored_lines' variable since the emitted line values will end with a CR and not an escape character.  That leads to comments associated with the line being incorrectly labeled with the STANDALONE_COMMENT type, affecting comment placement and line space management.

* Remove comment linking to ephemeral build log
parent 2d0c1498
Loading
Loading
Loading
Loading
+1 −1
Changes for src/black/__init__.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -2635,7 +2635,7 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
    consumed = 0
    nlines = 0
    ignored_lines = 0
    for index, line in enumerate(prefix.split("\n")):
    for index, line in enumerate(re.split("\r?\n", prefix)):
        consumed += len(line) + 1  # adding the length of the split '\n'
        line = line.lstrip()
        if not line:
+5 −0
Changes for tests/test_black.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1793,6 +1793,11 @@ def test_bpo_33660_workaround(self) -> None:
        finally:
            os.chdir(str(old_cwd))

    def test_newline_comment_interaction(self) -> None:
        source = "class A:\\\r\n# type: ignore\n pass\n"
        output = black.format_str(source, mode=DEFAULT_MODE)
        black.assert_stable(source, output, mode=DEFAULT_MODE)


with open(black.__file__, "r", encoding="utf-8") as _bf:
    black_source_lines = _bf.readlines()