From 55e1fbaca985b500cad1cc9ec25717b18cf5a17b Mon Sep 17 00:00:00 2001 From: Anton Kalmanovich Date: Mon, 15 Mar 2021 10:13:03 +0200 Subject: [PATCH] fix(eslint-plugin): [explicit-module-boundary-types] fixes #2864 related to functions in nested object properties (#3178) --- .../src/util/explicitReturnTypeUtils.ts | 6 ++-- .../explicit-function-return-type.test.ts | 28 +++++++++++++++++++ .../explicit-module-boundary-types.test.ts | 28 +++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index cd2f0b11826..c05d6c6dab3 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -52,11 +52,12 @@ function isConstructorArgument( } /** - * Checks if a node belongs to: + * Checks if a node is a property or a nested property of a typed object: * ``` * const x: Foo = { prop: () => {} } * const x = { prop: () => {} } as Foo * const x = { prop: () => {} } + * const x: Foo = { bar: { prop: () => {} } } * ``` */ function isPropertyOfObjectWithType( @@ -82,7 +83,8 @@ function isPropertyOfObjectWithType( isTypeAssertion(parent) || isClassPropertyWithTypeAnnotation(parent) || isVariableDeclaratorWithTypeAnnotation(parent) || - isFunctionArgument(parent) + isFunctionArgument(parent) || + isPropertyOfObjectWithType(parent) ); } diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index 6ce8210822b..4faf3fa18b7 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -156,6 +156,34 @@ const x = { code: ` const x: Foo = { foo: () => {}, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + // https://github.com/typescript-eslint/typescript-eslint/issues/2864 + { + filename: 'test.ts', + code: ` +const x = { + foo: { bar: () => {} }, +} as Foo; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +const x = { + foo: { bar: () => {} }, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +const x: Foo = { + foo: { bar: () => {} }, }; `, options: [{ allowTypedFunctionExpressions: true }], 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 3652ebc876a..e05f4f09230 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 @@ -181,6 +181,34 @@ export const x = { code: ` export const x: Foo = { foo: () => {}, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + // https://github.com/typescript-eslint/typescript-eslint/issues/2864 + { + filename: 'test.ts', + code: ` +export const x = { + foo: { bar: () => {} }, +} as Foo; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +export const x = { + foo: { bar: () => {} }, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +export const x: Foo = { + foo: { bar: () => {} }, }; `, options: [{ allowTypedFunctionExpressions: true }],