File tree Expand file tree Collapse file tree 5 files changed +346
-8
lines changed Expand file tree Collapse file tree 5 files changed +346
-8
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ const astUtils = require("./utils/ast-utils");
19
19
module . exports = {
20
20
meta : {
21
21
type : "suggestion" ,
22
+ hasSuggestions : true ,
22
23
23
24
docs : {
24
25
description : "Require the use of `===` and `!==`" ,
@@ -63,6 +64,8 @@ module.exports = {
63
64
messages : {
64
65
unexpected :
65
66
"Expected '{{expectedOperator}}' and instead saw '{{actualOperator}}'." ,
67
+ replaceOperator :
68
+ "Use '{{expectedOperator}}' instead of '{{actualOperator}}'." ,
66
69
} ,
67
70
} ,
68
71
@@ -138,22 +141,42 @@ module.exports = {
138
141
token => token . value === node . operator ,
139
142
) ;
140
143
141
- context . report ( {
144
+ const commonReportParams = {
142
145
node,
143
146
loc : operatorToken . loc ,
144
147
messageId : "unexpected" ,
145
148
data : { expectedOperator, actualOperator : node . operator } ,
146
- fix ( fixer ) {
147
- // If the comparison is a `typeof` comparison or both sides are literals with the same type, then it's safe to fix.
148
- if ( isTypeOfBinary ( node ) || areLiteralsAndSameType ( node ) ) {
149
+ } ;
150
+
151
+ if ( isTypeOfBinary ( node ) || areLiteralsAndSameType ( node ) ) {
152
+ context . report ( {
153
+ ...commonReportParams ,
154
+ fix ( fixer ) {
149
155
return fixer . replaceText (
150
156
operatorToken ,
151
157
expectedOperator ,
152
158
) ;
153
- }
154
- return null ;
155
- } ,
156
- } ) ;
159
+ } ,
160
+ } ) ;
161
+ } else {
162
+ context . report ( {
163
+ ...commonReportParams ,
164
+ suggest : [
165
+ {
166
+ messageId : "replaceOperator" ,
167
+ data : {
168
+ expectedOperator,
169
+ actualOperator : node . operator ,
170
+ } ,
171
+ fix : fixer =>
172
+ fixer . replaceText (
173
+ operatorToken ,
174
+ expectedOperator ,
175
+ ) ,
176
+ } ,
177
+ ] ,
178
+ } ) ;
179
+ }
157
180
}
158
181
159
182
return {
Original file line number Diff line number Diff line change @@ -2103,6 +2103,20 @@ describe("CLIEngine", () => {
2103
2103
nodeType : "BinaryExpression" ,
2104
2104
ruleId : "eqeqeq" ,
2105
2105
severity : 2 ,
2106
+ suggestions : [
2107
+ {
2108
+ data : {
2109
+ actualOperator : "==" ,
2110
+ expectedOperator : "===" ,
2111
+ }
1E0A
,
2112
+ desc : "Use '===' instead of '=='." ,
2113
+ fix : {
2114
+ range : [ 24 , 26 ] ,
2115
+ text : "===" ,
2116
+ } ,
2117
+ messageId : "replaceOperator" ,
2118
+ } ,
2119
+ ] ,
2106
2120
} ,
2107
2121
] ,
2108
2122
suppressedMessages : [ ] ,
@@ -7814,6 +7828,20 @@ describe("CLIEngine", () => {
7814
7828
messageId : "unexpected" ,
7815
7829
nodeType : "BinaryExpression" ,
7816
7830
ruleId : "eqeqeq" ,
7831
+ suggestions : [
7832
+ {
7833
+ data : {
7834
+ actualOperator : "==" ,
7835
+ expectedOperator : "===" ,
7836
+ } ,
7837
+ desc : "Use '===' instead of '=='." ,
7838
+ fix : {
7839
+ range : [ 2 , 4 ] ,
7840
+ text : "===" ,
7841
+ } ,
7842
+ messageId : "replaceOperator" ,
7843
+ } ,
7844
+ ] ,
7817
7845
severity : 2 ,
7818
7846
} ,
7819
7847
] ,
@@ -7934,6 +7962,20 @@ describe("CLIEngine", () => {
7934
7962
nodeType : "BinaryExpression" ,
7935
7963
ruleId : "eqeqeq" ,
7936
7964
severity : 2 ,
7965
+ suggestions : [
7966
+ {
7967
+ data : {
7968
+ actualOperator : "==" ,
7969
+ expectedOperator : "===" ,
7970
+ } ,
7971
+ desc : "Use '===' instead of '=='." ,
7972
+ fix : {
7973
+ range : [ 2 , 4 ] ,
7974
+ text : "===" ,
7975
+ } ,
7976
+ messageId : "replaceOperator" ,
7977
+ } ,
7978
+ ] ,
7937
7979
} ,
7938
7980
] ,
7939
7981
suppressedMessages : [ ] ,
Original file line number Diff line number Diff line change @@ -4437,6 +4437,20 @@ describe("ESLint", () => {
4437
4437
nodeType : "BinaryExpression" ,
4438
4438
ruleId : "eqeqeq" ,
4439
4439
severity : 2 ,
4440
+ suggestions : [
4441
+ {
4442
+ data : {
4443
+ actualOperator : "==" ,
4444
+ expectedOperator : "===" ,
4445
+ } ,
4446
+ desc : "Use '===' instead of '=='." ,
4447
+ fix : {
4448
+ range : [ 24 , 26 ] ,
4449
+ text : "===" ,
4450
+ } ,
4451
+ messageId : "replaceOperator" ,
4452
+ } ,
4453
+ ] ,
4440
4454
} ,
4441
4455
] ,
4442
4456
suppressedMessages : [ ] ,
@@ -11350,6 +11364,20 @@ describe("ESLint", () => {
11350
11364
nodeType : "BinaryExpression" ,
11351
11365
ruleId : "eqeqeq" ,
11352
11366
severity : 2 ,
11367
+ suggestions : [
11368
+ {
11369
+ data : {
11370
+ actualOperator : "==" ,
11371
+ expectedOperator : "===" ,
11372
+ } ,
11373
+ desc : "Use '===' instead of '=='." ,
11374
+ fix : {
11375
+ range : [ 2 , 4 ] ,
11376
+ text : "===" ,
11377
+ } ,
11378
+ messageId : "replaceOperator" ,
11379
+ } ,
11380
+ ] ,
11353
11381
} ,
11354
11382
] ,
11355
11383
source : "a == b" ,
@@ -11481,6 +11509,20 @@ describe("ESLint", () => {
11481
11509
nodeType : "BinaryExpression" ,
11482
11510
ruleId : "eqeqeq" ,
11483
11511
severity : 2 ,
11512
+ suggestions : [
11513
+ {
11514
+ data : {
11515
+ actualOperator : "==" ,
11516
+ expectedOperator : "===" ,
11517
+ } ,
11518
+ desc : "Use '===' instead of '=='." ,
11519
+ fix : {
11520
+ range : [ 2 , 4 ] ,
11521
+ text : "===" ,
11522
+ } ,
11523
+ messageId : "replaceOperator" ,
11524
+ } ,
11525
+ ] ,
11484
11526
} ,
11485
11527
] ,
11486
11528
source : "a == b" ,
Original file line number Diff line number Diff line change @@ -2262,6 +2262,20 @@ describe("LegacyESLint", () => {
2262
2262
nodeType : "BinaryExpression" ,
2263
2263
ruleId : "eqeqeq" ,
2264
2264
severity : 2 ,
2265
+ suggestions : [
2266
+ {
2267
+ data : {
2268
+ actualOperator : "==" ,
2269
+ expectedOperator : "===" ,
2270
+ } ,
2271
+ desc : "Use '===' instead of '=='." ,
2272
+ fix : {
2273
+ range : [ 24 , 26 ] ,
2274
+ text : "===" ,
2275
+ } ,
2276
+ messageId : "replaceOperator" ,
2277
+ } ,
2278
+ ] ,
2265
2279
} ,
2266
2280
] ,
2267
2281
suppressedMessages : [ ] ,
@@ -8759,6 +8773,20 @@ describe("LegacyESLint", () => {
8759
8773
nodeType : "BinaryExpression" ,
8760
8774
ruleId : "eqeqeq" ,
8761
8775
severity : 2 ,
8776
+ suggestions : [
8777
+ {
8778
+ data : {
8779
+ actualOperator : "==" ,
8780
+ expectedOperator : "===" ,
8781
+ } ,
8782
+ desc : "Use '===' instead of '=='." ,
8783
+ fix : {
8784
+ range : [ 2 , 4 ] ,
8785
+ text : "===" ,
8786
+ } ,
8787
+ messageId : "replaceOperator" ,
8788
+ } ,
8789
+ ] ,
8762
8790
} ,
8763
8791
] ,
8764
8792
suppressedMessages : [ ] ,
@@ -8881,6 +8909,20 @@ describe("LegacyESLint", () => {
8881
8909
nodeType : "BinaryExpression" ,
8882
8910
ruleId : "eqeqeq" ,
8883
8911
severity : 2 ,
8912
+ suggestions : [
8913
+ {
8914
+ data : {
8915
+ actualOperator : "==" ,
8916
+ expectedOperator : "===" ,
8917
+ } ,
8918
+ desc : "Use '===' instead of '=='." ,
8919
+ fix : {
8920
+ range : [ 2 , 4 ] ,
8921
+ text : "===" ,
8922
+ } ,
8923
+ messageId : "replaceOperator" ,
8924
+ } ,
8925
+ ] ,
8884
8926
} ,
8885
8927
] ,
8886
8928
suppressedMessages : [ ] ,
You can’t perform that action at this time.
0 commit comments