From 8651c89776aa0c867e9443e99bb08cc8b8ebc0ad Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 30 Jan 2022 14:51:31 +0800 Subject: [PATCH 1/2] fix(eslint-plugin): [no-restricted-imports] allow relative type imports --- .../src/rules/no-restricted-imports.ts | 5 ++++- .../tests/rules/no-restricted-imports.test.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-restricted-imports.ts b/packages/eslint-plugin/src/rules/no-restricted-imports.ts index ff2f8d77b4e3..bb78bfdeda0c 100644 --- a/packages/eslint-plugin/src/rules/no-restricted-imports.ts +++ b/packages/eslint-plugin/src/rules/no-restricted-imports.ts @@ -146,7 +146,10 @@ export default createRule({ typeof restrictedPattern === 'object' && restrictedPattern.allowTypeImports ) { - allowedImportTypeMatchers.push(ignore().add(restrictedPattern.group)); + // allowRelativePaths is true in the base rule + allowedImportTypeMatchers.push( + ignore({ allowRelativePaths: true }).add(restrictedPattern.group), + ); } } function isAllowedTypeImportPattern(importSource: string): boolean { 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 8e4ea58c55af..1758365e37dc 100644 --- a/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts @@ -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: [ { From 73ef8a2733edfe52fbea7d6b7955a8cc0a994b0f Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 30 Jan 2022 14:55:34 +0800 Subject: [PATCH 2/2] refactor: add caseSensitive typedef --- packages/eslint-plugin/src/rules/no-restricted-imports.ts | 7 +++++-- packages/eslint-plugin/typings/eslint-rules.d.ts | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-restricted-imports.ts b/packages/eslint-plugin/src/rules/no-restricted-imports.ts index bb78bfdeda0c..fd724c5fc8b5 100644 --- a/packages/eslint-plugin/src/rules/no-restricted-imports.ts +++ b/packages/eslint-plugin/src/rules/no-restricted-imports.ts @@ -146,9 +146,12 @@ export default createRule({ typeof restrictedPattern === 'object' && restrictedPattern.allowTypeImports ) { - // allowRelativePaths is true in the base rule + // Following how ignore is configured in the base rule allowedImportTypeMatchers.push( - ignore({ allowRelativePaths: true }).add(restrictedPattern.group), + ignore({ + allowRelativePaths: true, + ignoreCase: !restrictedPattern.caseSensitive, + }).add(restrictedPattern.group), ); } } diff --git a/packages/eslint-plugin/typings/eslint-rules.d.ts b/packages/eslint-plugin/typings/eslint-rules.d.ts index 5f6e1412f333..d53ef43a747f 100644 --- a/packages/eslint-plugin/typings/eslint-rules.d.ts +++ b/packages/eslint-plugin/typings/eslint-rules.d.ts @@ -905,6 +905,7 @@ declare module 'eslint/lib/rules/no-restricted-imports' { | { group: string[]; message?: string; + caseSensitive?: boolean; // extended allowTypeImports?: boolean; }[];