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

Run same rule multiple times with different configuration #7263

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BraisGabin
Copy link
Member

@BraisGabin BraisGabin commented May 6, 2024

With this PR we finally are able to run the same run multiple times with different configurations.

The most important part to discuss, I think, is if we like how those new instances are configured.

Right now if you want to create a new instance of the same rule you should add on your detekt.yml something like this:

styles:
  ForbiddenImport/compose:
    active: true
    ...

Close #6738

@cortinico
Copy link
Member

And it produces this output:

The output has rule ids like [ForbiddenImport/AtTestCode] while in your config you have ForbiddenImport/2 and /3. I assume that's a mistake

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

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

I really like this :D

@@ -1,3 +1,6 @@
config:
validation: false
Copy link
Member

Choose a reason for hiding this comment

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

Why this?

Copy link
Member Author

Choose a reason for hiding this comment

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

The validation code doesn't know about this type of config keys yet. I need to implement that. I disabled it just to make it work for now. As I said this is still not ready to merge. I just wanted to share it to show that this is near to be done.

@BraisGabin
Copy link
Member Author

And it produces this output:

The output has rule ids like [ForbiddenImport/AtTestCode] while in your config you have ForbiddenImport/2 and /3. I assume that's a mistake

Indeed. Copy&paste error. I fixed it now.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

detekt found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link

codecov bot commented Jun 21, 2024

Codecov Report

Attention: Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.

Project coverage is 84.50%. Comparing base (4fa634a) to head (41ea7be).

Files Patch % Lines
...otlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt 86.66% 0 Missing and 2 partials ⚠️
...io/gitlab/arturbosch/detekt/core/rules/RuleSets.kt 33.33% 1 Missing and 1 partial ⚠️
...fig/validation/InvalidPropertiesConfigValidator.kt 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7263      +/-   ##
============================================
- Coverage     84.50%   84.50%   -0.01%     
- Complexity     4160     4163       +3     
============================================
  Files           572      572              
  Lines         11890    11902      +12     
  Branches       2461     2465       +4     
============================================
+ Hits          10048    10058      +10     
- Misses          590      591       +1     
- Partials       1252     1253       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@BraisGabin
Copy link
Member Author

@detekt/maintainers This PR is ready to review and I think that this one should get multiple eyes on it.

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

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

LGTM 👍 We need docs for this also :)

@cortinico cortinico added the notable changes Marker for notable changes in the changelog label Jun 22, 2024
@@ -145,6 +154,11 @@ internal class Analyzer(
}
}

internal fun extractRuleName(key: String): Rule.Name? =
runCatching { Rule.Name(key.split("/", limit = 2).first()) }.getOrNull()
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to get better DX here? In what cases can this throw and return null? If I read it correctly it'll simply ignore the rule. Should we fail when config is incorrect rather than let the user scratch their head why things are "not working". I guess the Rule.Name validation was there before, but now the runCatching can fail due to 2 reasons. Consider to get back "original" behavior:

Suggested change
runCatching { Rule.Name(key.split("/", limit = 2).first()) }.getOrNull()
val name = key.split("/", limit = 2).first()
return runCatching { Rule.Name(name) }.getOrNull()

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we not allowing / in original rule id? Also instead of runCatching { Rule.Name(key.split("/", limit = 2).first()) }.getOrNull() can we do Rule.Name(key.split("/", limit = 2).firstOrNull()

active: true
WildcardImport:
active: true
WildcardImport/extra:
Copy link
Member

@TWiStErRob TWiStErRob Jun 23, 2024

Choose a reason for hiding this comment

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

(Probably the wrong place to flag it, but wanted to start a thread.)

Is this syntax suppress-compatible, i.e. can I suppress a specific instance of a rule? Can IDEA handle it?

Will this work and only suppress the extra instance?

@file:Suppress("WildcardImport/extra")

Will this work and suppress all the instances?

@file:Suppress("WildcardImport")

How can I suppress only the default instance, and not the extras?

@file:Suppress("WildcardImport/default")

(Same for baseline)

Copy link
Member Author

Choose a reason for hiding this comment

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

@file:Suppress("WildcardImport") This only suppress the default one (if it exists)

Copy link
Member

Choose a reason for hiding this comment

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

So there's no syntax to suppress all of them? (e.g. if there's 3 set up, and all 3 flag the same thing, need to list 3 IDs?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. It you can give them the same alias to those 3 and use that.

I don't see a real scenario where suppressing all the rules of the same name is a good idea. But if someone end up with an issue like that we could implement wildcards RuleName/* or something like that. But I prefer not to do that until a real use case appears.

@@ -62,6 +62,6 @@ interface RulesSpec {
/**
* Run a single rule.
*/
class RestrictToSingleRule(val ruleSetId: RuleSet.Id, val ruleName: Rule.Name) : RunPolicy()
class RestrictToSingleRule(val ruleSetId: RuleSet.Id, val ruleId: String) : RunPolicy()
Copy link
Member

@TWiStErRob TWiStErRob Jun 23, 2024

Choose a reason for hiding this comment

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

Could this be a value-class that is pre-split, or at least hides the complexity of split("/", 2).first() in a val ruleName: Rule.Name get() =?

Suggested change
class RestrictToSingleRule(val ruleSetId: RuleSet.Id, val ruleId: String) : RunPolicy()
class RestrictToSingleRule(val ruleSetId: RuleSet.Id, val ruleId: Rule.Id) : RunPolicy()

yamlConfigFromContent(
"""
custom:
MaxLineLength/foo:
Copy link
Contributor

Choose a reason for hiding this comment

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

one TC should be there with MaxLineLength/foo/bar and one with MaxLineLength/(no value after /) I think latter should be error in validation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api cli core notable changes Marker for notable changes in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the option to instantiate the same rule more than once
6 participants