From: James Addison Date: Thu, 11 Feb 2021 20:11:42 +0000 (+0000) Subject: Stability fixup: interaction between newlines and comments (#1975) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/b8c1020b526b488a1a216d9a4d58fc8616b61a99 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 --- diff --git a/src/black/__init__.py b/src/black/__init__.py index 7c1a013..6dbb765 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -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: diff --git a/tests/test_black.py b/tests/test_black.py index cfd3cbd..a2efef8 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1793,6 +1793,11 @@ class BlackTestCase(BlackBaseTestCase): 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()