From 0419d287b637e805c33036a45760badc2498e19b Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 4 May 2022 06:14:21 +0800 Subject: [PATCH] fix(eslint-plugin): [no-restricted-imports] allow type import as long as there's one matching pattern (#4898) * fix(eslint-plugin): [no-restricted-imports] allow type import as long as there's one matching pattern * chore: simplify .some expression Co-authored-by: Josh Goldberg --- .../src/rules/no-restricted-imports.ts | 6 ++--- .../tests/rules/no-restricted-imports.test.ts | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-restricted-imports.ts b/packages/eslint-plugin/src/rules/no-restricted-imports.ts index fd724c5fc8b..13afc7a895d 100644 --- a/packages/eslint-plugin/src/rules/no-restricted-imports.ts +++ b/packages/eslint-plugin/src/rules/no-restricted-imports.ts @@ -157,10 +157,8 @@ export default createRule({ } function isAllowedTypeImportPattern(importSource: string): boolean { return ( - allowedImportTypeMatchers.length > 0 && - allowedImportTypeMatchers.every(matcher => { - return matcher.ignores(importSource); - }) + // As long as there's one matching pattern that allows type import + allowedImportTypeMatchers.some(matcher => matcher.ignores(importSource)) ); } diff --git a/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts b/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts index 1758365e37d..64f9c91fe7c 100644 --- a/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts @@ -231,6 +231,28 @@ ruleTester.run('no-restricted-imports', rule, { }, ], }, + { + code: ` +import type { foo } from 'import1/private/bar'; +import type { foo } from 'import2/private/bar'; + `, + options: [ + { + patterns: [ + { + group: ['import1/private/*'], + message: 'usage of import1 private modules not allowed.', + allowTypeImports: true, + }, + { + group: ['import2/private/*'], + message: 'usage of import2 private modules not allowed.', + allowTypeImports: true, + }, + ], + }, + ], + }, ], invalid: [ {