Fix some stuff about backslashes.

This commit is contained in:
Dave Halter
2017-06-13 01:23:08 +02:00
parent 7981a309d1
commit 005e5f403a
2 changed files with 29 additions and 22 deletions

View File

@@ -42,6 +42,7 @@ class WhitespaceInfo(object):
for part in parts: for part in parts:
if part.type == 'backslash': if part.type == 'backslash':
self.has_backslash = True self.has_backslash = True
self.newline_count += 1
if part.type == 'comment': if part.type == 'comment':
self.comments.append(Comment(part, indentation_part)) self.comments.append(Comment(part, indentation_part))
@@ -73,7 +74,7 @@ class IndentationNode(object):
type = SUITE_TYPE type = SUITE_TYPE
def __init__(self, config, indentation_level): def __init__(self, config, indentation_level):
self.indentation = config.indentation * indentation_level self.bracket_indentation = self.indentation = config.indentation * indentation_level
class BracketNode(IndentationNode): class BracketNode(IndentationNode):
@@ -107,7 +108,7 @@ class BackslashNode(IndentationNode):
type = IndentationNode.BACKSLASH_TYPE type = IndentationNode.BACKSLASH_TYPE
def __init__(self, config, parent_indentation): def __init__(self, config, parent_indentation):
self.indentation = parent_indentation + config.indentation self.bracket_indentation = self.indentation = parent_indentation + config.indentation
def _is_magic_name(name): def _is_magic_name(name):
@@ -189,6 +190,9 @@ class PEP8Normalizer(Normalizer):
break break
if typ == 'suite': if typ == 'suite':
if self._indentation_stack[-1].type == IndentationNode.BACKSLASH_TYPE:
self._indentation_stack.pop()
self._indentation_stack.append( self._indentation_stack.append(
IndentationNode(self._config, len(self._indentation_stack)) IndentationNode(self._config, len(self._indentation_stack))
) )
@@ -206,18 +210,29 @@ class PEP8Normalizer(Normalizer):
def normalize(self, leaf): def normalize(self, leaf):
value = leaf.value value = leaf.value
info = WhitespaceInfo(leaf) info = WhitespaceInfo(leaf)
should_be_indentation = self._indentation_stack[-1].indentation
node = self._indentation_stack[-1]
if info.has_backslash and node.type != IndentationNode.BACKSLASH_TYPE:
if node.type != IndentationNode.SUITE_TYPE:
self.add_issue(502, 'The backslash is redundant between brackets', leaf)
else:
node = BackslashNode(
self._config,
self._indentation_stack[-1].indentation
)
self._indentation_stack.append(node)
if self._on_newline: if self._on_newline:
if self._indentation_stack and \ if self._indentation_stack and \
self._indentation_stack[-1].type == BackslashNode.BACKSLASH_TYPE: self._indentation_stack[-1].type == BackslashNode.BACKSLASH_TYPE:
self._indentation_stack.pop() self._indentation_stack.pop()
if info.indentation != should_be_indentation: if info.indentation != node.indentation:
if not self._check_tabs_spaces(info.indentation_part, info.indentation): if not self._check_tabs_spaces(info.indentation_part, info.indentation):
s = '%s %s' % (len(self._config.indentation), self._indentation_type) s = '%s %s' % (len(self._config.indentation), self._indentation_type)
self.add_issue(111, 'Indentation is not a multiple of ' + s, leaf) self.add_issue(111, 'Indentation is not a multiple of ' + s, leaf)
elif info.newline_count: elif info.newline_count:
if self._indentation_stack[-1]: if True:
node = self._indentation_stack[-1]
if value in '])}': if value in '])}':
should_be_indentation = node.bracket_indentation should_be_indentation = node.bracket_indentation
else: else:
@@ -230,6 +245,8 @@ class PEP8Normalizer(Normalizer):
else: else:
if node.type == BracketNode.SAME_INDENT_TYPE: if node.type == BracketNode.SAME_INDENT_TYPE:
self.add_issue(128, 'Continuation line under-indented for visual indent', leaf) self.add_issue(128, 'Continuation line under-indented for visual indent', leaf)
elif node.type == BracketNode.BACKSLASH_TYPE:
self.add_issue(122, 'Continuation line missing indentation or outdented', leaf)
else: else:
self.add_issue(121, 'Continuation line under-indented for hanging indent', leaf) self.add_issue(121, 'Continuation line under-indented for hanging indent', leaf)
else: else:
@@ -241,17 +258,6 @@ class PEP8Normalizer(Normalizer):
else: else:
self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf) self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf)
if info.has_backslash:
if self._indentation_stack[-1].type != IndentationNode.SUITE_TYPE:
self.add_issue(502, 'The backslash is redundant between brackets', leaf)
else:
self._indentation_stack.append(
BackslashNode(
self._config,
self._indentation_stack[-1].indentation
)
)
first = True first = True
for comment in info.comments: for comment in info.comments:
if first and not self._on_newline: if first and not self._on_newline:

View File

@@ -15,12 +15,13 @@ def collect_errors(code):
if match is not None: if match is not None:
codes = match.group(2) codes = match.group(2)
for code in codes.split(): for code in codes.split():
column = len(match.group(1)) code, _, add_indent = code.partition(':')
if ':' in code: column = int(add_indent or len(match.group(1)))
code, _, add_indent = code.partition(':')
column = int(add_indent)
yield "%s@(%s,%s)" % (code[1:], line_nr + 1, column) code, _, add_line = code.partition('+')
l = line_nr + 1 + int(add_line or 0)
yield "%s@(%s,%s)" % (code[1:], l, column)
def test_normalizer_issue(normalizer_issue_file): def test_normalizer_issue(normalizer_issue_file):