Fix unstable format involving backslash + whitespace at beginning of file (#948)

This commit is contained in:
Joe Antonakakis 2019-08-04 02:03:19 -07:00 committed by Zsolt Dollenstein
parent 65ea568e33
commit c7495b9aa0
3 changed files with 27 additions and 1 deletions

View File

@ -1480,7 +1480,13 @@ def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
lines (two on module-level).
"""
before, after = self._maybe_empty_lines(current_line)
before -= self.previous_after
before = (
# Black should not insert empty lines at the beginning
# of the file
0
if self.previous_line is None
else before - self.previous_after
)
self.previous_after = after
self.previous_line = current_line
return before, after

View File

@ -0,0 +1,12 @@
\
print("hello, world")
# output
print("hello, world")

View File

@ -639,6 +639,14 @@ def test_tuple_assign(self) -> None:
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
@patch("black.dump_to_file", dump_to_stderr)
def test_beginning_backslash(self) -> None:
source, expected = read_data("beginning_backslash")
actual = fs(source)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
def test_tab_comment_indentation(self) -> None:
contents_tab = "if 1:\n\tif 2:\n\t\tpass\n\t# comment\n\tpass\n"
contents_spc = "if 1:\n if 2:\n pass\n # comment\n pass\n"