@@ -61,7 +61,11 @@ type Options = [
61
61
IgnoreClassesOption &
62
62
IgnoreIdentifierPatternOption & {
63
63
ignoreImmediateMutation : boolean ;
64
- ignoreNonConstDeclarations : boolean ;
64
+ ignoreNonConstDeclarations :
65
+ | boolean
66
+ | {
67
+ treatParametersAsConst : boolean ;
68
+ } ;
65
69
} ,
66
70
] ;
67
71
@@ -80,7 +84,20 @@ const schema: JSONSchema4[] = [
80
84
type : "boolean" ,
81
85
} ,
82
86
ignoreNonConstDeclarations : {
83
- type : "boolean" ,
87
+ oneOf : [
88
+ {
89
+ type : "boolean" ,
90
+ } ,
91
+ {
92
+ type : "object" ,
93
+ properties : {
94
+ treatParametersAsConst : {
95
+ type : "boolean" ,
96
+ } ,
97
+ } ,
98
+ additionalProperties : false ,
99
+ } ,
100
+ ] ,
84
101
} ,
85
102
} satisfies JSONSchema4ObjectSchema [ "properties" ] ,
86
103
) ,
@@ -230,11 +247,16 @@ function checkAssignmentExpression(
230
247
} ;
231
248
}
232
249
233
- if ( ignoreNonConstDeclarations ) {
250
+ if ( ignoreNonConstDeclarations !== false ) {
234
251
const rootIdentifier = findRootIdentifier ( node . left . object ) ;
235
252
if (
236
253
rootIdentifier !== undefined &&
237
- isDefinedByMutableVariable ( rootIdentifier , context )
254
+ isDefinedByMutableVariable (
255
+ rootIdentifier ,
256
+ context ,
257
+ ignoreNonConstDeclarations === true ||
258
+ ! ignoreNonConstDeclarations . treatParametersAsConst ,
259
+ )
238
260
) {
239
261
return {
240
262
context,
@@ -283,11 +305,16 @@ function checkUnaryExpression(
283
305
} ;
284
306
}
285
307
286
- if ( ignoreNonConstDeclarations ) {
308
+ if ( ignoreNonConstDeclarations !== false ) {
287
309
const rootIdentifier = findRootIdentifier ( node . argument . object ) ;
288
310
if (
289
311
rootIdentifier !== undefined &&
290
- isDefinedByMutableVariable ( rootIdentifier , context )
312
+ isDefinedByMutableVariable (
313
+ rootIdentifier ,
314
+ context ,
315
+ ignoreNonConstDeclarations === true ||
316
+ ! ignoreNonConstDeclarations . treatParametersAsConst ,
317
+ )
291
318
) {
292
319
return {
293
320
context,
@@ -335,11 +362,16 @@ function checkUpdateExpression(
335
362
} ;
336
363
}
337
364
338
- if ( ignoreNonConstDeclarations ) {
365
+ if ( ignoreNonConstDeclarations !== false ) {
339
366
const rootIdentifier = findRootIdentifier ( node . argument . object ) ;
340
367
if (
341
368
rootIdentifier !== undefined &&
342
- isDefinedByMutableVariable ( rootIdentifier , context )
369
+ isDefinedByMutableVariable (
370
+ rootIdentifier ,
371
+ context ,
372
+ ignoreNonConstDeclarations === true ||
373
+ ! ignoreNonConstDeclarations . treatParametersAsConst ,
374
+ )
343
375
) {
344
376
return {
345
377
context,
@@ -473,18 +505,22 @@ function checkCallExpression(
473
505
! isInChainCallAndFollowsNew ( node . callee , context ) ) &&
474
506
isArrayType ( getTypeOfNode ( node . callee . object , context ) )
475
507
) {
476
- if ( ignoreNonConstDeclarations ) {
477
- const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
478
- if (
479
- rootIdentifier === undefined ||
480
- ! isDefinedByMutableVariable ( rootIdentifier , context )
481
- ) {
482
- return {
483
- context,
484
- descriptors : [ { node, messageId : "array" } ] ,
485
- } ;
486
- }
487
- } else {
508
+ if ( ignoreNonConstDeclarations === false ) {
509
+ return {
510
+ context,
511
+ descriptors : [ { node, messageId : "array" } ] ,
512
+ } ;
513
+ }
514
+ const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
515
+ if (
516
+ rootIdentifier === undefined ||
517
+ ! isDefinedByMutableVariable (
518
+ rootIdentifier ,
519
+ context ,
520
+ ignoreNonConstDeclarations === true ||
521
+ ! ignoreNonConstDeclarations . treatParametersAsConst ,
522
+ )
523
+ ) {
488
524
return {
489
525
context,
490
526
descriptors : [ { node, messageId : "array" } ] ,
@@ -507,18 +543,22 @@ function checkCallExpression(
507
543
) &&
508
544
isObjectConstructorType ( getTypeOfNode ( node . callee . object , context ) )
509
545
) {
510
- if ( ignoreNonConstDeclarations ) {
511
- const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
512
- if (
513
- rootIdentifier === undefined ||
514
- ! isDefinedByMutableVariable ( rootIdentifier , context )
515
- ) {
516
- return {
517
- context,
518
- descriptors : [ { node, messageId : "object" } ] ,
519
- } ;
520
- }
521
- } else {
546
+ if ( ignoreNonConstDeclarations === false ) {
547
+ return {
548
+ context,
549
+ descriptors : [ { node, messageId : "object" } ] ,
550
+ } ;
551
+ }
552
+ const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
553
+ if (
554
+ rootIdentifier === undefined ||
555
+ ! isDefinedByMutableVariable (
556
+ rootIdentifier ,
557
+ context ,
558
+ ignoreNonConstDeclarations === true ||
559
+ ! ignoreNonConstDeclarations . treatParametersAsConst ,
560
+ )
561
+ ) {
522
562
return {
523
563
context,
524
564
descriptors : [ { node, messageId : "object" } ] ,
0 commit comments