Skip to content

kmpbits/detektCustomRulesDemoKMP

Repository files navigation

Detekt Custom Rules Demo (KMP)

This project demonstrates how to implement and integrate custom Detekt rules into a Kotlin Multiplatform (KMP) project. It specifically focuses on enforcing Design System constraints to ensure consistency across the codebase.

🚀 Purpose

The goal of this demo is to show how static analysis can prevent developers from bypassing the design system (e.g., using hardcoded colors, spacing, or typography) and guide them toward using the project's official tokens.

🛠 Custom Rules

The project includes a :detekt-rules module with the following custom rules under the design-system-rules ruleset:

Rule ID Description Recommended Alternative
DirectColorUsage Prohibits direct use of Color literals or constructors (e.g., Color.Red, Color(0xFF...)). MaterialTheme.colorScheme
DirectSpacingUsage Prohibits hardcoded .dp values for layout spacing. Project spacing tokens
DirectTextStyleUsage Prohibits hardcoded .sp values or TextStyle constructor calls. MaterialTheme.typography

Suppressing Rules

If a specific line requires a deviation from the design system, you can use the // design-ignore comment:

val customPadding = 13.dp // design-ignore

📂 Project Structure

  • /composeApp: Shared Compose Multiplatform code for Android and iOS.
  • /detekt-rules: The Kotlin module containing the custom Detekt rule implementations and tests.
  • /iosApp: iOS-specific entry point and SwiftUI code.
  • detekt.yml: Configuration file where the custom rules are enabled and configured.

🔍 Running Detekt

To run detekt and check for design system violations:

./gradlew :composeApp:detekt

🛠 Setup & Configuration

Enabling the Ruleset

The custom rules are registered in DesignSystemRuleSetProvider.kt and enabled in the root detekt.yml:

design-system-rules:
  active: true
  DirectColorUsage:
    active: true
  DirectSpacingUsage:
    active: true
    allowedValues: ['0', '1', '2']
  DirectTextStyleUsage:
    active: true

Adding to your project

To use these rules in the :composeApp module (or any other module), they are added as a detektPlugins dependency in the build.gradle.kts:

dependencies {
    detektPlugins(project(":detekt-rules"))
}

📱 Build and Run

Android

  • Use the Android Studio run configuration or run:
    ./gradlew :composeApp:assembleDebug

iOS

  • Open the iosApp directory in Xcode or use the IDE's run configuration or change the configuration on Android Studio to iosApp and run.

Built with Kotlin Multiplatform and Compose Multiplatform.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors