8000 fix(eslint-plugin): [no-restricted-imports] prevent crash when `patte… · StyleShit/typescript-eslint@675e987 · GitHub
[go: up one dir, main page]

Skip to content

Commit 675e987

Browse files
auvredStyleShit
andauthored
fix(eslint-plugin): [no-restricted-imports] prevent crash when patterns or paths in options are empty (typescript-eslint#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>
1 parent cb5bac8 commit 675e987

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

packages/eslint-plugin/src/rules/no-restricted-imports.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
import type {
99
ArrayOfStringOrObject,
1010
ArrayOfStringOrObjectPatterns,
11+
RuleListener,
1112
} from 'eslint/lib/rules/no-restricted-imports';
1213
import type { Ignore } from 'ignore';
1314
import ignore from 'ignore';
@@ -211,6 +212,21 @@ function getRestrictedPatterns(
211212
return [];
212213
}
213214

215+
function shouldCreateRule(
216+
baseRules: RuleListener,
217+
options: Options,
218+
): baseRules is Exclude<RuleListener, Record<string, never>> {
219+
if (Object.keys(baseRules).length === 0 || options.length === 0) {
220+
return false 10000 ;
221+
}
222+
223+
if (!isOptionsArrayOfStringOrObject(options)) {
224+
return !!(options[0].paths?.length || options[0].patterns?.length);
225+
}
226+
227+
return true;
228+
}
229+
214230
export default createRule<Options, MessageIds>({
215231
name: 'no-restricted-imports',
216232
meta: {
@@ -228,7 +244,7 @@ export default createRule<Options, MessageIds>({
228244
const rules = baseRule.create(context);
229245
const { options } = context;
230246

231-
if (options.length === 0) {
247+
if (!shouldCreateRule(rules, options)) {
232248
return {};
233249
}
234250

packages/eslint-plugin/tests/rules/no-restricted-imports.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,35 @@ import type { foo } from 'import2/private/bar';
322322
},
323323
],
324324
},
325+
{
326+
code: "import foo from 'foo';",
327+
options: [],
328+
},
329+
{
330+
code: "import foo from 'foo';",
331+
options: [
332+
{
333+
paths: [],
334+
},
335+
],
336+
},
337+
{
338+
code: "import foo from 'foo';",
339+
options: [
340+
{
341+
patterns: [],
342+
},
343+
],
344+
},
345+
{
346+
code: "import foo from 'foo';",
347+
options: [
348+
{
349+
paths: [],
350+
patterns: [],
351+
},
352+
],
353+
},
325354
],
326355
invalid: [
327356
{

packages/eslint-plugin/typings/eslint-rules.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,13 @@ declare module 'eslint/lib/rules/no-restricted-imports' {
10571057
allowTypeImports?: boolean;
10581058
}[]
10591059
| string[];
1060+
export type RuleListener =
1061+
| Record<string, never>
1062+
| {
1063+
ImportDeclaration(node: TSESTree.ImportDeclaration): void;
1064+
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration): void;
1065+
ExportAllDeclaration(node: TSESTree.ExportAllDeclaration): void;
1066+
};
10601067
}
10611068

10621069
interface ObjectOfPathsAndPatterns {
@@ -1074,11 +1081,7 @@ declare module 'eslint/lib/rules/no-restricted-imports' {
10741081
| 'patterns'
10751082
| 'patternWithCustomMessage',
10761083
rule.ArrayOfStringOrObject | [ObjectOfPathsAndPatterns],
1077-
{
1078-
ImportDeclaration(node: TSESTree.ImportDeclaration): void;
1079-
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration): void;
1080-
ExportAllDeclaration(node: TSESTree.ExportAllDeclaration): void;
1081-
}
1084+
rule.RuleListener
10821085
>;
10831086
export = rule;
10841087
}

0 commit comments

Comments
 (0)
0