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 a sanity check for rules that should be mapped #4350

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

yassin-kammoun-sonarsource
Copy link
Contributor

No description provided.

@yassin-kammoun-sonarsource yassin-kammoun-sonarsource force-pushed the sanity-check branch 4 times, most recently from c338692 to fee37a8 Compare November 1, 2023 13:46
Copy link

sonarqube-next bot commented Nov 1, 2023

Quality Gate passed Quality Gate passed

Kudos, no new issues are introduced in this PR!

0 New issues
0 Security Hotspots
No Coverage information No data about Coverage
0.0% 0.0% Duplication on New Code

See analysis details on SonarQube

Comment on lines +36 to +39
const { rule } = require(path.join(ruleFolder, sonarKey));
if (!mappedRules.some(mapped => mapped === rule)) {
missing.push(sonarKey);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why wouldn't this be enough?

Suggested change
const { rule } = require(path.join(ruleFolder, sonarKey));
if (!mappedRules.some(mapped => mapped === rule)) {
missing.push(sonarKey);
}
if (!mappedRules[sonarKey]) {
missing.push(sonarKey);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

The check we want to implement is that we don't forget to add the rule redefinition/decoration/whatever, so it's the case where we:

  1. write it, and its units tests in packages/src/ rules/Sxxxx/
  2. forget to load it in packages/src/rules/index.ts

No?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I was saying in my comment, we are not able to do what you are suggesting because the keys in the mapping aren't Sonar keys:

rules['accessor-pairs'] = S2376;
rules['anchor-has-content'] = S6827;
rules['anchor-precedence'] = S5850;

We don't have rules['S2376'] = S2376; but rules['accessor-pairs'] = S2376; instead. This is why we need to check the other way around.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks for the explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're welcome :)


describe('index', () => {
it('should map keys to rules definitions', () => {
// FIXME: This test runs with a time complexity of O(n^2) where n is the number of rules.
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it O(n)?

  • gather sonarKeys: n
  • gather mappedRules: n
  • find missing: n

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The current implementation is O(n^2) since we iterate with the for-loop O(n) over all the rules available in the folder rules/SXXX, and for each of them, we look for a matching mapped definition with mappedRules.some(mapped => mapped === rule)) O(n).

Copy link
Contributor

Choose a reason for hiding this comment

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

indeed

Copy link
Contributor

@ilia-kebets-sonarsource ilia-kebets-sonarsource left a comment

Choose a reason for hiding this comment

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

I think you can simplify it as per my comments. I'm open to discuss it if you think we need a more thorough check, but I don't see what it brings, except checking that require works.

Copy link
Contributor

@ilia-kebets-sonarsource ilia-kebets-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM!

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