Skip to content

Commit

Permalink
extensions: added checkTypeImports option (#169)
Browse files Browse the repository at this point in the history
Co-authored-by: SukkaW <[email protected]>
  • Loading branch information
teidesu and SukkaW authored Oct 30, 2024
1 parent 9715220 commit 9c58269
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-crews-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": minor
---

Add new rule option `checkTypedImports` for `extensions`, backports https://github.com/import-js/eslint-plugin-import/pull/2817
16 changes: 16 additions & 0 deletions docs/rules/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ For example, `["error", "never", { "svg": "always" }]` would require that all ex
In that case, if you still want to specify extensions, you can do so inside the **pattern** property.
Default value of `ignorePackages` is `false`.

By default, `import type` and `export type` style imports/exports are ignored. If you want to check them as well, you can set the `checkTypeImports` option to `true`.

### Exception

When disallowing the use of certain extensions this rule makes an exception and allows the use of extension when the file would not be resolvable without extension.
Expand Down Expand Up @@ -104,6 +106,13 @@ import express from 'express/index'
import * as path from 'path'
```

The following patterns are considered problems when the configuration is set to "never" and the option "checkTypeImports" is set to `true`:

```js
import type { Foo } from './foo.ts';
export type { Foo } from './foo.ts';
```

The following patterns are considered problems when configuration set to "always":

```js
Expand Down Expand Up @@ -166,6 +175,13 @@ import express from 'express'
import foo from '@/foo'
```

The following patterns are considered problems when the configuration is set to "always" and the option "checkTypeImports" is set to `true`:

```js
import type { Foo } from './foo';
export type { Foo } from './foo';
```

## When Not To Use It

If you are not concerned about a consistent usage of file extension.
Expand Down
20 changes: 17 additions & 3 deletions src/rules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const properties = {
ignorePackages: {
type: 'boolean' as const,
},
checkTypeImports: {
type: 'boolean' as const,
},
},
}

Expand All @@ -38,6 +41,7 @@ type NormalizedOptions = {
defaultConfig?: DefaultConfig
pattern?: Record<FileExtension, DefaultConfig>
ignorePackages?: boolean
checkTypeImports?: boolean
}

type Options = DefaultConfig | NormalizedOptions
Expand All @@ -47,6 +51,7 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
defaultConfig: 'never',
pattern: {},
ignorePackages: false,
checkTypeImports: false,
}

for (const obj of context.options) {
Expand All @@ -57,7 +62,11 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
}

// If this is not the new structure, transfer all props to result.pattern
if (obj.pattern === undefined && obj.ignorePackages === undefined) {
if (
obj.pattern === undefined &&
obj.ignorePackages === undefined &&
obj.checkTypeImports === undefined
) {
Object.assign(result.pattern, obj)
continue
}
Expand All @@ -71,6 +80,10 @@ function buildProperties(context: RuleContext<MessageId, Options[]>) {
if (obj.ignorePackages !== undefined) {
result.ignorePackages = obj.ignorePackages
}

if (obj.checkTypeImports !== undefined) {
result.checkTypeImports = obj.checkTypeImports
}
}

if (result.defaultConfig === 'ignorePackages') {
Expand Down Expand Up @@ -213,8 +226,9 @@ export = createRule<Options[], MessageId>({
if (!extension || !importPath.endsWith(`.${extension}`)) {
// ignore type-only imports and exports
if (
('importKind' in node && node.importKind === 'type') ||
('exportKind' in node && node.exportKind === 'type')
!props.checkTypeImports &&
(('importKind' in node && node.importKind === 'type') ||
('exportKind' in node && node.exportKind === 'type'))
) {
return
}
Expand Down
61 changes: 61 additions & 0 deletions test/rules/extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { test, testFilePath } from '../utils'
import rule from 'eslint-plugin-import-x/rules/extensions'

const ruleTester = new TSESLintRuleTester()
const ruleTesterWithTypeScriptImports = new TSESLintRuleTester({
settings: {
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
},
})

ruleTester.run('extensions', rule, {
valid: [
Expand Down Expand Up @@ -694,6 +703,58 @@ describe('TypeScript', () => {
{ ts: 'never', tsx: 'never', js: 'never', jsx: 'never' },
],
}),
test({
code: 'import type T from "./typescript-declare";',
errors: ['Missing file extension for "./typescript-declare"'],
options: [
'always',
{
ts: 'never',
tsx: 'never',
js: 'never',
jsx: 'never',
checkTypeImports: true,
},
],
}),
test({
code: 'export type { MyType } from "./typescript-declare";',
errors: ['Missing file extension for "./typescript-declare"'],
options: [
'always',
{
ts: 'never',
tsx: 'never',
js: 'never',
jsx: 'never',
checkTypeImports: true,
},
],
}),
],
})
ruleTesterWithTypeScriptImports.run('extensions', rule, {
valid: [
test({
code: 'import type { MyType } from "./typescript-declare.ts";',
options: ['always', { checkTypeImports: true }],
}),
test({
code: 'export type { MyType } from "./typescript-declare.ts";',
options: ['always', { checkTypeImports: true }],
}),
],
invalid: [
test({
code: 'import type { MyType } from "./typescript-declare";',
errors: ['Missing file extension for "./typescript-declare"'],
options: ['always', { checkTypeImports: true }],
}),
test({
code: 'export type { MyType } from "./typescript-declare";',
errors: ['Missing file extension for "./typescript-declare"'],
options: ['always', { checkTypeImports: true }],
}),
],
})
})

0 comments on commit 9c58269

Please sign in to comment.