Skip to content

fix(eslint-plugin): [no-restricted-imports] support regex option #10699

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

Merged
merged 4 commits into from
Feb 3, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/eslint-plugin/src/rules/no-restricted-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,24 +267,38 @@ export default createRule<Options, MessageIds>({

const restrictedPatterns = getRestrictedPatterns(options);
const allowedImportTypeMatchers: Ignore[] = [];
const allowedImportTypeRegexMatchers: RegExp[] = [];
for (const restrictedPattern of restrictedPatterns) {
if (
typeof restrictedPattern === 'object' &&
restrictedPattern.allowTypeImports
) {
// Following how ignore is configured in the base rule
allowedImportTypeMatchers.push(
ignore({
allowRelativePaths: true,
ignoreCase: !restrictedPattern.caseSensitive,
}).add(restrictedPattern.group),
);
if (restrictedPattern.group) {
allowedImportTypeMatchers.push(
ignore({
allowRelativePaths: true,
ignoreCase: !restrictedPattern.caseSensitive,
}).add(restrictedPattern.group),
);
}
if (restrictedPattern.regex) {
allowedImportTypeRegexMatchers.push(
new RegExp(
restrictedPattern.regex,
restrictedPattern.caseSensitive ? 'u' : 'iu',
),
);
}
}
}
function isAllowedTypeImportPattern(importSource: string): boolean {
return (
// As long as there's one matching pattern that allows type import
allowedImportTypeMatchers.some(matcher => matcher.ignores(importSource))
allowedImportTypeMatchers.some(matcher =>
matcher.ignores(importSource),
) ||
allowedImportTypeRegexMatchers.some(regex => regex.test(importSource))
);
}

Expand Down
78 changes: 78 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 @@ -290,6 +290,43 @@ import type { foo } from 'import2/private/bar';
},
],
},
{
code: `
import type { foo } from 'import1/private/bar';
import type { foo } from 'import2/private/bar';
`,
options: [
{
patterns: [
{
allowTypeImports: true,
message: 'usage of import1 private modules not allowed.',
regex: 'import1/.*',
},
{
allowTypeImports: true,
message: 'usage of import2 private modules not allowed.',
regex: 'import2/.*',
},
],
},
],
},
{
code: "import { foo } from 'import1/private';",
options: [
{
patterns: [
{
allowTypeImports: true,
caseSensitive: true,
message: 'usage of import1 private modules not allowed.',
regex: 'import1/[A-Z]+',
},
],
},
],
},
{
code: "import { type Bar } from 'import-foo';",
options: [
Expand Down Expand Up @@ -722,6 +759,47 @@ import type { foo } from 'import2/private/bar';
},
],
},
{
code: "export { foo } from 'import1/private/bar';",
errors: [
{
messageId: 'patternWithCustomMessage',
type: AST_NODE_TYPES.ExportNamedDeclaration,
},
],
options: [
{
patterns: [
{
allowTypeImports: true,
message: 'usage of import1 private modules not allowed.',
regex: 'import1/.*',
},
],
},
],
},
{
code: "import { foo } from 'import1/private-package';",
errors: [
{
messageId: 'patternWithCustomMessage',
type: AST_NODE_TYPES.ImportDeclaration,
},
],
options: [
{
patterns: [
{
allowTypeImports: true,
caseSensitive: true,
message: 'usage of import1 private modules not allowed.',
regex: 'import1/private-[a-z]*',
},
],
},
],
},
{
code: "export * from 'import1';",
errors: [
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/typings/eslint-rules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ declare module 'eslint/lib/rules/no-restricted-imports' {
// extended
allowTypeImports?: boolean;
caseSensitive?: boolean;
group: string[];
group?: string[];
regex?: string;
message?: string;
}[]
| string[];
Expand Down
Loading