Git Product home page Git Product logo

diktat's Introduction

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

diktat's People

Contributors

shyiko avatar shashachu avatar orchestr7 avatar petertrr avatar akuleshov7 avatar tapchicoma avatar jaredsburrows avatar jelloranger avatar yokotaso avatar yukukotani avatar gabrielittner avatar jlleitschuh avatar z3d1k avatar kentr0w avatar charbgr avatar mydogtom avatar ilya-gh avatar sowmyav24 avatar arturbosch avatar cable729 avatar nightlynexus avatar romtsn avatar bethcutler avatar yegor256 avatar shiraji avatar kunny avatar locitao avatar jlmd avatar sockeqwe avatar adammc331 avatar

Watchers

James Cloos 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.