Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/no-restricted-imports.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionpackages/eslint-plugin/typings/eslint-rules.d.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp