Repository: psf/black

Issue:
Fix Bug exposed by Fuzz Tests
**Describe the bug**
Fuzz testing is failing with libcst errors I don't get.

```
fuzz run-test: commands[2] | coverage run fuzz.py
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
<string>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
Highest target scores:
               5  (label='(hypothesmith) number of unique ast node types')
               9  (label='(hypothesmith) instructions in bytecode')
              10  (label='(hypothesmith from_node) number of unique ast node types')
              13  (label='(hypothesmith) total number of ast nodes')
              35  (label='(hypothesmith from_node) total number of ast nodes')
              48  (label='(hypothesmith from_node) instructions in bytecode')

Traceback (most recent call last):
  File "<string>", line 7, in __init__
  File "/home/cooper/repos/black/.tox/fuzz/lib/python3.9/site-packages/libcst/_nodes/base.py", line 115, in __post_init__
    self._validate()
  File "/home/cooper/repos/black/.tox/fuzz/lib/python3.9/site-packages/libcst/_nodes/expression.py", line 1148, in _validate
    raise CSTValidationError(
  File "<string>", line None
libcst._nodes.base.CSTValidationError: Must have at least one space around comparison operator.
```

**To Reproduce** 
Run `tox -e fuzz` on head

**Expected behavior**
Fuzz tests to pass.

Discussion:
ichard26: I'm assuming that this is practically a draft PR where the actual fixing code will come later ... if so please mark this PR as a draft 
jayaddison: @ichard26 Yep, that's the plan.  Open pull requests require write permissions to be switched to draft mode; I don't need write permissions so if someone else could do that, I'd appreciate it.
ichard26: Isn't there an option to mark a PR as a draft in the "reviewers" section in the right sidebar?
![Screenshot from 2021-02-07 12-27-47](https://user-images.githubusercontent.com/63936253/107154256-f0476900-693f-11eb-8d2b-2a4a6870deb5.png)
 
jayaddison: Ah, thanks - yes, I do see the 'convert to draft' option now.  I'm not sure how I missed that previously.  Either way, a possible fix is available in the branch now.

Patch:
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:


Test patch:
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 @@ 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()