Skip to content

Commit

Permalink
fix(eslint-plugin): Fix allowExpressions false positives in explici…
Browse files Browse the repository at this point in the history
…t-function-return-type and incorrect documentation (#388)

Fixes #387
  • Loading branch information
gilbsgilbs authored and bradzacher committed Mar 28, 2019
1 parent cebcfe6 commit f29d1c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
Expand Up @@ -62,9 +62,11 @@ class Test {
The rule accepts an options object with the following properties:

- `allowExpressions` if true, only functions which are part of a declaration will be checked
- `allowTypedFunctionExpressions` if true, type annotations are also allowed on the variable
of a function expression rather than on the function directly.

By default, `allowExpressions: false` is used, meaning all declarations and
expressions _must_ have a return type.
By default, `allowExpressions: false` and `allowTypedFunctionExpressions: false` are used,
meaning all declarations and expressions _must_ have a return type.

### allowExpressions

Expand Down
22 changes: 11 additions & 11 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -39,7 +39,7 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: [
{
allowExpressions: true,
allowExpressions: false,
allowTypedFunctionExpressions: false,
},
],
Expand Down Expand Up @@ -88,6 +88,16 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
if (
options.allowExpressions &&
node.type !== AST_NODE_TYPES.FunctionDeclaration &&
node.parent &&
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
node.parent.type !== AST_NODE_TYPES.MethodDefinition
) {
return;
}

if (
!node.returnType &&
node.parent &&
Expand All @@ -112,16 +122,6 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
if (
options.allowExpressions &&
node.type !== AST_NODE_TYPES.ArrowFunctionExpression &&
node.parent &&
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
node.parent.type !== AST_NODE_TYPES.MethodDefinition
) {
return;
}

if (
options.allowTypedFunctionExpressions &&
node.parent &&
Expand Down
Expand Up @@ -54,6 +54,7 @@ function test() {
`,
},
{
filename: 'test.ts',
code: `fn(() => {});`,
options: [
{
Expand All @@ -62,6 +63,7 @@ function test() {
],
},
{
filename: 'test.ts',
code: `fn(function() {});`,
options: [
{
Expand All @@ -70,6 +72,7 @@ function test() {
],
},
{
filename: 'test.ts',
code: `[function() {}, () => {}]`,
options: [
{
Expand All @@ -78,6 +81,7 @@ function test() {
],
},
{
filename: 'test.ts',
code: `(function() {});`,
options: [
{
Expand All @@ -86,6 +90,7 @@ function test() {
],
},
{
filename: 'test.ts',
code: `(() => {})();`,
options: [
{
Expand Down Expand Up @@ -193,6 +198,20 @@ class Test {
},
],
},
{
filename: 'test.ts',
code: `function test() {
return;
}`,
options: [{ allowExpressions: true }],
errors: [
{
messageId: 'missingReturnType',
line: 1,
column: 1,
},
],
},
{
filename: 'test.ts',
code: `const foo = () => {};`,
Expand Down

0 comments on commit f29d1c9

Please sign in to comment.