From 73a63ee9ea00b2db0a29f148d7863c3778e4a483 Mon Sep 17 00:00:00 2001 From: pvanagtmaal Date: Wed, 25 Nov 2020 21:24:51 +0100 Subject: [PATCH] fix(eslint-plugin): [explicit-module-boundary-types] ignore functions exported within typed object/array literals (#2805) --- .../rules/explicit-module-boundary-types.ts | 4 ++++ .../explicit-module-boundary-types.test.ts | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) 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 f4c73ef0d9b..2214f8fa343 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -390,6 +390,10 @@ export default util.createRule({ function ancestorHasReturnType(node: FunctionNode): boolean { let ancestor = node.parent; + if (ancestor?.type === AST_NODE_TYPES.Property) { + ancestor = ancestor.value; + } + // if the ancestor is not a return, then this function was not returned at all, so we can exit early const isReturnStatement = ancestor?.type === AST_NODE_TYPES.ReturnStatement; 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 25b70794843..b2906d17a98 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 @@ -661,6 +661,26 @@ export class A { b = A; } `, + ` +interface Foo { + f: (x: boolean) => boolean; +} + +export const a: Foo[] = [ + { + f: (x: boolean) => x, + }, +]; + `, + ` +interface Foo { + f: (x: boolean) => boolean; +} + +export const a: Foo = { + f: (x: boolean) => x, +}; + `, ], invalid: [ {