Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): Fix allowExpressions false positives in explicit-function-return-type and incorrect documentation #388

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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