mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 01:20:24 +00:00
Move import-name matching into methods on BindingKind
(#4818)
This commit is contained in:
parent
2c41c54e0c
commit
935094c2ff
8 changed files with 96 additions and 134 deletions
|
@ -101,6 +101,35 @@ impl<'a> Binding<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
/// Returns the fully-qualified symbol name, if this symbol was imported from another module.
|
||||
pub fn qualified_name(&self) -> Option<&str> {
|
||||
match &self.kind {
|
||||
BindingKind::Importation(Importation { full_name }) => Some(full_name),
|
||||
BindingKind::FromImportation(FromImportation { full_name }) => Some(full_name),
|
||||
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
|
||||
Some(full_name)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the fully-qualified name of the module from which this symbol was imported, if this
|
||||
/// symbol was imported from another module.
|
||||
pub fn module_name(&self) -> Option<&str> {
|
||||
match &self.kind {
|
||||
BindingKind::Importation(Importation { full_name })
|
||||
| BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
|
||||
Some(full_name.split('.').next().unwrap_or(full_name))
|
||||
}
|
||||
BindingKind::FromImportation(FromImportation { full_name }) => Some(
|
||||
full_name
|
||||
.rsplit_once('.')
|
||||
.map_or(full_name, |(module, _)| module),
|
||||
),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the appropriate visual range for highlighting this binding.
|
||||
pub fn trimmed_range(&self, semantic_model: &SemanticModel, locator: &Locator) -> TextRange {
|
||||
match self.kind {
|
||||
|
|
|
@ -256,14 +256,8 @@ impl<'a> SemanticModel<'a> {
|
|||
// import pyarrow.csv
|
||||
// print(pa.csv.read_csv("test.csv"))
|
||||
// ```
|
||||
let full_name = match &self.bindings[binding_id].kind {
|
||||
BindingKind::Importation(Importation { full_name }) => *full_name,
|
||||
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => *full_name,
|
||||
BindingKind::FromImportation(FromImportation { full_name }) => full_name.as_str(),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let has_alias = full_name
|
||||
let qualified_name = self.bindings[binding_id].qualified_name()?;
|
||||
let has_alias = qualified_name
|
||||
.split('.')
|
||||
.last()
|
||||
.map(|segment| segment != symbol)
|
||||
|
@ -272,7 +266,7 @@ impl<'a> SemanticModel<'a> {
|
|||
return None;
|
||||
}
|
||||
|
||||
self.scopes[scope_id].get(full_name)
|
||||
self.scopes[scope_id].get(qualified_name)
|
||||
}
|
||||
|
||||
/// Resolves the [`Expr`] to a fully-qualified symbol-name, if `value` resolves to an imported
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue