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

Support lint ignore directives in more places #441

Merged
merged 2 commits into from
Apr 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/fixit/tests/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class Foo(object):
):
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