Skip to content

Commit

Permalink
fix(eslint-plugin): [no-restricted-imports] allow inline type qualifi…
Browse files Browse the repository at this point in the history
…ers when `allowTypeImports` enabled (#7379)

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
auvred and JoshuaKGoldberg committed Aug 5, 2023
1 parent 4087f48 commit cc9a46d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
@@ -1,4 +1,5 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import type {
JSONSchema4AnyOfSchema,
JSONSchema4ArraySchema,
Expand Down Expand Up @@ -269,8 +270,15 @@ export default createRule<Options, MessageIds>({
}

return {
ImportDeclaration(node): void {
if (node.importKind === 'type') {
ImportDeclaration(node: TSESTree.ImportDeclaration): void {
if (
node.importKind === 'type' ||
node.specifiers.every(
specifier =>
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.importKind === 'type',
)
) {
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
Expand All @@ -287,7 +295,10 @@ export default createRule<Options, MessageIds>({
source: NonNullable<TSESTree.ExportNamedDeclaration['source']>;
},
): void {
if (node.exportKind === 'type') {
if (
node.exportKind === 'type' ||
node.specifiers.every(specifier => specifier.exportKind === 'type')
) {
const importSource = node.source.value.trim();
if (
!isAllowedTypeImportPath(importSource) &&
Expand Down
80 changes: 80 additions & 0 deletions packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts
Expand Up @@ -254,6 +254,36 @@ import type { foo } from 'import2/private/bar';
},
],
},
{
code: "import { type Bar } from 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
importNames: ['Bar'],
message: 'Please use Bar from /import-bar/baz/ instead.',
allowTypeImports: true,
},
],
},
],
},
{
code: "export { type Bar } from 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
importNames: ['Bar'],
message: 'Please use Bar from /import-bar/baz/ instead.',
allowTypeImports: true,
},
],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -586,5 +616,55 @@ import type { foo } from 'import2/private/bar';
},
],
},
{
code: "import { Bar, type Baz } from 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
importNames: ['Bar', 'Baz'],
message: 'Please use Bar and Baz from /import-bar/baz/ instead.',
allowTypeImports: true,
},
],
},
],
errors: [
{
messageId: 'importNameWithCustomMessage',
type: AST_NODE_TYPES.ImportDeclaration,
},
{
messageId: 'importNameWithCustomMessage',
type: AST_NODE_TYPES.ImportDeclaration,
},
],
},
{
code: "export { Bar, type Baz } from 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
importNames: ['Bar', 'Baz'],
message: 'Please use Bar and Baz from /import-bar/baz/ instead.',
allowTypeImports: true,
},
],
},
],
errors: [
{
messageId: 'importNameWithCustomMessage',
type: AST_NODE_TYPES.ExportNamedDeclaration,
},
{
messageId: 'importNameWithCustomMessage',
type: AST_NODE_TYPES.ExportNamedDeclaration,
},
],
},
],
});

0 comments on commit cc9a46d

Please sign in to comment.