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

extensions: add the checkTypeImports option #2817

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/rules/extensions.md
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,14 @@ 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 @@ -167,6 +177,14 @@ 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
9 changes: 7 additions & 2 deletions src/rules/extensions.js
Expand Up @@ -14,6 +14,7 @@ const properties = {
type: 'object',
properties: {
pattern: patternProperties,
checkTypeImports: { type: 'boolean' },
ignorePackages: { type: 'boolean' },
},
};
Expand All @@ -35,7 +36,7 @@ function buildProperties(context) {
}

// 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);
return;
}
Expand All @@ -49,6 +50,10 @@ function buildProperties(context) {
if (obj.ignorePackages !== undefined) {
result.ignorePackages = obj.ignorePackages;
}

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

if (result.defaultConfig === 'ignorePackages') {
Expand Down Expand Up @@ -168,7 +173,7 @@ module.exports = {

if (!extension || !importPath.endsWith(`.${extension}`)) {
// ignore type-only imports and exports
if (node.importKind === 'type' || node.exportKind === 'type') { return; }
if (!props.checkTypeImports && (node.importKind === 'type' || node.exportKind === 'type')) { return; }
const extensionRequired = isUseOfExtensionRequired(extension, isPackage);
const extensionForbidden = isUseOfExtensionForbidden(extension);
if (extensionRequired && !extensionForbidden) {
Expand Down
67 changes: 67 additions & 0 deletions tests/src/rules/extensions.js
Expand Up @@ -3,6 +3,15 @@ import rule from 'rules/extensions';
import { getTSParsers, test, testFilePath, parsers } from '../utils';

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

ruleTester.run('extensions', rule, {
valid: [
Expand Down Expand Up @@ -689,6 +698,64 @@ describe('TypeScript', () => {
],
parser,
}),
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 },
],
parser,
}),
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 },
],
parser,
}),
ljharb marked this conversation as resolved.
Show resolved Hide resolved
],
});
ruleTesterWithTypeScriptImports.run(`${parser}: (with TS resolver) extensions are enforced for type imports/export when checkTypeImports is set`, rule, {
valid: [
test({
code: 'import type { MyType } from "./typescript-declare.ts";',
options: [
'always',
{ checkTypeImports: true },
],
parser,
}),
test({
code: 'export type { MyType } from "./typescript-declare.ts";',
options: [
'always',
{ checkTypeImports: true },
],
parser,
}),
],
invalid: [
test({
code: 'import type { MyType } from "./typescript-declare";',
errors: ['Missing file extension "ts" for "./typescript-declare"'],
options: [
'always',
{ checkTypeImports: true },
],
parser,
}),
test({
code: 'export type { MyType } from "./typescript-declare";',
errors: ['Missing file extension "ts" for "./typescript-declare"'],
options: [
'always',
{ checkTypeImports: true },
],
parser,
}),
],
});
});
Expand Down