-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
feat(linter): implement useExplicitFunctionReturnType #3990
Conversation
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'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I've added that point to the description!
/// return; | ||
/// } | ||
/// } | ||
/// ``` |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
Yes it is required, but I think it's nice to include it in the example. I added it to the invalid example!
CodSpeed Performance ReportMerging #3990 will degrade performances by 6.5%Comparing Summary
Benchmarks breakdown
|
Great stuff! Do you want to create a CHANGELOG entry? |
Hi! Thanks for the good stuff!
We usually refrain from implementing a rule's options unless they are widely used in the community. The number of options provided by Taking a look at GitHub Code search:
Without taking account that some of these occurrences actually set the default value... Moreover, I would like that Thus, I think we should not implement any option for this rule.
|
crates/biome_js_analyze/src/lint/nursery/use_explicit_function_return_type.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_explicit_function_return_type.rs
Outdated
Show resolved
Hide resolved
I totally agree with this. I implemented the same rule in Oxc Linter, but it was quite painful and I ran into some issues. That being said, I’m happy to work on these options you mentioned in separate PRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM :)
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
TodosI will implement each option for this rule separately in different PRsImplement allowExpressions optionImplement allowTypedFunctionExpression soptionImplement allowHigherOrderFunctions optionImplement allowDirectConstAssertionInArrowFunctions optionImplement allowConciseArrowFunctionExpressionsStartingWithVoid optionImplement allowFunctionsWithoutTypeParameters optionImplement allowedNames optionImplement allowIIFEs option