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: add option to skip arrow function for explicit function return … #4233

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

Conversation

nazarhussain
Copy link

@nazarhussain nazarhussain commented Oct 10, 2024

Summary

This PR introduce new option allowArrowFunctions to the lint/nursary/useExplicitFunctionReturnType rule.

In larger code base there are a lot of arrow functions used for a lot of inline functionality, mostly to overcome need of passing a function instead of a value. e.g. () => object.attr. Specifying explicit return type for such arrow functions is tedious to do and don't provide a of help, as the TS can infer the return type easily.

This option is similar to allowExpressions option for @typescript-eslint/explicit-function-return-type but not identical.

Test Plan

Tested manually by building binary and testing against a sample code.

Could not find a way to set custom option in the test-docs for specific rule, so could not add new test cases.

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Oct 10, 2024
@ematipico
Copy link
Member

@nazarhussain thank you for your contribution.

We would appreciate it if you'd get involved first with the relative issue: #2017

The issue was assigned to another contributor, which already implemented your use case: #4036

@Conaclos
Copy link
Member

We recently renamed the rule to useExplicitTypes, you will have to update your code. Also, we should not add any options. We could just extend the rule.

Copy link

codspeed-hq bot commented Oct 29, 2024

CodSpeed Performance Report

Merging #4233 will improve performances by 7.11%

Comparing nazarhussain:nh/explicit-return-type (dc0c38b) with main (92286ab)

Summary

⚡ 1 improvements
✅ 96 untouched benchmarks

Benchmarks breakdown

Benchmark main nazarhussain:nh/explicit-return-type Change
react.production.min_3378072959512366797.js[cached] 2 ms 1.9 ms +7.11%

Comment on lines -94 to -95
const x = { prop: () => {} }
const x = { bar: { prop: () => {} } }
Copy link
Member

Choose a reason for hiding this comment

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

These examples should be still invalid.

Comment on lines +375 to +379
/// interface Behavior {
/// attribute: string;
/// func: () => string;
/// arrowFunc: () => string;
/// }
Copy link
Member

Choose a reason for hiding this comment

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

This example seems not related to what we check. We could remove it.

Comment on lines +381 to +387
/// function getObjectWithFunction(): Behavior {
/// return {
/// attribute: 'value',
/// func: function myFunc(): string { return "value" },
/// arrowFunc: () => {},
/// }
/// }
Copy link
Member

Choose a reason for hiding this comment

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

Code example should be in code block.

Suggested change
/// function getObjectWithFunction(): Behavior {
/// return {
/// attribute: 'value',
/// func: function myFunc(): string { return "value" },
/// arrowFunc: () => {},
/// }
/// }
/// ```ts
/// function getObjectWithFunction(): Behavior {
/// return {
/// attribute: 'value',
/// func: function myFunc(): string { return "value" },
/// arrowFunc: () => {},
/// }
/// }
/// ```

Comment on lines +390 to +393
matches!(
func.syntax().parent().kind(),
Some(JsSyntaxKind::JS_PROPERTY_OBJECT_MEMBER)
)
Copy link
Member

Choose a reason for hiding this comment

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

This is not enough because this will accept cases we want to reject.
For example, the following case should be rejected:

const x = {
  prop: () => {}
}

In contrast, the following could be accepted:

const X: Type  = {
  prop: () => {}
};
f({  prop: () => {} })

Comment on lines +396 to +399
/// Check if a function is an arrow function
fn is_arrow_func(func: &AnyJsFunction) -> bool {
func.as_js_arrow_function_expression().is_some()
}
Copy link
Member

Choose a reason for hiding this comment

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

This check is to permissive. This accepts too much code.

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

Successfully merging this pull request may close these issues.

3 participants