<commit_before># __detekt__

[![build status](https://travis-ci.org/arturbosch/detekt.svg?branch=master)](https://travis-ci.org/arturbosch/detekt)
[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin)
[ ![Download](https://api.bintray.com/packages/arturbosch/code-analysis/detekt/images/download.svg) ](https://bintray.com/arturbosch/code-analysis/detekt/_latestVersion)

Meet _detekt_, a static code analysis tool for the _Kotlin_ programming language.
It operates on the abstract syntax tree provided by the Kotlin compiler.

![detekt in action](img/detekt_in_action.png "detekt in action")

### Features

- code smell analysis for your kotlin projects
- complexity report based on logical lines of code, McCabe complexity and amount of code smells
- highly configurable (rule set or rule level)
- suppress findings with Kotlin's @Suppress and Java's @SuppressWarnings annotations
- specify code smell thresholds to break your build or print a warning
- extensible by own rule sets and `FileProcessListener's`
- format your code with the formatting rule set
- code Smell baseline and ignore lists for legacy projects
- **NEW** - [gradle plugin](#gradleplugin) for code analysis, formatting and import migration


### Table of contents
1. [Usage/Build](#build)
2. [Parameters for CLI](#cli)
3. [As gradle plugin](#gradleplugin)
3. [As gradle task](#gradle)
4. [As maven task](#maventask)
5. [Rule sets](#rulesets)
6. [Rule set configuration](#rulesetconfig)
7. [Suppress rules](#suppress)
7. [Build failure](#failure)
7. [Custom rule sets](#customruleset)
8. [Rule testing](#testing)
9. [Formatting - Code Style](#formatting)
10. [Black- and Whitelist code smells](#baseline)

### <a name="build">Usage/Build</a>

##### Building all submodules ( + executables)

- cd detekt
- gradle clean build (install shadow)

##### Using the command line interface

- cd detekt-cli
- gradle shadow
- java -jar build/libs/detekt-cli-[version]-all.jar

#### <a name="cli">Parameters for CLI</a>
The CLI uses jcommander for argument parsing.

```
The following option is required: --project, -p

Usage: detekt [options]
  Options:
    --baseline, -b
      Treats current analysis findings as a smell baseline for further detekt
      runs. If a baseline xml file exists, only new code smells not in the
      baseline are printed in the console.
      Default: false
    --config, -c
      Path to the config file (path/to/config.yml).
    --config-resource, -cr
      Path to the config resource on detekt's classpath (path/to/config.yml).
    --debug, -d
      Debugs given ktFile by printing its elements.
      Default: false
    --disableDefaultRuleSets, -dd
      Disables default rule sets.
      Default: false
    --filters, -f
      Path filters defined through regex with separator ';' (".*test.*").
    --format
      Enables formatting of source code. Cannot be used together with
      --config.
      Default: false
    --help, -h
      Shows the usage.
    --output, -o
      True if findings should be written into a report.detekt file inside the
      report folder.
      Default: false
    --parallel
      Enables parallel compilation of source files. Should only be used if the
      analyzing project has more than ~200 kotlin files.
      Default: false
  * --project, -p
      Project path to analyze (path/to/project).
    --report, -rp
      Path to the report directory where findings should be stored (if
      --output) and baseline.xml generated (if --baseline).
    --rules, -r
      Extra paths to ruleset jars separated by ';'.
    --useTabs
      Tells the formatter that indentation with tabs are valid.
      Default: false
```

`project` can either be a directory or a single Kotlin file.
The currently only supported configuration format is yaml. `config` should point to one.
`filters` can be used for example to exclude all test directories.
With `rules` you can point to additional ruleset.jar's creating by yourself or others. 
More on this topic see section _Custom RuleSets_.


#### <a name="gradleplugin">Using the detekt-gradle-plugin</a>

For gradle version >= 2.1
 
```groovy
buildscript {
    repositories {
        jcenter()
    }
}

plugins {
    id "io.gitlab.arturbosch.detekt" version "1.0.0.M10.3"
}

detekt {
    version = "1.0.0.M10.3"  // Specify current detekt version
    input = "$input/src/main/kotlin" // input is preconfigured to 'project.projectDir.absolutePath'
    config = "$project.projectDir/detekt.yml" // Use $project.projectDir to navigate inside your project 
    filters = ".*test.*, .*/resources/.*" // What paths to exclude? Use comma oder semicolon to separate
    rulesets = "other/optional/ruleset.jar" // Custom rule sets can be linked to this, use comma oder semicolon to separate, remove if unused.
}
```

For all gradle versions:

```groovy
buildscript {
  repositories {
    jcenter
    maven { url "https://plugins.gradle.org/m2/" }
  }
  dependencies {
    classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.M10.3"
  }
}

apply plugin: "io.gitlab.arturbosch.detekt"

detekt {
    ...
}
```

Now three new tasks are available: 
- `detekt` - runs normal _detekt_ analysis and complexity report
- `detektFormat` - formats your kotlin code, needs `config` parameter (see [Formatting - Code Style](#formatting))
- `detektMigrate` - migrates imports, needs `config` parameter (experimental)

##### Migration
<commit_msg>Write documentation how to use the gradle-plugin with gradle-script-kotlin - closes #59<commit_after># __detekt__

[![build status](https://travis-ci.org/arturbosch/detekt.svg?branch=master)](https://travis-ci.org/arturbosch/detekt)
[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin)
[ ![Download](https://api.bintray.com/packages/arturbosch/code-analysis/detekt/images/download.svg) ](https://bintray.com/arturbosch/code-analysis/detekt/_latestVersion)

Meet _detekt_, a static code analysis tool for the _Kotlin_ programming language.
It operates on the abstract syntax tree provided by the Kotlin compiler.

![detekt in action](img/detekt_in_action.png "detekt in action")

### Features

- code smell analysis for your kotlin projects
- complexity report based on logical lines of code, McCabe complexity and amount of code smells
- highly configurable (rule set or rule level)
- suppress findings with Kotlin's @Suppress and Java's @SuppressWarnings annotations
- specify code smell thresholds to break your build or print a warning
- extensible by own rule sets and `FileProcessListener's`
- format your code with the formatting rule set
- code Smell baseline and ignore lists for legacy projects
- **NEW** - [gradle plugin](#gradleplugin) for code analysis, formatting and import migration


### Table of contents
1. [Usage/Build](#build)
2. [Parameters for CLI](#cli)
3. [As gradle plugin](#gradleplugin)
3. [As gradle task](#gradle)
4. [As maven task](#maventask)
5. [Rule sets](#rulesets)
6. [Rule set configuration](#rulesetconfig)
7. [Suppress rules](#suppress)
7. [Build failure](#failure)
7. [Custom rule sets](#customruleset)
8. [Rule testing](#testing)
9. [Formatting - Code Style](#formatting)
10. [Black- and Whitelist code smells](#baseline)

### <a name="build">Usage/Build</a>

##### Building all submodules ( + executables)

- cd detekt
- gradle clean build (install shadow)

##### Using the command line interface

- cd detekt-cli
- gradle shadow
- java -jar build/libs/detekt-cli-[version]-all.jar

#### <a name="cli">Parameters for CLI</a>
The CLI uses jcommander for argument parsing.

```
The following option is required: --project, -p

Usage: detekt [options]
  Options:
    --baseline, -b
      Treats current analysis findings as a smell baseline for further detekt
      runs. If a baseline xml file exists, only new code smells not in the
      baseline are printed in the console.
      Default: false
    --config, -c
      Path to the config file (path/to/config.yml).
    --config-resource, -cr
      Path to the config resource on detekt's classpath (path/to/config.yml).
    --debug, -d
      Debugs given ktFile by printing its elements.
      Default: false
    --disableDefaultRuleSets, -dd
      Disables default rule sets.
      Default: false
    --filters, -f
      Path filters defined through regex with separator ';' (".*test.*").
    --format
      Enables formatting of source code. Cannot be used together with
      --config.
      Default: false
    --help, -h
      Shows the usage.
    --output, -o
      True if findings should be written into a report.detekt file inside the
      report folder.
      Default: false
    --parallel
      Enables parallel compilation of source files. Should only be used if the
      analyzing project has more than ~200 kotlin files.
      Default: false
  * --project, -p
      Project path to analyze (path/to/project).
    --report, -rp
      Path to the report directory where findings should be stored (if
      --output) and baseline.xml generated (if --baseline).
    --rules, -r
      Extra paths to ruleset jars separated by ';'.
    --useTabs
      Tells the formatter that indentation with tabs are valid.
      Default: false
```

`project` can either be a directory or a single Kotlin file.
The currently only supported configuration format is yaml. `config` should point to one.
`filters` can be used for example to exclude all test directories.
With `rules` you can point to additional ruleset.jar's creating by yourself or others. 
More on this topic see section _Custom RuleSets_.


#### <a name="gradleplugin">Using the detekt-gradle-plugin</a>

For gradle version >= 2.1
 
```groovy
buildscript {
    repositories {
        jcenter()
    }
}

plugins {
    id "io.gitlab.arturbosch.detekt" version "1.0.0.M10.3"
}

detekt {
    version = "1.0.0.M10.3"  // Specify current detekt version
    input = "$input/src/main/kotlin" // input is preconfigured to 'project.projectDir.absolutePath'
    config = "$project.projectDir/detekt.yml" // Use $project.projectDir to navigate inside your project 
    filters = ".*test.*, .*/resources/.*" // What paths to exclude? Use comma oder semicolon to separate
    rulesets = "other/optional/ruleset.jar" // Custom rule sets can be linked to this, use comma oder semicolon to separate, remove if unused.
}
```

For all gradle versions:

```groovy
buildscript {
  repositories {
    jcenter
    maven { url "https://plugins.gradle.org/m2/" }
  }
  dependencies {
    classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0.M10.3"
  }
}

apply plugin: "io.gitlab.arturbosch.detekt"

detekt {
    version = "1.0.0.M10.3"  // Specify current detekt version
    input = "$input/src/main/kotlin" // input is preconfigured to 'project.projectDir.absolutePath'
    config = "$project.projectDir/detekt.yml" // Use $project.projectDir to navigate inside your project 
    filters = ".*test.*, .*/resources/.*" // What paths to exclude? Use comma oder semicolon to separate
    rulesets = "other/optional/ruleset.jar" // Custom rule sets can be linked to this, use comma oder semicolon to separate, remove if unused.
}
```

For gradle-script-kotlin:

```kotlin
import io.gitlab.arturbosch.detekt.DetektExtension

buildscript {
    repositories {
        jcenter()
    }
}
plugins {
    id("io.gitlab.arturbosch.detekt").version("1.0.0.M10.3")
}

configure<DetektExtension> {
    version = "1.0.0.M10.3"  // Specify current detekt version
    config = "${project.projectDir}/detekt.yml" // Use $project.projectDir to navigate inside your project
    filters = ".*test.*,.*/resources/.*,.*/tmp/.*" // What paths to exclude? Use comma oder semicolon to separate    
    // rulesets = "other/optional/ruleset.jar" // Custom rule sets can be linked to this, use comma oder semicolon to separate, remove if unused.
}
```

Now three new tasks are available: 
- `detekt` - runs normal _detekt_ analysis and complexity report
- `detektFormat` - formats your kotlin code, needs `config` parameter (see [Formatting - Code Style](#formatting))
- `detektMigrate` - migrates imports, needs `config` parameter (experimental)

##### Migration

{'fraction_non_alphanumeric': 0.11269334642769457, 'fraction_numerical': 0.007856616744414437, 'mean_word_length': 3.4420210832424574, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5638492', 'n_tokens_mistral': 3795, 'n_tokens_neox': 3655, 'n_words': 1307}