@@ -10,20 +10,22 @@ import {
10
10
resolve ,
11
11
} from '../utils'
12
12
13
- const enumValues = {
13
+ const modifierValues = [ 'always' , 'ignorePackages' , 'never' ] as const
14
+
15
+ const modifierSchema = {
14
16
type : 'string' as const ,
15
- enum : [ 'always' , 'ignorePackages' , 'never' ] ,
17
+ enum : [ ... modifierValues ] ,
16
18
}
17
19
18
- const patternProperties = {
20
+ const modifierByFileExtensionSchema = {
19
21
type : 'object' as const ,
20
- patternProperties : { '.*' : enumValues } ,
22
+ patternProperties : { '.*' : modifierSchema } ,
21
23
}
22
24
23
25
const properties = {
24
26
type : 'object' as const ,
25
27
properties : {
26
- pattern : patternProperties ,
28
+ pattern : modifierByFileExtensionSchema ,
27
29
ignorePackages : {
28
30
type : 'boolean' as const ,
29
31
} ,
@@ -33,20 +35,33 @@ const properties = {
33
35
} ,
34
36
}
35
37
36
- type DefaultConfig = ( typeof enumValues ) [ 'enum' ] [ number ]
38
+ type Modifier = ( typeof modifierValues ) [ number ]
37
39
38
- type MessageId = 'missing' | 'unexpected'
40
+ type ModifierByFileExtension = Partial < Record < string , Modifier > >
41
+
42
+ type OptionsItemWithPatternProperty = {
43
+ ignorePackages ?: boolean
44
+ checkTypeImports ?: boolean
45
+ pattern : ModifierByFileExtension
46
+ }
47
+
48
+ type Options =
49
+ | [ ]
50
+ | [ Modifier ]
51
+ | [ Modifier , OptionsItemWithPatternProperty ]
52
+ | [ Modifier , ModifierByFileExtension ]
53
+ | [ ModifierByFileExtension ]
39
54
40
55
type NormalizedOptions = {
41
- defaultConfig ?: DefaultConfig
42
- pattern ?: Record < FileExtension , DefaultConfig >
56
+ defaultConfig ?: Modifier
57
+ pattern ?: Record < string , Modifier >
43
58
ignorePackages ?: boolean
44
59
checkTypeImports ?: boolean
45
60
}
46
61
47
- type Options = DefaultConfig | NormalizedOptions
62
+ type MessageId = 'missing' | 'unexpected'
48
63
49
- function buildProperties ( context : RuleContext < MessageId , Options [ ] > ) {
64
+ function buildProperties ( context : RuleContext < MessageId , Options > ) {
50
65
const result : Required < NormalizedOptions > = {
51
66
defaultConfig : 'never' ,
52
67
pattern : { } ,
@@ -61,9 +76,13 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
61
76
continue
62
77
}
63
78
79
+ if ( typeof obj !== 'object' || ! obj ) {
80
+ continue
81
+ }
82
+
64
83
// If this is not the new structure, transfer all props to result.pattern
65
84
if (
66
- obj . pattern === undefined &&
85
+ ( ! ( 'pattern' in obj ) || obj . pattern === undefined ) &&
67
86
obj . ignorePackages === undefined &&
68
87
obj . checkTypeImports === undefined
69
88
) {
@@ -72,16 +91,16 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
72
91
}
73
92
74
93
// If pattern is provided, transfer all props
75
- if ( obj . pattern !== undefined ) {
94
+ if ( 'pattern' in obj && obj . pattern !== undefined ) {
76
95
Object . assign ( result . pattern , obj . pattern )
77
96
}
78
97
79
98
// If ignorePackages is provided, transfer it to result
80
- if ( obj . ignorePackages !== undefined ) {
99
+ if ( typeof obj . ignorePackages === 'boolean' ) {
81
100
result . ignorePackages = obj . ignorePackages
82
101
}
83
102
84
- if ( obj . checkTypeImports !== undefined ) {
103
+ if ( typeof obj . checkTypeImports === 'boolean' ) {
85
104
result . checkTypeImports = obj . checkTypeImports
86
105
}
87
106
}
@@ -109,7 +128,7 @@ function isExternalRootModule(file: string) {
109
128
return false
110
129
}
111
130
112
- export = createRule < Options [ ] , MessageId > ( {
131
+ export = createRule < Options , MessageId > ( {
113
132
name : 'extensions' ,
114
133
meta : {
115
134
type : 'suggestion' ,
@@ -122,12 +141,12 @@ export = createRule<Options[], MessageId>({
122
141
anyOf : [
123
142
{
124
143
type : 'array' ,
125
- items : [ enumValues ] ,
144
+ items : [ modifierSchema ] ,
126
145
additionalItems : false ,
127
146
} ,
128
147
{
129
148
type : 'array' ,
130
- items : [ enumValues , properties ] ,
149
+ items : [ modifierSchema , properties ] ,
131
150
additionalItems : false ,
132
151
} ,
133
152
{
@@ -137,12 +156,12 @@ export = createRule<Options[], MessageId>({
137
156
} ,
138
157
{
139
158
type : 'array' ,
140
- items : [ patternProperties ] ,
159
+ items : [ modifierSchema , modifierByFileExtensionSchema ] ,
141
160
additionalItems : false ,
142
161
} ,
143
162
{
144
163
type : 'array' ,
145
- items : [ enumValues , patternProperties ] ,
164
+ items : [ modifierByFileExtensionSchema ] ,
146
165
additionalItems : false ,
147
166
} ,
148
167
] ,
0 commit comments