Use qualified_name terminology in more structs for consistency (#4873)

This commit is contained in:
Charlie Marsh 2023-06-05 15:06:48 -04:00 committed by GitHub
parent 33434fcb9c
commit 8938b2d555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 116 additions and 105 deletions

View file

@ -48,36 +48,36 @@ impl<'a> Binding<'a> {
/// Return `true` if this binding redefines the given binding.
pub fn redefines(&self, existing: &'a Binding) -> bool {
match &self.kind {
BindingKind::Importation(Importation { full_name }) => {
BindingKind::Importation(Importation { qualified_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
qualified_name: existing,
}) = &existing.kind
{
return full_name == existing;
return qualified_name == existing;
}
}
BindingKind::FromImportation(FromImportation { full_name }) => {
BindingKind::FromImportation(FromImportation { qualified_name }) => {
if let BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
qualified_name: existing,
}) = &existing.kind
{
return full_name == existing;
return qualified_name == existing;
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name }) => {
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
match &existing.kind {
BindingKind::Importation(Importation {
full_name: existing,
qualified_name: existing,
})
| BindingKind::SubmoduleImportation(SubmoduleImportation {
full_name: existing,
qualified_name: existing,
}) => {
return full_name == existing;
return qualified_name == existing;
}
BindingKind::FromImportation(FromImportation {
full_name: existing,
qualified_name: existing,
}) => {
return full_name == existing;
return qualified_name == existing;
}
_ => {}
}
@ -104,10 +104,12 @@ 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)
BindingKind::Importation(Importation { qualified_name }) => Some(qualified_name),
BindingKind::FromImportation(FromImportation { qualified_name }) => {
Some(qualified_name)
}
BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
Some(qualified_name)
}
_ => None,
}
@ -117,14 +119,14 @@ impl<'a> Binding<'a> {
/// 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::Importation(Importation { qualified_name })
| BindingKind::SubmoduleImportation(SubmoduleImportation { qualified_name }) => {
Some(qualified_name.split('.').next().unwrap_or(qualified_name))
}
BindingKind::FromImportation(FromImportation { full_name }) => Some(
full_name
BindingKind::FromImportation(FromImportation { qualified_name }) => Some(
qualified_name
.rsplit_once('.')
.map_or(full_name, |(module, _)| module),
.map_or(qualified_name, |(module, _)| module),
),
_ => None,
}
@ -241,9 +243,9 @@ pub struct Export<'a> {
#[derive(Clone, Debug)]
pub struct Importation<'a> {
/// The full name of the module being imported.
/// Ex) Given `import foo`, `full_name` would be "foo".
/// Ex) Given `import foo as bar`, `full_name` would be "foo".
pub full_name: &'a str,
/// Ex) Given `import foo`, `qualified_name` would be "foo".
/// Ex) Given `import foo as bar`, `qualified_name` would be "foo".
pub qualified_name: &'a str,
}
/// A binding for a member imported from a module, keyed on the name to which the member is bound.
@ -252,9 +254,9 @@ pub struct Importation<'a> {
#[derive(Clone, Debug)]
pub struct FromImportation {
/// The full name of the member being imported.
/// Ex) Given `from foo import bar`, `full_name` would be "foo.bar".
/// Ex) Given `from foo import bar as baz`, `full_name` would be "foo.bar".
pub full_name: String,
/// Ex) Given `from foo import bar`, `qualified_name` would be "foo.bar".
/// Ex) Given `from foo import bar as baz`, `qualified_name` would be "foo.bar".
pub qualified_name: String,
}
/// A binding for a submodule imported from a module, keyed on the name of the parent module.
@ -262,8 +264,8 @@ pub struct FromImportation {
#[derive(Clone, Debug)]
pub struct SubmoduleImportation<'a> {
/// The full name of the submodule being imported.
/// Ex) Given `import foo.bar`, `full_name` would be "foo.bar".
pub full_name: &'a str,
/// Ex) Given `import foo.bar`, `qualified_name` would be "foo.bar".
pub qualified_name: &'a str,
}
#[derive(Clone, Debug, is_macro::Is)]

View file

@ -327,7 +327,9 @@ impl<'a> SemanticModel<'a> {
let head = call_path.first()?;
let binding = self.find_binding(head)?;
match &binding.kind {
BindingKind::Importation(Importation { full_name: name }) => {
BindingKind::Importation(Importation {
qualified_name: name,
}) => {
if name.starts_with('.') {
let mut source_path = from_relative_import(self.module_path?, name);
if source_path.is_empty() {
@ -342,13 +344,17 @@ impl<'a> SemanticModel<'a> {
Some(source_path)
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation { full_name: name }) => {
BindingKind::SubmoduleImportation(SubmoduleImportation {
qualified_name: name,
}) => {
let name = name.split('.').next().unwrap_or(name);
let mut source_path: CallPath = from_unqualified_name(name);
source_path.extend(call_path.into_iter().skip(1));
Some(source_path)
}
BindingKind::FromImportation(FromImportation { full_name: name }) => {
BindingKind::FromImportation(FromImportation {
qualified_name: name,
}) => {
if name.starts_with('.') {
let mut source_path = from_relative_import(self.module_path?, name);
if source_path.is_empty() {
@ -397,8 +403,8 @@ impl<'a> SemanticModel<'a> {
// Ex) Given `module="sys"` and `object="exit"`:
// `import sys` -> `sys.exit`
// `import sys as sys2` -> `sys2.exit`
BindingKind::Importation(Importation { full_name }) => {
if full_name == &module {
BindingKind::Importation(Importation { qualified_name }) => {
if qualified_name == &module {
if let Some(source) = binding.source {
// Verify that `sys` isn't bound in an inner scope.
if self
@ -418,8 +424,9 @@ impl<'a> SemanticModel<'a> {
// Ex) Given `module="os.path"` and `object="join"`:
// `from os.path import join` -> `join`
// `from os.path import join as join2` -> `join2`
BindingKind::FromImportation(FromImportation { full_name }) => {
if let Some((target_module, target_member)) = full_name.split_once('.') {
BindingKind::FromImportation(FromImportation { qualified_name }) => {
if let Some((target_module, target_member)) = qualified_name.split_once('.')
{
if target_module == module && target_member == member {
if let Some(source) = binding.source {
// Verify that `join` isn't bound in an inner scope.