@@ -12,6 +12,8 @@ import { Immutability } from "is-immutable-type";
12
12
13
13
import {
14
14
type IgnoreClassesOption ,
15
+ type OverridableOptions ,
16
+ getCoreOptions ,
15
17
ignoreClassesOptionSchema ,
16
18
shouldIgnoreClasses ,
17
19
shouldIgnoreInFunction ,
@@ -42,6 +44,8 @@ import {
42
44
isTSTypePredicate ,
43
45
} from "#eslint-plugin-functional/utils/type-guards" ;
44
46
47
+ import { overridableOptionsSchema } from "../utils/schemas" ;
48
+
45
49
/**
46
50
* The name of this rule.
47
51
*/
@@ -55,7 +59,8 @@ export const fullName = `${ruleNameScope}/${name}`;
55
59
type RawEnforcement =
56
60
| Exclude < Immutability | keyof typeof Immutability , "Unknown" | "Mutable" >
57
61
| "None"
58
- | false ;
62
+ | false
63
+ | undefined ;
59
64
60
65
type Option = IgnoreClassesOption & {
61
66
enforcement : RawEnforcement ;
@@ -64,6 +69,20 @@ type Option = IgnoreClassesOption & {
64
69
ignoreTypePattern ?: string [ ] | string ;
65
70
} ;
66
71
72
+ type CoreOptions = Option & {
73
+ parameters ?: Partial < Option > | RawEnforcement ;
74
+ returnTypes ?: Partial < Option > | RawEnforcement ;
75
+ variables ?:
76
+ | Partial <
77
+ Option & {
78
+ ignoreInFunctions ?: boolean ;
79
+ }
80
+ >
81
+ | RawEnforcement ;
82
+ fixer ?: FixerConfigRawMap ;
83
+ suggestions ?: SuggestionConfigRawMap ;
84
+ } ;
85
+
67
86
type FixerConfigRaw = {
68
87
pattern : string ;
69
88
replace : string ;
@@ -93,21 +112,7 @@ type SuggestionsConfig = FixerConfig[];
93
112
/**
94
113
* The options this rule can take.
95
114
*/
96
- type Options = [
97
- Option & {
98
- parameters ?: Partial < Option > | RawEnforcement ;
99
- returnTypes ?: Partial < Option > | RawEnforcement ;
100
- variables ?:
101
- | Partial <
102
- Option & {
103
- ignoreInFunctions ?: boolean ;
104
- }
105
- >
106
- | RawEnforcement ;
107
- fixer ?: FixerConfigRawMap ;
108
- suggestions ?: SuggestionConfigRawMap ;
109
- } ,
110
- ] ;
115
+ type Options = [ OverridableOptions < CoreOptions > ] ;
111
116
112
117
/**
113
118
* The enum options for the level of enforcement.
@@ -211,53 +216,53 @@ const suggestionsSchema: JSONSchema4 = {
211
216
} ,
212
217
} ;
213
218
214
- /**
215
- * The schema for the rule options.
216
- */
217
- const schema : JSONSchema4 [ ] = [
218
- {
219
- type : "object" ,
220
- properties : deepmerge ( optionExpandedSchema , {
221
- parameters : optionSchema ,
222
- returnTypes : optionSchema ,
223
- variables : {
224
- oneOf : [
225
- {
226
- type : "object" ,
227
- properties : deepmerge ( optionExpandedSchema , {
228
- ignoreInFunctions : {
229
- type : "boolean" ,
230
- } ,
231
- } ) ,
232
- additionalProperties : false ,
233
- } ,
234
- {
235
- type : [ "string" , "number" , "boolean" ] ,
236
- enum : enforcementEnumOptions ,
237
- } ,
238
- ] ,
239
- } ,
240
- fixer : {
219
+ const coreOptionsPropertiesSchema : NonNullable <
220
+ JSONSchema4ObjectSchema [ "properties" ]
221
+ > = deepmerge ( optionExpandedSchema , {
222
+ parameters : optionSchema ,
223
+ returnTypes : optionSchema ,
224
+ variables : {
225
+ oneOf : [
226
+ {
241
227
type : "object" ,
242
- properties : {
243
- ReadonlyShallow : fixerSchema ,
244
- ReadonlyDeep : fixerSchema ,
245
- Immutable : fixerSchema ,
246
- } ,
228
+ properties : deepmerge ( optionExpandedSchema , {
229
+ ignoreInFunctions : {
230
+ type : "boolean" ,
231
+ } ,
232
+ } ) ,
247
233
additionalProperties : false ,
248
234
} ,
249
- suggestions : {
250
- type : "object" ,
251
- properties : {
252
- ReadonlyShallow : suggestionsSchema ,
253
- ReadonlyDeep : suggestionsSchema ,
254
- Immutable : suggestionsSchema ,
255
- } ,
256
- additionalProperties : false ,
235
+ {
236
+ type : [ "string" , "number" , "boolean" ] ,
237
+ enum : enforcementEnumOptions ,
257
238
} ,
258
- } ) ,
239
+ ] ,
240
+ } ,
241
+ fixer : {
242
+ type : "object" ,
243
+ properties : {
244
+ ReadonlyShallow : fixerSchema ,
245
+ ReadonlyDeep : fixerSchema ,
246
+ Immutable : fixerSchema ,
247
+ } ,
259
248
additionalProperties : false ,
260
249
} ,
250
+ suggestions : {
251
+ type : "object" ,
252
+ properties : {
253
+ ReadonlyShallow : suggestionsSchema ,
254
+ ReadonlyDeep : suggestionsSchema ,
255
+ Immutable : suggestionsSchema ,
256
+ } ,
257
+ additionalProperties : false ,
258
+ } ,
259
+ } ) ;
260
+
261
+ /**
262
+ * The schema for the rule options.
263
+ */
264
+ const schema : JSONSchema4 [ ] = [
265
+ overridableOptionsSchema ( coreOptionsPropertiesSchema ) ,
261
266
] ;
262
267
263
268
/**
@@ -398,7 +403,7 @@ function getConfiguredSuggestionFixers(
398
403
* Get the level of enforcement from the raw value given.
399
404
*/
400
405
function parseEnforcement ( rawEnforcement : RawEnforcement ) {
401
- return rawEnforcement === "None"
406
+ return rawEnforcement === "None" || rawEnforcement === undefined
402
407
? false
403
408
: typeof rawEnforcement === "string"
404
409
? Immutability [ rawEnforcement ]
@@ -454,35 +459,32 @@ function parseSuggestionsConfigs(
454
459
function getParameterTypeViolations (
455
460
node : ESFunctionType ,
456
461
context : Readonly < RuleContext < keyof typeof errorMessages , Options > > ,
457
- options : Readonly < Options > ,
462
+ options : Readonly < CoreOptions > ,
458
463
) : Descriptor [ ] {
459
- const [ optionsObject ] = options ;
460
464
const {
461
465
parameters : rawOption ,
462
466
fixer : rawFixerConfig ,
463
467
suggestions : rawSuggestionsConfigs ,
464
- } = optionsObject ;
468
+ } = options ;
465
469
const {
466
470
enforcement : rawEnforcement ,
467
471
ignoreInferredTypes,
468
472
ignoreClasses,
469
473
ignoreNamePattern,
470
474
ignoreTypePattern,
471
475
} = {
472
- ignoreInferredTypes : optionsObject . ignoreInferredTypes ,
473
- ignoreClasses : optionsObject . ignoreClasses ,
474
- ignoreNamePattern : optionsObject . ignoreNamePattern ,
475
- ignoreTypePattern : optionsObject . ignoreTypePattern ,
476
+ ignoreInferredTypes : options . ignoreInferredTypes ,
477
+ ignoreClasses : options . ignoreClasses ,
478
+ ignoreNamePattern : options . ignoreNamePattern ,
479
+ ignoreTypePattern : options . ignoreTypePattern ,
476
480
...( typeof rawOption === "object"
477
481
? rawOption
478
482
: {
479
483
enforcement : rawOption ,
480
484
} ) ,
481
485
} ;
482
486
483
- const enforcement = parseEnforcement (
484
- rawEnforcement ?? optionsObject . enforcement ,
485
- ) ;
487
+ const enforcement = parseEnforcement ( rawEnforcement ?? options . enforcement ) ;
486
488
if (
487
489
enforcement === false ||
488
490
shouldIgnoreClasses ( node , context , ignoreClasses )
@@ -592,31 +594,28 @@ function getParameterTypeViolations(
592
594
function getReturnTypeViolations (
593
595
node : ESFunctionType ,
594
596
context : Readonly < RuleContext < keyof typeof errorMessages , Options > > ,
595
- options : Readonly < Options > ,
597
+ options : Readonly < CoreOptions > ,
596
598
) : Descriptor [ ] {
597
- const [ optionsObject ] = options ;
598
599
const {
599
600
returnTypes : rawOption ,
600
601
fixer : rawFixerConfig ,
601
602
suggestions : rawSuggestionsConfigs ,
602
- } = optionsObject ;
603
+ } = options ;
603
604
const {
604
605
enforcement : rawEnforcement ,
605
606
ignoreInferredTypes,
606
607
ignoreClasses,
607
608
ignoreNamePattern,
608
609
ignoreTypePattern,
609
610
} = {
610
- ignoreInferredTypes : optionsObject . ignoreInferredTypes ,
611
- ignoreClasses : optionsObject . ignoreClasses ,
612
- ignoreNamePattern : optionsObject . ignoreNamePattern ,
613
- ignoreTypePattern : optionsObject . ignoreTypePattern ,
611
+ ignoreInferredTypes : options . ignoreInferredTypes ,
612
+ ignoreClasses : options . ignoreClasses ,
613
+ ignoreNamePattern : options . ignoreNamePattern ,
614
+ ignoreTypePattern : options . ignoreTypePattern ,
614
615
...( typeof rawOption === "object" ? rawOption : { enforcement : rawOption } ) ,
615
616
} ;
616
617
617
- const enforcement = parseEnforcement (
618
- rawEnforcement ?? optionsObject . enforcement ,
619
- ) ;
618
+ const enforcement = parseEnforcement ( rawEnforcement ?? options . enforcement ) ;
620
619
621
620
if (
622
621
enforcement === false ||
@@ -743,10 +742,19 @@ function checkFunction(
743
742
context : Readonly < RuleContext < keyof typeof errorMessages , Options > > ,
744
743
options : Readonly < Options > ,
745
744
) : RuleResult < keyof typeof errorMessages , Options > {
746
- const descriptors = [
747
- ...getParameterTypeViolations ( node , context , options ) ,
748
- ...getReturnTypeViolations ( node , context , options ) ,
749
- ] ;
745
+ const optionsToUse = getCoreOptions < CoreOptions , Options > (
746
+ node ,
747
+ context ,
748
+ options ,
749
+ ) ;
750
+
751
+ const descriptors =
752
+ optionsToUse === null
753
+ ? [ ]
754
+ : [
755
+ ...getParameterTypeViolations ( node , context , optionsToUse ) ,
756
+ ...getReturnTypeViolations ( node , context , optionsToUse ) ,
757
+ ] ;
750
758
751
759
return {
752
760
context,
@@ -762,13 +770,24 @@ function checkVariable(
762
770
context : Readonly < RuleContext < keyof typeof errorMessages , Options > > ,
763
771
options : Readonly < Options > ,
764
772
) : RuleResult < keyof typeof errorMessages , Options > {
765
- const [ optionsObject ] = options ;
773
+ const optionsToUse = getCoreOptions < CoreOptions , Options > (
774
+ node ,
775
+ context ,
776
+ options ,
777
+ ) ;
778
+
779
+ if ( optionsToUse === null ) {
780
+ return {
781
+ context,
782
+ descriptors : [ ] ,
783
+ } ;
784
+ }
766
785
767
786
const {
768
787
variables : rawOption ,
769
788
fixer : rawFixerConfig ,
770
789
suggestions : rawSuggestionsConfigs ,
771
- } = optionsObject ;
790
+ } = optionsToUse ;
772
791
const {
773
792
enforcement : rawEnforcement ,
774
793
ignoreInferredTypes,
@@ -777,16 +796,16 @@ function checkVariable(
777
796
ignoreTypePattern,
778
797
ignoreInFunctions,
779
798
} = {
780
- ignoreInferredTypes : optionsObject . ignoreInferredTypes ,
781
- ignoreClasses : optionsObject . ignoreClasses ,
782
- ignoreNamePattern : optionsObject . ignoreNamePattern ,
783
- ignoreTypePattern : optionsObject . ignoreTypePattern ,
799
+ ignoreInferredTypes : optionsToUse . ignoreInferredTypes ,
800
+ ignoreClasses : optionsToUse . ignoreClasses ,
801
+ ignoreNamePattern : optionsToUse . ignoreNamePattern ,
802
+ ignoreTypePattern : optionsToUse . ignoreTypePattern ,
784
803
ignoreInFunctions : false ,
785
804
...( typeof rawOption === "object" ? rawOption : { enforcement : rawOption } ) ,
786
805
} ;
787
806
788
807
const enforcement = parseEnforcement (
789
- rawEnforcement ?? optionsObject . enforcement ,
808
+ rawEnforcement ?? optionsToUse . enforcement ,
790
809
) ;
791
810
792
811
if (
0 commit comments