File tree 2 files changed +58
-11
lines changed
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>({
160
160
* @private
161
161
*/
162
162
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 &&
165
173
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 ;
168
180
}
169
181
170
182
return {
@@ -174,13 +186,6 @@ export default util.createRule<Options, MessageIds>({
174
186
return ;
175
187
}
176
188
177
- if (
178
- options . ignoreReadonlyClassProperties &&
179
- isParentTSReadonlyClassProperty ( node )
180
- ) {
181
- return ;
182
- }
183
-
184
189
// Check TypeScript specific nodes for Numeric Literal
185
190
if (
186
191
options . ignoreNumericLiteralTypes &&
@@ -190,6 +195,34 @@ export default util.createRule<Options, MessageIds>({
190
195
return ;
191
196
}
192
197
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
+
193
226
// Let the base rule deal with the rest
194
227
rules . Literal ( node ) ;
195
228
} ,
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ class Foo {
48
48
readonly B = 2;
49
49
public static readonly C = 1;
50
50
static readonly D = 1;
51
+ readonly E = -1;
52
+ readonly F = +1;
51
53
}
52
54
` ,
53
55
options : [ { ignoreReadonlyClassProperties : true } ] ,
@@ -184,6 +186,8 @@ class Foo {
184
186
readonly B = 2;
185
187
public static readonly C = 1;
186
188
static readonly D = 1;
189
+ readonly E = -1;
190
+ readonly F = +1;
187
191
}
188
192
` ,
189
193
options : [ { ignoreReadonlyClassProperties : false } ] ,
@@ -208,6 +212,16 @@ class Foo {
208
212
line : 6 ,
209
213
column : 23 ,
210
214
} ,
215
+ {
216
+ messageId : 'noMagic' ,
217
+ line : 7 ,
218
+ column : 16 ,
219
+ } ,
220
+ {
221
+ messageId : 'noMagic' ,
222
+ line : 8 ,
223
+ column : 16 ,
224
+ } ,
211
225
] ,
212
226
} ,
213
227
] ,
You can’t perform that action at this time.
0 commit comments