Skip to content

Commit

Permalink
fix(eslint-plugin): [no-restricted-imports] allow relative type impor…
Browse files Browse the repository at this point in the history
…ts with patterns configured (#4494)

* fix(eslint-plugin): [no-restricted-imports] allow relative type imports

* refactor: add caseSensitive typedef
  • Loading branch information
Josh-Cena authored Jan 30, 2022
1 parent 41c6474 commit 4a6d217
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/eslint-plugin/src/rules/no-restricted-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ export default createRule<Options, MessageIds>({
typeof restrictedPattern === 'object' &&
restrictedPattern.allowTypeImports
) {
allowedImportTypeMatchers.push(ignore().add(restrictedPattern.group));
// Following how ignore is configured in the base rule
allowedImportTypeMatchers.push(
ignore({
allowRelativePaths: true,
ignoreCase: !restrictedPattern.caseSensitive,
}).add(restrictedPattern.group),
);
}
}
function isAllowedTypeImportPattern(importSource: string): boolean {
Expand Down
14 changes: 14 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 @@ -217,6 +217,20 @@ ruleTester.run('no-restricted-imports', rule, {
code: "export * from 'foo';",
options: ['import1'],
},
{
code: "import type { MyType } from './types';",
options: [
{
patterns: [
{
group: ['fail'],
message: "Please do not load from 'fail'.",
allowTypeImports: true,
},
],
},
],
},
],
invalid: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ declare module 'eslint/lib/rules/no-restricted-imports' {
| {
group: string[];
message?: string;
caseSensitive?: boolean;
// extended
allowTypeImports?: boolean;
}[];
Expand Down

0 comments on commit 4a6d217

Please sign in to comment.