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

Add exception to Cognitive Complexity for React forwardRef components #3777

Closed
timphamcmc opened this issue Mar 21, 2023 · 1 comment · Fixed by SonarSource/eslint-plugin-sonarjs#444

Comments

@timphamcmc
Copy link

timphamcmc commented Mar 21, 2023

consider React forwardRef component such as

const FancyButton = React.forwardRef((props, ref) => {
  const handleSomething = () => {
    if (something) {
      do something
    }
  }
  const handleSomethingElse = () => {
    if (somethingElse) {
      do somethingElse
    }
  }
  if (condition) {
    return sth
  }
  if (condition2) {
    return sth2
  }
  if (condition3) {
    return sth3
  }
  if (condition4) {
    return sth4
  }
  return (
    <button ref= { ref } className = "FancyButton" >
      { props.children }
      < /button>
  );
});

Currently, cognitive complexity of such component would be 15, while the equivalent component using class syntax and regular functional syntax will have only one. We should avoid adding complexity of nested functions to the outer function in such case.

@vartuom
Copy link

vartuom commented Oct 31, 2023

I'm developing a UI library and I have the same problem. Maybe there is some workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants