Skip to content
forked from saveourtool/diktat

Code style and rule set for automatic fixing and checking of Kotlin code

License

Notifications You must be signed in to change notification settings

aivinog1/diKTat

 
 

Repository files navigation

Build and test deteKT static analysis diKTat code style

Releases License Hits-of-Code codecov

FOSSA Status Awesome Kotlin Badge ktlint Chat on Telegram

(!) See diKTat codestyle first.

(!) Have a look at maven and gradle examples of usage diKTat with plugins

DiKTat is a collection of Kotlin code style rules implemented as AST visitors on top of KTlint. The full list of available supported rules and inspections is here.

Run as CLI-application

  1. Install KTlint (until this PR is merged you will need to use KTlint fork):

    $ curl -sSLO https://central.artipie.com/akuleshov7/files/ktlint && chmod a+x ktlint
  2. Load diKTat manually: here

    OR use curl:

    $ curl -sSLO https://github.com/cqfn/diKTat/releases/download/v1.0.2/diktat.jar
  3. Finally, run KTlint (with diKTat injected) to check your *.kt files in dir/your/dir:

    $ ./ktlint -R diktat.jar "dir/your/dir/**/*.kt"

To autofix all violations use -F option.

Run with Maven Plugin

You can see how it is configured in our project for self-checks: pom.xml

First, add this to your pom.xml file:

<project>
  [...]
  <repositories>
    <repository>
      <id>artipie</id>
      <url>https://central.artipie.com/akuleshov7/diktat</url>
    </repository>
  </repositories>
    <pluginRepositories>
      <pluginRepository>
        <id>artipie</id>
        <url>https://central.artipie.com/akuleshov7/diktat</url>
      </pluginRepository>
    </pluginRepositories>
</project>

Then, add this plugin:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
              <execution>
                  <id>diktat</id>
                  <phase>none</phase>
                  <configuration>
                      <target name="ktlint">
                          <java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
                                classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
                              <arg value="src/main/**/*.kt"/>
                              <arg value="src/test/kotlin/**/*.kt"/>
                          </java>
                      </target>
                  </configuration>
                  <goals>
                      <goal>run</goal>
                  </goals>
              </execution>
          </executions>
          <dependencies>
              <dependency>
                  <groupId>com.pinterest</groupId>
                  <artifactId>ktlint</artifactId>
                  <version>0.37.1-fork</version> <!-- use this fork to be compatible with diktat -->
                  <exclusions>
                      <exclusion>  <!-- without this exclusion both rulesets are enabled which we discourage -->
                          <groupId>com.pinterest.ktlint</groupId>
                          <artifactId>ktlint-ruleset-standard</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <dependency>
                  <groupId>org.cqfn.diktat</groupId>
                  <artifactId>diktat-rules</artifactId>
                  <version>1.0.2</version> <!-- replace it with diktat latest version -->
                  <exclusions>
                      <exclusion>
                          <groupId>org.slf4j</groupId>
                          <artifactId>slf4j-log4j12</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
          </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

In case you want to add autofixer with diKTat ruleset just extend the snippet above with <arg value="-F"/>.

To run diktat to check/fix code style - run $ mvn antrun:run@diktat.

Run with Gradle Plugin

You can see how it is configured in our project for self-checks: build.gradle.kts. Add the code below to your build.gradle.kts:

val ktlint by configurations.creating

repositories {
    // artipie - an open source project that is used to store ktlint and diktat dependencies
    maven {
        url = uri("https://central.artipie.com/akuleshov7/diktat")
    }
    mavenCentral()
    jcenter()


}
dependencies {
    ktlint("com.pinterest:ktlint:0.37.1-fork") {
        // need to exclude standard ruleset to use only diktat rules
        exclude("com.pinterest.ktlint", "ktlint-ruleset-standard")
    }

    // diktat ruleset
    ktlint("org.cqfn.diktat:diktat-rules:1.0.2") {
        exclude("org.slf4j", "slf4j-log4j12")
    }
}

val outputDir = "${project.buildDir}/reports/diktat/"
val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))

val diktatCheck by tasks.creating(JavaExec::class) {
    inputs.files(inputFiles)
    outputs.dir(outputDir)

    description = "Check Kotlin code style."
    classpath = ktlint
    main = "com.pinterest.ktlint.Main"

    // specify proper path to sources that should be checked here
    args = listOf("src/main/kotlin/**/*.kt")
}

val diktatFormat by tasks.creating(JavaExec::class) {
    inputs.files(inputFiles)
    outputs.dir(outputDir)

    description = "Fix Kotlin code style deviations."
    classpath = ktlint
    main = "com.pinterest.ktlint.Main"

    // specify proper path to sources that should be checked here
    args = listOf("-F", "src/main/kotlin/**/*.kt")
}

To run diktat to check/fix code style - run $ gradle diktatCheck.

Customizations via rules-config.json

In KTlint, rules can be configured via .editorconfig, but this does not give a chance to customize or enable/disable each and every rule independently. That is why we have supported rules-config.json that can be easily changed and help in customization of your own rule set. It has simple fields: name — name of the rule, enabled (true/false) — to enable or disable that rule, and configuration — a simple map of some extra unique configurations for the rule. For example:

"configuration": {
  "isCopyrightMandatory": true,
  "copyrightText": "Copyright (c) Jeff Lebowski, 2012-2020. All rights reserved."
}

Note, that you can specify and put rules-config.json that contains configuration of diktat in the parent directory of your project on the same level where build.gradle/pom.xml is stored.
See default configuration in rules-config.json
Also see the list of all rules supported by diKTat.

How to contribute?

Main components are:

  1. diktat-ruleset — number of rules that are supported by diKTat;
  2. diktat-test-framework — functional/unit test framework that can be used for running your code fixer on the initial code and compare it with the expected result;
  3. also see our demo: diktat-demo in a separate repository.

Mainly we wanted to create a common configurable mechanism that will give us a chance to enable/disable and customize all rules. That's why we added logic for:

  1. Parsing .json file with configurations of rules and passing it to visitors;
  2. Passing information about properties to visitors. This information is very useful, when you are trying to get, for example, a filename of file where the code is stored;
  3. We added a bunch of visitors that will extended KTlint functionaliity.

Before you make a pull request, make sure the build is clean:

$ mvn clean install

Also see our Contributing Policy and Code of Conduct

About

Code style and rule set for automatic fixing and checking of Kotlin code

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 99.6%
  • Other 0.4%