From 5f143821032f6098adbc90e9fc8973c5e46c79fb Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 12 Jun 2017 09:54:56 +0200 Subject: [PATCH] Start analyzing backslashes. --- parso/python/normalizer.py | 21 +++++++++++++++++++-- parso/python/tree.py | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 3dbbc21..b5e1ab3 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -94,6 +94,14 @@ class BracketNode(object): self.type = self.SAME_INDENT_TYPE +class BackslashNode(object): + BACKSLASH_TYPE = object() + def __init__(self, indentation_level): + self.bracket_indentation = self.item_indentation = \ + config.indentation * indentation_level + self.type = self.NEWLINE_TYPE + + def _is_magic_name(name): return name.value.startswith('__') and name.value.startswith('__') @@ -190,6 +198,9 @@ class PEP8Normalizer(Normalizer): info = WhitespaceInfo(leaf) should_be_indenation = self._indentation_level * self._config.indentation if self._on_newline: + if self._bracket_stack and \ + self._bracket_stack[-1].type == BackslashNode.BACKSLASH_TYPE: + self._bracket_stack.pop() if info.indentation != should_be_indenation: if not self._check_tabs_spaces(info.indentation_part, info.indentation): s = '%s %s' % (len(self._config.indentation), self._indentation_type) @@ -219,7 +230,12 @@ class PEP8Normalizer(Normalizer): self.add_issue(127, 'Continuation line over-indented for visual indent', leaf) else: self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf) - # TODO else? + + if info.has_backslash: + if self._bracket_stack: + self.add_issue(502, 'The backslash is redundant between brackets', leaf) + else: + self._bracket_stack.append(BackslashNode(self._indentation_level)) first = True for comment in info.comments: @@ -252,7 +268,8 @@ class PEP8Normalizer(Normalizer): self._analyse_non_prefix(leaf) # Finalize the state. - if value and value in '()[]{}' and leaf.parent.type not in ('error_node', 'error_leaf'): + if value and value in '()[]{}' and leaf.type != 'error_leaf' \ + and leaf.parent.type != 'error_node': if value in '([{': node = BracketNode(self._config, self._indentation_level, leaf) self._bracket_stack.append(node) diff --git a/parso/python/tree.py b/parso/python/tree.py index 8f0b7fd..c11921c 100644 --- a/parso/python/tree.py +++ b/parso/python/tree.py @@ -128,7 +128,7 @@ class PythonErrorNode(PythonMixin, ErrorNode): __slots__ = () -class PythonErrorLeaf(PythonMixin, ErrorLeaf): +class PythonErrorLeaf(ErrorLeaf, PythonLeaf): __slots__ = ()