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

Improve S3776: Exclude complexity of JSX attribute short-circuits #3289

Closed
gabriel-vivas-sonarsource opened this issue Aug 1, 2022 · 2 comments · Fixed by SonarSource/eslint-plugin-sonarjs#377
Assignees
Labels
mmf-2854 https://sonarsource.atlassian.net/browse/MMF-2854
Milestone

Comments

@gabriel-vivas-sonarsource
Copy link

gabriel-vivas-sonarsource commented Aug 1, 2022

Label Description
Rule S3776 "Cognitive Complexity of functions should not be too high"
FP Example * Short-circuits in JSX attribute values

Explanation

In JSX, it is fairly common to use short-circuit logic for conditionally rendering values. Currently, we count this as Cognitive Complexity.

See example:

See the dogwood thread (internal):

Suggestion

When counting the Cognitive Complexity, we should consider short circuits, nulls coalescing, and optional chaining as shorthand.

Propagating

  • something && something.prop
  • something.?prop

Coalescing

  • something || other
  • something ?? other

This is true for the following cases at least:

In JSX

  • JSX attribute values
  • JSX embedded JS expressions

Everywhere, not only in JSX

  • return statements
  • Assignments

Perhaps there are more cases where it makes sense.
Perhaps we don't need to count them in any place?

@gabriel-vivas-sonarsource gabriel-vivas-sonarsource added this to the 9.6 milestone Aug 1, 2022
@gabriel-vivas-sonarsource gabriel-vivas-sonarsource changed the title S3776 should not include the complexity of enclosed function definitions S3776 should exclude the complexity of JSX attribute short-circuits and nested function definitions Aug 1, 2022
@saberduck saberduck removed this from the 9.6 milestone Aug 9, 2022
@saberduck
Copy link
Contributor

we didn't manage to do this in current sprint, removing from milestone

@gabriel-vivas-sonarsource gabriel-vivas-sonarsource added this to the 9.10 milestone Oct 12, 2022
@gabriel-vivas-sonarsource
Copy link
Author

See some of the comments in #2238
As people keep coming to this subject.

@vilchik-elena vilchik-elena changed the title S3776 should exclude the complexity of JSX attribute short-circuits and nested function definitions Improve S3776: exclude complexity of JSX attribute short-circuits and nested function definitions Oct 18, 2022
@vilchik-elena vilchik-elena changed the title Improve S3776: exclude complexity of JSX attribute short-circuits and nested function definitions Improve S3776: Exclude complexity of JSX attribute short-circuits and nested function definitions Oct 18, 2022
@vilchik-elena vilchik-elena changed the title Improve S3776: Exclude complexity of JSX attribute short-circuits and nested function definitions Improve S3776: Exclude complexity of JSX attribute short-circuits Oct 27, 2022
@ilia-kebets-sonarsource ilia-kebets-sonarsource removed this from the 9.10 milestone Oct 31, 2022
@ilia-kebets-sonarsource ilia-kebets-sonarsource added this to the 9.12 milestone Nov 24, 2022
@gabriel-vivas-sonarsource gabriel-vivas-sonarsource added mmf-2854 https://sonarsource.atlassian.net/browse/MMF-2854 and removed mmf-2835 labels Nov 28, 2022
@francoismora francoismora self-assigned this Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mmf-2854 https://sonarsource.atlassian.net/browse/MMF-2854
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants