knot 1.16.1

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
Documentation
/// The package and imports of one source file. JVM call and type resolution
/// uses them to tell same-named types of different packages apart (issue #45);
/// JavaScript/TypeScript resolution uses the module bindings to prevent
/// receiverless calls from crossing module boundaries without an import.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImportScope {
    /// The file's `package`, if any.
    pub package: Option<String>,
    /// Single-type imports: local name (the simple name, or a Kotlin `as`
    /// alias) → imported FQN.
    pub imports: Vec<(String, String)>,
    /// Packages imported on demand (`import com.x.*`).
    pub wildcards: Vec<String>,
    /// Local names imported by a JavaScript/TypeScript module, including
    /// CommonJS `require` aliases. Empty for JVM files.
    pub module_bindings: Vec<String>,
}

impl ImportScope {
    /// The FQN a single-type import binds `local_name` to.
    pub fn imported(&self, local_name: &str) -> Option<&str> {
        self.imports
            .iter()
            .find(|(local, _)| local == local_name)
            .map(|(_, fqn)| fqn.as_str())
    }

    /// The FQNs an unqualified type `name` may denote without a single-type
    /// import, in precedence order: the file's own package, then each
    /// on-demand import.
    pub fn implicit_candidates<'a>(&'a self, name: &'a str) -> impl Iterator<Item = String> + 'a {
        let package = match &self.package {
            Some(package) => format!("{package}.{name}"),
            None => name.to_string(),
        };
        std::iter::once(package).chain(self.wildcards.iter().map(move |w| format!("{w}.{name}")))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_import_scope_precedence() {
        let scope = ImportScope {
            package: Some("com.app".to_string()),
            imports: vec![("BConfig".to_string(), "com.b.Config".to_string())],
            wildcards: vec!["com.c".to_string()],
            module_bindings: Vec::new(),
        };
        assert_eq!(scope.imported("BConfig"), Some("com.b.Config"));
        assert_eq!(scope.imported("Config"), None);
        assert_eq!(
            scope.implicit_candidates("Config").collect::<Vec<_>>(),
            vec!["com.app.Config", "com.c.Config"]
        );
    }
}