Skip to content

Commit

Permalink
feat(eslint-plugin): allow explicit variable type with arrow functions
Browse files Browse the repository at this point in the history
Fixes #149
  • Loading branch information
gilbsgilbs committed Feb 12, 2019
1 parent 7be5657 commit 3ec706f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -65,6 +65,17 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* 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.
Expand All @@ -80,6 +91,7 @@ export default util.createRule<Options, MessageIds>({
node.parent &&
!isConstructor(node.parent) &&
!isSetter(node.parent) &&
!hasTypeAnnotation(node.parent) &&
util.isTypeScriptFile(context.getFilename())
) {
context.report({
Expand Down
Expand Up @@ -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 {
Expand Down

0 comments on commit 3ec706f

Please sign in to comment.