Move the error node checking to a rule.

This commit is contained in:
Dave Halter
2017-08-19 12:43:47 +02:00
parent 5ff33c3736
commit 820e94e03a
4 changed files with 40 additions and 15 deletions

View File

@@ -283,17 +283,7 @@ class ErrorFinder(Normalizer):
def visit_node(self, node):
self._check_type_rules(node)
if node.type == 'error_node':
leaf = node.get_next_leaf()
if node.children[-1].type == 'newline':
# This is the beginning of a suite that is not indented.
spacing = list(leaf._split_prefix())[-1]
self._add_indentation_error(spacing, 'expected an indented block')
else:
if leaf.type != 'error_leaf':
# Error leafs will be added later as an error.
self._add_syntax_error(leaf, "invalid syntax")
elif node.type in _BLOCK_STMTS:
if node.type in _BLOCK_STMTS:
with self.context.add_block(node):
if len(self.context.blocks) == _MAX_BLOCK_SIZE:
self._add_syntax_error(node, "too many statically nested blocks")
@@ -395,6 +385,27 @@ class ErrorFinder(Normalizer):
self.issues.append(Issue(node, code, message))
class IndentationRule(Rule):
code = 903
def _get_message(self, message):
message = super(IndentationRule, self)._get_message(message)
return "IndentationError: " + message
@ErrorFinder.register_rule(type='error_node')
class _ExpectIndentedBlock(IndentationRule):
message = 'expected an indented block'
def get_node(self, node):
leaf = node.get_next_leaf()
return list(leaf._split_prefix())[-1]
def is_issue(self, node):
# This is the beginning of a suite that is not indented.
return node.children[-1].type == 'newline'
class ErrorFinderConfig(NormalizerConfig):
normalizer_class = ErrorFinder
@@ -407,8 +418,16 @@ class SyntaxRule(Rule):
return "SyntaxError: " + message
class IndentationRule(Rule):
code = 903
@ErrorFinder.register_rule(type='error_node')
class _InvalidSyntaxRule(SyntaxRule):
message = "invalid syntax"
def get_node(self, node):
return node.get_next_leaf()
def is_issue(self, node):
# Error leafs will be added later as an error.
return node.get_next_leaf().type != 'error_leaf'
@ErrorFinder.register_rule(value='await')

View File

@@ -692,8 +692,12 @@ class PEP8Normalizer(ErrorFinder):
return
if search_ancestor(node, 'error_node') is not None:
return
# Skip ErrorFinder here, because it has custom behavior.
super(ErrorFinder, self).add_issue(node, code, message)
if code in (901, 903):
# 901 and 903 are raised by the ErrorFinder.
super(PEP8Normalizer, self).add_issue(node, code, message)
else:
# Skip ErrorFinder here, because it has custom behavior.
super(ErrorFinder, self).add_issue(node, code, message)
class PEP8NormalizerConfig(ErrorFinderConfig):

View File

@@ -137,6 +137,7 @@ FAILING_EXAMPLES = [
'def x():\n 1\n 2',
'def x():\n 1\n 2',
'if 1:\nfoo',
'if 1: blubb\nif 1:\npass\nTrue and False',
]
GLOBAL_NONLOCAL_ERROR = [

View File

@@ -21,6 +21,7 @@ def _get_error_list(code, version=None):
tree = grammar.parse(code)
return list(grammar.iter_errors(tree))
def assert_comparison(code, error_code, positions):
errors = [(error.start_pos, error.code) for error in _get_error_list(code)]
assert [(pos, error_code) for pos in positions] == errors