Split CallPath into QualifiedName and UnqualifiedName (#10210)

## Summary

Charlie can probably explain this better than I but it turns out,
`CallPath` is used for two different things:

* To represent unqualified names like `version` where `version` can be a
local variable or imported (e.g. `from sys import version` where the
full qualified name is `sys.version`)
* To represent resolved, full qualified names

This PR splits `CallPath` into two types to make this destinction clear.

> Note: I haven't renamed all `call_path` variables to `qualified_name`
or `unqualified_name`. I can do that if that's welcomed but I first want
to get feedback on the approach and naming overall.

## Test Plan

`cargo test`
This commit is contained in:
Micha Reiser 2024-03-04 10:06:51 +01:00 committed by GitHub
parent ba4328226d
commit a6d892b1f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
181 changed files with 1692 additions and 1412 deletions

View file

@ -7,7 +7,7 @@ use ruff_python_trivia::{indentation_at_offset, CommentRanges, SimpleTokenKind,
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::call_path::{CallPath, CallPathBuilder};
use crate::name::{QualifiedName, QualifiedNameBuilder};
use crate::parenthesize::parenthesized_range;
use crate::statement_visitor::StatementVisitor;
use crate::visitor::Visitor;
@ -800,8 +800,8 @@ pub fn collect_import_from_member<'a>(
level: Option<u32>,
module: Option<&'a str>,
member: &'a str,
) -> CallPath<'a> {
let mut call_path_builder = CallPathBuilder::with_capacity(
) -> QualifiedName<'a> {
let mut qualified_name_builder = QualifiedNameBuilder::with_capacity(
level.unwrap_or_default() as usize
+ module
.map(|module| module.split('.').count())
@ -813,20 +813,20 @@ pub fn collect_import_from_member<'a>(
if let Some(level) = level {
if level > 0 {
for _ in 0..level {
call_path_builder.push(".");
qualified_name_builder.push(".");
}
}
}
// Add the remaining segments.
if let Some(module) = module {
call_path_builder.extend(module.split('.'));
qualified_name_builder.extend(module.split('.'));
}
// Add the member.
call_path_builder.push(member);
qualified_name_builder.push(member);
call_path_builder.build()
qualified_name_builder.build()
}
/// Format the call path for a relative import, or `None` if the relative import extends beyond
@ -838,29 +838,29 @@ pub fn from_relative_import<'a>(
import: &[&'a str],
// The remaining segments to the call path (e.g., given `bar.baz`, `["baz"]`).
tail: &[&'a str],
) -> Option<CallPath<'a>> {
let mut call_path_builder =
CallPathBuilder::with_capacity(module.len() + import.len() + tail.len());
) -> Option<QualifiedName<'a>> {
let mut qualified_name_builder =
QualifiedNameBuilder::with_capacity(module.len() + import.len() + tail.len());
// Start with the module path.
call_path_builder.extend(module.iter().map(String::as_str));
qualified_name_builder.extend(module.iter().map(String::as_str));
// Remove segments based on the number of dots.
for segment in import {
if *segment == "." {
if call_path_builder.is_empty() {
if qualified_name_builder.is_empty() {
return None;
}
call_path_builder.pop();
qualified_name_builder.pop();
} else {
call_path_builder.push(segment);
qualified_name_builder.push(segment);
}
}
// Add the remaining segments.
call_path_builder.extend_from_slice(tail);
qualified_name_builder.extend_from_slice(tail);
Some(call_path_builder.build())
Some(qualified_name_builder.build())
}
/// Given an imported module (based on its relative import level and module name), return the