From 675e987ca1d13244c03d7e09d4e42c6539689d9a Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Fri, 29 Dec 2023 22:59:24 +0300 Subject: [PATCH] fix(eslint-plugin): [no-restricted-imports] prevent crash when `patterns` or `paths` in options are empty (#8108) * fix(eslint-plugin): [no-restricted-imports] prevent crash when patterns or paths are empty * Update packages/eslint-plugin/src/rules/no-restricted-imports.ts Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com> * refactor: explicit zero-length check * revert: "refactor: explicit zero-length check" --------- Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com> --- .../src/rules/no-restricted-imports.ts | 18 +++++++++++- .../tests/rules/no-restricted-imports.test.ts | 29 +++++++++++++++++++ .../eslint-plugin/typings/eslint-rules.d.ts | 13 +++++---- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-restricted-imports.ts b/packages/eslint-plugin/src/rules/no-restricted-imports.ts index 1fbd584a120..b1e2d229f6a 100644 --- a/packages/eslint-plugin/src/rules/no-restricted-imports.ts +++ b/packages/eslint-plugin/src/rules/no-restricted-imports.ts @@ -8,6 +8,7 @@ import type { import type { ArrayOfStringOrObject, ArrayOfStringOrObjectPatterns, + RuleListener, } from 'eslint/lib/rules/no-restricted-imports'; import type { Ignore } from 'ignore'; import ignore from 'ignore'; @@ -211,6 +212,21 @@ function getRestrictedPatterns( return []; } +function shouldCreateRule( + baseRules: RuleListener, + options: Options, +): baseRules is Exclude> { + if (Object.keys(baseRules).length === 0 || options.length === 0) { + return false; + } + + if (!isOptionsArrayOfStringOrObject(options)) { + return !!(options[0].paths?.length || options[0].patterns?.length); + } + + return true; +} + export default createRule({ name: 'no-restricted-imports', meta: { @@ -228,7 +244,7 @@ export default createRule({ const rules = baseRule.create(context); const { options } = context; - if (options.length === 0) { + if (!shouldCreateRule(rules, options)) { return {}; } 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 fcbdcd625ca..0345cfa17ec 100644 --- a/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts @@ -322,6 +322,35 @@ import type { foo } from 'import2/private/bar'; }, ], }, + { + code: "import foo from 'foo';", + options: [], + }, + { + code: "import foo from 'foo';", + options: [ + { + paths: [], + }, + ], + }, + { + code: "import foo from 'foo';", + options: [ + { + patterns: [], + }, + ], + }, + { + code: "import foo from 'foo';", + options: [ + { + paths: [], + patterns: [], + }, + ], + }, ], invalid: [ { diff --git a/packages/eslint-plugin/typings/eslint-rules.d.ts b/packages/eslint-plugin/typings/eslint-rules.d.ts index 4490df6104b..5d301e04f2b 100644 --- a/packages/eslint-plugin/typings/eslint-rules.d.ts +++ b/packages/eslint-plugin/typings/eslint-rules.d.ts @@ -1057,6 +1057,13 @@ declare module 'eslint/lib/rules/no-restricted-imports' { allowTypeImports?: boolean; }[] | string[]; + export type RuleListener = + | Record + | { + ImportDeclaration(node: TSESTree.ImportDeclaration): void; + ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration): void; + ExportAllDeclaration(node: TSESTree.ExportAllDeclaration): void; + }; } interface ObjectOfPathsAndPatterns { @@ -1074,11 +1081,7 @@ declare module 'eslint/lib/rules/no-restricted-imports' { | 'patterns' | 'patternWithCustomMessage', rule.ArrayOfStringOrObject | [ObjectOfPathsAndPatterns], - { - ImportDeclaration(node: TSESTree.ImportDeclaration): void; - ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration): void; - ExportAllDeclaration(node: TSESTree.ExportAllDeclaration): void; - } + rule.RuleListener >; export = rule; }