Typescript cognitive complexity is 0 since January 2024

Hi,
Since January, the cognitive complexity of all our Typescript project dropped to 0 (or almost 0).


In some other project the cognitive complexity drop from 3K to ~100.
This happened in several project across several teams during the month of January.

Context:

  • All the projects are “single” language (ts, js)
  • It is all analyzed in the Github Action pipelines
  • We are using the official runner updated automatically using dependabot
  • No changes were done in the quality gates or configurations in sonarcloud.
  • Log of an example analysis:
    sonarcloud-analysis-log.txt (25.8 KB)

Does anybody know how to fix this?

Thank you in advance. I can gladly provide more information if required.

Hi,

Did any other metrics of the projects in question change significantly around the same time? Insignificantly?

 
Thx,
Ann

Hi G Ann Campbell, thank you for your questions.

Not really. Everything else looks normal, probably that is why it went so long unnoticed.

We are just very confused, because this happened in several projects around the same time. These projects are managed by different teams and each manage its pipelines and it sonarcloud configuration independently.

I wasn’t able to find much information in google. I found 2 issues reported in this forum, one about sonarcloud and another for sonarqube.

Thanks
Marcos

Hi Marcos,

Would you be able to share a sample file from one of these projects? Or if not, a reproducer - a file that should be non-0 but comes out at 0 instead?

 
Thx,
Ann

Hi G Ann Campbell,
Thank you for following up. I cannot share many of the files content, but I found an easy example where the complexity should be higher than 0:

    event.preventDefault();

    const oldStatus = courseTemplate.status;
    const statusUpdatedCourseTheme: CourseThemeStatusUpdateDataItem = {
      status: status,
    };

    courseTemplate.status = status;

    this.courseTemplateAdminService
      .update(courseTemplate.uuid, statusUpdatedCourseTheme)
      .pipe(takeUntil(this.ngOnDestroy$))
      .subscribe({
        next: (updatedCourseTemplate: CourseTemplate) => {
          if (updatedCourseTemplate.status === 'success') {
            const courseTemplates = this.courseTemplates$.value || [];
            for (let i = 0; i < courseTemplates.length; i++) {
              if (courseTemplates[i].uuid === updatedCourseTemplate.data.uuid) {
                courseTemplates[i] = updatedCourseTemplate.data;
              }
            }
            this.courseTemplates$.next(courseTemplates);
          } else {
            console.error(updatedCourseTemplate.message);
          }
          this.store.dispatch(notifyActions.notify({ title: updatedCourseTemplate.message }));
        },
        error: (error) => {
          console.error(error);
          this.store.dispatch(notifyActions.notify({ title: error.message }));
          courseTemplate.status = oldStatus;
        },
      });
  }

The code is part of templates/overview/overview.component.ts.

According to 5 Clean Code Tips for Reducing Cognitive Complexity this file should not be 0.

What do you think?

Hi,

Just to verify, when you analyze that file/snippet, it comes out to 0? (You didn’t actually say that, just that it should be non-0 :nerd_face:)

And now that we have a reproducer (:crossed_fingers:) I’m going to flag this for the language experts.

 
Ann

Hi,

Thank you for following up and sorry for the misunderstanding. Yes, the analysis says that all files (inclusive this one) has 0 complexity. The only file in the project that has anything greater than zero is the main.ts (that has 1). It is as if no file are really scanned.

Here is the screenshot of this file in sonarcloud.

Thank you in advance and have a great day.

1 Like

Hi @marcosfad,

Thanks for the logs, code sample and screenshot, we are looking at your issue and it helps a lot.

Is the Cyclomatic Complexity metric impacted too?

Could you also provide the date of the point, in your first graph, that is just after the 11th of January; the one where the value dropped for the first time to something close to 0?

Hi Eric,
Thank you the follow up. Only the cognitive complexity drop to 0.
I cannot give a better date, as in the project was not much activity during January and February. I post below some more detail graph.


I hope this help. In the meanwhile all our ts files are reporting 0 cognitive complexity. Even in mixed projects, the ts files are contributing 0 to the cognitive complexity. Just for the record, I’ve also set back all the changes in the quality gates to the sonar way, to avoid any problem coming from configurations.

I’m quite sure that there is a bug in the software. Just a few ideas that cross my mind, I don’t remember when came the update for the github action runner, but it might be related to that update (somehow I have in my memory that that happened around that time).

Look forward hearing from you.

1 Like

Hey Marcos,

Thank you for the feedback.

Indeed, we have introduced a change in how cognitive complexity (CC) in the rule S3776 was computed at the beginning of the year.

This had a side effect on the way we compute CC at a file level that we did not anticipate. We will dive into it and come back with a solution proposal.

Best,
Ilia

1 Like

I have created an ticket to track this issue here: [JS-255] - Jira