This repository contains Detekt custom rules and configuration that are based on Deloitte Digital's Kotlin Style Guide.
Make sure to add detekt to your project by applying the following configuration to your app-level build.gradle file:
plugins {
id("io.gitlab.arturbosch.detekt").version("1.19.0")
}
-
Copy the jar file (
build/libs/dd-detekt-custom-rules-1.0.jar
) from this repo. -
Go to the project folder of the project where you want to use these custom rules.
-
In your project folder, create a
detekt
folder and paste the jar file here. -
Navigate to the app-level build.gradle file and add the jar file as dependency:
dependencies { detektPlugins files("$rootDir/detekt/dd-detekt-custom-rules-1.0.jar") }
-
Copy
detekt.yml
file from the this repo. -
Go to the project folder of the project where you want to use the custom configuration and paste detekt.yml file inside the detekt folder.
-
Navigate to the app-level build.gradle file and apply the following configuration:
detekt { config = files("$rootDir/detekt/detekt.yml") }
- If you're using the Detekt Plugin on your IDE, go to Preferences > Tools > Detekt.
- Set Plugin Jars to the path of your jar file and Configuration Files to the path of your configuration file.
-
Create a branch from master and follow this naming convention for your branch:
rule/your-custom-rule
(E.g.rule/collapsible-if-statements
). -
From the project folder, create your custom rules inside
src/main/kotlin/com/dd/detektcustomrules/rules
. -
Create a test for your custom rule.
-
Add the new rule to
DDRuleSetProvider
. -
Open detekt.yml and under custom, add your custom rule and set active to true
custom: MyRule: active: true
-
Commit and push your changes.
-
Create a Pull Request.
-
Once your Request is merged, run
.gradlew build
in your terminal. This should create a jar file.
- Use the visit method that gets you the closest to the PSI node that you want to check for violation. E.g. If your custom rule checks function names, use visitNamedFunction instead of visitDeclaration.
- Although we can't avoid traversing through a PSI node, it's always best to keep the number of loops/iteration over a collection as minimal as possible to prevent performance overhead.
- When calling the report method to report a violation, make sure to use a message that best describes the violation and add some information that will help the developers solve it.
- When writing tests, make sure all possible scenarios are covered.
- You can find the documentation about how to write custom rules here.
- Kotlin PSI Classes - link here
- Sample Detekt Rules - link here