Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpaceConsistencyBear: Add leading_blankline option
Browse files Browse the repository at this point in the history
Add allow_trailing_blanklines option for whether to allow
leading blanklines in starting of a file.

Closes coala#2207
khanchi97 committed Jan 8, 2018
1 parent 9223889 commit fae49c9
Showing 2 changed files with 71 additions and 2 deletions.
48 changes: 47 additions & 1 deletion bears/general/SpaceConsistencyBear.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ def run(self,
file,
use_spaces: bool,
allow_trailing_whitespace: bool=False,
allow_leading_blanklines: bool=False,
indent_size: int=SpacingHelper.DEFAULT_TAB_WIDTH,
enforce_newline_at_EOF: bool=True):
'''
@@ -29,6 +30,8 @@ def run(self,
of tabs.
:param allow_trailing_whitespace: Whether to allow trailing whitespace
or not.
:param allow_leading_blanklines: Whether to allow leading blanklines
at start of file or not.
:param indent_size: Number of spaces per indentation
level.
:param enforce_newline_at_EOF: Whether to enforce a newline at the
@@ -38,7 +41,50 @@ def run(self,
result_texts = []
additional_info_texts = []

for line_number, line in enumerate(file, start=1):
def get_blanklines_nr_end():
line_nr_end = False
if not allow_leading_blanklines:
enumerated_zip_obj = zip(range(1, len(file) + 1),
file)
enumerated_tuple = tuple(enumerated_zip_obj)

for line_number, line in enumerated_tuple:
replacement = line
if replacement.strip() == '':
line_nr_end = line_number
else:
break

return line_nr_end

if allow_leading_blanklines:
start_line_of_file = 1

else:
blanklines_nr_end = get_blanklines_nr_end()
start_line_of_file = 1
if blanklines_nr_end:
start_line_of_file = blanklines_nr_end + 1
result_texts.append('Leading blanklines.')
additional_info_texts.append(
'Your source code contains leading blanklines.'
'Those usually have no meaning. Please consider'
'removing them.')
diff = Diff(file)
diff.delete_lines(1, blanklines_nr_end)
inconsistencies = ''.join('\n- ' + string
for string in result_texts)
yield Result.from_values(
self,
'Line contains following spacing inconsistencies:'
+ inconsistencies,
diffs={filename: diff},
file=filename,
additional_info='\n\n'.join(additional_info_texts))
result_texts = []
additional_info_texts = []

for line_number, line in enumerate(file[start_line_of_file - 1:], start = start_line_of_file):
replacement = line

if enforce_newline_at_EOF:
25 changes: 24 additions & 1 deletion tests/general/SpaceConsistencyBearTest.py
Original file line number Diff line number Diff line change
@@ -44,21 +44,27 @@ def test_data_sets_tabs(self):
self.section.append(Setting('use_spaces', 'false'))
self.section.append(Setting('allow_trailing_whitespace', 'true'))
self.section.append(Setting('enforce_newline_at_EOF', 'false'))
self.section.append(Setting('allow_leading_blanklines', 'false'))

self.check_invalidity(self.uut, [' t'])
self.check_validity(self.uut, ['t \n'])
self.check_validity(self.uut, ['\tt\n'])
self.check_validity(self.uut, [])

def test_enforce_newline_at_eof(self):
self.section.append(Setting('use_spaces', 'true'))
self.section.append(Setting('allow_trailing_whitespace', 'true'))
self.section.append(Setting('enforce_newline_at_EOF', 'true'))
self.section.append(Setting('allow_leading_blanklines', 'true'))

self.check_validity(self.uut,
['hello world \n'],
force_linebreaks=False)
self.check_validity(self.uut,
['def somecode():\n',
[' \n',
'\n',
' \n',
'def somecode():\n',
" print('funny')\n",
" print('funny end.')\n"],
force_linebreaks=False)
@@ -70,3 +76,20 @@ def test_enforce_newline_at_eof(self):
" print('funny')\n",
" print('the result is not funny...')"],
force_linebreaks=False)

def test_leading_blanklines(self):
self.section.append(Setting('use_spaces', 'true'))
self.section.append(Setting('allow_trailing_whitespace', 'false'))
self.section.append(Setting('enforce_newline_at_EOF', 'true'))
self.section.append(Setting('allow_leading_blanklines', 'false'))

self.check_invalidity(self.uut,
['\n',
' \n',
'def code():\n',
" print('Am I coding?')\n"],
force_linebreaks=False)
self.check_validity(self.uut,
['def code():\n',
" print('Am I coding?')\n"],
force_linebreaks=False)

0 comments on commit fae49c9

Please sign in to comment.