Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit be3a99d

Browse files
committedMar 19, 2019
Bears: Remove leading blank line option
Implemented a feature for spaceconsistencyBear that control the removal of blank lines. Closes #2207
1 parent fd5a5a7 commit be3a99d

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed
 

‎bears/general/SpaceConsistencyBear.py

+48-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def run(self,
1818
file,
1919
use_spaces: bool,
2020
allow_trailing_whitespace: bool = False,
21+
allow_leading_blanklines: bool = False,
2122
indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
2223
enforce_newline_at_EOF: bool = True,
2324
):
@@ -34,13 +35,58 @@ def run(self,
3435
Number of spaces per indentation level.
3536
:param enforce_newline_at_EOF:
3637
Whether to enforce a newline at the End Of File.
38+
:param allow_leading_blanklines:
39+
Whether to allow leading blank lines at the start
40+
of file or not.
3741
'''
3842
spacing_helper = SpacingHelper(indent_size)
3943
result_texts = []
4044
additional_info_texts = []
4145

42-
for line_number, line in enumerate(file, start=1):
43-
replacement = line
46+
def end_blanklines():
47+
end_line = False
48+
enumerated_zip_obj = zip(range(1, len(file) + 1),
49+
file)
50+
enumerated_tuple = tuple(enumerated_zip_obj)
51+
52+
for line_number, line in enumerated_tuple:
53+
if replacement.strip() == '':
54+
end_line = line_number
55+
else:
56+
break
57+
58+
return end_line
59+
60+
if allow_leading_blanklines:
61+
start_line_of_file = 1
62+
63+
else:
64+
end_blanklines = end_blanklines()
65+
start_line_of_file = 1
66+
if end_blanklines:
67+
start_line_of_file = end_blanklines + 1
68+
result_texts.append('Leading blank lines.')
69+
additional_info_texts.append(
70+
'Your source code contains leading blank lines.'
71+
'Those usually have no meaning. Please consider '
72+
'removing them.')
73+
diff = Diff(file)
74+
diff.delete_lines(1, end_blanklines)
75+
inconsistencies = ''.join('\n- ' + string
76+
for string in result_texts)
77+
yield Result.from_values(
78+
self,
79+
'Line contains following spacing inconsistencies:'
80+
+ inconsistencies,
81+
diffs={filename: diff},
82+
file=filename,
83+
additional_info='\n\n'.join(additional_info_texts))
84+
result_texts = []
85+
additional_info_texts = []
86+
87+
for line_number, line in enumerate(file[start_line_of_file - 1:],
88+
start=start_line_of_file):
89+
replacement = line
4490

4591
if enforce_newline_at_EOF:
4692
# Since every line contains at the end at least one \n, only

‎tests/general/SpaceConsistencyBearTest.py

+24
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,28 @@ def test_data_sets_tabs(self):
4444
self.section.append(Setting('use_spaces', 'false'))
4545
self.section.append(Setting('allow_trailing_whitespace', 'true'))
4646
self.section.append(Setting('enforce_newline_at_EOF', 'false'))
47+
self.section.append(Setting('allow_leading_blanklines', 'false'))
4748

4849
self.check_invalidity(self.uut, [' t'])
4950
self.check_validity(self.uut, ['t \n'])
5051
self.check_validity(self.uut, ['\tt\n'])
52+
self.check_validity(self.uut, [])
5153

5254
def test_enforce_newline_at_eof(self):
5355
self.section.append(Setting('use_spaces', 'true'))
5456
self.section.append(Setting('allow_trailing_whitespace', 'true'))
5557
self.section.append(Setting('enforce_newline_at_EOF', 'true'))
58+
self.section.append(Setting('allow_leading_blanklines', 'true'))
5659

5760
self.check_validity(self.uut,
5861
['hello world \n'],
5962
force_linebreaks=False)
6063
self.check_validity(self.uut,
6164
['def somecode():\n',
65+
[' \n',
66+
'\n',
67+
' \n',
68+
'def somecode():\n',
6269
" print('funny')\n",
6370
" print('funny end.')\n"],
6471
force_linebreaks=False)
@@ -70,3 +77,20 @@ def test_enforce_newline_at_eof(self):
7077
" print('funny')\n",
7178
" print('the result is not funny...')"],
7279
force_linebreaks=False)
80+
81+
def test_leading_blanklines(self):
82+
self.section.append(Setting('use_spaces', 'true'))
83+
self.section.append(Setting('allow_trailing_whitespace', 'false'))
84+
self.section.append(Setting('enforce_newline_at_EOF', 'true'))
85+
self.section.append(Setting('allow_leading_blanklines', 'false'))
86+
87+
self.check_invalidity(self.uut,
88+
['\n',
89+
' \n',
90+
'def code():\n',
91+
" print('Am I coding?')\n"],
92+
force_linebreaks=False)
93+
self.check_validity(self.uut,
94+
['def code():\n',
95+
" print('Am I coding?')\n"],
96+
force_linebreaks=False)

‎tests/go/GoImportsBearTest.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from bears.go.GoImportsBear import GoImportsBear
32
from coalib.testing.LocalBearTestHelper import verify_local_bear
43

0 commit comments

Comments
 (0)
Please sign in to comment.