From bc55ca848161cee141f9dcdba813c82a144e5af3 Mon Sep 17 00:00:00 2001 From: PLR <51248199+plredmond@users.noreply.github.com> Date: Mon, 29 Apr 2024 13:31:17 -0700 Subject: [PATCH] [ruff F401 #10390 #10391] unit tests for make_redundant_alias --- crates/ruff_linter/src/fix/edits.rs | 41 ++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/crates/ruff_linter/src/fix/edits.rs b/crates/ruff_linter/src/fix/edits.rs index 3bb09cc40f5791..11d16d581ddde6 100644 --- a/crates/ruff_linter/src/fix/edits.rs +++ b/crates/ruff_linter/src/fix/edits.rs @@ -122,9 +122,6 @@ pub(crate) fn remove_unused_imports<'a>( } } -// TODO: unit tests -// TODO: test for `a as a` -// TODO: test for `a as b` /// Edits to make the specified imports explicit, e.g. change `import x` to `import x as x`. pub(crate) fn make_redundant_alias<'a>( member_names: impl Iterator, @@ -141,7 +138,7 @@ pub(crate) fn make_redundant_alias<'a>( .filter_map(|name| { aliases .iter() - .find(|alias| name == alias.name.id) + .find(|alias| name == alias.name.id && alias.asname.is_none()) .map(|alias| Edit::range_replacement(format!("{name} as {name}"), alias.range)) }) .collect() @@ -482,11 +479,12 @@ fn all_lines_fit( mod tests { use anyhow::Result; + use ruff_diagnostics::Edit; use ruff_python_parser::parse_suite; use ruff_source_file::Locator; - use ruff_text_size::{Ranged, TextSize}; + use ruff_text_size::{Ranged, TextRange, TextSize}; - use crate::fix::edits::{next_stmt_break, trailing_semicolon}; + use crate::fix::edits::{make_redundant_alias, next_stmt_break, trailing_semicolon}; #[test] fn find_semicolon() -> Result<()> { @@ -557,4 +555,35 @@ x = 1 \ TextSize::from(12) ); } + + #[test] + fn redundant_alias() { + let contents = "import x, y as y, z as bees"; + let program = parse_suite(contents).unwrap(); + let stmt = program.first().unwrap(); + assert_eq!( + make_redundant_alias(vec!["x"].into_iter(), stmt), + vec![Edit::range_replacement( + String::from("x as x"), + TextRange::new(TextSize::new(7), TextSize::new(8)), + )], + "make just one item redundant" + ); + assert_eq!( + make_redundant_alias(vec!["x", "y"].into_iter(), stmt), + vec![Edit::range_replacement( + String::from("x as x"), + TextRange::new(TextSize::new(7), TextSize::new(8)), + )], + "the second item is already a redundant alias" + ); + assert_eq!( + make_redundant_alias(vec!["x", "z"].into_iter(), stmt), + vec![Edit::range_replacement( + String::from("x as x"), + TextRange::new(TextSize::new(7), TextSize::new(8)), + )], + "the third item is already aliased to something else" + ); + } }