Move more stuff and fix tests.

This commit is contained in:
Dave Halter
2017-08-17 10:38:45 +02:00
parent 06d7da3f3e
commit f055ba198f
3 changed files with 21 additions and 15 deletions

View File

@@ -76,12 +76,12 @@ class Normalizer(use_metaclass(_NormalizerMeta)):
@classmethod
def register_rule(cls, **kwargs):
"""
Use it as a class decorator:
Use it as a class decorator::
>>> normalizer = Normalizer('grammar', 'config')
>>> @normalizer.register_rule(value='foo')
... class MyRule(Rule):
... error_code = 42
normalizer = Normalizer('grammar', 'config')
@normalizer.register_rule(value='foo')
class MyRule(Rule):
error_code = 42
"""
return cls._register_rule(**kwargs)

View File

@@ -655,13 +655,6 @@ class ErrorFinder(Normalizer):
if not in_loop:
message = "'continue' not properly in loop"
self._add_syntax_error(message, leaf)
elif leaf.value == 'break':
in_loop = False
for block in self.context.blocks:
if block.type in ('for_stmt', 'while_stmt'):
in_loop = True
if not in_loop:
self._add_syntax_error("'break' outside loop", leaf)
elif leaf.value in ('yield', 'return'):
if self.context.node.type != 'funcdef':
self._add_syntax_error("'%s' outside function" % leaf.value, leaf.parent)
@@ -797,7 +790,7 @@ class IndentationRule(Rule):
@ErrorFinder.register_rule(value='await')
class AwaitOutsideAsync(SyntaxRule):
class _AwaitOutsideAsync(SyntaxRule):
message = "'await' outside async function"
def is_issue(self, leaf):
@@ -806,3 +799,15 @@ class AwaitOutsideAsync(SyntaxRule):
def get_error_node(self, node):
# Return the whole await statement.
return node.parent
@ErrorFinder.register_rule(value='break')
class _BreakOutsideLoop(SyntaxRule):
message = "'break' outside loop"
def is_issue(self, leaf):
in_loop = False
for block in self._normalizer.context.blocks:
if block.type in ('for_stmt', 'while_stmt'):
in_loop = True
return not in_loop

View File

@@ -712,10 +712,11 @@ class PEP8NormalizerConfig(ErrorFinderConfig):
self.spaces_before_comment = spaces_before_comment
@PEP8Normalizer.register_rule(type='endmarker')
# TODO this is not yet ready.
#@PEP8Normalizer.register_rule(type='endmarker')
class BlankLineAtEnd(Rule):
code = 392
message = 'Blank line at end of file'
def check(self, leaf):
def is_issue(self, leaf):
return self._newline_count >= 2