1
- # -*- coding: utf-8 -*-
2
-
3
1
import tokenize
4
- from typing import Iterable , Tuple
2
+ from typing import Iterable , Iterator , List , Sequence , Tuple , Type
5
3
6
4
import pkg_resources
7
5
from eradicate import Eradicator
13
11
#: We store the version number inside the `pyproject.toml`:
14
12
pkg_version = pkg_resources .get_distribution (pkg_name ).version
15
13
14
+ #: Const for `stdin` mode of `flake8`:
16
15
STDIN = 'stdin'
17
16
18
17
@@ -21,18 +20,27 @@ class Checker(object):
21
20
22
21
name = pkg_name
23
22
version = pkg_version
23
+
24
24
_error_template = 'E800 Found commented out code'
25
25
26
26
options = None
27
27
28
- def __init__ (self , physical_line , tokens ) -> None :
28
+ def __init__ (
29
+ self ,
30
+ tree , # that's the hack we use to trigger this check
31
+ file_tokens : List [tokenize .TokenInfo ],
32
+ lines : Sequence [str ],
33
+ ) -> None :
29
34
"""
30
- Creates new checker instance.
35
+ ``flake8`` plugin constructor.
36
+
37
+ Arguments:
38
+ file_tokens: all tokens for this file.
39
+ lines: all file lines.
31
40
32
- When performance will be an issue - we can refactor it.
33
41
"""
34
- self ._physical_line = physical_line
35
- self ._tokens = tokens
42
+ self ._file_tokens = file_tokens
43
+ self ._lines = lines
36
44
self ._options = {
37
45
'aggressive' : self .options .eradicate_aggressive , # type: ignore
38
46
}
@@ -44,11 +52,13 @@ def __init__(self, physical_line, tokens) -> None:
44
52
45
53
if whitelist_ext :
46
54
self ._eradicator .update_whitelist (
47
- whitelist_ext .split ('#' ), True ,
55
+ whitelist_ext .split ('#' ),
56
+ extend_default = True ,
48
57
)
49
58
elif whitelist :
50
59
self ._eradicator .update_whitelist (
51
- whitelist .split ('#' ), False ,
60
+ whitelist .split ('#' ),
61
+ extend_default = False ,
52
62
)
53
63
54
64
@classmethod
@@ -103,14 +113,14 @@ def parse_options(cls, options) -> None:
103
113
"""Parses registered options for providing them to each visitor."""
104
114
cls .options = options
105
115
106
- def __iter__ (self ) -> Iterable [Tuple [int , str ]]:
116
+ def run (self ) -> Iterator [Tuple [int , int , str , Type [ 'Checker' ] ]]:
107
117
"""Runs on each step of flake8."""
108
- if self ._contains_commented_out_code ():
109
- yield ( 1 , self ._error_template )
118
+ for line_no in self ._lines_with_commented_out_code ():
119
+ yield line_no , 0 , self ._error_template , type ( self )
110
120
111
- def _contains_commented_out_code (self ) -> bool :
121
+ def _lines_with_commented_out_code (self ) -> Iterable [ int ] :
112
122
"""
113
- Check if the current physical line contains commented out code.
123
+ Yield the physical line number that contain commented out code.
114
124
115
125
This test relies on eradicate function to remove commented out code
116
126
from a physical line.
@@ -121,19 +131,19 @@ def _contains_commented_out_code(self) -> bool:
121
131
To prevent this false-positive, the tokens of the physical line are
122
132
checked for a comment. The eradicate function is only invokes,
123
133
when the tokens indicate a comment in the physical line.
124
-
125
134
"""
126
- comment_in_line = any (
127
- token_type == tokenize .COMMENT
128
- for token_type , _ , _ , _ , _ in self ._tokens
135
+ comment_in_file = any (
136
+ token . type == tokenize .COMMENT
137
+ for token in self ._file_tokens
129
138
)
130
139
131
- if comment_in_line :
132
- filtered_source = '' .join (
133
- self ._eradicator .filter_commented_out_code (
134
- self ._physical_line ,
135
- self ._options ['aggressive' ],
136
- ),
137
- )
138
- return self ._physical_line != filtered_source
139
- return False
140
+ if comment_in_file :
141
+ for line_no , line in enumerate (self ._lines ):
142
+ filtered_source = '' .join (
143
+ self ._eradicator .filter_commented_out_code (
144
+ line ,
145
+ aggressive = self ._options ['aggressive' ],
146
+ ),
147
+ )
148
+ if line != filtered_source :
149
+ yield line_no + 1
0 commit comments