From 6884f8eaf0d2342ac668b35635f2f77a717ac1eb Mon Sep 17 00:00:00 2001 From: Tadhg McDonald-Jensen Date: Thu, 3 Sep 2020 19:17:32 -0400 Subject: [PATCH 1/3] fix: counts nodes to avoid recursion --- .../src/rules/explicit-module-boundary-types.ts | 5 +++-- .../tests/rules/explicit-module-boundary-types.test.ts | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 5671e36dbdd..4080c396b51 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -106,6 +106,7 @@ export default util.createRule({ After it's finished traversing the AST, it then iterates through the list of found functions, and checks to see if any of them are part of a higher-order function */ + const alreadyVisited = new Set(); return { ExportDefaultDeclaration(node): void { @@ -309,11 +310,11 @@ export default util.createRule({ } } } - function checkNode(node: TSESTree.Node | null): void { - if (node == null) { + if (node == null || alreadyVisited.has(node)) { return; } + alreadyVisited.add(node); switch (node.type) { case AST_NODE_TYPES.ArrowFunctionExpression: diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index c79cc34a314..801667548cf 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -654,6 +654,11 @@ export abstract class Foo { ` export declare class Foo { set time(seconds: number); +} + `, + ` +class A { + b = A; } `, ], From 18ebd87e9721c4da27b3074e2e20aae91b989d44 Mon Sep 17 00:00:00 2001 From: Tadhg McDonald-Jensen Date: Thu, 3 Sep 2020 21:47:09 -0400 Subject: [PATCH 2/3] chore: moved declaration up. --- .../src/rules/explicit-module-boundary-types.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 4080c396b51..f4c73ef0d9b 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -96,6 +96,10 @@ export default util.createRule({ // tracks functions that were found whilst traversing const foundFunctions: FunctionNode[] = []; + // all nodes visited, avoids infinite recursion for cyclic references + // (such as class member referring to itself) + const alreadyVisited = new Set(); + /* # How the rule works: @@ -106,7 +110,6 @@ export default util.createRule({ After it's finished traversing the AST, it then iterates through the list of found functions, and checks to see if any of them are part of a higher-order function */ - const alreadyVisited = new Set(); return { ExportDefaultDeclaration(node): void { @@ -310,6 +313,7 @@ export default util.createRule({ } } } + function checkNode(node: TSESTree.Node | null): void { if (node == null || alreadyVisited.has(node)) { return; From 5b84a9c724055d9531bc147f66d6f08b29cdac6b Mon Sep 17 00:00:00 2001 From: Tadhg McDonald-Jensen Date: Fri, 4 Sep 2020 08:16:33 -0400 Subject: [PATCH 3/3] fix: forgot to put export in test case so it didn't actually check the failing case. --- .../tests/rules/explicit-module-boundary-types.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index 801667548cf..25b70794843 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -657,7 +657,7 @@ export declare class Foo { } `, ` -class A { +export class A { b = A; } `,