-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add exception to Cognitive Complexity for React functional components #2238
Comments
+1 I want to mention another rule that is closely related in this case, which is "Functions should not have too many lines of code". React functional components sometimes tend to break this rule because they tend to contain many nested functions within, whereas an equivalent class component would not trigger the same issue. If possible, it would be nice to address this together with the complexity issue for functional components. |
The functional component is still a function. I don't think you need to make an exception to it. What is the rule for making exceptions? What about reducers, hooks, etc.? they could also easier get long as well. |
In JavaScript /react a class component is just a faux name space for a function. So yes this rule should treat all components similar ( because now the class component can now be written as a functional component with closures). ( the complexity of closures increasing the complexity of enclosing function only in functional component and not in class component is not right) Hence this rule of treating all components similar should be present for react.js Here’s an excerpt from the whitepaper
"So as not to penalize JavaScript users, such outer functions are ignored " hence , you should also treat the functional components(outer functions) in a similar way ( ignore it) |
@saberduck Is this issue still on any roadmap to be corrected? Functional components, and hooks, are becoming the de facto standard for modern React applications. It doesn't make sense to penalize code quality and weigh negative scores on modern coding standards, compared to using class based components. |
I second that @arevi, I'm finding that the negative scores on functional components are actually causing worse code quality and less maintainable code. |
Note that we already would not report if top level function does not have any constructs increasing the complexity. Still it's possible to have Example in real project of such component with some complexity inside of the top level function |
When adding this exception I will apply following criteria:
It would be great if someone from community could confirm that those criteria are making sense for real life projects. |
Hi @vilchik-elena , thank you for working on this issue! I'm an end-user of the SonarJS plugin and those criteria would work for all of my projects. |
Hello @vilchik-elena, i just updated SonarQube to version 9.0 and I still get this error message. For deploying the project to SonarQube I am using sonar-scanner (2.8.0). Could you please check it? |
@GeorgeBalat if you are returning JSX fragment then the fix is not yet released (SonarSource/eslint-plugin-sonarjs#245) |
Thank you for the fast response. Can I change now something to pass this rule? Maybe another return type (example please)? |
@GeorgeBalat just won't fix the issue or mark it as false positive. Fix is part of analyzer release but I can't tell you yet when it will be available in SQ. |
Okay, thank you for the info. |
Hi there! I can see that JSX fix for this is released. |
@kirillito yep, it applies to tsx as well. Create a new issue with your reproducer |
Hi @vilchik-elena |
@GeorgeBalat latest :) I am not sure which version exactly, but the recommendation is anyway to rely on LTS or latest. |
Hello @vilchik-elena , it looks like it is still not fixed in 9.3 |
@GeorgeBalat Could you provide full definition of react component on which issue is reported? |
Hello Elena, |
@GeorgeBalat whole code of this function |
|
@GeorgeBalat I need at least full signature and return statement(s). Also note that even if it's react component, complexity of code not nested inside inner function will be computed function ChatBoardComponent(props: ChatBoardComponentProps) {
function handleSomethingElse() {
if(something){
do something
}
if(something){} // if there are 20 statements like this, we will report on the component function
return <p>Hello world</p>;
} |
@vilchik-elena |
install SonarLint to have instant analysis (if not yet) and just try to simplify your code (extract some logic into functions external to it). Also don't forget that you can simply 'won't fix' the issue if you believe the simplification does not make sense. |
@vilchik-elena ok, thank you. I will try it out. |
does this exception not apply to Preact ? |
Consider React functional component such as
Currently, cognitive complexity of such component would be 7, while the equivalent component using class syntax will have only one. We should avoid adding complexity of nested functions to the outer function in such case.
See thread on community forum https://community.sonarsource.com/t/sonarqube-counts-incorrect-cognitive-complexity-for-react-functional-components-bug-no-functionality/30852
The text was updated successfully, but these errors were encountered: