Skip to content

Commit

Permalink
Prepare Detekt 1.22.0 (#5544)
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico committed Nov 21, 2022
1 parent c97b8ef commit 4b1da0d
Show file tree
Hide file tree
Showing 40 changed files with 8,958 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -110,7 +110,7 @@ Gradle 6.7.1+ is the minimum requirement. However, the recommended versions toge

| Detekt Version | Gradle | Kotlin | AGP | Java Target Level | JDK Max Version |
| -------------- | ------- | -------- | ------- | ----------------- | --------------- |
| `1.21.0` | `7.5` | `1.6.21` | `7.2.1` | `1.8` | `17` |
| `1.22.0` | `7.5.1` | `1.7.21` | `7.3.1` | `1.8` | `17` |

The list of [recommended versions for previous detekt version is listed here](https://detekt.dev/compatibility.html).

Expand Down
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/Versions.kt
@@ -1,6 +1,6 @@
object Versions {

const val DETEKT: String = "1.22.0-RC3"
const val DETEKT: String = "1.22.0"
const val SNAPSHOT_NAME: String = "main"
const val JVM_TARGET: String = "1.8"

Expand Down
2 changes: 1 addition & 1 deletion website/docs/introduction/compatibility.md
Expand Up @@ -28,7 +28,7 @@ Consider **aligning** your Gradle plugin versions with the one listed below, as

| Detekt Version | Gradle Version | Kotlin Version | AGP Version | Java Target Level | JDK Version |
|----------------|----------------|----------------|-------------|-------------------|-------------|
| `1.22.0` | `7.5.1` | `1.7.20` | `7.3.1` | `1.8` | `17` |
| `1.22.0` | `7.5.1` | `1.7.21` | `7.3.1` | `1.8` | `17` |
| `1.21.0` | `7.5` | `1.6.21` | `7.2.1` | `1.8` | `17` |
| `1.20.0` | `7.4.2` | `1.6.20` | `7.1.3` | `1.8` | `17` |
| `1.19.0` | `7.3.0` | `1.5.31` | `4.2.2` | `1.8` | `17` |
Expand Down
12 changes: 10 additions & 2 deletions website/src/pages/changelog.md
Expand Up @@ -6,7 +6,7 @@ keywords: [changelog, release-notes, migration]

# Changelog and Migration Guide

#### 1.22.0-RC3 - 2022-11-06
#### 1.22.0 - 2022-11-20

##### Notable Changes

Expand Down Expand Up @@ -47,7 +47,7 @@ keywords: [changelog, release-notes, migration]
- `WildcardImport` is now running also on tests by default - [#5121](https://github.com/detekt/detekt/pull/5121)
- `ForbiddenImport` allows now to specify a reason for every forbidden import - [#4909](https://github.com/detekt/detekt/pull/4909)
- `IgnoredReturnValue`: option `restrictToAnnotatedMethods` is now deprecated in favor of `restrictToConfig` - [#4922](https://github.com/detekt/detekt/pull/4922)
- This version of Detekt is built with Gradle `v7.5.1`, AGP `7.3.1`, Kotlin `1.7.20` and KtLint `0.47.1` (see [#5363](https://github.com/detekt/detekt/pull/5363) [#5189](https://github.com/detekt/detekt/pull/5189) [#5411](https://github.com/detekt/detekt/pull/5411) [#5312](https://github.com/detekt/detekt/pull/5312)
- This version of Detekt is built with Gradle `v7.5.1`, AGP `7.3.1`, Kotlin `1.7.21` and KtLint `0.47.1` (see [#5363](https://github.com/detekt/detekt/pull/5363) [#5189](https://github.com/detekt/detekt/pull/5189) [#5411](https://github.com/detekt/detekt/pull/5411) [#5312](https://github.com/detekt/detekt/pull/5312) [#5519](https://github.com/detekt/detekt/pull/5519))
- The minimum supported Gradle version is now `v6.7.1` - [#4964](https://github.com/detekt/detekt/pull/4964)

##### Migration
Expand Down Expand Up @@ -132,6 +132,13 @@ formatting:

##### Changelog

- ReturnCount: correctly count assignment expressions with elvis return as guard clauses - [#5539](https://github.com/detekt/detekt/pull/5539)
- UnnecessaryPartOfBinaryExpression: fix false positive with pair creation - [#5516](https://github.com/detekt/detekt/pull/5516)
- False positive at `UnnecessaryPartOfBinaryExpression` - [#5514](https://github.com/detekt/detekt/issues/5514)
- Update documentation for TrailingComma rules - [#5513](https://github.com/detekt/detekt/pull/5513)
- `TrimMultilineRawString` false-positive on annotation parameters - [#5476](https://github.com/detekt/detekt/issues/5476)
- Detekt 1.22.0-RC1 -> 1.22.0-RC2 breaks UnreachableCode - [#5435](https://github.com/detekt/detekt/issues/5435)
- Detekt 1.22.0-RC1 -> 1.22.0-RC2 breaks ignoreAnnotated - [#5427](https://github.com/detekt/detekt/issues/5427)
- Fix issues introduced by #5152 - [#5508](https://github.com/detekt/detekt/pull/5508)
- MultilineLambdaItParameter: fix false positive for one-line statements with a lambda argument - [#5505](https://github.com/detekt/detekt/pull/5505)
- UseArrayLiteralsInAnnotations: fix false negative with primitive array factory calls - [#5482](https://github.com/detekt/detekt/pull/5482)
Expand Down Expand Up @@ -394,6 +401,7 @@ Because of this, some users might have to **recreate their baseline** as the loc

##### Dependency Updates

- Update plugin binaryCompatibilityValidator to v0.12.0 - [#5456](https://github.com/detekt/detekt/pull/5456)
- Update dependency gradle to v7.5 - [#5074](https://github.com/detekt/detekt/pull/5074)
- Update plugin binaryCompatibilityValidator to v0.11.0 - [#5069](https://github.com/detekt/detekt/pull/5069)
- Update dependency org.jetbrains.kotlinx:kotlinx-coroutines-core to v1.6.3 - [#4976](https://github.com/detekt/detekt/pull/4976)
Expand Down
2 changes: 1 addition & 1 deletion website/src/remark/detektVersionReplace.js
Expand Up @@ -3,7 +3,7 @@ const visit = require("unist-util-visit");
// Remark plugin that is replacing the [detekt_version] with the latest
// released version. Please note that this field is updated automatically
// by the `applyDocVersion` task.
const detektVersion = "1.22.0-RC3";
const detektVersion = "1.22.0";

const plugin = (options) => {
const transformer = async (ast) => {
Expand Down
@@ -0,0 +1,4 @@
{
"label": "Getting Started",
"position": 3
}
@@ -0,0 +1,10 @@
```
Usage: java -jar detekt-generator-[detekt_version]-all.jar [options]
Options:
--generate-custom-rule-config, -gcrc
Generate custom rules configuration files. The files will be
placed under 'resources' folder of each rule respectively
(e.g. 'custom-rule/src/main/resources/config/config.yml').
--input, -i
Input paths to rules to analyze. Multiple paths are separated by comma.
```
@@ -0,0 +1,89 @@
```
Usage: detekt [options]
Options:
--all-rules
Activates all available (even unstable) rules.
Default: false
--auto-correct, -ac
Allow rules to auto correct code if they support it. The default rule
sets do NOT support auto correcting and won't change any line in the
users code base. However custom rules can be written to support auto
correcting. The additional 'formatting' rule set, added with
'--plugins', does support it and needs this flag.
Default: false
--base-path, -bp
Specifies a directory as the base path.Currently it impacts all file
paths in the formatted reports. File paths in console output and txt
report are not affected and remain as absolute paths.
--baseline, -b
If a baseline xml file is passed in, only new code smells not in the
baseline are printed in the console.
--build-upon-default-config
Preconfigures detekt with a bunch of rules and some opinionated defaults
for you. Allows additional provided configurations to override the
defaults.
Default: false
--classpath, -cp
EXPERIMENTAL: Paths where to find user class files and depending jar
files. Used for type resolution.
--config, -c
Path to the config file (path/to/config.yml). Multiple configuration
files can be specified with ',' or ';' as separator.
--config-resource, -cr
Path to the config resource on detekt's classpath (path/to/config.yml).
--create-baseline, -cb
Treats current analysis findings as a smell baseline for future detekt
runs.
Default: false
--debug
Prints extra information about configurations and extensions.
Default: false
--disable-default-rulesets, -dd
Disables default rule sets.
Default: false
--excludes, -ex
Globbing patterns describing paths to exclude from the analysis.
--generate-config, -gc
Export default config. Path can be specified with --config option
(default path: default-detekt-config.yml)
Default: false
--help, -h
Shows the usage.
--includes, -in
Globbing patterns describing paths to include in the analysis. Useful in
combination with 'excludes' patterns.
--input, -i
Input paths to analyze. Multiple paths are separated by comma. If not
specified the current working directory is used.
--jdk-home
EXPERIMENTAL: Use a custom JDK home directory to include into the
classpath
--jvm-target
EXPERIMENTAL: Target version of the generated JVM bytecode that was
generated during compilation and is now being used for type resolution
(1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16 or 17)
Default: 1.8
--language-version
EXPERIMENTAL: Compatibility mode for Kotlin language version X.Y,
reports errors for all language features that came out later
Possible Values: [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]
--max-issues
Return exit code 0 only when found issues count does not exceed
specified issues count.
--parallel
Enables parallel compilation and analysis of source files. Do some
benchmarks first before enabling this flag. Heuristics show performance
benefits starting from 2000 lines of Kotlin code.
Default: false
--plugins, -p
Extra paths to plugin jars separated by ',' or ';'.
--report, -r
Generates a report for given 'report-id' and stores it on given 'path'.
Entry should consist of: [report-id:path]. Available 'report-id' values:
'txt', 'xml', 'html', 'md', 'sarif'. These can also be used in
combination with each other e.g. '-r txt:reports/detekt.txt -r
xml:reports/detekt.xml'
--version
Prints the detekt CLI version.
Default: false
```
61 changes: 61 additions & 0 deletions website/versioned_docs/version-1.22.0/gettingstarted/cli.mdx
@@ -0,0 +1,61 @@
---
title: "Run detekt using Command Line Interface"
keywords: [cli]
sidebar:
permalink: cli.html
folder: gettingstarted
summary:
sidebar_position: 1
---

import CliOptions from "./_cli-options.md";
import CliGeneratorOptions from "./_cli-generator-options.md";

## Install the cli

There are different ways to install the Command Line Interface (CLI):

### MacOS, with [Homebrew](https://brew.sh/):

```sh
brew install detekt
detekt [options]
```

### Windows, with [Scoop](https://scoop.sh/)

```powershell
scoop install detekt
detekt [options]
```

### Any OS:

```sh
curl -sSLO https://github.com/detekt/detekt/releases/download/v[detekt_version]/detekt-cli-[detekt_version].zip
unzip detekt-cli-[detekt_version].zip
./detekt-cli-[detekt_version]/bin/detekt-cli --help
```

## Use the cli

detekt will exit with one of the following exit codes:

| Exit code | Description |
| --------- | ------------------------------------------------------------------------------ |
| 0 | detekt ran normally and maxIssues count was not reached in BuildFailureReport. |
| 1 | An unexpected error occurred |
| 2 | MaxIssues count was reached in BuildFailureReport. |
| 3 | Invalid detekt configuration file detected. |

The following parameters are shown when `--help` is entered.

<CliOptions />

## Use the cli to generate configuration for custom rules

<CliGeneratorOptions />

```sh
java -jar detekt-generator-[detekt_version]-all.jar -gcrc -i /path/to/rule1, /path/to/rule2
```
@@ -0,0 +1,82 @@
---
title: "Run detekt using a Git pre-commit hook"
keywords: [detekt, static, analysis, code, kotlin]
sidebar:
permalink: git-pre-commit-hook.html
folder: gettingstarted
summary:
sidebar_position: 6
---

Detekt can be integrated into your development workflow by using a Git pre-commit hook.
For that reason Git supports to run custom scripts automatically, when a specific action occurs.
The mentioned pre-commit hook can be setup locally on your dev-machine.
The following client-side detekt hook is triggered by a commit operation, and checks all files via the gradle task.

```bash
#!/usr/bin/env bash
echo "Running detekt check..."
OUTPUT="/tmp/detekt-$(date +%s)"
./gradlew detekt > $OUTPUT
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
cat $OUTPUT
rm $OUTPUT
echo "***********************************************"
echo " Detekt failed "
echo " Please fix the above issues before committing "
echo "***********************************************"
exit $EXIT_CODE
fi
rm $OUTPUT
```

The shell script can be installed by copying the content over to `<<your-repo>>/.git/hooks/pre-commit`.
This pre-commit hook needs to be executable, so you may need to change the permission (`chmod +x pre-commit`).
More information about Git hooks and how to install them can be found in
[Atlassian's tutorial](https://www.atlassian.com/git/tutorials/git-hooks).

A special thanks goes to Mohit Sarveiya for providing this shell script.
You can watch his excellent talk about **Static Code Analysis For Kotlin** on
[YouTube](https://www.youtube.com/watch?v=LT6m5_LO2DQ).

## Only run on staged files

It is also possible to use [the CLI](/docs/gettingstarted/cli) to create a hook that only runs on staged files. This has the advantage of speedier execution, by running on fewer files and avoiding the warm-up time of the gradle daemon.

Please note, however, that a handful of checks requiring [type resolution](/docs/gettingstarted/type-resolution) will not work correctly with this approach. If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline.

This example has been put together using [pre-commit](https://pre-commit.com/), but the same principle can be applied to any kind of hook.

Hook definition in pre-commit:

```yml
- id: detekt
name: detekt check
description: Runs `detekt` on modified .kt files.
language: script
entry: detekt.sh
files: \.kt
```

Script `detekt.sh`:

```bash
#!/bin/bash

echo "Running detekt check..."
fileArray=($@)
detektInput=$(IFS=,;printf "%s" "${fileArray[*]}")
echo "Input files: $detektInput"

OUTPUT=$(detekt --input "$detektInput" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo $OUTPUT
echo "***********************************************"
echo " Detekt failed "
echo " Please fix the above issues before committing "
echo "***********************************************"
exit $EXIT_CODE
fi
```

0 comments on commit 4b1da0d

Please sign in to comment.