Skip to content

Commit a8082f4

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

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

bears/general/SpaceConsistencyBear.py

+46-1
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,12 +35,56 @@ 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):
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+
replacement = line
54+
if replacement.strip() == '':
55+
end_line = line_number
56+
else:
57+
break
58+
return end_line
59+
60+
if allow_leading_blanklines:
61+
start_line_of_file = 1
62+
else:
63+
end_blanklines = end_blanklines()
64+
start_line_of_file = 1
65+
if end_blanklines:
66+
start_line_of_file = end_blanklines + 1
67+
result_texts.append('Leading blank lines.')
68+
additional_info_texts.append(
69+
'Your source code contains leading blank lines.'
70+
'Those usually have no meaning. Please consider '
71+
'removing them.')
72+
diff = Diff(file)
73+
diff.delete_lines(1, end_blanklines)
74+
inconsistencies = ''.join('\n- ' + string
75+
for string in result_texts)
76+
yield Result.from_values(
77+
self,
78+
'Line contains following spacing inconsistencies:'
79+
+ inconsistencies,
80+
diffs = {filename: diff},
81+
file = filename,
82+
additional_info = '\n\n'.join(additional_info_texts))
83+
result_texts = []
84+
additional_info_texts = []
85+
86+
for line_number, line in enumerate(file[start_line_of_file - 1:],
87+
start = start_line_of_file):
4388
replacement = line
4489

4590
if enforce_newline_at_EOF:

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)

0 commit comments

Comments
 (0)