Fix uppercase and lowercase check (#461)

This commit is contained in:
Harutaka Kawamura 2022-10-23 01:49:13 +09:00 committed by GitHub
parent 2c24e2fd28
commit b75ea94f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 151 additions and 35 deletions

View File

@ -1,2 +1,3 @@
import mod.SUBMOD as submod
from mod import BAD as bad
import mod.CONST as const
from mod import CONSTANT as constant
from mod import ANOTHER_CONSTANT as another_constant

View File

@ -1,2 +1,3 @@
import mod.submod as SubMod
from mod import bad as Bad
import modl.lowercase as Lower
from mod import lowercase as Lowercase
from mod import another_lowercase as AnotherLowercase

View File

@ -1,2 +1,3 @@
import mod.SubMod as submod
import mod.Camel as camel
from mod import CamelCase as camelcase
from mod import AnotherCamelCase as another_camelcase

View File

@ -1,2 +1,3 @@
import mod.SubMod as SUBMOD
import mod.Camel as CAMEL
from mod import CamelCase as CAMELCASE
from mod import AnotherCamelCase as ANOTHER_CAMELCASE

View File

@ -1,2 +1,2 @@
import mod.SubMod as SM
import mod.CaMel as CM
from mod import CamelCase as CC

View File

@ -114,12 +114,36 @@ pub fn dunder_function_name(func_def: &Stmt, scope: &Scope, name: &str) -> Optio
None
}
fn is_lower(s: &str) -> bool {
let mut cased = false;
for c in s.chars() {
if c.is_uppercase() {
return false;
} else if !cased && c.is_lowercase() {
cased = true;
}
}
cased
}
fn is_upper(s: &str) -> bool {
let mut cased = false;
for c in s.chars() {
if c.is_lowercase() {
return false;
} else if (!cased) && c.is_uppercase() {
cased = true;
}
}
cased
}
pub fn constant_imported_as_non_constant(
import_from: &Stmt,
name: &str,
asname: &str,
) -> Option<Check> {
if name.chars().all(|c| c.is_uppercase()) && !asname.chars().all(|c| c.is_uppercase()) {
if is_upper(name) && !is_upper(asname) {
return Some(Check::new(
CheckKind::ConstantImportedAsNonConstant(name.to_string(), asname.to_string()),
Range::from_located(import_from),
@ -133,7 +157,7 @@ pub fn lowercase_imported_as_non_lowercase(
name: &str,
asname: &str,
) -> Option<Check> {
if name.chars().all(|c| c.is_lowercase()) && asname.to_lowercase() != asname {
if is_lower(name) && asname.to_lowercase() != asname {
return Some(Check::new(
CheckKind::LowercaseImportedAsNonLowercase(name.to_string(), asname.to_string()),
Range::from_located(import_from),
@ -143,9 +167,8 @@ pub fn lowercase_imported_as_non_lowercase(
}
fn is_camelcase(name: &str) -> bool {
!name.chars().all(|c| c.is_uppercase()) && !name.chars().all(|c| c.is_lowercase())
!is_lower(name) && !is_upper(name) && !name.contains('_')
}
fn is_acronym(name: &str, asname: &str) -> bool {
name.chars().filter(|c| c.is_uppercase()).join("") == asname
}
@ -155,7 +178,7 @@ pub fn camelcase_imported_as_lowercase(
name: &str,
asname: &str,
) -> Option<Check> {
if is_camelcase(name) && asname.chars().all(|c| c.is_lowercase()) {
if is_camelcase(name) && is_lower(asname) {
return Some(Check::new(
CheckKind::CamelcaseImportedAsLowercase(name.to_string(), asname.to_string()),
Range::from_located(import_from),
@ -169,7 +192,7 @@ pub fn camelcase_imported_as_constant(
name: &str,
asname: &str,
) -> Option<Check> {
if is_camelcase(name) && asname.chars().all(|c| c.is_uppercase()) && !is_acronym(name, asname) {
if is_camelcase(name) && is_upper(asname) && !is_acronym(name, asname) {
return Some(Check::new(
CheckKind::CamelcaseImportedAsConstant(name.to_string(), asname.to_string()),
Range::from_located(import_from),
@ -183,7 +206,7 @@ pub fn camelcase_imported_as_acronym(
name: &str,
asname: &str,
) -> Option<Check> {
if is_camelcase(name) && asname.chars().all(|c| c.is_uppercase()) && is_acronym(name, asname) {
if is_camelcase(name) && is_upper(asname) && is_acronym(name, asname) {
return Some(Check::new(
CheckKind::CamelcaseImportedAsAcronym(name.to_string(), asname.to_string()),
Range::from_located(import_from),
@ -191,3 +214,48 @@ pub fn camelcase_imported_as_acronym(
}
None
}
#[cfg(test)]
mod tests {
use super::{is_acronym, is_camelcase, is_lower, is_upper};
#[test]
fn test_is_lower() -> () {
assert!(is_lower("abc"));
assert!(is_lower("a_b_c"));
assert!(is_lower("a2c"));
assert!(!is_lower("aBc"));
assert!(!is_lower("ABC"));
assert!(!is_lower(""));
assert!(!is_lower("_"));
}
#[test]
fn test_is_upper() -> () {
assert!(is_upper("ABC"));
assert!(is_upper("A_B_C"));
assert!(is_upper("A2C"));
assert!(!is_upper("aBc"));
assert!(!is_upper("abc"));
assert!(!is_upper(""));
assert!(!is_upper("_"));
}
#[test]
fn test_is_camelcase() -> () {
assert!(is_camelcase("Camel"));
assert!(is_camelcase("CamelCase"));
assert!(!is_camelcase("camel"));
assert!(!is_camelcase("camel_case"));
assert!(!is_camelcase("CAMEL"));
assert!(!is_camelcase("CAMEL_CASE"));
}
#[test]
fn test_is_acronym() -> () {
assert!(is_acronym("AB", "AB"));
assert!(is_acronym("AbcDef", "AD"));
assert!(!is_acronym("AbcDef", "Ad"));
assert!(!is_acronym("AbcDef", "AB"));
}
}

View File

@ -4,24 +4,35 @@ expression: checks
---
- kind:
ConstantImportedAsNonConstant:
- SUBMOD
- submod
- CONST
- const
location:
row: 1
column: 1
end_location:
row: 1
column: 28
column: 26
fix: ~
- kind:
ConstantImportedAsNonConstant:
- BAD
- bad
- CONSTANT
- constant
location:
row: 2
column: 1
end_location:
row: 2
column: 27
column: 37
fix: ~
- kind:
ConstantImportedAsNonConstant:
- ANOTHER_CONSTANT
- another_constant
location:
row: 3
column: 1
end_location:
row: 3
column: 53
fix: ~

View File

@ -4,24 +4,35 @@ expression: checks
---
- kind:
LowercaseImportedAsNonLowercase:
- submod
- SubMod
- lowercase
- Lower
location:
row: 1
column: 1
end_location:
row: 1
column: 28
column: 31
fix: ~
- kind:
LowercaseImportedAsNonLowercase:
- bad
- Bad
- lowercase
- Lowercase
location:
row: 2
column: 1
end_location:
row: 2
column: 27
column: 39
fix: ~
- kind:
LowercaseImportedAsNonLowercase:
- another_lowercase
- AnotherLowercase
location:
row: 3
column: 1
end_location:
row: 3
column: 54
fix: ~

View File

@ -4,14 +4,14 @@ expression: checks
---
- kind:
CamelcaseImportedAsLowercase:
- SubMod
- submod
- Camel
- camel
location:
row: 1
column: 1
end_location:
row: 1
column: 28
column: 26
fix: ~
- kind:
CamelcaseImportedAsLowercase:
@ -24,4 +24,15 @@ expression: checks
row: 2
column: 39
fix: ~
- kind:
CamelcaseImportedAsLowercase:
- AnotherCamelCase
- another_camelcase
location:
row: 3
column: 1
end_location:
row: 3
column: 54
fix: ~

View File

@ -4,14 +4,14 @@ expression: checks
---
- kind:
CamelcaseImportedAsConstant:
- SubMod
- SUBMOD
- Camel
- CAMEL
location:
row: 1
column: 1
end_location:
row: 1
column: 28
column: 26
fix: ~
- kind:
CamelcaseImportedAsConstant:
@ -24,4 +24,15 @@ expression: checks
row: 2
column: 39
fix: ~
- kind:
CamelcaseImportedAsConstant:
- AnotherCamelCase
- ANOTHER_CAMELCASE
location:
row: 3
column: 1
end_location:
row: 3
column: 54
fix: ~

View File

@ -4,14 +4,14 @@ expression: checks
---
- kind:
CamelcaseImportedAsAcronym:
- SubMod
- SM
- CaMel
- CM
location:
row: 1
column: 1
end_location:
row: 1
column: 24
column: 23
fix: ~
- kind:
CamelcaseImportedAsAcronym: