Skip to content

Commit

Permalink
fix(eslint-plugin): [no-restricted-imports] prevent crash when `patte…
Browse files Browse the repository at this point in the history
…rns` 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>
  • Loading branch information
auvred and StyleShit committed Dec 29, 2023
1 parent cb5bac8 commit 675e987
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
18 changes: 17 additions & 1 deletion packages/eslint-plugin/src/rules/no-restricted-imports.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -211,6 +212,21 @@ function getRestrictedPatterns(
return [];
}

function shouldCreateRule(
baseRules: RuleListener,
options: Options,
): baseRules is Exclude<RuleListener, Record<string, never>> {
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<Options, MessageIds>({
name: 'no-restricted-imports',
meta: {
Expand All @@ -228,7 +244,7 @@ export default createRule<Options, MessageIds>({
const rules = baseRule.create(context);
const { options } = context;

if (options.length === 0) {
if (!shouldCreateRule(rules, options)) {
return {};
}

Expand Down
29 changes: 29 additions & 0 deletions packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts
Expand Up @@ -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: [
{
Expand Down
13 changes: 8 additions & 5 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -1057,6 +1057,13 @@ declare module 'eslint/lib/rules/no-restricted-imports' {
allowTypeImports?: boolean;
}[]
| string[];
export type RuleListener =
| Record<string, never>
| {
ImportDeclaration(node: TSESTree.ImportDeclaration): void;
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration): void;
ExportAllDeclaration(node: TSESTree.ExportAllDeclaration): void;
};
}

interface ObjectOfPathsAndPatterns {
Expand All @@ -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;
}

0 comments on commit 675e987

Please sign in to comment.