First imports are working with goto.

This commit is contained in:
Dave Halter
2014-09-19 01:21:17 +02:00
parent 610b2fc832
commit 83d2af5138
3 changed files with 36 additions and 3 deletions

View File

@@ -318,6 +318,17 @@ class Evaluator(object):
return types
def goto(self, stmt, call_path):
if isinstance(stmt, pr.Import):
# Nowhere to goto for aliases
if stmt.alias_name_part == call_path[0]:
return [call_path[0]]
names = stmt.get_all_import_name_parts()
# Filter names that are after our Name
removed_names = len(names) - names.index(call_path[0]) - 1
i = imports.ImportWrapper(self, stmt, kill_count=removed_names)
return i.follow(is_goto=True)
# Return the name defined in the call_path, if it's part of the
# statement name definitions. Only return, if it's one name and one
# name only. Otherwise it's a mixture between a definition and a

View File

@@ -840,6 +840,28 @@ class Import(Simple):
n.append(self.alias)
return n
def get_all_import_name_parts(self):
"""
TODO refactor and use this method, because NamePart will not exist in
the future.
"""
n = []
if self.from_ns:
n += self.from_ns.names
if self.namespace:
n += self.namespace.names
if self.alias:
n += self.alias.names
return n
@property
def alias_name_part(self):
"""
TODO refactor and dont use this method, because NamePart will not exist in
the future.
"""
return self.alias.names[0] if self.alias else None
def is_nested(self):
"""
This checks for the special case of nested imports, without aliases and

View File

@@ -255,15 +255,15 @@ class TestGotoAssignments(TestCase):
assert n.goto_assignments()[0].name == 'upper'
def test_import(self):
nms = names('from json import decode', references=True)
nms = names('from json import load', references=True)
assert nms[0].name == 'json'
assert nms[0].type == 'import'
n = nms[0].goto_assignments()[0]
assert n.name == 'json'
assert n.type == 'module'
assert nms[1].name == 'decode'
assert nms[1].name == 'load'
assert nms[1].type == 'import'
n = nms[1].goto_assignments()[0]
assert n.name == 'decode'
assert n.name == 'load'
assert n.type == 'function'