From 3ec706f0bebff653c1ca130f018f32d794be63bb Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Tue, 12 Feb 2019 11:02:46 +0100 Subject: [PATCH] feat(eslint-plugin): allow explicit variable type with arrow functions Fixes #149 --- .../src/rules/explicit-function-return-type.ts | 12 ++++++++++++ .../rules/explicit-function-return-type.test.ts | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts index 0a7a0db73995..7c2931cc7dea 100644 --- a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts +++ b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts @@ -65,6 +65,17 @@ export default util.createRule({ ); } + /** + * Checks if the parent of a function expression has a type annotation. + * @param parent The parent of a function expression node + */ + function hasTypeAnnotation(parent: TSESTree.Node): boolean { + return ( + parent.type === AST_NODE_TYPES.VariableDeclarator && + 'typeAnnotation' in parent.id + ); + } + /** * Checks if a function declaration/expression has a return type. * @param node The node representing a function. @@ -80,6 +91,7 @@ export default util.createRule({ node.parent && !isConstructor(node.parent) && !isSetter(node.parent) && + !hasTypeAnnotation(node.parent) && util.isTypeScriptFile(context.getFilename()) ) { context.report({ 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 3348942e4d06..9851a376d625 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 @@ -32,6 +32,12 @@ var arrowFn = (): string => 'test'; { filename: 'test.ts', code: ` +var arrowFn: Foo = () => 'test'; + ` + }, + { + filename: 'test.ts', + code: ` class Test { constructor() {} get prop(): number {