Skip to content

Commit

Permalink
Revert "Use file"
Browse files Browse the repository at this point in the history
This reverts commit 3582a5e.
  • Loading branch information
charliermarsh committed Sep 21, 2024
1 parent 3582a5e commit b8b3515
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
8 changes: 1 addition & 7 deletions crates/ruff_graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ pub use crate::db::ModuleDb;
use crate::resolver::Resolver;
pub use crate::settings::{AnalyzeSettings, Direction};
use anyhow::Result;
use red_knot_python_semantic::SemanticModel;
use ruff_db::files::system_path_to_file;
use ruff_db::system::{SystemPath, SystemPathBuf};
use ruff_python_ast::helpers::to_module_path;
use ruff_python_parser::{parse, Mode};
Expand All @@ -28,10 +26,6 @@ impl ModuleImports {
package: Option<&SystemPath>,
string_imports: bool,
) -> Result<Self> {
// Initialize the semantic model.
let file = system_path_to_file(db, path)?;
let semantic = SemanticModel::new(db, file);

// Read and parse the source code.
let source = std::fs::read_to_string(path)?;
let parsed = parse(&source, Mode::Module)?;
Expand All @@ -46,7 +40,7 @@ impl ModuleImports {
// Resolve the imports.
let mut resolved_imports = ModuleImports::default();
for import in imports {
let Some(resolved) = Resolver::new(&semantic).resolve(import) else {
let Some(resolved) = Resolver::new(&db).resolve(import) else {
continue;
};
let Some(path) = resolved.as_system_path() else {
Expand Down
29 changes: 14 additions & 15 deletions crates/ruff_graph/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
use red_knot_python_semantic::SemanticModel;
use red_knot_python_semantic::resolve_module;
use ruff_db::files::FilePath;

use crate::collector::CollectedImport;
use crate::ModuleDb;

/// Collect all imports for a given Python file.
pub(crate) struct Resolver<'a> {
semantic: &'a SemanticModel<'a>,
db: &'a ModuleDb,
}

impl<'a> Resolver<'a> {
/// Initialize a [`Resolver`] with a given [`SemanticModel`].
pub(crate) fn new(semantic: &'a SemanticModel<'a>) -> Self {
Self { semantic }
/// Initialize a [`Resolver`] with a given [`ModuleDb`].
pub(crate) fn new(db: &'a ModuleDb) -> Self {
Self { db }
}

/// Resolve the [`CollectedImport`] into a [`FilePath`].
pub(crate) fn resolve(&self, import: CollectedImport) -> Option<&'a FilePath> {
match import {
CollectedImport::Import(import) => self
.semantic
.resolve_module(import)
.map(|module| module.file().path(self.semantic.db())),
CollectedImport::Import(import) => {
resolve_module(self.db, import).map(|module| module.file().path(self.db))
}
CollectedImport::ImportFrom(import) => {
// Attempt to resolve the member (e.g., given `from foo import bar`, look for `foo.bar`).
let parent = import.parent();
self.semantic
.resolve_module(import)
.map(|module| module.file().path(self.semantic.db()))

resolve_module(self.db, import)
.map(|module| module.file().path(self.db))
.or_else(|| {
// Attempt to resolve the module (e.g., given `from foo import bar`, look for `foo`).
self.semantic
.resolve_module(parent?)
.map(|module| module.file().path(self.semantic.db()))

resolve_module(self.db, parent?).map(|module| module.file().path(self.db))
})
}
}
Expand Down

0 comments on commit b8b3515

Please sign in to comment.