mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Make ImportFrom level just a u32 (#11170)
This commit is contained in:
parent
5994414739
commit
845ba7cf5f
34 changed files with 100 additions and 182 deletions
|
@ -1306,7 +1306,7 @@ pub struct StmtImport<'a> {
|
|||
pub struct StmtImportFrom<'a> {
|
||||
module: Option<&'a str>,
|
||||
names: Vec<ComparableAlias<'a>>,
|
||||
level: Option<u32>,
|
||||
level: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ pub struct Import<'a> {
|
|||
pub struct ImportFrom<'a> {
|
||||
pub module: Option<&'a str>,
|
||||
pub name: Alias<'a>,
|
||||
pub level: Option<u32>,
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
@ -51,7 +51,7 @@ impl<'a> ImportFrom<'a> {
|
|||
name,
|
||||
as_name: None,
|
||||
},
|
||||
level: None,
|
||||
level: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,8 +78,8 @@ impl std::fmt::Display for Import<'_> {
|
|||
impl std::fmt::Display for ImportFrom<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "from ")?;
|
||||
if let Some(level) = self.level {
|
||||
write!(f, "{}", ".".repeat(level as usize))?;
|
||||
if self.level > 0 {
|
||||
write!(f, "{}", ".".repeat(self.level as usize))?;
|
||||
}
|
||||
if let Some(module) = self.module {
|
||||
write!(f, "{module}")?;
|
||||
|
|
|
@ -471,7 +471,7 @@ pub struct StmtImportFrom {
|
|||
pub range: TextRange,
|
||||
pub module: Option<Identifier>,
|
||||
pub names: Vec<Alias>,
|
||||
pub level: Option<u32>,
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
impl From<StmtImportFrom> for Stmt {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue