mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 05:14:29 +08:00
Add E292 (eof should end with newline) with tests.
This commit is contained in:
@@ -233,6 +233,13 @@ class PEP8Normalizer(Normalizer):
|
|||||||
and atom.value == 'type':
|
and atom.value == 'type':
|
||||||
self.add_issue(721, "Do not compare types, use 'isinstance()", node)
|
self.add_issue(721, "Do not compare types, use 'isinstance()", node)
|
||||||
break
|
break
|
||||||
|
elif typ == 'file_input':
|
||||||
|
endmarker = node.children[-1]
|
||||||
|
prev = endmarker.get_previous_leaf()
|
||||||
|
prefix = endmarker.prefix
|
||||||
|
if (not prefix.endswith('\n') and (
|
||||||
|
prefix or prev is None or prev.value != '\n')):
|
||||||
|
self.add_issue(292, "Do not compare types, use 'isinstance()", endmarker)
|
||||||
|
|
||||||
if typ in _IMPORT_TYPES:
|
if typ in _IMPORT_TYPES:
|
||||||
simple_stmt = node.parent
|
simple_stmt = node.parent
|
||||||
@@ -440,6 +447,14 @@ class PEP8Normalizer(Normalizer):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def _check_spacing(self, leaf, info):
|
def _check_spacing(self, leaf, info):
|
||||||
|
def add_if_spaces(*args):
|
||||||
|
if spaces:
|
||||||
|
return self.add_issue(*args)
|
||||||
|
|
||||||
|
def add_not_spaces(*args):
|
||||||
|
if not spaces:
|
||||||
|
return self.add_issue(*args)
|
||||||
|
|
||||||
spaces = info.indentation
|
spaces = info.indentation
|
||||||
prev = self._previous_leaf
|
prev = self._previous_leaf
|
||||||
if prev is not None and prev.type == 'error_leaf' or leaf.type == 'error_leaf':
|
if prev is not None and prev.type == 'error_leaf' or leaf.type == 'error_leaf':
|
||||||
@@ -447,22 +462,14 @@ class PEP8Normalizer(Normalizer):
|
|||||||
|
|
||||||
if '\t' in spaces:
|
if '\t' in spaces:
|
||||||
self.add_issue(223, 'Used tab to separate tokens', info.indentation_part)
|
self.add_issue(223, 'Used tab to separate tokens', info.indentation_part)
|
||||||
|
elif leaf.type == 'newline':
|
||||||
|
add_if_spaces(291, 'Trailing whitespace', info.indentation_part)
|
||||||
elif len(spaces) > 1:
|
elif len(spaces) > 1:
|
||||||
self.add_issue(221, 'Multiple spaces used', info.indentation_part)
|
self.add_issue(221, 'Multiple spaces used', info.indentation_part)
|
||||||
elif info.comments:
|
elif info.comments:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
def add_if_spaces(*args):
|
if prev in _OPENING_BRACKETS:
|
||||||
if spaces:
|
|
||||||
return self.add_issue(*args)
|
|
||||||
|
|
||||||
def add_not_spaces(*args):
|
|
||||||
if not spaces:
|
|
||||||
return self.add_issue(*args)
|
|
||||||
|
|
||||||
if leaf.type == 'newline':
|
|
||||||
add_if_spaces(291, 'Trailing whitespace', info.indentation_part)
|
|
||||||
elif prev in _OPENING_BRACKETS:
|
|
||||||
message = "Whitespace after '%s'" % leaf.value
|
message = "Whitespace after '%s'" % leaf.value
|
||||||
add_if_spaces(201, message, info.indentation_part)
|
add_if_spaces(201, message, info.indentation_part)
|
||||||
elif leaf in _CLOSING_BRACKETS:
|
elif leaf in _CLOSING_BRACKETS:
|
||||||
@@ -583,6 +590,7 @@ class PEP8Normalizer(Normalizer):
|
|||||||
def add_issue(self, code, message, node):
|
def add_issue(self, code, message, node):
|
||||||
from parso.python.tree import search_ancestor
|
from parso.python.tree import search_ancestor
|
||||||
if search_ancestor(node, 'error_node') is not None or \
|
if search_ancestor(node, 'error_node') is not None or \
|
||||||
|
self._previous_leaf is not None and \
|
||||||
search_ancestor(self._previous_leaf, 'error_node') is not None:
|
search_ancestor(self._previous_leaf, 'error_node') is not None:
|
||||||
return
|
return
|
||||||
super(PEP8Normalizer, self).add_issue(code, message, node)
|
super(PEP8Normalizer, self).add_issue(code, message, node)
|
||||||
|
|||||||
20
test/test_pep8.py
Normal file
20
test/test_pep8.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import parso
|
||||||
|
|
||||||
|
|
||||||
|
def issues(code):
|
||||||
|
module = parso.parse(code)
|
||||||
|
return module._get_normalizer_issues()
|
||||||
|
|
||||||
|
|
||||||
|
def test_eof_newline():
|
||||||
|
def assert_issue(code):
|
||||||
|
found = issues(code)
|
||||||
|
assert len(found) == 1
|
||||||
|
issue, = found
|
||||||
|
assert issue.code == 292
|
||||||
|
|
||||||
|
assert not issues('asdf = 1\n')
|
||||||
|
assert_issue('asdf = 1')
|
||||||
|
assert_issue('asdf = 1\n#')
|
||||||
|
assert_issue('# foobar')
|
||||||
|
assert_issue('')
|
||||||
Reference in New Issue
Block a user