Skip to content
/ diKTat Public
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

kentr0w/diKTat

 
 

Repository files navigation

Build and test deteKT static analysis Releases License

FOSSA Status Awesome Kotlin Badge ktlint Hits-of-Code codecov

Chat on Telegram

(!) See diKTat codestyle first.

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.

  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.0/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.

Maven Plugin

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>1.8</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/**/*.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 -->
              </dependency>
              <dependency>
                  <groupId>org.cqfn.diktat</groupId>
                  <artifactId>diktat-rules</artifactId>
                  <version>1.0.0</version> <!-- replace it with diktat latest version -->
              </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.

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."
}

See default configuration in rules-config.json

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.1%
  • Other 0.9%