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

Replace KtLintMultiRule #6094

Merged
merged 13 commits into from May 27, 2023

Conversation

marschwar
Copy link
Contributor

This relates to #5192 and removes the last MutliRule usage in the detekt code base.

Unfortunately I had to change the documentation generation mechanism here as well as it did not work when the rule list has a function call attached such as RuleSet("formatting", listOf(Rule1(config).sorted()).

@codecov
Copy link

codecov bot commented May 12, 2023

Codecov Report

Merging #6094 (0d76036) into main (6f166ec) will decrease coverage by 0.49%.
The diff coverage is 96.90%.

@@             Coverage Diff              @@
##               main    #6094      +/-   ##
============================================
- Coverage     84.94%   84.45%   -0.49%     
+ Complexity     4026     3988      -38     
============================================
  Files           569      568       -1     
  Lines         13498    13416      -82     
  Branches       2381     2366      -15     
============================================
- Hits          11466    11331     -135     
- Misses          869      935      +66     
+ Partials       1163     1150      -13     
Impacted Files Coverage Δ
...t/generator/collection/RuleSetProviderCollector.kt 78.88% <66.66%> (+0.47%) ⬆️
...arturbosch/detekt/formatting/FormattingProvider.kt 100.00% <100.00%> (ø)
...osch/detekt/formatting/FormattingRuleComparator.kt 100.00% <100.00%> (ø)
...sch/detekt/generator/collection/DetektCollector.kt 75.00% <100.00%> (-0.76%) ⬇️

... and 16 files with indirect coverage changes

@marschwar
Copy link
Contributor Author

Coming back to your comment @chao2zhang

The reason behind this is because there are internal orders to execute ktlint rules, hence we need to extract the ordering logic into Detekt's Analyzer.

Looking at the analyzer it seems to me that the execution order of the rules within a rule set is the same as the order returned by the rule set provider. I have not tried to write a test for it though. Do you think there needs to be some more work around that?

Comment on lines 31 to 33

@Configuration("if rules should auto correct style violation")
val autoCorrect by ruleSetConfig(true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This property was seemingly unused. What am I missing?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

So this Kotlin property is not used in production

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I have reverted the change.

@BraisGabin
Copy link
Member

This can be merged after 1.23.0, right? As it is a big change and it doesn't provide any feature for the user.

@marschwar
Copy link
Contributor Author

Yes, it is just a prerequisite for the deprecation removal and for whatever we end up going with the formatting rule set.

@BraisGabin BraisGabin added this to the 2.0.0 milestone May 13, 2023
Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

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

😍 LGTM!

@chao2zhang
Copy link
Member

Coming back to your comment @chao2zhang

The reason behind this is because there are internal orders to execute ktlint rules, hence we need to extract the ordering logic into Detekt's Analyzer.

Looking at the analyzer it seems to me that the execution order of the rules within a rule set is the same as the order returned by the rule set provider. I have not tried to write a test for it though. Do you think there needs to be some more work around that?

  • The ordering logic can be found in Analyzer - So the order is actually determined by FormattingProvider.kt's autoCorrect config.

  • Before this PR, all ktlint rules are supposed to run ahead of other detekt rules, simply because is io.gitlab.arturbosch.detekt.api.MultiRule -> rule.rules.any { it.autoCorrect }

  • For maximum compatibility, we should still consider maintaining this behavior though

* Currently only RunAsLateAsPossible is supported.
*/
internal object FormattingRuleComparator : Comparator<FormattingRule> {
override fun compare(o1: FormattingRule, o2: FormattingRule): Int {
Copy link
Member

Choose a reason for hiding this comment

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

Ktlint also defines VisitorModifier.RunAfterRule, and we should respect that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yap, that is described in #5259

Copy link
Member

Choose a reason for hiding this comment

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

😭. Would you mind if I contribute to this PR as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not mind at all. But I would not want to fix the RunAfterRule issue with this pr or do you see them as strongly connected?

Copy link
Member

Choose a reason for hiding this comment

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

It seems that we agreed that this PR can be merged post 1.23
Let me just open a new PR post the merge of this PR.

@marschwar
Copy link
Contributor Author

  • The ordering logic can be found in Analyzer - So the order is actually determined by FormattingProvider.kt's autoCorrect config.

  • Before this PR, all ktlint rules are supposed to run ahead of other detekt rules, simply because is io.gitlab.arturbosch.detekt.api.MultiRule -> rule.rules.any { it.autoCorrect }

  • For maximum compatibility, we should still consider maintaining this behavior though

Thank you for pointing this out. I can see why rules that might change the source code (autocorrect==true) should run first. That totally makes sense. But why must the other formatting rules run before all other rules? Honestly this feels more like a coincidence to me.

@chao2zhang chao2zhang force-pushed the feature/5192-multi-rule-formatting branch from 9f8bada to 0d76036 Compare May 27, 2023 06:05
@chao2zhang chao2zhang merged commit 2f31eb6 into detekt:main May 27, 2023
22 of 23 checks passed
@marschwar marschwar deleted the feature/5192-multi-rule-formatting branch May 27, 2023 21:37
mgroth0 pushed a commit to mgroth0/detekt that referenced this pull request Feb 11, 2024
* remove unused autoCorrect property

* WIP

* WIP

* fix documentation generation

* Allow multiple classes in rule set provider file

* remove multirule support in docs

* fix style and formatting violations

* revert removal of autocorrect prop

---------

Co-authored-by: Chao Zhang <chao.zhang@instacart.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants