Skip to content

Commit

Permalink
fix(eslint-plugin): [no-restricted-imports] disallow side effect impo…
Browse files Browse the repository at this point in the history
…rts when allowTypeImports is enabled (#7560)

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
joshkel and bradzacher committed Sep 8, 2023
1 parent ec6a62e commit 4908905
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Expand Up @@ -273,11 +273,12 @@ export default createRule<Options, MessageIds>({
ImportDeclaration(node: TSESTree.ImportDeclaration): void {
if (
node.importKind === 'type' ||
node.specifiers.every(
specifier =>
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.importKind === 'type',
)
(node.specifiers.length > 0 &&
node.specifiers.every(
specifier =>
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
specifier.importKind === 'type',
))
) {
const importSource = node.source.value.trim();
if (
Expand All @@ -297,7 +298,8 @@ export default createRule<Options, MessageIds>({
): void {
if (
node.exportKind === 'type' ||
node.specifiers.every(specifier => specifier.exportKind === 'type')
(node.specifiers.length > 0 &&
node.specifiers.every(specifier => specifier.exportKind === 'type'))
) {
const importSource = node.source.value.trim();
if (
Expand Down
42 changes: 42 additions & 0 deletions packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts
Expand Up @@ -10,6 +10,7 @@ const ruleTester = new RuleTester({
ruleTester.run('no-restricted-imports', rule, {
valid: [
"import foo from 'foo';",
"import 'foo';",
{
code: "import foo from 'foo';",
options: ['import1', 'import2'],
Expand All @@ -26,6 +27,10 @@ ruleTester.run('no-restricted-imports', rule, {
code: "export { foo } from 'foo';",
options: [{ paths: ['import1', 'import2'] }],
},
{
code: "import 'foo';",
options: ['import1', 'import2'],
},
{
code: "import foo from 'foo';",
options: [
Expand Down Expand Up @@ -490,6 +495,43 @@ import type { foo } from 'import2/private/bar';
},
],
},
{
code: "import 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
},
],
},
],
errors: [
{
messageId: 'path',
type: AST_NODE_TYPES.ImportDeclaration,
},
],
},
{
code: "import 'import-foo';",
options: [
{
paths: [
{
name: 'import-foo',
allowTypeImports: true,
},
],
},
],
errors: [
{
messageId: 'path',
type: AST_NODE_TYPES.ImportDeclaration,
},
],
},
{
code: "import foo from 'import-foo';",
options: [
Expand Down

0 comments on commit 4908905

Please sign in to comment.