Tslint: Enable/Disable switch cases from the cyclomatic complexity rule

Created on 4 Jan 2019  ยท  6Comments  ยท  Source: palantir/tslint

Enable/Disable switch cases from the cyclomatic complexity rule

It would be great to be able to disable switches from being taken into account for cyclomatic complexity, specially

Imagine the following: you have to render an icon depending on a status prop on a pure function. This piece of code can render up to as many icons as the application requires for it's component statuses.

switch(this.status) {
 case State.Idle:
        return <IdleIcon />
 case State.Active:
        return <ActiveIcon/>
 (...)
 case State.Deleted:
        return <DeletedIcon/>
}

You're now left with a couple of options:

  • Create helper functions to satisfy the linter (i.e this.renderActiveOrIdle, pretty annoying)
  • Add a disabler comment to workaround the linter
  • Keep increasing the cyclomatic complexity threshold each time this issue pops up

I think these kind of switches are really easy to visualize. That said, I understand that more complex switches could be the reason to negate the request. Maybe adding a character threshold or line threshold between cases could suffice?

We have the threshold at 6 and it is often the case that we add the disabler comment (though we only allow it for switches).

At least in our case we tend to use switches for quick conditionals only, such as the case above. Any chance this could be a feature in the future?

Rule Enhancement ๐ŸŒน R.I.P. ๐ŸŒน

Most helpful comment

4489 had an interesting configuration proposal (@mouadennaciri):

   "cyclomatic-complexity": [
      true,
      10,
      { "ignoredClauses": ["caseClause"] }
    ]

All 6 comments

This is an interesting request. Would it be reasonable to also add options for other control flow constructs such as while loops?

Marking as in discussion to gather more community feedback. ๐Ÿ˜Š

I'm guessing the more granular the better, maybe having the ability to set the default weight of each thing is enough. Say we can set the switch cases to have a cyclomatic complexity of 0 and such.

4489 had an interesting configuration proposal (@mouadennaciri):

   "cyclomatic-complexity": [
      true,
      10,
      { "ignoredClauses": ["caseClause"] }
    ]

I think the proposed config is great and solves the problem we were having. What other clauses (other than cases and ifs) could be ignored on that ignoredClauses field?

Maybe having a compoundConditionals (A && B && C) could help so we can make those scenarios count as 1?

โ˜ ๏ธ TSLint's time has come! โ˜ ๏ธ

TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. โœจ

It was a pleasure open sourcing with you all!

๐Ÿค– Beep boop! ๐Ÿ‘‰ TSLint is deprecated ๐Ÿ‘ˆ _(#4534)_ and you should switch to typescript-eslint! ๐Ÿค–

๐Ÿ”’ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings