mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-30 08:23:53 +00:00
Move keyword checks into is_identifier
(#3834)
This commit is contained in:
parent
968c7df770
commit
66d72b1c7b
9 changed files with 41 additions and 42 deletions
|
@ -13,7 +13,16 @@ pub fn is_identifier(name: &str) -> bool {
|
|||
}
|
||||
|
||||
// Are the rest of the characters letters, digits, or underscores?
|
||||
chars.all(|c| c.is_alphanumeric() || c == '_')
|
||||
if !chars.all(|c| c.is_alphanumeric() || c == '_') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is the identifier a keyword?
|
||||
if KWLIST.contains(&name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Returns `true` if a string is a private identifier, such that, when the
|
||||
|
@ -28,11 +37,6 @@ pub fn is_mangled_private(id: &str) -> bool {
|
|||
/// Returns `true` if a string is a PEP 8-compliant module name (i.e., consists of lowercase
|
||||
/// letters, numbers, underscores, and is not a keyword).
|
||||
pub fn is_module_name(name: &str) -> bool {
|
||||
// Is the string a keyword?
|
||||
if KWLIST.contains(&name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is the first character a letter or underscore?
|
||||
let mut chars = name.chars();
|
||||
if !chars
|
||||
|
@ -43,19 +47,34 @@ pub fn is_module_name(name: &str) -> bool {
|
|||
}
|
||||
|
||||
// Are the rest of the characters letters, digits, or underscores?
|
||||
chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
|
||||
}
|
||||
if !chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Returns `true` if a string appears to be a valid migration file name (e.g., `0001_initial.py`).
|
||||
pub fn is_migration_name(name: &str) -> bool {
|
||||
// Is the string a keyword?
|
||||
// Is the identifier a keyword?
|
||||
if KWLIST.contains(&name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Returns `true` if a string appears to be a valid migration file name (e.g., `0001_initial.py`).
|
||||
pub fn is_migration_name(name: &str) -> bool {
|
||||
// Are characters letters, digits, or underscores?
|
||||
name.chars()
|
||||
if !name
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is the identifier a keyword?
|
||||
if KWLIST.contains(&name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// See: https://github.com/python/cpython/blob/9d692841691590c25e6cf5b2250a594d3bf54825/Lib/keyword.py#L18
|
||||
pub const KWLIST: [&str; 35] = [
|
||||
pub(crate) const KWLIST: [&str; 35] = [
|
||||
"False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue",
|
||||
"def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import",
|
||||
"in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try", "while",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue