Skip to content

Commit 3ec706f

Browse files
committed
feat(eslint-plugin): allow explicit variable type with arrow functions
Fixes typescript-eslint#149
1 parent 7be5657 commit 3ec706f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Diff for: packages/eslint-plugin/src/rules/explicit-function-return-type.ts

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ export default util.createRule<Options, MessageIds>({
6565
);
6666
}
6767

68+
/**
69+
* Checks if the parent of a function expression has a type annotation.
70+
* @param parent The parent of a function expression node
71+
*/
72+
function hasTypeAnnotation(parent: TSESTree.Node): boolean {
73+
return (
74+
parent.type === AST_NODE_TYPES.VariableDeclarator &&
75+
'typeAnnotation' in parent.id
76+
);
77+
}
78+
6879
/**
6980
* Checks if a function declaration/expression has a return type.
7081
* @param node The node representing a function.
@@ -80,6 +91,7 @@ export default util.createRule<Options, MessageIds>({
8091
node.parent &&
8192
!isConstructor(node.parent) &&
8293
!isSetter(node.parent) &&
94+
!hasTypeAnnotation(node.parent) &&
8395
util.isTypeScriptFile(context.getFilename())
8496
) {
8597
context.report({

Diff for: packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ var arrowFn = (): string => 'test';
3232
{
3333
filename: 'test.ts',
3434
code: `
35+
var arrowFn: Foo = () => 'test';
36+
`
37+
},
38+
{
39+
filename: 'test.ts',
40+
code: `
3541
class Test {
3642
constructor() {}
3743
get prop(): number {

0 commit comments

Comments
 (0)