Skip to content

Commit acebd98

Browse files
authored
fix(extensions): correct Options type (#175)
1 parent 3a45170 commit acebd98

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

.changeset/polite-snakes-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": patch
3+
---
4+
5+
fix(extensions): correct `Options` type

src/rules/extensions.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ import {
1010
resolve,
1111
} from '../utils'
1212

13-
const enumValues = {
13+
const modifierValues = ['always', 'ignorePackages', 'never'] as const
14+
15+
const modifierSchema = {
1416
type: 'string' as const,
15-
enum: ['always', 'ignorePackages', 'never'],
17+
enum: [...modifierValues],
1618
}
1719

18-
const patternProperties = {
20+
const modifierByFileExtensionSchema = {
1921
type: 'object' as const,
20-
patternProperties: { '.*': enumValues },
22+
patternProperties: { '.*': modifierSchema },
2123
}
2224

2325
const properties = {
2426
type: 'object' as const,
2527
properties: {
26-
pattern: patternProperties,
28+
pattern: modifierByFileExtensionSchema,
2729
ignorePackages: {
2830
type: 'boolean' as const,
2931
},
@@ -33,20 +35,33 @@ const properties = {
3335
},
3436
}
3537

36-
type DefaultConfig = (typeof enumValues)['enum'][number]
38+
type Modifier = (typeof modifierValues)[number]
3739

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]
3954

4055
type NormalizedOptions = {
41-
defaultConfig?: DefaultConfig
42-
pattern?: Record<FileExtension, DefaultConfig>
56+
defaultConfig?: Modifier
57+
pattern?: Record<string, Modifier>
4358
ignorePackages?: boolean
4459
checkTypeImports?: boolean
4560
}
4661

47-
type Options = DefaultConfig | NormalizedOptions
62+
type MessageId = 'missing' | 'unexpected'
4863

49-
function buildProperties(context: RuleContext<MessageId, Options[]>) {
64+
function buildProperties(context: RuleContext<MessageId, Options>) {
5065
const result: Required<NormalizedOptions> = {
5166
defaultConfig: 'never',
5267
pattern: {},
@@ -61,9 +76,13 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
6176
continue
6277
}
6378

79+
if (typeof obj !== 'object' || !obj) {
80+
continue
81+
}
82+
6483
// If this is not the new structure, transfer all props to result.pattern
6584
if (
66-
obj.pattern === undefined &&
85+
(!('pattern' in obj) || obj.pattern === undefined) &&
6786
obj.ignorePackages === undefined &&
6887
obj.checkTypeImports === undefined
6988
) {
@@ -72,16 +91,16 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
7291
}
7392

7493
// If pattern is provided, transfer all props
75-
if (obj.pattern !== undefined) {
94+
if ('pattern' in obj && obj.pattern !== undefined) {
7695
Object.assign(result.pattern, obj.pattern)
7796
}
7897

7998
// If ignorePackages is provided, transfer it to result
80-
if (obj.ignorePackages !== undefined) {
99+
if (typeof obj.ignorePackages === 'boolean') {
81100
result.ignorePackages = obj.ignorePackages
82101
}
83102

84-
if (obj.checkTypeImports !== undefined) {
103+
if (typeof obj.checkTypeImports === 'boolean') {
85104
result.checkTypeImports = obj.checkTypeImports
86105
}
87106
}
@@ -109,7 +128,7 @@ function isExternalRootModule(file: string) {
109128
return false
110129
}
111130

112-
export = createRule<Options[], MessageId>({
131+
export = createRule<Options, MessageId>({
113132
name: 'extensions',
114133
meta: {
115134
type: 'suggestion',
@@ -122,12 +141,12 @@ export = createRule<Options[], MessageId>({
122141
anyOf: [
123142
{
124143
type: 'array',
125-
items: [enumValues],
144+
items: [modifierSchema],
126145
additionalItems: false,
127146
},
128147
{
129148
type: 'array',
130-
items: [enumValues, properties],
149+
items: [modifierSchema, properties],
131150
additionalItems: false,
132151
},
133152
{
@@ -137,12 +156,12 @@ export = createRule<Options[], MessageId>({
137156
},
138157
{
139158
type: 'array',
140-
items: [patternProperties],
159+
items: [modifierSchema, modifierByFileExtensionSchema],
141160
additionalItems: false,
142161
},
143162
{
144163
type: 'array',
145-
items: [enumValues, patternProperties],
164+
items: [modifierByFileExtensionSchema],
146165
additionalItems: false,
147166
},
148167
],

0 commit comments

Comments
 (0)