Skip to content

Lightsaber is a Dagger 2 plugin that flags the unused dependencies declared in your Modules and Components

License

Notifications You must be signed in to change notification settings

SchwarzIT/dagger-lightsaber

Repository files navigation

Lightsaber

Lightsaber is a Dagger 2 plugin that detects unused code in your Modules, Components and Subcomponents

Works with

  • Dagger 2 (kapt, ksp and javac annotation processor)
  • Anvil (kapt)

What to expect

/path/module/com/example/MyComponent.java:6:8: e: The @BindsInstance `myInt` declared in `test.MyComponent` is not used. [UnusedBindsInstances]

This plugin contains several rules:

  • Empty @Component and @Subcomponent
  • Unused @BindsInstance
  • Unused @Provides or @Binds inside @Modules
  • Unused @Component(dependencies)
  • Unused @Inject
  • Unused member injection methods (Component.inject(Foo))
  • Unused @Modules
  • Unused Scopes (for example: @Singleton)

How to use it

Add the plugin to your project:

// build.gradle.kts
plugins {
    id("io.github.schwarzit.lightsaber") version "0.0.19"
}

And run ./gradlew lightsaberCheck. Lightsaber will check your code and fail if there is any issue.

Configuration

You can change that default for each rule from error to warnings or even ignore it completely like this:

import schwarz.it.lightsaber.gradle.Severity

lightsaber {
  emptyComponents = Severity.Error
  unusedBindsInstances = Severity.Error
  unusedBindsAndProvides = Severity.Error
  unusedDependencies = Severity.Error
  unusedInjects = Severity.Error
  unusedMembersInjection = Severity.Error
  unusedModules = Severity.Error
  unusedScopes = Severity.Error
}

Suppress

Lightsaber supports the @Suppress annotation. If you want to suppress an issue, you only have to add the @Suppress annotation above the reported element with the rule name. Example:

@Module
class MyModule {
    @Suppress("UnusedBindsAndProvides")
    @Provides
    fun providesPotatoes() = "potatoes"
}

How to build it

Clone the repo and execute:

./gradlew build

About

Lightsaber is a Dagger 2 plugin that flags the unused dependencies declared in your Modules and Components

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published