Skip to content

Commit

Permalink
[ruff F401 #10390 #10391] edit to make imports explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
plredmond committed Apr 26, 2024
1 parent a65bd84 commit 5fbe05e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Interface for generating fix edits from higher-level actions (e.g., "remove an argument").
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};

use ruff_diagnostics::Edit;
use ruff_python_ast::parenthesize::parenthesized_range;
Expand Down Expand Up @@ -122,6 +122,30 @@ pub(crate) fn remove_unused_imports<'a>(
}
}

/// Edits to make the specified imports explicit, e.g. change `import x` to `import x as x`.
pub(crate) fn make_imports_explicit<'a>(
member_names: impl Iterator<Item = &'a str>,
stmt: &Stmt,
locator: &Locator,
) -> Vec<Edit> {
let aliases = match stmt {
Stmt::Import(ast::StmtImport { names, .. }) => names,
Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) => names,
_ => {
return Vec::new();
}
};
// FIXME: what if the import is already `a as b` or `a as a`?
member_names
.filter_map(|name| {
aliases
.iter()
.find(|alias| name == alias.name.id)
.map(|alias| Edit::range_replacement(format!("{name} as {name}"), alias.range))
})
.collect()
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum Parentheses {
/// Remove parentheses, if the removed argument is the only argument left.
Expand Down

0 comments on commit 5fbe05e

Please sign in to comment.