File tree Expand file tree Collapse file tree 2 files changed +58
-11
lines changed
Expand file tree Collapse file tree 2 files changed +58
-11
lines changed Original file line number Diff line number Diff line change @@ -160,11 +160,23 @@ export default util.createRule<Options, MessageIds>({
160160 * @private
161161 */
162162 function isParentTSReadonlyClassProperty ( node : TSESTree . Node ) : boolean {
163- return (
164- ! ! node . parent &&
163+ if (
164+ node . parent &&
165+ node . parent . type === AST_NODE_TYPES . UnaryExpression &&
166+ [ '-' , '+' ] . includes ( node . parent . operator )
167+ ) {
168+ node = node . parent ;
169+ }
170+
171+ if (
172+ node . parent &&
165173 node . parent . type === AST_NODE_TYPES . ClassProperty &&
166- ! ! node . parent . readonly
167- ) ;
174+ node . parent . readonly
175+ ) {
176+ return true ;
177+ }
178+
179+ return false ;
168180 }
169181
170182 return {
@@ -174,13 +186,6 @@ export default util.createRule<Options, MessageIds>({
174186 return ;
175187 }
176188
177- if (
178- options . ignoreReadonlyClassProperties &&
179- isParentTSReadonlyClassProperty ( node )
180- ) {
181- return ;
182- }
183-
184189 // Check TypeScript specific nodes for Numeric Literal
185190 if (
186191 options . ignoreNumericLiteralTypes &&
@@ -190,6 +195,34 @@ export default util.createRule<Options, MessageIds>({
190195 return ;
191196 }
192197
198+ // Check if the node is a readonly class property
199+ if ( isNumber ( node ) && isParentTSReadonlyClassProperty ( node ) ) {
200+ if ( options . ignoreReadonlyClassProperties ) {
201+ return ;
202+ }
203+
204+ let fullNumberNode :
205+ | TSESTree . Literal
206+ | TSESTree . UnaryExpression = node ;
207+ let raw = node . raw ;
208+
209+ if (
210+ node . parent &&
211+ node . parent . type === AST_NODE_TYPES . UnaryExpression
212+ ) {
213+ fullNumberNode = node . parent ;
214+ raw = `${ node . parent . operator } ${ node . raw } ` ;
215+ }
216+
217+ context . report ( {
218+ messageId : 'noMagic' ,
219+ node : fullNumberNode ,
220+ data : { raw } ,
221+ } ) ;
222+
223+ return ;
224+ }
225+
193226 // Let the base rule deal with the rest
194227 rules . Literal ( node ) ;
195228 } ,
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ class Foo {
4848 readonly B = 2;
4949 public static readonly C = 1;
5050 static readonly D = 1;
51+ readonly E = -1;
52+ readonly F = +1;
5153}
5254 ` ,
5355 options : [ { ignoreReadonlyClassProperties : true } ] ,
@@ -184,6 +186,8 @@ class Foo {
184186 readonly B = 2;
185187 public static readonly C = 1;
186188 static readonly D = 1;
189+ readonly E = -1;
190+ readonly F = +1;
187191}
188192 ` ,
189193 options : [ { ignoreReadonlyClassProperties : false } ] ,
@@ -208,6 +212,16 @@ class Foo {
208212 line : 6 ,
209213 column : 23 ,
210214 } ,
215+ {
216+ messageId : 'noMagic' ,
217+ line : 7 ,
218+ column : 16 ,
219+ } ,
220+ {
221+ messageId : 'noMagic' ,
222+ line : 8 ,
223+ column : 16 ,
224+ } ,
211225 ] ,
212226 } ,
213227 ] ,
You can’t perform that action at this time.
0 commit comments