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 2 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
10 changes: 10 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 @@ -167,6 +169,14 @@ import express from 'express';
import foo from '@/foo';
```

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

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

export type { Foo } from './foo';
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a similar section under "never".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 6ac3d03


## 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 @@ -167,7 +172,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 !== true && (node.importKind === 'type' || node.exportKind === 'type')) { return; }
phryneas marked this conversation as resolved.
Show resolved Hide resolved
const extensionRequired = isUseOfExtensionRequired(extension, isPackage);
const extensionForbidden = isUseOfExtensionForbidden(extension);
if (extensionRequired && !extensionForbidden) {
Expand Down
18 changes: 18 additions & 0 deletions tests/src/rules/extensions.js
Expand Up @@ -640,6 +640,24 @@ 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
],
});
});
Expand Down