Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extensions): correct Options type #175

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-snakes-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

fix(extensions): correct `Options` type
59 changes: 39 additions & 20 deletions src/rules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ import {
resolve,
} from '../utils'

const enumValues = {
const modifierValues = ['always', 'ignorePackages', 'never'] as const

const modifierSchema = {
type: 'string' as const,
enum: ['always', 'ignorePackages', 'never'],
enum: [...modifierValues],
}

const patternProperties = {
const modifierByFileExtensionSchema = {
type: 'object' as const,
patternProperties: { '.*': enumValues },
patternProperties: { '.*': modifierSchema },
}

const properties = {
type: 'object' as const,
properties: {
pattern: patternProperties,
pattern: modifierByFileExtensionSchema,
ignorePackages: {
type: 'boolean' as const,
},
Expand All @@ -33,20 +35,33 @@ const properties = {
},
}

type DefaultConfig = (typeof enumValues)['enum'][number]
type Modifier = (typeof modifierValues)[number]

type MessageId = 'missing' | 'unexpected'
type ModifierByFileExtension = Partial<Record<string, Modifier>>

type OptionsItemWithPatternProperty = {
ignorePackages?: boolean
checkTypeImports?: boolean
pattern: ModifierByFileExtension
}

type Options =
| []
| [Modifier]
| [Modifier, OptionsItemWithPatternProperty]
| [Modifier, ModifierByFileExtension]
| [ModifierByFileExtension]

type NormalizedOptions = {
defaultConfig?: DefaultConfig
pattern?: Record<FileExtension, DefaultConfig>
defaultConfig?: Modifier
pattern?: Record<string, Modifier>
ignorePackages?: boolean
checkTypeImports?: boolean
}

type Options = DefaultConfig | NormalizedOptions
type MessageId = 'missing' | 'unexpected'

function buildProperties(context: RuleContext<MessageId, Options[]>) {
function buildProperties(context: RuleContext<MessageId, Options>) {
const result: Required<NormalizedOptions> = {
defaultConfig: 'never',
pattern: {},
Expand All @@ -61,9 +76,13 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
continue
}

if (typeof obj !== 'object' || !obj) {
continue
}

// If this is not the new structure, transfer all props to result.pattern
if (
obj.pattern === undefined &&
(!('pattern' in obj) || obj.pattern === undefined) &&
obj.ignorePackages === undefined &&
obj.checkTypeImports === undefined
) {
Expand All @@ -72,16 +91,16 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
}

// If pattern is provided, transfer all props
if (obj.pattern !== undefined) {
if ('pattern' in obj && obj.pattern !== undefined) {
Object.assign(result.pattern, obj.pattern)
}

// If ignorePackages is provided, transfer it to result
if (obj.ignorePackages !== undefined) {
if (typeof obj.ignorePackages === 'boolean') {
result.ignorePackages = obj.ignorePackages
}

if (obj.checkTypeImports !== undefined) {
if (typeof obj.checkTypeImports === 'boolean') {
result.checkTypeImports = obj.checkTypeImports
}
}
Expand Down Expand Up @@ -109,7 +128,7 @@ function isExternalRootModule(file: string) {
return false
}

export = createRule<Options[], MessageId>({
export = createRule<Options, MessageId>({
name: 'extensions',
meta: {
type: 'suggestion',
Expand All @@ -122,12 +141,12 @@ export = createRule<Options[], MessageId>({
anyOf: [
{
type: 'array',
items: [enumValues],
items: [modifierSchema],
additionalItems: false,
},
{
type: 'array',
items: [enumValues, properties],
items: [modifierSchema, properties],
additionalItems: false,
},
{
Expand All @@ -137,12 +156,12 @@ export = createRule<Options[], MessageId>({
},
{
type: 'array',
items: [patternProperties],
items: [modifierSchema, modifierByFileExtensionSchema],
additionalItems: false,
},
{
type: 'array',
items: [enumValues, patternProperties],
items: [modifierByFileExtensionSchema],
additionalItems: false,
},
],
Expand Down
Loading