From 3783e0304755c94c8193f08d677a51896546d82b Mon Sep 17 00:00:00 2001 From: Yauheni Date: Fri, 17 May 2024 11:23:53 +0200 Subject: [PATCH 1/2] Added motivation block to README.md --- README.md | 91 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 459add7..5a20f25 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,24 @@ [![](https://jitpack.io/v/Kiolk/Detekt-rules.svg)](https://jitpack.io/#Kiolk/Detekt-rules) ![Maven Central Version](https://img.shields.io/maven-central/v/io.github.kiolk/kiolk-detekt-rules) ![GitHub Release](https://img.shields.io/github/v/release/kiolk/detekt-rules?color=yellow) # 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! - +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.  +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 ```kotlin repositories { - mavenCentral() - } + 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 + +In `dependencies` block in `build.gradle.kt` of module where you will use detekt add reference on library that points on +the latest version + ```kotlin detektPlugins("io.github.kiolk:kiolk-detekt-rules:1.0.4") ``` @@ -29,30 +28,86 @@ To use Jitpack, you should add link on jitpack in repositories block in `build.g ```kotlin repositories { - maven { url 'https://jitpack.io' } - } + 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 + +In `dependencies` block in `build.gradle.kt` of module where you will use detekt add reference on library that points on +the latest version + ```kotlin 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](https://github.com/Kiolk/Detekt-rules/releases) of repository. +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](https://github.com/Kiolk/Detekt-rules/releases) of repository. + ```kotlin 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. ---- - -# Rules: +```yaml +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 +```kotlin +fun someFunction() { + val classWithMethodeInvoke = ClassWithMethodeInvoke() + classWithMethodeInvoke.invoke() +} +``` +When lambda expression execute +```kotlin +fun methodeWithNotNullableLambda(notNullableLambda: (() -> Unit)) { + notNullableLambda.invoke() +} +``` + +It does not trigger for nullable variable, because is not possible to call invoke without safety check + +```kotlin + fun methodeWithNullableLambda(nullableLambda: (() -> Unit)?) { + nullableLambda?.invoke() +} +``` +But triggers, if safety check is not necessary +```kotlin +fun methodeWithNotNullableLambdaButWithSaveCall(notNullableLambda: (() -> Unit)) { + notNullableLambda?.invoke() +} +``` + ### AutoCorrection +Replaces `invoke` on direct call. + +Before +```kotlin +fun methodeWithNotNullableLambda(callback: (Int, String) -> Unit) { + callback.invoke(3, "string") + } +``` +After +```kotlin +fun methodeWithNotNullableLambda(callback: (Int, String) -> Unit) { + callback(3, "string") + } +``` From 3bf57de6f9eacd72255e941219ccf12010e928b3 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Mon, 8 Jul 2024 10:06:02 +0200 Subject: [PATCH 2/2] Added some examples to README.md --- .github/workflows/create_github_release.yml | 1 - README.md | 3 +-- gradle.properties | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/create_github_release.yml b/.github/workflows/create_github_release.yml index 536892a..c448891 100644 --- a/.github/workflows/create_github_release.yml +++ b/.github/workflows/create_github_release.yml @@ -46,7 +46,6 @@ jobs: - name: Generate jar run: | ./gradlew jar - echo ${{ needs.check_version.outputs.versionName }} - uses: ncipollo/release-action@v1 with: tag: ${{ needs.check_version.outputs.versionName }} diff --git a/README.md b/README.md index 5a20f25..c927d6d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -[![](https://jitpack.io/v/Kiolk/Detekt-rules.svg)](https://jitpack.io/#Kiolk/Detekt-rules) ![Maven Central Version](https://img.shields.io/maven-central/v/io.github.kiolk/kiolk-detekt-rules) ![GitHub Release](https://img.shields.io/github/v/release/kiolk/detekt-rules?color=yellow) - +[![](https://jitpack.io/v/Kiolk/Detekt-rules.svg)](https://jitpack.io/#Kiolk/Detekt-rules) ![Maven Central Version](https://img.shields.io/maven-central/v/io.github.kiolk/kiolk-detekt-rules) ![GitHub Release](https://img.shields.io/github/v/release/kiolk/detekt-rules?color=yellow) ![Create Github Release](https://github.com/Kiolk/Detekt-rules/actions/workflows/create_github_release.yml/badge.svg) ![Publish to Maven Central Repository](https://github.com/Kiolk/Detekt-rules/actions/workflows/publish_to_maven_central.yml/badge.svg) # 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! diff --git a/gradle.properties b/gradle.properties index e2ace52..b377403 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ kotlin.code.style=official -versionName=1.0.4 +versionName=1.0.5 pomGroupId=io.github.kiolk pomDescription=Detekt rules pomUrl=https://github.com/Kiolk/Detekt-rules