Coder Social home page Coder Social logo

detekt-rules's Introduction

Maven Central Version GitHub Release Create Github Release Publish to Maven Central Repository

kiolk-detekt-rules

Hello folks. This repository contains my own Detekt rules that born in result my work how developer. I am planning to add new rules in the future. You are welcome to contribute!

How add to the project

You have different options how you can add this set of rules to your project.

Maven

To use Maven Central repository, you should add link on mavenCentral() in repositories block in build.gradle.kt file

repositories {
    mavenCentral()
}

In dependencies block in build.gradle.kt of module where you will use detekt add reference on library that points on the latest version

detektPlugins("io.github.kiolk:kiolk-detekt-rules:1.0.4")

Jitpack

To use Jitpack, you should add link on jitpack in repositories block in build.gradle.kt file

repositories {
    maven { url 'https://jitpack.io' }
}

In dependencies block in build.gradle.kt of module where you will use detekt add reference on library that points on the latest version

detektPlugins("com.github.Kiolk:Detekt-rules:v1.0.4")

Local artifacts

If you want to use only locally, you should add path to jar file on your local machine. Latest artifacts you can find in release section of repository.

detektPlugins(files("local_path_to_artifact.jar"))

Configuration

You can add this configuration of rule set to root detekt.yml file for more custom configuration.

kiolk-detekt-rules:
  active: true
  UseInvokeForOperator:
    active: true
    autoCorrect: true #is false by default

Rules:

UseInvokeForOperator

Motivation

If the class defines a function with name invoke and with keyword operator, it can execute this function by directly call with round brackets. This simplification makes code more readable. This rule detekt such cases and can replace it on direct call. It is applicable and for lambda functions.

Cases

When class defines operator invoke

fun someFunction() {
    val classWithMethodeInvoke = ClassWithMethodeInvoke()
    classWithMethodeInvoke.invoke()
}

When lambda expression execute

fun methodeWithNotNullableLambda(notNullableLambda: (() -> Unit)) {
    notNullableLambda.invoke()
}

It does not trigger for nullable variable, because is not possible to call invoke without safety check

 fun methodeWithNullableLambda(nullableLambda: (() -> Unit)?) {
    nullableLambda?.invoke()
}

But triggers, if safety check is not necessary

fun methodeWithNotNullableLambdaButWithSaveCall(notNullableLambda: (() -> Unit)) {
    notNullableLambda?.invoke()
}

AutoCorrection

Replaces invoke on direct call.

Before

fun methodeWithNotNullableLambda(callback: (Int, String) -> Unit) {
        callback.invoke(3, "string")
    }

After

fun methodeWithNotNullableLambda(callback: (Int, String) -> Unit) {
        callback(3, "string")
    }

detekt-rules's People

Contributors

yauhendaresay avatar kiolk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.