Skip to content

Commit

Permalink
Fix NFKC normalization bug when removing unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jul 29, 2024
1 parent 381bd1f commit 41192cb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ruff_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ toml = { workspace = true }
typed-arena = { workspace = true }
unicode-width = { workspace = true }
unicode_names2 = { workspace = true }
unicode-normalization = { workspace = true }
url = { workspace = true }

[dev-dependencies]
Expand Down
17 changes: 12 additions & 5 deletions crates/ruff_linter/src/fix/codemods.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! Interface for editing code snippets. These functions take statements or expressions as input,
//! and return the modified code snippet as output.
use std::borrow::Cow;

use anyhow::{bail, Result};
use libcst_native::{
Codegen, CodegenState, Expression, ImportNames, NameOrAttribute, ParenthesizableWhitespace,
SmallStatement, Statement,
};
use ruff_python_ast::name::UnqualifiedName;
use smallvec::{smallvec, SmallVec};
use unicode_normalization::UnicodeNormalization;

use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::Stmt;
use ruff_python_codegen::Stylist;
use ruff_source_file::Locator;
Expand Down Expand Up @@ -194,12 +197,16 @@ fn unqualified_name_from_expression<'a>(expr: &'a Expression<'a>) -> Option<Unqu
}

fn qualified_name_from_name_or_attribute(module: &NameOrAttribute) -> String {
match module {
NameOrAttribute::N(name) => name.value.to_string(),
let unnormalized = match module {
NameOrAttribute::N(name) => Cow::Borrowed(name.value),
NameOrAttribute::A(attr) => {
let name = attr.attr.value;
let prefix = unqualified_name_from_expression(&attr.value);
prefix.map_or_else(|| name.to_string(), |prefix| format!("{prefix}.{name}"))
prefix.map_or_else(
|| Cow::Borrowed(name),
|prefix| Cow::Owned(format!("{prefix}.{name}")),
)
}
}
};
unnormalized.nfkc().collect()
}

0 comments on commit 41192cb

Please sign in to comment.