Skip to content

Commit 07962c0

Browse files
committed
Extract import analysis into ruff_python_imports
This provides a clean API for import analysis that can be used by both Ruff and uv, by moving the import collector, resolver, database setup, and string-import settings out of ruff_graph into a dedicated crate.
1 parent 2ab6210 commit 07962c0

14 files changed

Lines changed: 1356 additions & 334 deletions

File tree

Cargo.lock

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ruff_python_ast = { path = "crates/ruff_python_ast" }
3131
ruff_python_codegen = { path = "crates/ruff_python_codegen" }
3232
ruff_python_formatter = { path = "crates/ruff_python_formatter" }
3333
ruff_python_importer = { path = "crates/ruff_python_importer" }
34+
ruff_python_imports = { path = "crates/ruff_python_imports" }
3435
ruff_python_index = { path = "crates/ruff_python_index" }
3536
ruff_python_literal = { path = "crates/ruff_python_literal" }
3637
ruff_python_parser = { path = "crates/ruff_python_parser" }

crates/ruff_graph/Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ ruff_db = { workspace = true, features = ["serde"] }
1919
ruff_linter = { workspace = true }
2020
ruff_macros = { workspace = true }
2121
ruff_python_ast = { workspace = true }
22-
ruff_python_parser = { workspace = true }
23-
ty_module_resolver = { workspace = true }
24-
ty_site_packages = { workspace = true }
22+
ruff_python_imports = { workspace = true }
2523

2624
anyhow = { workspace = true }
2725
clap = { workspace = true, optional = true }
28-
memchr = { workspace = true }
29-
salsa = { workspace = true }
3026
schemars = { workspace = true, optional = true }
3127
serde = { workspace = true, optional = true }
32-
zip = { workspace = true, features = [] }
3328

3429
[lints]
3530
workspace = true

crates/ruff_graph/src/db.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.

crates/ruff_graph/src/lib.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ use anyhow::Result;
44

55
use ruff_db::system::{SystemPath, SystemPathBuf};
66
use ruff_python_ast::PySourceType;
7-
use ruff_python_ast::helpers::to_module_path;
8-
use ruff_python_parser::{ParseOptions, parse};
7+
use ruff_python_imports::{AnalyzeOptions, analyze_file};
98

10-
use crate::collector::Collector;
11-
pub use crate::db::ModuleDb;
12-
use crate::resolver::Resolver;
13-
pub use crate::settings::{AnalyzeSettings, Direction, StringImports};
9+
pub use crate::settings::{AnalyzeSettings, Direction};
10+
pub use ruff_python_imports::ImportDb as ModuleDb;
11+
pub use ruff_python_imports::StringImports;
1412

15-
mod collector;
16-
mod db;
17-
mod resolver;
1813
mod settings;
1914

2015
#[derive(Debug, Default)]
@@ -32,27 +27,20 @@ impl ModuleImports {
3227
string_imports: StringImports,
3328
type_checking_imports: bool,
3429
) -> Result<Self> {
35-
// Parse the source code.
36-
let parsed = parse(source, ParseOptions::from(source_type))?;
37-
38-
let module_path =
39-
package.and_then(|package| to_module_path(package.as_std_path(), path.as_std_path()));
40-
41-
// Collect the imports.
42-
let imports = Collector::new(
43-
module_path.as_deref(),
44-
string_imports,
45-
type_checking_imports,
46-
)
47-
.collect(parsed.syntax());
48-
49-
// Resolve the imports.
5030
let mut resolved_imports = ModuleImports::default();
51-
for import in imports {
52-
for resolved in Resolver::new(db, path).resolve(import) {
53-
if let Some(path) = resolved.as_system_path() {
54-
resolved_imports.insert(path.to_path_buf());
55-
}
31+
for resolved in analyze_file(
32+
db,
33+
path,
34+
package,
35+
source,
36+
source_type,
37+
&AnalyzeOptions {
38+
string_imports,
39+
type_checking_imports,
40+
},
41+
)? {
42+
if let Some(path) = resolved.resolved_path {
43+
resolved_imports.insert(path);
5644
}
5745
}
5846

crates/ruff_graph/src/resolver.rs

Lines changed: 0 additions & 120 deletions
This file was deleted.

crates/ruff_graph/src/settings.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ruff_linter::display_settings;
22
use ruff_linter::settings::types::{ExtensionMapping, FilePatternSet, PreviewMode};
33
use ruff_macros::CacheKey;
44
use ruff_python_ast::PythonVersion;
5+
use ruff_python_imports::StringImports;
56
use std::collections::BTreeMap;
67
use std::fmt;
78
use std::path::PathBuf;
@@ -51,31 +52,6 @@ impl fmt::Display for AnalyzeSettings {
5152
}
5253
}
5354

54-
#[derive(Debug, Copy, Clone, CacheKey)]
55-
pub struct StringImports {
56-
pub enabled: bool,
57-
pub min_dots: usize,
58-
}
59-
60-
impl Default for StringImports {
61-
fn default() -> Self {
62-
Self {
63-
enabled: false,
64-
min_dots: 2,
65-
}
66-
}
67-
}
68-
69-
impl fmt::Display for StringImports {
70-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
if self.enabled {
72-
write!(f, "enabled (min_dots: {})", self.min_dots)
73-
} else {
74-
write!(f, "disabled")
75-
}
76-
}
77-
}
78-
7955
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, CacheKey)]
8056
#[cfg_attr(
8157
feature = "serde",

0 commit comments

Comments
 (0)