Skip to content

Commit

Permalink
add imports to symbol table
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Apr 19, 2024
1 parent f4a72b6 commit fe7213e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions crates/red_knot/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ impl<'a> PreorderVisitor<'a> for SymbolTableBuilder {
ast::visitor::preorder::walk_stmt(self, stmt);
self.pop_scope();
}
ast::Stmt::Import(ast::StmtImport { names, .. }) => {
for alias in names {
self.add_symbol(alias.name.id.split('.').next().unwrap());
}
}
ast::Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) => {
for alias in names {
self.add_symbol(&alias.name.id);
}
}
_ => {
ast::visitor::preorder::walk_stmt(self, stmt);
}
Expand Down Expand Up @@ -338,6 +348,24 @@ mod tests {
assert_eq!(names(table.root_symbols()), vec!["int", "x"]);
}

#[test]
fn import() {
let table = build("import foo");
assert_eq!(names(table.root_symbols()), vec!["foo"]);
}

#[test]
fn import_sub() {
let table = build("import foo.bar");
assert_eq!(names(table.root_symbols()), vec!["foo"]);
}

#[test]
fn import_from() {
let table = build("from bar import foo");
assert_eq!(names(table.root_symbols()), vec!["foo"]);
}

#[test]
fn class_scope() {
let table = build(
Expand Down

0 comments on commit fe7213e

Please sign in to comment.