|
|
|
|
@@ -732,13 +732,13 @@ where
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// # use ruff_python_ast::helpers::format_import_from;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(format_import_from(None, None), "".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from(Some(1), None), ".".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from(Some(1), Some("foo")), ".foo".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from(0, None), "".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from(1, None), ".".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from(1, Some("foo")), ".foo".to_string());
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn format_import_from(level: Option<u32>, module: Option<&str>) -> String {
|
|
|
|
|
pub fn format_import_from(level: u32, module: Option<&str>) -> String {
|
|
|
|
|
let mut module_name = String::with_capacity(16);
|
|
|
|
|
if let Some(level) = level {
|
|
|
|
|
if level > 0 {
|
|
|
|
|
for _ in 0..level {
|
|
|
|
|
module_name.push('.');
|
|
|
|
|
}
|
|
|
|
|
@@ -756,18 +756,15 @@ pub fn format_import_from(level: Option<u32>, module: Option<&str>) -> String {
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// # use ruff_python_ast::helpers::format_import_from_member;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(format_import_from_member(None, None, "bar"), "bar".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from_member(Some(1), None, "bar"), ".bar".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from_member(Some(1), Some("foo"), "bar"), ".foo.bar".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from_member(0, None, "bar"), "bar".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from_member(1, None, "bar"), ".bar".to_string());
|
|
|
|
|
/// assert_eq!(format_import_from_member(1, Some("foo"), "bar"), ".foo.bar".to_string());
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn format_import_from_member(level: Option<u32>, module: Option<&str>, member: &str) -> String {
|
|
|
|
|
pub fn format_import_from_member(level: u32, module: Option<&str>, member: &str) -> String {
|
|
|
|
|
let mut qualified_name = String::with_capacity(
|
|
|
|
|
(level.unwrap_or(0) as usize)
|
|
|
|
|
+ module.as_ref().map_or(0, |module| module.len())
|
|
|
|
|
+ 1
|
|
|
|
|
+ member.len(),
|
|
|
|
|
(level as usize) + module.as_ref().map_or(0, |module| module.len()) + 1 + member.len(),
|
|
|
|
|
);
|
|
|
|
|
if let Some(level) = level {
|
|
|
|
|
if level > 0 {
|
|
|
|
|
for _ in 0..level {
|
|
|
|
|
qualified_name.push('.');
|
|
|
|
|
}
|
|
|
|
|
@@ -801,17 +798,17 @@ pub fn to_module_path(package: &Path, path: &Path) -> Option<Vec<String>> {
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// # use ruff_python_ast::helpers::collect_import_from_member;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(collect_import_from_member(None, None, "bar").segments(), ["bar"]);
|
|
|
|
|
/// assert_eq!(collect_import_from_member(Some(1), None, "bar").segments(), [".", "bar"]);
|
|
|
|
|
/// assert_eq!(collect_import_from_member(Some(1), Some("foo"), "bar").segments(), [".", "foo", "bar"]);
|
|
|
|
|
/// assert_eq!(collect_import_from_member(0, None, "bar").segments(), ["bar"]);
|
|
|
|
|
/// assert_eq!(collect_import_from_member(1, None, "bar").segments(), [".", "bar"]);
|
|
|
|
|
/// assert_eq!(collect_import_from_member(1, Some("foo"), "bar").segments(), [".", "foo", "bar"]);
|
|
|
|
|
/// ```
|
|
|
|
|
pub fn collect_import_from_member<'a>(
|
|
|
|
|
level: Option<u32>,
|
|
|
|
|
level: u32,
|
|
|
|
|
module: Option<&'a str>,
|
|
|
|
|
member: &'a str,
|
|
|
|
|
) -> QualifiedName<'a> {
|
|
|
|
|
let mut qualified_name_builder = QualifiedNameBuilder::with_capacity(
|
|
|
|
|
level.unwrap_or_default() as usize
|
|
|
|
|
level as usize
|
|
|
|
|
+ module
|
|
|
|
|
.map(|module| module.split('.').count())
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
@@ -819,11 +816,9 @@ pub fn collect_import_from_member<'a>(
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Include the dots as standalone segments.
|
|
|
|
|
if let Some(level) = level {
|
|
|
|
|
if level > 0 {
|
|
|
|
|
for _ in 0..level {
|
|
|
|
|
qualified_name_builder.push(".");
|
|
|
|
|
}
|
|
|
|
|
if level > 0 {
|
|
|
|
|
for _ in 0..level {
|
|
|
|
|
qualified_name_builder.push(".");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -875,14 +870,10 @@ pub fn from_relative_import<'a>(
|
|
|
|
|
/// Given an imported module (based on its relative import level and module name), return the
|
|
|
|
|
/// fully-qualified module path.
|
|
|
|
|
pub fn resolve_imported_module_path<'a>(
|
|
|
|
|
level: Option<u32>,
|
|
|
|
|
level: u32,
|
|
|
|
|
module: Option<&'a str>,
|
|
|
|
|
module_path: Option<&[String]>,
|
|
|
|
|
) -> Option<Cow<'a, str>> {
|
|
|
|
|
let Some(level) = level else {
|
|
|
|
|
return Some(Cow::Borrowed(module.unwrap_or("")));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if level == 0 {
|
|
|
|
|
return Some(Cow::Borrowed(module.unwrap_or("")));
|
|
|
|
|
}
|
|
|
|
|
@@ -1572,14 +1563,14 @@ mod tests {
|
|
|
|
|
fn resolve_import() {
|
|
|
|
|
// Return the module directly.
|
|
|
|
|
assert_eq!(
|
|
|
|
|
resolve_imported_module_path(None, Some("foo"), None),
|
|
|
|
|
resolve_imported_module_path(0, Some("foo"), None),
|
|
|
|
|
Some(Cow::Borrowed("foo"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Construct the module path from the calling module's path.
|
|
|
|
|
assert_eq!(
|
|
|
|
|
resolve_imported_module_path(
|
|
|
|
|
Some(1),
|
|
|
|
|
1,
|
|
|
|
|
Some("foo"),
|
|
|
|
|
Some(&["bar".to_string(), "baz".to_string()])
|
|
|
|
|
),
|
|
|
|
|
@@ -1588,19 +1579,16 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
// We can't return the module if it's a relative import, and we don't know the calling
|
|
|
|
|
// module's path.
|
|
|
|
|
assert_eq!(
|
|
|
|
|
resolve_imported_module_path(Some(1), Some("foo"), None),
|
|
|
|
|
None
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(resolve_imported_module_path(1, Some("foo"), None), None);
|
|
|
|
|
|
|
|
|
|
// We can't return the module if it's a relative import, and the path goes beyond the
|
|
|
|
|
// calling module's path.
|
|
|
|
|
assert_eq!(
|
|
|
|
|
resolve_imported_module_path(Some(1), Some("foo"), Some(&["bar".to_string()])),
|
|
|
|
|
resolve_imported_module_path(1, Some("foo"), Some(&["bar".to_string()])),
|
|
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
resolve_imported_module_path(Some(2), Some("foo"), Some(&["bar".to_string()])),
|
|
|
|
|
resolve_imported_module_path(2, Some("foo"), Some(&["bar".to_string()])),
|
|
|
|
|
None
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|