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

Refactored filterLineCoverage() and filterBranchCoverage() in renderer.ts #406

Merged
merged 4 commits into from
May 22, 2023

Conversation

MylesJP
Copy link
Contributor

@MylesJP MylesJP commented May 7, 2023

Description

Cleanup some to-dos around using more modern typescript array helpers.

Work

  • Used .filter() to reduce number of if-statements and consolidated if-statements where possible.
  • Used .some() rather than .find() to determine if match exists, since we only need boolean return.
  • Potentially faster since .some() returns after first match.

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

@MylesJP
Copy link
Contributor Author

MylesJP commented May 9, 2023

Interesting that the Windows build of this failed. It seems to pass all tests when I run 'npm run test' on my local machine after installing all dependencies so I think the logic with my change is correct. Any thoughts as to why that is?

@ryanluker
Copy link
Owner

Interesting that the Windows build of this failed. It seems to pass all tests when I run 'npm run test' on my local machine after installing all dependencies so I think the logic with my change is correct. Any thoughts as to why that is?

Sometimes pulling the vscode image from microsoft's website can fail, and it causes 1/3 build pipelines to implode.
Usually I just hit the retry button as the CI minutes are free for open source projects (afaik hah).

@@ -116,22 +116,19 @@ export class Renderer {
if (!section || !section.lines) {
return;
}
// TODO cleanup this area by using maps, filters, etc
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling these to-dos!

section.branches.details.forEach((detail) => {
if (detail.taken === 0) {
if (detail.line < 0) { return; }
section.branches.details.filter((detail) => detail.taken === 0 && detail.line >= 0).forEach((detail) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this filter, but we might want to put it on multiple lines to aid in readability (if possible).

  section.branches.details
  .filter((detail) => detail.taken === 0 && detail.line >= 0)
  .forEach((detail) => {
      const partialRange = new Range(detail.line - 1, 0, detail.line - 1, 0);
      // Evaluates to true if at least one element in range is equal to partialRange
      if (coverageLines.full.some((range) => range.isEqual(partialRange))){
          coverageLines.full = coverageLines.full.filter((range) => !range.isEqual(partialRange));
          coverageLines.partial.push(partialRange);
      }
  });

// only add a none coverage if no full ones exist
coverageLines.none.push(lineRange);
}
section.lines.details.filter((detail) => detail.line >= 0).forEach((detail) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here as below (around re-ability and a small tweak to the indent)

  section.lines.details
  .filter((detail) => detail.line >= 0)
  .forEach((detail) => {
      const lineRange = new Range(detail.line - 1, 0, detail.line - 1, 0);
      if (detail.hit > 0) {
          // Evaluates to true if at least one element in range is equal to LineRange
          if (coverageLines.none.some((range) => range.isEqual(lineRange))) {
              coverageLines.none = coverageLines.none.filter((range) => !range.isEqual(lineRange))
          }
          coverageLines.full.push(lineRange);
      } else {
          if (!coverageLines.full.some((range) => range.isEqual(lineRange))) {
              // only add a none coverage if no full ones exist
              coverageLines.none.push(lineRange);
          }
      }
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for the feedback Ryan. I'll make those changes right away.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks! I will try to take a look within the week.

Copy link
Owner

@ryanluker ryanluker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some initial thoughts and cleaned up the PR body for yeah!

@ryanluker
Copy link
Owner

Looking great, thanks for the contribution @MylesJP!

@ryanluker ryanluker added this to the 2.11.0 milestone May 22, 2023
@ryanluker ryanluker merged commit 2e5f78e into ryanluker:master May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants