Skip to content

Commit

Permalink
fix from review.
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-bagerard committed May 21, 2021
1 parent 6893743 commit 7e34ae4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
23 changes: 19 additions & 4 deletions flake8_eradicate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-

import ast
import tokenize
from typing import Iterable, Tuple
from typing import Iterable, Iterator, Tuple, Type

import pkg_resources
from eradicate import Eradicator
Expand All @@ -25,7 +26,15 @@ class Checker(object):

options = None

def __init__(self, tree, filename: str): # noqa: D107
def __init__(self, tree: ast.AST, filename: str):
"""
``flake8`` plugin constructor.
Arguments:
tree: the file abstract syntax tree.
filename: the name of the file to process
"""
self.filename = filename

self._options = {
Expand Down Expand Up @@ -98,7 +107,7 @@ def parse_options(cls, options) -> None:
"""Parses registered options for providing them to each visitor."""
cls.options = options

def run(self) -> Iterable[Tuple[int, str]]:
def run(self) -> Iterator[Tuple[int, int, str, Type['Checker']]]:
"""Runs on each step of flake8."""
for line_no in self._lines_with_commented_out_code():
yield line_no, 0, self._error_template, type(self)
Expand All @@ -118,14 +127,20 @@ def _lines_with_commented_out_code(self) -> Iterable[int]:
when the tokens indicate a comment in the physical line.
"""
with open(self.filename) as f:
# Skip python commented encoding line
first_line = f.readline()
if not first_line.startswith('# -*- coding: utf-8 -*-'):
# rewind as we don't want to skip it during tokenization
f.seek(0)

file_tokens = tokenize.generate_tokens(f.readline)
comment_in_file = any(
token.type == tokenize.COMMENT
for token in file_tokens
)

if comment_in_file:
f.seek(0) # rewind file
f.seek(0)
for line_no, line in enumerate(f.readlines(), start=1):
filtered_source = ''.join(
self._eradicator.filter_commented_out_code(
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/correct_no_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

class Some(object):

property_name = 1

other_property: int = 2

def some_method(self) -> None:
print('not True and not False')

print(12 + 23 / 3)


def some_function():
return "something"
25 changes: 23 additions & 2 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path):

OptionsStub = namedtuple(
'Options',
'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend'
'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend',
)
Checker.options = OptionsStub(
eradicate_aggressive=True,
eradicate_whitelist=False,
eradicate_whitelist_extend=False
eradicate_whitelist_extend=False,
)

checker = Checker(tree=None, filename=filename)
Expand All @@ -150,3 +150,24 @@ def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path):
assert output == [3, 4, 9, 10, 14, 15, 16, 18, 19, 21, 22, 24, 25]
else:
assert output == [3, 9, 10, 14, 15, 16, 18, 19, 21, 22, 24, 25]


def test_lines_with_commented_out_code_file_without_comment_output(
absolute_path,
):
"""Verify central underlying method is returning correct output."""
filename = absolute_path('fixtures', 'correct_no_comment.py')

OptionsStub = namedtuple(
'Options',
'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend',
)
Checker.options = OptionsStub(
eradicate_aggressive=True,
eradicate_whitelist=False,
eradicate_whitelist_extend=False,
)

checker = Checker(tree=None, filename=filename)
output = list(checker._lines_with_commented_out_code())
assert output == []

0 comments on commit 7e34ae4

Please sign in to comment.