Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate fixme comments that are not being respected #413

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/fixit/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
from libcst import (
BaseSuite,
BatchableCSTVisitor,
Comma,
CSTNode,
Decorator,
EmptyLine,
IndentedBlock,
LeftSquareBracket,
Module,
RightSquareBracket,
SimpleStatementSuite,
TrailingWhitespace,
)
Expand Down Expand Up @@ -134,14 +138,42 @@ def node_comments(self, node: CSTNode) -> Generator[str, None, None]:
if tw and tw.comment:
yield tw.comment.value

comma: Optional[Comma] = getattr(node, "comma", None)
if isinstance(comma, Comma):
tw = getattr(comma.whitespace_after, "first_line", None)
if tw and tw.comment:
yield tw.comment.value

rb: Optional[RightSquareBracket] = getattr(node, "rbracket", None)
if rb is not None:
tw = getattr(rb.whitespace_before, "first_line", None)
if tw and tw.comment:
yield tw.comment.value

el: Optional[Sequence[EmptyLine]] = None
lb: Optional[LeftSquareBracket] = getattr(node, "lbracket", None)
if lb is not None:
el = getattr(lb.whitespace_after, "empty_lines", None)
if el is not None:
for line in el:
if line.comment:
yield line.comment.value

el = getattr(node, "lines_after_decorators", None)
if el is not None:
for line in el:
if line.comment:
yield line.comment.value

ll: Optional[Sequence[EmptyLine]] = getattr(node, "leading_lines", None)
if ll is not None:
for line in ll:
if line.comment:
yield line.comment.value
# stop looking once we've gone up far enough for leading_lines,
# even if there are no comment lines here at all
break
if not isinstance(node, Decorator):
# stop looking once we've gone up far enough for leading_lines,
# even if there are no comment lines here at all
break

node = self.get_metadata(ParentNodeProvider, node)

Expand Down
123 changes: 122 additions & 1 deletion src/fixit/tests/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pathlib import Path
from textwrap import dedent, indent
from typing import Optional, Sequence, Tuple
from unittest import TestCase
from unittest.mock import MagicMock

Expand Down Expand Up @@ -72,6 +73,13 @@ def visit_Module(self, node: cst.Module) -> bool:

def visit_ClassDef(self, node: cst.ClassDef) -> bool:
self.report(node, "class def")
for d in node.decorators:
self.report(d, "class decorator")
return False

def visit_FunctionDef(self, node: cst.FunctionDef) -> bool:
if node.name.value == "problem":
self.report(node, "problem function")
return False

def visit_Pass(self, node: cst.Pass) -> bool:
Expand Down Expand Up @@ -279,10 +287,123 @@ class Foo(object):
"class def",
(5, 0),
),
(
# before function decorators
"""
import sys

# lint-fixme: ExerciseReport
@contextmanager
def problem():
yield True
""",
None,
None,
),
(
# after function decorators
"""
import sys

@contextmanager
# lint-fixme: ExerciseReport
def problem():
yield True
""",
None,
None,
),
(
# before class decorators
"""
import dataclasses

# lint-fixme: ExerciseReport
@dataclasses.dataclass
class C:
value = 1
""",
None,
None,
),
(
# after class decorators
"""
import dataclasses

@dataclasses.dataclass
# lint-fixme: ExerciseReport
class C:
value = 1
""",
None,
None,
),
(
# above comprehension
"""
# lint-fixme: ExerciseReport
[... for _ in range(1)]
""",
None,
None,
),
(
# inside comprehension
"""
[
# lint-fixme: ExerciseReport
... for _ in range(1)
]
""",
None,
None,
),
(
# after comprehension
"""
[... for _ in range(1)] # lint-fixme: ExerciseReport
""",
None,
None,
),
(
# trailing inline comprehension
"""
[
... for _ in range(1) # lint-fixme: ExerciseReport
]
""",
None,
None,
),
(
# before list element
"""
[
# lint-fixme: ExerciseReport
...,
None,
]
""",
None,
None,
),
(
# trailing list element
"""
[
..., # lint-fixme: ExerciseReport
None,
]
""",
None,
None,
),
):
idx += 1
content = dedent(code).encode("utf-8")
with self.subTest(f"test case {idx}"):
with self.subTest(f"test ignore {idx}"):
runner = LintRunner(Path("fake.py"), content)
violations = list(
runner.collect_violations([ExerciseReportRule()], Config())
Expand Down
Loading