@@ -25,6 +25,27 @@ function typeViolates(leftTypeParts: ts.Type[], right: ts.Type): boolean {
25
25
) ;
26
26
}
27
27
28
+ function getEnumKeyForLiteral (
29
+ leftEnumTypes : ts . Type [ ] ,
30
+ rightNode : TSESTree . Node ,
31
+ ) : string | null {
32
+ const right = util . getStaticValue ( rightNode ) ;
33
+
34
+ if ( right === null ) {
35
+ return null ;
36
+ }
37
+
38
+ for ( const leftEnumType of leftEnumTypes ) {
39
+ if ( leftEnumType . value === right . value ) {
40
+ const enumParentName = leftEnumType . symbol . parent . name ;
41
+
42
+ return `${ enumParentName } .${ leftEnumType . symbol . name } ` ;
43
+ }
44
+ }
45
+
46
+ return null ;
47
+ }
48
+
28
49
/**
29
50
* @returns What type a type's enum value is (number or string), if either.
30
51
*/
@@ -48,6 +69,8 @@ export default util.createRule({
48
69
messages : {
49
70
mismatched :
50
71
'The two values in this comparison do not have a shared enum type.' ,
72
+ mismatchedSimilar :
73
+ 'The two values in this comparison do not have a shared enum type. Did you mean to compare to `{{replacement}}`?' ,
51
74
} ,
52
75
schema : [ ] ,
53
76
} ,
@@ -100,11 +123,44 @@ export default util.createRule({
100
123
}
101
124
}
102
125
103
- if (
104
- typeViolates ( leftTypeParts , right ) ||
105
- typeViolates ( rightTypeParts , left )
106
- ) {
107
- context . report ( {
126
+ if ( typeViolates ( leftTypeParts , right ) ) {
127
+ const leftEnumKey = getEnumKeyForLiteral ( leftEnumTypes , node . right ) ;
128
+
129
+ if ( leftEnumKey ) {
130
+ // TODO: Add fixer.
131
+ return context . report ( {
132
+ messageId : 'mismatchedSimilar' ,
133
+ node,
134
+ data : {
135
+ replacement : leftEnumKey ,
136
+ } ,
137
+ } ) ;
138
+ }
139
+
140
+ return context . report ( {
141
+ messageId : 'mismatched' ,
142
+ node,
143
+ } ) ;
144
+ }
145
+
146
+ if ( typeViolates ( rightTypeParts , left ) ) {
147
+ const rightEnumKey = getEnumKeyForLiteral (
148
+ [ ...rightEnumTypes . values ( ) ] ,
149
+ node . left ,
150
+ ) ;
151
+
152
+ if ( rightEnumKey ) {
153
+ // TODO: Add fixer.
154
+ return context . report ( {
155
+ messageId : 'mismatchedSimilar' ,
156
+ node,
157
+ data : {
158
+ replacement : rightEnumKey ,
159
+ } ,
160
+ } ) ;
161
+ }
162
+
163
+ return context . report ( {
108
164
messageId : 'mismatched' ,
109
165
node,
110
166
} ) ;
0 commit comments