forked from VimPlug/jedi
More Flow cleanups.
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from jedi.parser_utils import get_flow_branch_keyword
|
||||||
class Status(object):
|
class Status(object):
|
||||||
lookup_table = {}
|
lookup_table = {}
|
||||||
|
|
||||||
@@ -46,8 +47,8 @@ def reachability_check(context, context_scope, node, origin_scope=None):
|
|||||||
branch_matches = True
|
branch_matches = True
|
||||||
for flow_scope in origin_flow_scopes:
|
for flow_scope in origin_flow_scopes:
|
||||||
if flow_scope in node_flow_scopes:
|
if flow_scope in node_flow_scopes:
|
||||||
node_keyword = flow_scope.get_branch_keyword(node)
|
node_keyword = get_flow_branch_keyword(flow_scope, node)
|
||||||
origin_keyword = flow_scope.get_branch_keyword(origin_scope)
|
origin_keyword = get_flow_branch_keyword(flow_scope, origin_scope)
|
||||||
branch_matches = node_keyword == origin_keyword
|
branch_matches = node_keyword == origin_keyword
|
||||||
if flow_scope.type == 'if_stmt':
|
if flow_scope.type == 'if_stmt':
|
||||||
if not branch_matches:
|
if not branch_matches:
|
||||||
@@ -76,8 +77,8 @@ def reachability_check(context, context_scope, node, origin_scope=None):
|
|||||||
def _break_check(context, context_scope, flow_scope, node):
|
def _break_check(context, context_scope, flow_scope, node):
|
||||||
reachable = REACHABLE
|
reachable = REACHABLE
|
||||||
if flow_scope.type == 'if_stmt':
|
if flow_scope.type == 'if_stmt':
|
||||||
if flow_scope.node_after_else(node):
|
if flow_scope.is_node_after_else(node):
|
||||||
for check_node in flow_scope.check_nodes():
|
for check_node in flow_scope.get_check_nodes():
|
||||||
reachable = _check_if(context, check_node)
|
reachable = _check_if(context, check_node)
|
||||||
if reachable in (REACHABLE, UNSURE):
|
if reachable in (REACHABLE, UNSURE):
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -672,30 +672,13 @@ class Lambda(Function):
|
|||||||
|
|
||||||
class Flow(PythonBaseNode):
|
class Flow(PythonBaseNode):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
FLOW_KEYWORDS = (
|
|
||||||
'try', 'except', 'finally', 'else', 'if', 'elif', 'with', 'for', 'while'
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_branch_keyword(self, node):
|
|
||||||
start_pos = node.start_pos
|
|
||||||
if not (self.start_pos < start_pos <= self.end_pos):
|
|
||||||
raise ValueError('The node is not part of the flow.')
|
|
||||||
|
|
||||||
keyword = None
|
|
||||||
for i, child in enumerate(self.children):
|
|
||||||
if start_pos < child.start_pos:
|
|
||||||
return keyword
|
|
||||||
first_leaf = child.get_first_leaf()
|
|
||||||
if first_leaf in self.FLOW_KEYWORDS:
|
|
||||||
keyword = first_leaf
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
class IfStmt(Flow):
|
class IfStmt(Flow):
|
||||||
type = 'if_stmt'
|
type = 'if_stmt'
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def check_nodes(self):
|
def get_check_nodes(self):
|
||||||
"""
|
"""
|
||||||
Returns all the `test` nodes that are defined as x, here:
|
Returns all the `test` nodes that are defined as x, here:
|
||||||
|
|
||||||
@@ -715,7 +698,7 @@ class IfStmt(Flow):
|
|||||||
suite return None.
|
suite return None.
|
||||||
"""
|
"""
|
||||||
start_pos = node.start_pos
|
start_pos = node.start_pos
|
||||||
for check_node in reversed(list(self.check_nodes())):
|
for check_node in reversed(list(self.get_check_nodes())):
|
||||||
if check_node.start_pos < start_pos:
|
if check_node.start_pos < start_pos:
|
||||||
if start_pos < check_node.end_pos:
|
if start_pos < check_node.end_pos:
|
||||||
return None
|
return None
|
||||||
@@ -724,7 +707,7 @@ class IfStmt(Flow):
|
|||||||
else:
|
else:
|
||||||
return check_node
|
return check_node
|
||||||
|
|
||||||
def node_after_else(self, node):
|
def is_node_after_else(self, node):
|
||||||
"""
|
"""
|
||||||
Checks if a node is defined after `else`.
|
Checks if a node is defined after `else`.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -6,10 +6,9 @@ _EXECUTE_NODES = set([
|
|||||||
'shift_expr', 'arith_expr', 'atom_expr', 'term', 'factor', 'power', 'atom'
|
'shift_expr', 'arith_expr', 'atom_expr', 'term', 'factor', 'power', 'atom'
|
||||||
])
|
])
|
||||||
|
|
||||||
return_ = 'import_name', 'import_from'
|
_FLOW_KEYWORDS = (
|
||||||
|
'try', 'except', 'finally', 'else', 'if', 'elif', 'with', 'for', 'while'
|
||||||
|
)
|
||||||
# last added: Flow, KeywordStatement
|
|
||||||
|
|
||||||
|
|
||||||
def get_executable_nodes(node, last_added=False):
|
def get_executable_nodes(node, last_added=False):
|
||||||
@@ -68,3 +67,18 @@ def for_stmt_defines_one_name(for_stmt):
|
|||||||
:returns: bool
|
:returns: bool
|
||||||
"""
|
"""
|
||||||
return for_stmt.children[1].type == 'name'
|
return for_stmt.children[1].type == 'name'
|
||||||
|
|
||||||
|
|
||||||
|
def get_flow_branch_keyword(flow_node, node):
|
||||||
|
start_pos = node.start_pos
|
||||||
|
if not (flow_node.start_pos < start_pos <= flow_node.end_pos):
|
||||||
|
raise ValueError('The node is not part of the flow.')
|
||||||
|
|
||||||
|
keyword = None
|
||||||
|
for i, child in enumerate(flow_node.children):
|
||||||
|
if start_pos < child.start_pos:
|
||||||
|
return keyword
|
||||||
|
first_leaf = child.get_first_leaf()
|
||||||
|
if first_leaf in _FLOW_KEYWORDS:
|
||||||
|
keyword = first_leaf
|
||||||
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user