mihon icon indicating copy to clipboard operation
mihon copied to clipboard

Adding Detekt in the project

Open theolm opened this issue 2 years ago • 6 comments

Description:

This pull request aims to enhance the quality of our source code by replacing the ktlint code formatter with Detekt. Detekt is a powerful static analysis tool for Kotlin, providing comprehensive analyses to improve code readability and maintainability. Here are some of the benefits of using this tool:

  1. Comprehensive Static Analysis:

    • Detekt goes beyond basic formatting, offering more extensive static analyses to identify code patterns, anti-patterns, and other issues that can impact code quality.
  2. Flexible Configuration:

    • Detekt allows flexible configuration, adapting to the specific needs of our project. I already added all the ktlint rules and Compose rules so we don't need to rely on other linters.
  3. Integration with Android Studio:

    • Detekt provides direct integration with Android Studio via a plugin, enhancing the development experience for our Kotlin-based Android projects. When setting up the plugin, please don't forget to use our baseline and config, otherwise it will not work properly.
  4. Baseline for Existing Errors:

    • A baseline has been added to prevent detection of existing errors, allowing for a smoother transition. This ensures that the current codebase's issues are not flagged immediately.

Other changes

Workflow Updates:

  • Existing workflows have been updated to incorporate the changes related to Detekt integration, ensuring a seamless integration into our development processes.

Future work

In a subsequent PR, the baseline will be removed, and existing errors will be addressed. This phased approach allows us to adopt Detekt gradually without overwhelming the team with immediate corrections.

How to Test:

  1. Ensure the environment is correctly configured after merging this PR.
  2. Run ./gradlew detekt to ensure Detekt's static analyses are executed successfully.

More info:

Here is a report for all the issues found by the tool.

report.zip

Please let me know what you guys think.

theolm avatar Jan 23 '24 22:01 theolm

Is detekt being applied to all modules?

AntsyLich avatar Jan 24 '24 04:01 AntsyLich

The plugin is applied only to the app module, but the configuration is set to check files across the entire project (all the modules). I wrote a pre-compiled plugin with the detekt configuration. buildSrc/src/main/kotlin/detekt.gradle.kts

private val projectSource = file("$rootDir")
private val configFile = files("$rootDir/config/detekt/detekt.yml")
private val baselineFile = file("$rootDir/config/detekt/baseline.xml")
private val kotlinFiles = "**/*.kt"
private val resourceFiles = "**/resources/**"
private val buildFiles = "**/build/**"
private val generatedFiles = "**/generated/**"
private val scriptsFiles = "**/*.kts"

Basically, the tool will start in the projectSource and recursively look for files that match **/*.kt.

include(kotlinFiles)
exclude(resourceFiles, buildFiles, generatedFiles, scriptsFiles)

theolm avatar Jan 24 '24 12:01 theolm

Can you like make a convention plugin and apply it to per module? Don't really want to run the whole task with :app

AntsyLich avatar Jan 24 '24 12:01 AntsyLich

@AntsyLich Sure, I can add it pre-module. I just don't see the reason for using a convention plugin. do you have any strong feelings about it? I would rather modify my Kotlin script and apply it in the subprojects (like it was with the ktlint).

I just realized that the pre-compiled plugin that I wrote is, in fact, a convention plugin. I was under the idea that the convention plugin was the old plugins written in groovy.

theolm avatar Jan 24 '24 15:01 theolm

I plan to migrate everything to convention plugin

AntsyLich avatar Jan 24 '24 15:01 AntsyLich

@AntsyLich Let me know what you think.

theolm avatar Jan 24 '24 16:01 theolm

@AntsyLich Please ping me before merging this PR. Several PRs have been merged since I opened this one, so I would like to update the baseline before the merge. This will avoid problems.

theolm avatar Jan 28 '24 17:01 theolm

I may have forgotten to ask you to rebase. Do that and ping me I'll merge it afterwards

AntsyLich avatar Jan 28 '24 17:01 AntsyLich

@AntsyLich Done.

theolm avatar Jan 28 '24 19:01 theolm

Optionally, it is possible to slim down the configuration file to only the changes from the default configuration, by applying the buildUponDefaultConfig option

We don't need that huge ass yml file do we? Also use https://github.com/mrmans0n/compose-rules (https://mrmans0n.github.io/compose-rules/detekt)

AntsyLich avatar Jan 28 '24 19:01 AntsyLich

@AntsyLich Ok, I'll replace the Compose rule set. about the config file, are you sure you to slim down the file? I found very useful to have all the rules listed.

theolm avatar Jan 28 '24 20:01 theolm

it's fine to slim down the config file. chances are we will just keep following the default

AntsyLich avatar Jan 28 '24 20:01 AntsyLich

@AntsyLich Done.

theolm avatar Jan 28 '24 20:01 theolm

According to https://mrmans0n.github.io/compose-rules/detekt/ you need to enable some rules 🤔 one of them also seem to supersede FunctionNaming if I'm not wrong

AntsyLich avatar Jan 28 '24 20:01 AntsyLich

@AntsyLich The plugin rules are enabled by default after applying the plugin. I trimmed down the config files removing all the rules with default values (as you asked).

Here is the generated config file after applying the plugin. Screenshot 2024-01-28 at 6 24 16 PM

You can also see that the baseline has several compose-rules on it.

theolm avatar Jan 28 '24 21:01 theolm

Any reason why you didn't use detekt v1.23.4?

AntsyLich avatar Jan 28 '24 21:01 AntsyLich