Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-restricted-imports] prevent crash when patterns or paths in options are empty #8108

Merged
merged 4 commits into from Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}