Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f29d1c9

Browse files
gilbsgilbsbradzacher
authored andcommitted
fix(eslint-plugin): Fix allowExpressions false positives in explicit-function-return-type and incorrect documentation (typescript-eslint#388)
Fixes typescript-eslint#387
1 parent cebcfe6 commit f29d1c9

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

packages/eslint-plugin/docs/rules/explicit-function-return-type.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ class Test {
6262
The rule accepts an options object with the following properties:
6363

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

66-
By default, `allowExpressions: false` is used, meaning all declarations and
67-
expressions _must_ have a return type.
68+
By default, `allowExpressions: false` and `allowTypedFunctionExpressions: false` are used,
69+
meaning all declarations and expressions _must_ have a return type.
6870

6971
### allowExpressions
7072

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default util.createRule<Options, MessageIds>({
3939
},
4040
defaultOptions: [
4141
{
42-
allowExpressions: true,
42+
allowExpressions: false,
4343
allowTypedFunctionExpressions: false,
4444
},
4545
],
@@ -88,6 +88,16 @@ export default util.createRule<Options, MessageIds>({
8888
| TSESTree.FunctionDeclaration
8989
| TSESTree.FunctionExpression,
9090
): void {
91+
if (
92+
options.allowExpressions &&
93+
node.type !== AST_NODE_TYPES.FunctionDeclaration &&
94+
node.parent &&
95+
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
96+
node.parent.type !== AST_NODE_TYPES.MethodDefinition
97+
) {
98+
return;
99+
}
100+
91101
if (
92102
!node.returnType &&
93103
node.parent &&
@@ -112,16 +122,6 @@ export default util.createRule<Options, MessageIds>({
112122
| TSESTree.FunctionDeclaration
113123
| TSESTree.FunctionExpression,
114124
): void {
115-
if (
116-
options.allowExpressions &&
117-
node.type !== AST_NODE_TYPES.ArrowFunctionExpression &&
118-
node.parent &&
119-
node.parent.type !== AST_NODE_TYPES.VariableDeclarator &&
120-
node.parent.type !== AST_NODE_TYPES.MethodDefinition
121-
) {
122-
return;
123-
}
124-
125125
if (
126126
options.allowTypedFunctionExpressions &&
127127
node.parent &&

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function test() {
5454
`,
5555
},
5656
{
57+
filename: 'test.ts',
5758
code: `fn(() => {});`,
5859
options: [
5960
{
@@ -62,6 +63,7 @@ function test() {
6263
],
6364
},
6465
{
66+
filename: 'test.ts',
6567
code: `fn(function() {});`,
6668
options: [
6769
{
@@ -70,6 +72,7 @@ function test() {
7072
],
7173
},
7274
{
75+
filename: 'test.ts',
7376
code: `[function() {}, () => {}]`,
7477
options: [
7578
{
@@ -78,6 +81,7 @@ function test() {
7881
],
7982
},
8083
{
84+
filename: 'test.ts',
8185
code: `(function() {});`,
8286
options: [
8387
{
@@ -86,6 +90,7 @@ function test() {
8690
],
8791
},
8892
{
93+
filename: 'test.ts',
8994
code: `(() => {})();`,
9095
options: [
9196
{
@@ -193,6 +198,20 @@ class Test {
193198
},
194199
],
195200
},
201+
{
202+
filename: 'test.ts',
203+
code: `function test() {
204+
return;
205+
}`,
206+
options: [{ allowExpressions: true }],
207+
errors: [
208+
{
209+
messageId: 'missingReturnType',
210+
line: 1,
211+
column: 1,
212+
},
213+
],
214+
},
196215
{
197216
filename: 'test.ts',
198217
code: `const foo = () => {};`,

0 commit comments

Comments
 (0)