@@ -3,28 +3,29 @@ use typomania::{
3
3
Corpus , Package ,
4
4
} ;
5
5
6
- /// A typomania check that checks if commonly used suffixes have been added or removed.
7
- pub struct Suffixes {
6
+ /// A typomania check that checks if commonly used prefixes or suffixes have been added to or
7
+ /// removed from a package name.
8
+ pub struct Affixes {
9
+ affixes : Vec < String > ,
8
10
separators : Vec < String > ,
9
- suffixes : Vec < String > ,
10
11
}
11
12
12
- impl Suffixes {
13
- pub fn new < Sep , Suf > ( separators : Sep , suffixes : Suf ) -> Self
13
+ impl Affixes {
14
+ pub fn new < Aff , Sep > ( affixes : Aff , separators : Sep ) -> Self
14
15
where
16
+ Aff : Iterator ,
17
+ Aff :: Item : ToString ,
15
18
Sep : Iterator ,
16
19
Sep :: Item : ToString ,
17
- Suf : Iterator ,
18
- Suf :: Item : ToString ,
19
20
{
20
21
Self {
22
+ affixes : affixes. map ( |s| s. to_string ( ) ) . collect ( ) ,
21
23
separators : separators. map ( |s| s. to_string ( ) ) . collect ( ) ,
22
- suffixes : suffixes. map ( |s| s. to_string ( ) ) . collect ( ) ,
23
24
}
24
25
}
25
26
}
26
27
27
- impl Check for Suffixes {
28
+ impl Check for Affixes {
28
29
fn check (
29
30
& self ,
30
31
corpus : & dyn Corpus ,
@@ -34,11 +35,32 @@ impl Check for Suffixes {
34
35
let mut squats = Vec :: new ( ) ;
35
36
36
37
for separator in self . separators . iter ( ) {
37
- for suffix in self . suffixes . iter ( ) {
38
- let combo = format ! ( "{separator}{suffix}" ) ;
38
+ for affix in self . affixes . iter ( ) {
39
+ // If the package being examined starts with this prefix and separator combo, then
40
+ // we should see if it exists without that prefix in the popular crate corpus.
41
+ let combo = format ! ( "{affix}{separator}" ) ;
42
+ if let Some ( stem) = name. strip_prefix ( & combo) {
43
+ if corpus. possible_squat ( stem, name, package) ? {
44
+ squats. push ( Squat :: Custom {
45
+ message : format ! ( "adds the {combo} prefix" ) ,
46
+ package : stem. to_string ( ) ,
47
+ } )
48
+ }
49
+ }
50
+
51
+ // Alternatively, let's see if adding the prefix and separator combo to the package
52
+ // results in something popular; eg somebody trying to squat `foo` with `rs-foo`.
53
+ let prefixed = format ! ( "{combo}{name}" ) ;
54
+ if corpus. possible_squat ( & prefixed, name, package) ? {
55
+ squats. push ( Squat :: Custom {
56
+ message : format ! ( "removes the {combo} prefix" ) ,
57
+ package : prefixed,
58
+ } ) ;
59
+ }
39
60
40
61
// If the package being examined ends in this separator and suffix combo, then we
41
- // should see if it exists in the popular crate corpus.
62
+ // should see if it exists without that suffix in the popular crate corpus.
63
+ let combo = format ! ( "{separator}{affix}" ) ;
42
64
if let Some ( stem) = name. strip_suffix ( & combo) {
43
65
if corpus. possible_squat ( stem, name, package) ? {
44
66
squats. push ( Squat :: Custom {
@@ -74,16 +96,17 @@ mod tests {
74
96
use super :: * ;
75
97
76
98
#[ test]
77
- fn test_suffixes ( ) -> anyhow:: Result < ( ) > {
99
+ fn test_affixes ( ) -> anyhow:: Result < ( ) > {
78
100
let popular = TestCorpus :: default ( )
79
101
. with_package ( TestPackage :: new ( "foo" , "foo" , [ "Alice" , "Bob" ] ) )
80
102
. with_package ( TestPackage :: new ( "bar-rs" , "Rust bar" , [ "Charlie" ] ) )
81
- . with_package ( TestPackage :: new ( "quux_sys" , "libquux" , [ "Alice" ] ) ) ;
103
+ . with_package ( TestPackage :: new ( "quux_sys" , "libquux" , [ "Alice" ] ) )
104
+ . with_package ( TestPackage :: new ( "core-xyz" , "Core xyz" , [ "Alice" ] ) ) ;
82
105
83
106
let harness = Harness :: empty_builder ( )
84
- . with_check ( Suffixes :: new (
85
- [ "-" , "_" ] . iter ( ) ,
107
+ . with_check ( Affixes :: new (
86
108
[ "core" , "rs" , "sys" ] . iter ( ) ,
109
+ [ "-" , "_" ] . iter ( ) ,
87
110
) )
88
111
. build ( popular) ;
89
112
@@ -103,8 +126,10 @@ mod tests {
103
126
// Now try some packages that should be.
104
127
for package in [
105
128
TestPackage :: new ( "foo-rs" , "no shared author" , [ "Charlie" ] ) ,
129
+ TestPackage :: new ( "rs-foo" , "no shared author" , [ "Charlie" ] ) ,
106
130
TestPackage :: new ( "quux" , "libquux" , [ "Charlie" ] ) ,
107
131
TestPackage :: new ( "quux_sys_rs" , "libquux... for Rust?" , [ "Charlie" ] ) ,
132
+ TestPackage :: new ( "xyz" , "unprefixed core-xyz" , [ "Charlie" ] ) ,
108
133
]
109
134
. into_iter ( )
110
135
{
0 commit comments