@@ -17,27 +17,6 @@ const rule = require("../../../lib/rules/no-unsafe-negation"),
1717//------------------------------------------------------------------------------
1818
1919const ruleTester = new RuleTester ( ) ;
20- const unexpectedInError = { messageId : "unexpected" , data : { operator : "in" } } ;
21- const unexpectedInstanceofError = {
22- messageId : "unexpected" ,
23- data : { operator : "instanceof" }
24- } ;
25- const unexpectedLessThanOperatorError = {
26- messageId : "unexpected" ,
27- data : { operator : "<" }
28- } ;
29- const unexpectedMoreThanOperatorError = {
30- messageId : "unexpected" ,
31- data : { operator : ">" }
32- } ;
33- const unexpectedMoreThanOrEqualOperatorError = {
34- messageId : "unexpected" ,
35- data : { operator : ">=" }
36- } ;
37- const unexpectedLessThanOrEqualOperatorError = {
38- messageId : "unexpected" ,
39- data : { operator : "<=" }
40- } ;
4120
4221ruleTester . run ( "no-unsafe-negation" , rule , {
4322 valid : [
@@ -83,52 +62,194 @@ ruleTester.run("no-unsafe-negation", rule, {
8362 invalid : [
8463 {
8564 code : "!a in b" ,
86- errors : [ unexpectedInError ]
65+ errors : [ {
66+ message : "Unexpected negating the left operand of 'in' operator." ,
67+ suggestions : [
68+ {
69+ desc : "Negate 'in' expression instead of its left operand. This changes the current behavior." ,
70+ output : "!(a in b)"
71+ } ,
72+ {
73+ desc : "Wrap negation in '()' to make the intention explicit. This preserves the current behavior." ,
74+ output : "(!a) in b"
75+ }
76+ ]
77+ } ]
8778 } ,
8879 {
8980 code : "(!a in b)" ,
90- errors : [ unexpectedInError ]
81+ errors : [ {
82+ messageId : "unexpected" ,
83+ data : { operator : "in" } ,
84+ suggestions : [
85+ {
86+ messageId : "suggestNegatedExpression" ,
87+ output : "(!(a in b))"
88+ } ,
89+ {
90+ messageId : "suggestParenthesisedNegation" ,
91+ output : "((!a) in b)"
92+ }
93+ ]
94+ } ]
9195 } ,
9296 {
9397 code : "!(a) in b" ,
94- errors : [ unexpectedInError ]
98+ errors : [ {
99+ messageId : "unexpected" ,
100+ data : { operator : "in" } ,
101+ suggestions : [
102+ {
103+ messageId : "suggestNegatedExpression" ,
104+ output : "!((a) in b)"
105+ } ,
106+ {
107+ messageId : "suggestParenthesisedNegation" ,
108+ output : "(!(a)) in b"
109+ }
110+ ]
111+ } ]
95112 } ,
96113 {
97114 code : "!a instanceof b" ,
98- errors : [ unexpectedInstanceofError ]
115+ errors : [ {
116+ messageId : "unexpected" ,
117+ data : { operator : "instanceof" } ,
118+ suggestions : [
119+ {
120+ messageId : "suggestNegatedExpression" ,
121+ output : "!(a instanceof b)"
122+ } ,
123+ {
124+ messageId : "suggestParenthesisedNegation" ,
125+ output : "(!a) instanceof b"
126+ }
127+ ]
128+ } ]
99129 } ,
100130 {
101131 code : "(!a instanceof b)" ,
102- errors : [ unexpectedInstanceofError ]
132+ errors : [ {
133+ messageId : "unexpected" ,
134+ data : { operator : "instanceof" } ,
135+ suggestions : [
136+ {
137+ messageId : "suggestNegatedExpression" ,
138+ output : "(!(a instanceof b))"
139+ } ,
140+ {
141+ messageId : "suggestParenthesisedNegation" ,
142+ output : "((!a) instanceof b)"
143+ }
144+ ]
145+ } ]
103146 } ,
104147 {
105148 code : "!(a) instanceof b" ,
106- errors : [ unexpectedInstanceofError ]
149+ errors : [ {
150+ messageId : "unexpected" ,
151+ data : { operator : "instanceof" } ,
152+ suggestions : [
153+ {
154+ messageId : "suggestNegatedExpression" ,
155+ output : "!((a) instanceof b)"
156+ } ,
157+ {
158+ messageId : "suggestParenthesisedNegation" ,
159+ output : "(!(a)) instanceof b"
160+ }
161+ ]
162+ } ]
107163 } ,
108164 {
109165 code : "if (! a < b) {}" ,
110166 options : [ { enforceForOrderingRelations : true } ] ,
111- errors : [ unexpectedLessThanOperatorError ]
167+ errors : [ {
168+ messageId : "unexpected" ,
169+ data : { operator : "<" } ,
170+ suggestions : [
171+ {
172+ messageId : "suggestNegatedExpression" ,
173+ output : "if (!( a < b)) {}"
174+ } ,
175+ {
176+ messageId : "suggestParenthesisedNegation" ,
177+ output : "if ((! a) < b) {}"
178+ }
179+ ]
180+ } ]
112181 } ,
113182 {
114183 code : "while (! a > b) {}" ,
115184 options : [ { enforceForOrderingRelations : true } ] ,
116- errors : [ unexpectedMoreThanOperatorError ]
185+ errors : [ {
186+ messageId : "unexpected" ,
187+ data : { operator : ">" } ,
188+ suggestions : [
189+ {
190+ messageId : "suggestNegatedExpression" ,
191+ output : "while (!( a > b)) {}"
192+ } ,
193+ {
194+ messageId : "suggestParenthesisedNegation" ,
195+ output : "while ((! a) > b) {}"
196+ }
197+ ]
198+ } ]
117199 } ,
118200 {
119201 code : "foo = ! a <= b;" ,
120202 options : [ { enforceForOrderingRelations : true } ] ,
121- errors : [ unexpectedLessThanOrEqualOperatorError ]
203+ errors : [ {
204+ messageId : "unexpected" ,
205+ data : { operator : "<=" } ,
206+ suggestions : [
207+ {
208+ messageId : "suggestNegatedExpression" ,
209+ output : "foo = !( a <= b);"
210+ } ,
211+ {
212+ messageId : "suggestParenthesisedNegation" ,
213+ output : "foo = (! a) <= b;"
214+ }
215+ ]
216+ } ]
122217 } ,
123218 {
124219 code : "foo = ! a >= b;" ,
125220 options : [ { enforceForOrderingRelations : true } ] ,
126- errors : [ unexpectedMoreThanOrEqualOperatorError ]
221+ errors : [ {
222+ messageId : "unexpected" ,
223+ data : { operator : ">=" } ,
224+ suggestions : [
225+ {
226+ messageId : "suggestNegatedExpression" ,
227+ output : "foo = !( a >= b);"
228+ } ,
229+ {
230+ messageId : "suggestParenthesisedNegation" ,
231+ output : "foo = (! a) >= b;"
232+ }
233+ ]
234+ } ]
127235 } ,
128236 {
129237 code : "! a <= b" ,
130238 options : [ { enforceForOrderingRelations : true } ] ,
131- errors : [ unexpectedLessThanOrEqualOperatorError ]
239+ errors : [ {
240+ messageId : "unexpected" ,
241+ data : { operator : "<=" } ,
242+ suggestions : [
243+ {
244+ messageId : "suggestNegatedExpression" ,
245+ output : "!( a <= b)"
246+ } ,
247+ {
248+ messageId : "suggestParenthesisedNegation" ,
249+ output : "(! a) <= b"
250+ }
251+ ]
252+ } ]
132253 }
133254 ]
134255} ) ;
0 commit comments