@@ -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,23 @@ 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
+ ( variableNode ) =>
258
+ ignoreNonConstDeclarations === true ||
259
+ ! ignoreNonConstDeclarations . treatParametersAsConst ||
260
+ shouldIgnorePattern (
261
+ variableNode ,
262
+ context ,
263
+ ignoreIdentifierPattern ,
264
+ ignoreAccessorPattern ,
265
+ ) ,
266
+ )
238
267
) {
239
268
return {
240
269
context,
@@ -283,11 +312,23 @@ function checkUnaryExpression(
283
312
} ;
284
313
}
285
314
286
- if ( ignoreNonConstDeclarations ) {
315
+ if ( ignoreNonConstDeclarations !== false ) {
287
316
const rootIdentifier = findRootIdentifier ( node . argument . object ) ;
288
317
if (
289
318
rootIdentifier !== undefined &&
290
- isDefinedByMutableVariable ( rootIdentifier , context )
319
+ isDefinedByMutableVariable (
320
+ rootIdentifier ,
321
+ context ,
322
+ ( variableNode ) =>
323
+ ignoreNonConstDeclarations === true ||
324
+ ! ignoreNonConstDeclarations . treatParametersAsConst ||
325
+ shouldIgnorePattern (
326
+ variableNode ,
327
+ context ,
328
+ ignoreIdentifierPattern ,
329
+ ignoreAccessorPattern ,
330
+ ) ,
331
+ )
291
332
) {
292
333
return {
293
334
context,
@@ -335,11 +376,23 @@ function checkUpdateExpression(
335
376
} ;
336
377
}
337
378
338
- if ( ignoreNonConstDeclarations ) {
379
+ if ( ignoreNonConstDeclarations !== false ) {
339
380
const rootIdentifier = findRootIdentifier ( node . argument . object ) ;
340
381
if (
341
382
rootIdentifier !== undefined &&
342
- isDefinedByMutableVariable ( rootIdentifier , context )
383
+ isDefinedByMutableVariable (
384
+ rootIdentifier ,
385
+ context ,
386
+ ( variableNode ) =>
387
+ ignoreNonConstDeclarations === true ||
388
+ ! ignoreNonConstDeclarations . treatParametersAsConst ||
389
+ shouldIgnorePattern (
390
+ variableNode ,
391
+ context ,
392
+ ignoreIdentifierPattern ,
393
+ ignoreAccessorPattern ,
394
+ ) ,
395
+ )
343
396
) {
344
397
return {
345
398
context,
@@ -473,18 +526,29 @@ function checkCallExpression(
473
526
! isInChainCallAndFollowsNew ( node . callee , context ) ) &&
474
527
isArrayType ( getTypeOfNode ( node . callee . object , context ) )
475
528
) {
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 {
529
+ if ( ignoreNonConstDeclarations === false ) {
530
+ return {
531
+ context,
532
+ descriptors : [ { node, messageId : "array" } ] ,
533
+ } ;
534
+ }
535
+ const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
536
+ if (
537
+ rootIdentifier === undefined ||
538
+ ! isDefinedByMutableVariable (
539
+ rootIdentifier ,
540
+ context ,
541
+ ( variableNode ) =>
542
+ ignoreNonConstDeclarations === true ||
543
+ ! ignoreNonConstDeclarations . treatParametersAsConst ||
544
+ shouldIgnorePattern (
545
+ variableNode ,
546
+ context ,
547
+ ignoreIdentifierPattern ,
548
+ ignoreAccessorPattern ,
549
+ ) ,
550
+ )
551
+ ) {
488
552
return {
489
553
context,
490
554
descriptors : [ { node, messageId : "array" } ] ,
@@ -507,18 +571,29 @@ function checkCallExpression(
507
571
) &&
508
572
isObjectConstructorType ( getTypeOfNode ( node . callee . object , context ) )
509
573
) {
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 {
574
+ if ( ignoreNonConstDeclarations === false ) {
575
+ return {
576
+ context,
577
+ descriptors : [ { node, messageId : "object" } ] ,
578
+ } ;
579
+ }
580
+ const rootIdentifier = findRootIdentifier ( node . callee . object ) ;
581
+ if (
582
+ rootIdentifier === undefined ||
583
+ ! isDefinedByMutableVariable (
584
+ rootIdentifier ,
585
+ context ,
586
+ ( variableNode ) =>
587
+ ignoreNonConstDeclarations === true ||
588
+ ! ignoreNonConstDeclarations . treatParametersAsConst ||
589
+ shouldIgnorePattern (
590
+ variableNode ,
591
+ context ,
592
+ ignoreIdentifierPattern ,
593
+ ignoreAccessorPattern ,
594
+ ) ,
595
+ )
596
+ ) {
522
597
return {
523
598
context,
524
599
descriptors : [ { node, messageId : "object" } ] ,
0 commit comments