1
1
import { expect , it } from 'vitest'
2
2
3
- import { compareMessages , joinFromRoot , parseIntoFolder as parseIntoFsdRoot } from '@steiger/toolkit'
3
+ import { joinFromRoot , parseIntoFolder as parseIntoFsdRoot } from '@steiger/toolkit'
4
4
import inconsistentNaming from './index.js'
5
5
6
- it ( 'reports no errors on slice names that are pluralized consistently' , ( ) => {
7
- const root = parseIntoFsdRoot (
6
+ it ( 'reports no errors on entity names that are pluralized consistently' , ( ) => {
7
+ const root1 = parseIntoFsdRoot (
8
8
`
9
9
📂 entities
10
10
📂 users
@@ -16,11 +16,41 @@ it('reports no errors on slice names that are pluralized consistently', () => {
16
16
` ,
17
17
joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
18
18
)
19
+ const root2 = parseIntoFsdRoot (
20
+ `
21
+ 📂 entities
22
+ 📂 user
23
+ 📂 ui
24
+ 📄 index.ts
25
+ 📂 post
26
+ 📂 ui
27
+ 📄 index.ts
28
+ ` ,
29
+ joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
30
+ )
31
+
32
+ expect ( inconsistentNaming . check ( root1 ) ) . toEqual ( { diagnostics : [ ] } )
33
+ expect ( inconsistentNaming . check ( root2 ) ) . toEqual ( { diagnostics : [ ] } )
34
+ } )
35
+
36
+ it ( 'reports no errors on multi-word entity names that are pluralized consistently' , ( ) => {
37
+ const root = parseIntoFsdRoot (
38
+ `
39
+ 📂 entities
40
+ 📂 admin-users
41
+ 📂 ui
42
+ 📄 index.ts
43
+ 📂 employers-of-record
44
+ 📂 ui
45
+ 📄 index.ts
46
+ ` ,
47
+ joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
48
+ )
19
49
20
50
expect ( inconsistentNaming . check ( root ) ) . toEqual ( { diagnostics : [ ] } )
21
51
} )
22
52
23
- it ( 'reports an error on slice names that are not pluralized consistently' , ( ) => {
53
+ it ( 'reports an error on entity names that are not pluralized consistently' , ( ) => {
24
54
const root = parseIntoFsdRoot (
25
55
`
26
56
📂 entities
@@ -34,15 +64,15 @@ it('reports an error on slice names that are not pluralized consistently', () =>
34
64
joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
35
65
)
36
66
37
- const diagnostics = inconsistentNaming . check ( root ) . diagnostics . sort ( compareMessages )
67
+ const diagnostics = inconsistentNaming . check ( root ) . diagnostics
38
68
expect ( diagnostics ) . toEqual ( [
39
69
{
40
- message : 'Inconsistent pluralization of slice names. Prefer all plural names' ,
70
+ message : 'Inconsistent pluralization of entity names. Prefer all singular names. ' ,
41
71
fixes : [
42
72
{
43
73
type : 'rename' ,
44
- path : joinFromRoot ( 'users' , 'user' , 'project' , 'src' , 'entities' , 'user ' ) ,
45
- newName : 'users ' ,
74
+ path : joinFromRoot ( 'users' , 'user' , 'project' , 'src' , 'entities' , 'posts ' ) ,
75
+ newName : 'post ' ,
46
76
} ,
47
77
] ,
48
78
location : { path : joinFromRoot ( 'users' , 'user' , 'project' , 'src' , 'entities' ) } ,
@@ -54,10 +84,10 @@ it('prefers the singular form when there are more singular slices', () => {
54
84
const root = parseIntoFsdRoot (
55
85
`
56
86
📂 entities
57
- 📂 user
87
+ 📂 admin- user
58
88
📂 ui
59
89
📄 index.ts
60
- 📂 post
90
+ 📂 news- post
61
91
📂 ui
62
92
📄 index.ts
63
93
📂 comments
@@ -67,10 +97,10 @@ it('prefers the singular form when there are more singular slices', () => {
67
97
joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
68
98
)
69
99
70
- const diagnostics = inconsistentNaming . check ( root ) . diagnostics . sort ( compareMessages )
100
+ const diagnostics = inconsistentNaming . check ( root ) . diagnostics
71
101
expect ( diagnostics ) . toEqual ( [
72
102
{
73
- message : 'Inconsistent pluralization of slice names. Prefer all singular names' ,
103
+ message : 'Inconsistent pluralization of entity names. Prefer all singular names. ' ,
74
104
fixes : [
75
105
{
76
106
type : 'rename' ,
@@ -82,3 +112,48 @@ it('prefers the singular form when there are more singular slices', () => {
82
112
} ,
83
113
] )
84
114
} )
115
+
116
+ it ( 'recognizes the special case when there is a plural and singular form of the same name' , ( ) => {
117
+ const root = parseIntoFsdRoot (
118
+ `
119
+ 📂 entities
120
+ 📂 admin-user
121
+ 📂 ui
122
+ 📄 index.ts
123
+ 📂 admin-users
124
+ 📂 ui
125
+ 📄 index.ts
126
+ ` ,
127
+ joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
128
+ )
129
+
130
+ const diagnostics = inconsistentNaming . check ( root ) . diagnostics
131
+ expect ( diagnostics ) . toEqual ( [
132
+ {
133
+ message : 'Avoid having both "admin-user" and "admin-users" entities.' ,
134
+ location : { path : joinFromRoot ( 'users' , 'user' , 'project' , 'src' , 'entities' , 'admin-user' ) } ,
135
+ } ,
136
+ ] )
137
+ } )
138
+
139
+ it ( 'allows inconsistency between different slice groups' , ( ) => {
140
+ const root = parseIntoFsdRoot (
141
+ `
142
+ 📂 entities
143
+ 📂 admin-user
144
+ 📂 ui
145
+ 📄 index.ts
146
+ 📂 group
147
+ 📄 index.ts
148
+ 📂 post-parts
149
+ 📂 posts
150
+ 📄 index.ts
151
+ 📂 authors
152
+ 📄 index.ts
153
+ ` ,
154
+ joinFromRoot ( 'users' , 'user' , 'project' , 'src' ) ,
155
+ )
156
+
157
+ const diagnostics = inconsistentNaming . check ( root ) . diagnostics
158
+ expect ( diagnostics ) . toEqual ( [ ] )
159
+ } )
0 commit comments