Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jun 22, 2021
1 parent 6a3ccfc commit 5f6b875
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 164 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: test

on: [push, pull_request, workflow_dispatch]
on:
push:
branches:
- master
pull_request:
workflow_dispatch:

jobs:
build:
Expand All @@ -25,7 +30,7 @@ jobs:
echo "$HOME/.poetry/bin" >> $GITHUB_PATH
- name: Set up cache
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
We follow Semantic Versions since the `0.1.0` release.


## 1.1.0

### Features

- Imrpoves performance on long files #210


## 1.0.0

### Features
Expand Down
61 changes: 30 additions & 31 deletions flake8_eradicate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# -*- coding: utf-8 -*-

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

import pkg_resources
from eradicate import Eradicator
Expand All @@ -14,6 +11,7 @@
#: We store the version number inside the `pyproject.toml`:
pkg_version = pkg_resources.get_distribution(pkg_name).version

#: Const for `stdin` mode of `flake8`:
STDIN = 'stdin'


Expand All @@ -22,26 +20,27 @@ class Checker(object):

name = pkg_name
version = pkg_version

_error_template = 'E800 Found commented out code'

options = None

def __init__(
self, tree: ast.AST,
filename: str,
self,
tree, # that's the hack we use to trigger this check
file_tokens: List[tokenize.TokenInfo],
):
lines: Sequence[str],
) -> None:
"""
``flake8`` plugin constructor.
Arguments:
tree: the file abstract syntax tree.
filename: the name of the file to process
file_tokens: file tokens
"""
self.filename = filename
self.file_tokens = file_tokens
file_tokens: all tokens for this file.
lines: all file lines.
"""
self._file_tokens = file_tokens
self._lines = lines
self._options = {
'aggressive': self.options.eradicate_aggressive, # type: ignore
}
Expand All @@ -53,11 +52,13 @@ def __init__(

if whitelist_ext:
self._eradicator.update_whitelist(
whitelist_ext.split('#'), True,
whitelist_ext.split('#'),
extend_default=True,
)
elif whitelist:
self._eradicator.update_whitelist(
whitelist.split('#'), False,
whitelist.split('#'),
extend_default=False,
)

@classmethod
Expand Down Expand Up @@ -131,20 +132,18 @@ def _lines_with_commented_out_code(self) -> Iterable[int]:
checked for a comment. The eradicate function is only invokes,
when the tokens indicate a comment in the physical line.
"""
with open(self.filename) as f:
comment_in_file = any(
token.type == tokenize.COMMENT
for token in self.file_tokens
)
comment_in_file = any(
token.type == tokenize.COMMENT
for token in self._file_tokens
)

if comment_in_file:
f.seek(0)
for line_no, line in enumerate(f.readlines(), start=1):
filtered_source = ''.join(
self._eradicator.filter_commented_out_code(
line,
self._options['aggressive'],
),
)
if line != filtered_source:
yield line_no
if comment_in_file:
for line_no, line in enumerate(self._lines):
filtered_source = ''.join(
self._eradicator.filter_commented_out_code(
line,
aggressive=self._options['aggressive'],
),
)
if line != filtered_source:
yield line_no + 1
Loading

0 comments on commit 5f6b875

Please sign in to comment.