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

feat(linter): implement useExplicitFunctionReturnType #3990

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kaykdm
Copy link

@kaykdm kaykdm commented Sep 19, 2024

Summary

Related issue: #2017

This PR introduces the @typescript-eslint/explicit-function-return-type rule to Biome. Currently, it only enforces the basic cases without any additional options configured.

In future PRs, I will implement each option for this rule separately. Due to the complexity of the implementation, handling each option individually will allow for thorough testing and easier review.

original implementation

Todos

I will implement each option for this rule separately in different PRs

  • Implement allowExpressions option
  • Implement allowTypedFunctionExpression soption
  • Implement allowHigherOrderFunctions option
  • Implement allowDirectConstAssertionInArrowFunctions option
  • Implement allowConciseArrowFunctionExpressionsStartingWithVoid option
  • Implement allowFunctionsWithoutTypeParameters option
  • Implement allowedNames option
  • Implement allowIIFEs option

@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels Sep 19, 2024
Comment on lines +2 to +22
function test(): void {
return;
}

var fn = function (): number {
return 1;
};

var arrowFn = (): string => 'test';

class Test {
constructor() {}
get prop(): number {
return 1;
}
set prop() {}
method(): void {
return;
}
arrow = (): string => 'arrow';
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1 to +28
function test(a: number, b: number) {
return;
}

function test() {
return;
}

var fn = function () {
return 1;
};

var arrowFn = () => "test";

class Test {
constructor() {}
get prop() {
return 1;
}
set prop() {}
method() {
return;
}
arrow = () => "arrow";
private method() {
return;
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to have this rule!

/// Leaving off the return type is less code to read or write and allows the compiler to infer it from the contents of the function.
///
/// However, explicit return types do make it visually more clear what type is returned by a function.
/// They can also speed up TypeScript type checking performance in large codebases with many large functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to add that explicit return types also reduce the chance of bugs by asserting the return type and it avoids surprising "action at a distance", where changing the body of one function may cause failures inside another function.

/// return;
/// }
/// }
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a function without any explicit return statement? Is the type annotation required there too? If not, might be good to point out in the examples.

Copy link

codspeed-hq bot commented Sep 19, 2024

CodSpeed Performance Report

Merging #3990 will degrade performances by 6.15%

Comparing kaykdm:use-explicit-function-return-type (7d41348) with main (33d0958)

Summary

❌ 1 regressions
✅ 106 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main kaykdm:use-explicit-function-return-type Change
js_analyzer[lint_13640784270757307929.ts] 30.2 ms 32.2 ms -6.15%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants