@@ -69,6 +69,8 @@ type FixerConfigRaw = {
69
69
replace : string ;
70
70
} ;
71
71
72
+ type SuggestionsConfigRaw = Array < FixerConfigRaw & { message ?: string } > ;
73
+
72
74
type FixerConfigRawMap = Partial <
73
75
Record <
74
76
"ReadonlyShallow" | "ReadonlyDeep" | "Immutable" ,
@@ -79,7 +81,7 @@ type FixerConfigRawMap = Partial<
79
81
type SuggestionConfigRawMap = Partial <
80
82
Record <
81
83
"ReadonlyShallow" | "ReadonlyDeep" | "Immutable" ,
82
- FixerConfigRaw [ ] [ ] | undefined
84
+ SuggestionsConfigRaw [ ] | undefined
83
85
>
84
86
> ;
85
87
@@ -88,7 +90,7 @@ type FixerConfig = {
88
90
replace : string ;
89
91
} ;
90
92
91
- type SuggestionsConfig = FixerConfig [ ] ;
93
+ type SuggestionsConfig = Array < FixerConfig & { message ?: string } > ;
92
94
93
95
/**
94
96
* The options this rule can take.
@@ -205,6 +207,7 @@ const suggestionsSchema: JSONSchema4 = {
205
207
properties : {
206
208
pattern : { type : "string" } ,
207
209
replace : { type : "string" } ,
210
+ message : { type : "string" } ,
208
211
} ,
209
212
additionalProperties : false ,
210
213
} ,
@@ -275,14 +278,19 @@ const defaultOptions: Options = [
275
278
pattern :
276
279
"^([_$a-zA-Z\\xA0-\\uFFFF][_$a-zA-Z0-9\\xA0-\\uFFFF]*\\[\\])$" ,
277
280
replace : "readonly $1" ,
281
+ message : "Prepend with readonly." ,
278
282
} ,
279
283
{
280
284
pattern : "^(Array|Map|Set)<(.+)>$" ,
281
285
replace : "Readonly$1<$2>" ,
286
+ message : "Use Readonly$1 instead of $1." ,
282
287
} ,
288
+ ] ,
289
+ [
283
290
{
284
291
pattern : "^(.+)$" ,
285
292
replace : "Readonly<$1>" ,
293
+ message : "Surround with Readonly." ,
286
294
} ,
287
295
] ,
288
296
] ,
@@ -303,6 +311,8 @@ const errorMessages = {
303
311
propertyImmutability :
304
312
'Property should have an immutability of at least "{{ expected }}" (actual: "{{ actual }}").' ,
305
313
propertyModifier : "Property should have a readonly modifier." ,
314
+ propertyModifierSuggestion : "Add readonly modifier." ,
315
+ userDefined : "{{ message }}" ,
306
316
} as const ;
307
317
308
318
/**
@@ -331,7 +341,7 @@ type Descriptor = RuleResult<
331
341
332
342
type AllFixers = {
333
343
fix : ReportFixFunction | null ;
334
- suggestionFixers : ReportFixFunction [ ] | null ;
344
+ suggestionFixers : Array < { fix : ReportFixFunction ; message : string } > | null ;
335
345
} ;
336
346
337
347
/**
@@ -383,14 +393,27 @@ function getConfiguredSuggestionFixers(
383
393
suggestionsConfigs : SuggestionsConfig [ ] ,
384
394
) {
385
395
return suggestionsConfigs
386
- . map ( ( configs ) : NonNullable < Descriptor [ "fix" ] > | null => {
387
- const config = configs . find ( ( c ) => c . pattern . test ( text ) ) ;
388
- if ( config === undefined ) {
389
- return null ;
390
- }
391
- return ( fixer ) =>
392
- fixer . replaceText ( node , text . replace ( config . pattern , config . replace ) ) ;
393
- } )
396
+ . map (
397
+ (
398
+ configs ,
399
+ ) : { fix : NonNullable < Descriptor [ "fix" ] > ; message : string } | null => {
400
+ const config = configs . find ( ( c ) => c . pattern . test ( text ) ) ;
401
+ if ( config === undefined ) {
402
+ return null ;
403
+ }
404
+ return {
405
+ fix : ( fixer ) =>
406
+ fixer . replaceText (
407
+ node ,
408
+ text . replace ( config . pattern , config . replace ) ,
409
+ ) ,
410
+ message :
411
+ config . message === undefined
412
+ ? `Replace with: ${ text . replace ( config . pattern , config . replace ) } `
413
+ : text . replace ( config . pattern , config . message ) ,
414
+ } ;
415
+ } ,
416
+ )
394
417
. filter ( isDefined ) ;
395
418
}
396
419
@@ -504,17 +527,16 @@ function getParameterTypeViolations(
504
527
505
528
const parameterProperty = isTSParameterProperty ( param ) ;
506
529
if ( parameterProperty && ! param . readonly ) {
507
- const messageId = "propertyModifier" ;
508
530
const fix : NonNullable < Descriptor [ "fix" ] > | null = ( fixer ) =>
509
531
fixer . insertTextBefore ( param . parameter , "readonly " ) ;
510
532
511
533
return {
512
534
node : param ,
513
- messageId,
535
+ messageId : "propertyModifier" ,
514
536
fix : fixerConfigs === false ? null : fix ,
515
537
suggest : [
516
538
{
517
- messageId,
539
+ messageId : "propertyModifierSuggestion" ,
518
540
fix,
519
541
} ,
520
542
] ,
@@ -564,21 +586,20 @@ function getParameterTypeViolations(
564
586
suggestionsConfigs ,
565
587
) ;
566
588
567
- const messageId = "parameter" ;
568
- const data = {
569
- actual : Immutability [ immutability ] ,
570
- expected : Immutability [ enforcement ] ,
571
- } ;
572
-
573
589
return {
574
590
node : actualParam ,
575
- messageId,
576
- data,
591
+ messageId : "parameter" ,
592
+ data : {
593
+ actual : Immutability [ immutability ] ,
594
+ expected : Immutability [ enforcement ] ,
595
+ } ,
577
596
fix,
578
597
suggest :
579
- suggestionFixers ?. map ( ( fix ) => ( {
580
- messageId,
581
- data,
598
+ suggestionFixers ?. map ( ( { fix, message } ) => ( {
599
+ messageId : "userDefined" ,
600
+ data : {
601
+ message,
602
+ } ,
582
603
fix,
583
604
} ) ) ?? null ,
584
605
} ;
@@ -658,22 +679,21 @@ function getReturnTypeViolations(
658
679
suggestionsConfigs ,
659
680
) ;
660
681
661
- const messageId = "returnType" ;
662
- const data = {
663
- actual : Immutability [ immutability ] ,
664
- expected : Immutability [ enforcement ] ,
665
- } ;
666
-
667
682
return [
668
683
{
669
684
node : node . returnType ,
670
- messageId,
671
- data,
685
+ messageId : "returnType" ,
686
+ data : {
687
+ actual : Immutability [ immutability ] ,
688
+ expected : Immutability [ enforcement ] ,
689
+ } ,
672
690
fix,
673
691
suggest :
674
- suggestionFixers ?. map ( ( fix ) => ( {
675
- messageId,
676
- data,
692
+ suggestionFixers ?. map ( ( { fix, message } ) => ( {
693
+ messageId : "userDefined" ,
694
+ data : {
695
+ message,
696
+ } ,
677
697
fix,
678
698
} ) ) ?? null ,
679
699
} ,
@@ -713,22 +733,21 @@ function getReturnTypeViolations(
713
733
suggestionsConfigs ,
714
734
) ;
715
735
716
- const messageId = "returnType" ;
717
- const data = {
718
- actual : Immutability [ immutability ] ,
719
- expected : Immutability [ enforcement ] ,
720
- } ;
721
-
722
736
return [
723
737
{
724
738
node : hasID ( node ) && node . id !== null ? node . id : node ,
725
- messageId,
726
- data,
739
+ messageId : "returnType" ,
740
+ data : {
741
+ actual : Immutability [ immutability ] ,
742
+ expected : Immutability [ enforcement ] ,
743
+ } ,
727
744
fix,
728
745
suggest :
729
- suggestionFixers ?. map ( ( fix ) => ( {
730
- messageId,
731
- data,
746
+ suggestionFixers ?. map ( ( { fix, message } ) => ( {
747
+ messageId : "userDefined" ,
748
+ data : {
749
+ message,
750
+ } ,
732
751
fix,
733
752
} ) ) ?? null ,
734
753
} ,
@@ -807,17 +826,16 @@ function checkVariable(
807
826
const fix : NonNullable < Descriptor [ "fix" ] > | null = ( fixer ) =>
808
827
fixer . insertTextBefore ( node . key , "readonly " ) ;
809
828
810
- const messageId = "propertyModifier" ;
811
829
return {
812
830
context,
813
831
descriptors : [
814
832
{
815
833
node,
816
- messageId,
834
+ messageId : "propertyModifier" ,
817
835
fix : rawFixerConfig === undefined ? null : fix ,
818
836
suggest : [
819
837
{
820
- messageId,
838
+ messageId : "propertyModifierSuggestion" ,
821
839
fix,
822
840
} ,
823
841
] ,
@@ -912,9 +930,11 @@ function checkVariable(
912
930
data,
913
931
fix,
914
932
suggest :
915
- suggestionFixers ?. map ( ( fix ) => ( {
916
- messageId,
917
- data,
933
+ suggestionFixers ?. map ( ( { fix, message } ) => ( {
934
+ messageId : "userDefined" ,
935
+ data : {
936
+ message,
937
+ } ,
918
938
fix,
919
939
} ) ) ?? null ,
920
940
} ;
0 commit comments