From de836a657506981c7d2eb29f5c0d8a5eb8da858c Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 15 Sep 2015 15:15:09 +0200 Subject: [PATCH] Start implementing nodes_to_execute in the parser. --- jedi/parser/tree.py | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index da62bd6a..2e4394f6 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -144,6 +144,9 @@ class Base(object): # Default is not being a scope. Just inherit from Scope. return False + def nodes_to_execute(self, last_added=False): + raise NotImplementedError() + class Leaf(Base): __slots__ = ('position_modifier', 'value', 'parent', '_start_pos', 'prefix') @@ -223,6 +226,9 @@ class Leaf(Base): return None return self.parent.children[i - 1] + def nodes_to_execute(self, last_added=False): + return [] + @utf8_repr def __repr__(self): return "<%s: %s>" % (type(self).__name__, self.value) @@ -330,6 +336,9 @@ class Name(Leaf): node = node.parent return indexes + def nodes_to_execute(self, last_added=False): + yield self + class Literal(LeafWithNewLines): __slots__ = () @@ -488,6 +497,15 @@ class Node(BaseNode): super(Node, self).__init__(children) self.type = type + def nodes_to_execute(self, last_added=False): + """ + For static analysis. + """ + result = [] + for child in self.children: + result += child.nodes_to_execute(last_added) + return result + def __repr__(self): return "%s(%s, %r)" % (self.__class__.__name__, self.type, self.children) @@ -699,6 +717,20 @@ class Class(ClassOrFunc): sub.get_call_signature(func_name=self.name), docstr) return docstr + def nodes_to_execute(self, last_added=False): + # Yield itself, class needs to be executed for decorator checks. + yield self + for param in self.params: + if param.default is None: + yield param.default + else: + # metaclass= + raise NotImplementedError('Metaclasses not implemented') + # care for the class suite: + for node_to_execute in self.children[-1].nodes_to_execute(False): + yield node_to_execute + + def _create_params(parent, argslist_list): """ @@ -801,6 +833,16 @@ class Function(ClassOrFunc): docstr = self.raw_doc return '%s\n\n%s' % (self.get_call_signature(), docstr) + def nodes_to_execute(self, last_added=False): + # Yield itself, functions needs to be executed for decorator checks. + yield self + for param in self.params: + if param.default is not None: + yield param.default + # care for the function suite: + for node_to_execute in self.children[-1].nodes_to_execute(False): + yield node_to_execute + class Lambda(Function): """ @@ -826,6 +868,14 @@ class Lambda(Function): def yields(self): return [] + def nodes_to_execute(self, last_added=False): + for param in self.params: + if param.default is not None: + yield param.default + # Care for the lambda test (last child): + for node_to_execute in self.children[-1].nodes_to_execute(False): + yield node_to_execute + def __repr__(self): return "<%s@%s>" % (self.__class__.__name__, self.start_pos) @@ -833,6 +883,11 @@ class Lambda(Function): class Flow(BaseNode): __slots__ = () + def nodes_to_execute(self, last_added=False): + for child in self.children: + for node_to_execute in child.nodes_to_execute(False): + yield node_to_execute + class IfStmt(Flow): type = 'if_stmt'