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(eslint-plugin): [no-restricted-imports] allow inline type qualifiers when allowTypeImports enabled #7379

Merged
merged 2 commits into from
Aug 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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,
},
],
},
],
});