use tree_sitter::Node;
use sephera_core::{
declarations::{
DeclarationRules, DeclaredNames, collect_declared_names_with,
},
types::{ImportKind, ImportStatement},
};
use sephera_core::plugins::{line_of, node_text};
const PYTHON_DECLARATION_KINDS: &[&str] =
&["function_definition", "class_definition"];
pub fn declared_names(
source: &[u8],
tree: &tree_sitter::Tree,
) -> DeclaredNames {
let mut declared = collect_declared_names_with(
source,
tree,
DeclarationRules {
declaration_kinds: PYTHON_DECLARATION_KINDS,
reexport_kinds: &[],
},
);
collect_module_level_assignments(source, tree.root_node(), &mut declared);
declared
}
fn collect_module_level_assignments(
source: &[u8],
module: Node<'_>,
declared: &mut DeclaredNames,
) {
let mut cursor = module.walk();
for statement in module.named_children(&mut cursor) {
if statement.kind() != "expression_statement" {
continue;
}
let Some(assignment) = statement.named_child(0) else {
continue;
};
if assignment.kind() != "assignment" {
continue;
}
for target in assignment_targets(assignment) {
declared.declare(node_text(source, &target));
}
}
}
fn assignment_targets(assignment: Node<'_>) -> Vec<Node<'_>> {
let mut cursor = assignment.walk();
let Some(left) = assignment.named_children(&mut cursor).next() else {
return Vec::new();
};
match left.kind() {
"identifier" => vec![left],
"pattern_list" | "tuple_pattern" => left
.named_children(&mut cursor)
.filter(|element| element.kind() == "identifier")
.collect(),
_ => Vec::new(),
}
}
pub fn extract_from_node(
source: &[u8],
node: &Node<'_>,
) -> Option<Vec<ImportStatement>> {
let line = line_of(node)?;
match node.kind() {
"import_statement" => plain_imports(source, node, line),
"import_from_statement" => from_imports(source, node, line),
_ => None,
}
}
fn plain_imports(
source: &[u8],
node: &Node<'_>,
line: u64,
) -> Option<Vec<ImportStatement>> {
let mut statements = Vec::new();
let mut cursor = node.walk();
for child in node.named_children(&mut cursor) {
let raw = match child.kind() {
"aliased_import" => child
.child_by_field_name("name")
.map_or_else(String::new, |name| node_text(source, &name)),
_ => node_text(source, &child),
};
if raw.is_empty() {
continue;
}
statements.push(ImportStatement::new(raw, line));
}
(!statements.is_empty()).then_some(statements)
}
fn from_imports(
source: &[u8],
node: &Node<'_>,
line: u64,
) -> Option<Vec<ImportStatement>> {
let module = node.child_by_field_name("module_name")?;
let raw = node_text(source, &module).trim().to_owned();
if raw.is_empty() {
return None;
}
let mut statements = vec![ImportStatement::new(raw.clone(), line)];
let dots_only = raw.chars().all(|character| character == '.');
let mut cursor = node.walk();
let mut names = node.named_children(&mut cursor);
let _ = names.next(); for name in names {
let module_name = name.child_by_field_name("name").map_or_else(
|| node_text(source, &name),
|field| node_text(source, &field),
);
if module_name.is_empty() {
continue;
}
let full_path = if dots_only {
format!("{raw}{module_name}")
} else {
format!("{raw}.{module_name}")
};
let kind = if dots_only && module_name == "*" {
ImportKind::Namespace
} else {
ImportKind::Dependency
};
statements.push(ImportStatement::new(full_path, line).with_kind(kind));
}
Some(statements)
}
#[cfg(test)]
mod tests {
use super::*;
use sephera_compression::SupportedLanguage;
use sephera_compression::with_parser;
use sephera_core::plugins::imports_found_by;
fn imports(source: &[u8]) -> Vec<ImportStatement> {
with_parser(SupportedLanguage::Python, source.len(), |parser| {
Ok(imports_found_by(source, parser, &crate::PythonPlugin))
})
.expect("a parser exists")
}
fn paths(source: &[u8]) -> Vec<String> {
imports(source).into_iter().map(|s| s.raw_path).collect()
}
#[test]
fn a_binding_named_after_the_module_is_not_mistaken_for_the_path() {
assert_eq!(paths(b"import typing as t\n"), vec!["typing".to_owned()]);
}
#[test]
fn a_comma_separated_import_names_each_module() {
assert_eq!(
paths(b"import os, sys\n"),
vec!["os".to_owned(), "sys".to_owned()]
);
}
#[test]
fn a_from_import_reports_the_module_half() {
assert_eq!(
paths(b"from a.b.c import d\n"),
vec!["a.b.c".to_owned(), "a.b.c.d".to_owned()]
);
}
#[test]
fn a_relative_import_keeps_its_dots() {
for (source, expected) in [
(
&b"from .mod import thing\n"[..],
vec![".mod".to_owned(), ".mod.thing".to_owned()],
),
(
&b"from ..pkg import other\n"[..],
vec!["..pkg".to_owned(), "..pkg.other".to_owned()],
),
] {
assert_eq!(
paths(source),
expected,
"lost leading dots or submodule"
);
}
}
#[test]
fn importing_a_name_from_a_package_names_the_submodule_too() {
let found = paths(b"from . import helper\n");
assert!(
found.contains(&".helper".to_owned()),
"the submodule is missing from {found:?}"
);
}
#[test]
fn an_aliased_package_import_drops_the_alias_from_the_submodule_path() {
let found = paths(b"from . import typing as ft\n");
assert!(
found.contains(&".typing".to_owned()),
"the alias leaked into the path: {found:?}"
);
}
#[test]
fn a_relative_submodule_import_is_a_dependency() {
let found = imports(b"from . import helper\n");
let submodule = found
.iter()
.find(|statement| statement.raw_path == ".helper")
.expect("the submodule must be present");
assert_eq!(submodule.kind, ImportKind::Dependency);
}
#[test]
fn a_wildcard_import_is_a_namespace() {
let found = imports(b"from . import *\n");
let wildcard = found
.iter()
.find(|statement| statement.raw_path == ".*")
.expect("the wildcard must be present");
assert_eq!(wildcard.kind, ImportKind::Namespace);
}
#[test]
fn each_import_keeps_its_own_line() {
let found = imports(b"import os\nimport sys\n");
assert_eq!(
found.iter().map(|s| s.line).collect::<Vec<_>>(),
vec![1, 2]
);
}
#[test]
fn a_file_with_no_imports_reports_none() {
assert_eq!(paths(b"def f():\n pass\n"), Vec::<String>::new());
}
}