Skip to content

Commit

Permalink
Merge branch 'explicit-variable-type-arrow-function' of github.com:gi…
Browse files Browse the repository at this point in the history
…lbsgilbs/typescript-eslint into explicit-variable-type-arrow-function
  • Loading branch information
gilbsgilbs committed Feb 23, 2019
2 parents 9a88363 + 1ffc59d commit 28d6df6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -84,6 +84,20 @@ node.addEventListener('click', function() {});
const foo = arr.map(i => i * i);
```

### allowTypedFunctionExpressions

Examples of additional **correct** code for this rule with `{ allowTypedFunctionExpressions: true }`:

```ts
type FuncType = () => string;

let arrowFn: FuncType = () => 'test';

let funcExpr: FuncType = function() {
return 'test';
};
```

## When Not To Use It

If you don't wish to prevent calling code from using function return values in unexpected ways, then
Expand Down
24 changes: 24 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -4,6 +4,7 @@ import * as util from '../util';
type Options = [
{
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
}
];
type MessageIds = 'missingReturnType';
Expand All @@ -28,6 +29,9 @@ export default util.createRule<Options, MessageIds>({
allowExpressions: {
type: 'boolean',
},
allowTypedFunctionExpressions: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -36,6 +40,7 @@ export default util.createRule<Options, MessageIds>({
defaultOptions: [
{
allowExpressions: true,
allowTypedFunctionExpressions: false,
},
],
create(context, [options]) {
Expand All @@ -60,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 &&
!!parent.id.typeAnnotation
);
}

/**
* Checks if a function declaration/expression has a return type.
* @param node The node representing a function.
Expand Down Expand Up @@ -103,6 +119,14 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (
options.allowTypedFunctionExpressions &&
node.parent &&
hasTypeAnnotation(node.parent)
) {
return;
}

checkFunctionReturnType(node);
}

Expand Down
Expand Up @@ -92,6 +92,28 @@ function test() {
},
],
},
{
filename: 'test.ts',
code: `
var arrowFn: Foo = () => 'test';
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
},
{
filename: 'test.ts',
code: `
var funcExpr: Foo = function() { return 'test'; };
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -188,5 +210,29 @@ class Test {
},
],
},
{
filename: 'test.ts',
code: `var arrowFn = () => 'test';`,
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 1,
column: 15,
},
],
},
{
filename: 'test.ts',
code: `var funcExpr = function() { return 'test'; };`,
options: [{ allowTypedFunctionExpressions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 1,
column: 16,
},
],
},
],
});

0 comments on commit 28d6df6

Please sign in to comment.