Repository: psf/black

Issue:
Black produced different code on the second pass of the formatter: Explicit line join in the first line
Operating system: macOS Mojave 10.14.5 (18F132)
Python version: Python 3.7.3
*Black* version: 19.3b0 (f3bb22a828)
Does also happen on master: yes

Minified reproducer:
```python
\


pass
```

Black diff:
```diff
--- source
+++ first pass
@@ -1,5 +1,3 @@
-\
-

 pass

--- first pass
+++ second pass
@@ -1,3 +1,2 @@
-
 pass
```

Looks like some kind of boundary condition in `EmptyLineTracker`?


Discussion:
Jma353: Moved the check to `EmptyLineTracker` @zsol, this should be good for another review.

Patch:
diff --git a/black.py b/black.py
index 910a0ed..05edf1a 100644
--- a/black.py
+++ b/black.py
@@ -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
@@ -3008,8 +3014,7 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
         # make parentheses invisible
         first.value = ""  # type: ignore
         last.value = ""  # type: ignore
-        if len(node.children) > 1:
-            maybe_make_parens_invisible_in_atom(node.children[1], parent=parent)
+        maybe_make_parens_invisible_in_atom(node.children[1], parent=parent)
         return False
 
     return True


Test patch:
diff --git a/tests/data/beginning_backslash.py b/tests/data/beginning_backslash.py
new file mode 100644
index 0000000..66c347d
--- /dev/null
+++ b/tests/data/beginning_backslash.py
@@ -0,0 +1,12 @@
+\
+
+
+
+
+
+print("hello, world")
+
+# output
+
+
+print("hello, world")
diff --git a/tests/test_black.py b/tests/test_black.py
index 7b3a8b6..015243f 100644
--- a/tests/test_black.py
+++ b/tests/test_black.py
@@ -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"