Skip to content

Commit

Permalink
Merge branch 'main' into suppression-to-core
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed May 5, 2024
2 parents 360ec92 + 5fa82dc commit 7c76aa3
Show file tree
Hide file tree
Showing 52 changed files with 1,695 additions and 1,145 deletions.
4 changes: 4 additions & 0 deletions .github/labeler.yml
Expand Up @@ -44,6 +44,10 @@ compiler-plugin:
- changed-files:
- any-glob-to-any-file: ["detekt-compiler-plugin/src/**/*"]

parser:
- changed-files:
- any-glob-to-any-file: ["detekt-parser/src/**/*"]

reports:
- changed-files:
- any-glob-to-any-file:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codecoverage.yaml
Expand Up @@ -37,7 +37,7 @@ jobs:

- name: Publish Coverage
if: success()
uses: codecov/codecov-action@84508663e988701840491b86de86b666e8a86bed # v4
uses: codecov/codecov-action@5ecb98a3c6b747ed38dc09f787459979aebb39be # v4
with:
files: ./code-coverage-report/build/reports/jacoco/jacocoMergedReport/jacocoMergedReport.xml
verbose: true
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -166,20 +166,18 @@ As mentioned in...
Integrations:

- [IntelliJ integration](https://github.com/detekt/detekt-intellij-plugin)
- [SonarQube integration](https://github.com/detekt/sonar-kotlin)
- [SonarQube integration](https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/importing-external-issues/external-analyzer-reports/)
- [TCA(Tencent CodeAnalysis) integration](https://github.com/Tencent/CodeAnalysis/blob/main/client/tool/detekt.py)
- [Codacy](https://www.codacy.com)
- [Gradle plugin that configures Error Prone, Checkstyle, PMD, CPD, Lint, Detekt & Ktlint](https://github.com/vanniktech/gradle-code-quality-tools-plugin)
- [Violations Lib](https://github.com/tomasbjerre/violations-lib) is a Java library for parsing report files like static code analysis.
- [sputnik](https://github.com/TouK/sputnik) is a free tool for static code review and provides support for detekt
- [Gradle Static Analysis plugin](https://github.com/GradleUp/static-analysis-plugin)
- [Detekt Maven plugin](https://github.com/Ozsie/detekt-maven-plugin) that wraps the Detekt CLI
- [Detekt Bazel plugin](https://github.com/buildfoundation/bazel_rules_detekt) that wraps the Detekt CLI
- [Gradle plugin that helps facilitate GitHub PR checking and automatic commenting of violations](https://github.com/btkelly/gnag)
- [Codefactor](http://codefactor.io/)
- [GitHub Action: Detekt All](https://github.com/marketplace/actions/detekt-all)
- [GitHub Action: Setup detekt](https://github.com/marketplace/actions/setup-detekt)
- [Sonatype Lift](https://github.com/marketplace/muse-dev)

Custom rules and reports from 3rd parties can be found on our [**Detekt Marketplace**](https://detekt.dev/marketplace).

Expand Down
16 changes: 7 additions & 9 deletions detekt-api/api/detekt-api.api
Expand Up @@ -18,11 +18,6 @@ public class io/gitlab/arturbosch/detekt/api/CodeSmell : io/gitlab/arturbosch/de

public abstract interface class io/gitlab/arturbosch/detekt/api/Compactable {
public abstract fun compact ()Ljava/lang/String;
public abstract fun compactWithSignature ()Ljava/lang/String;
}

public final class io/gitlab/arturbosch/detekt/api/Compactable$DefaultImpls {
public static fun compactWithSignature (Lio/gitlab/arturbosch/detekt/api/Compactable;)Ljava/lang/String;
}

public final class io/gitlab/arturbosch/detekt/api/CompilerResources {
Expand Down Expand Up @@ -122,7 +117,6 @@ public final class io/gitlab/arturbosch/detekt/api/Entity : io/gitlab/arturbosch
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/gitlab/arturbosch/detekt/api/Location;Lorg/jetbrains/kotlin/psi/KtElement;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/gitlab/arturbosch/detekt/api/Location;Lorg/jetbrains/kotlin/psi/KtElement;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun compact ()Ljava/lang/String;
public fun compactWithSignature ()Ljava/lang/String;
public final fun getKtElement ()Lorg/jetbrains/kotlin/psi/KtElement;
public final fun getLocation ()Lio/gitlab/arturbosch/detekt/api/Location;
public final fun getName ()Ljava/lang/String;
Expand Down Expand Up @@ -196,11 +190,10 @@ public abstract interface class io/gitlab/arturbosch/detekt/api/Issue$RuleInfo {

public final class io/gitlab/arturbosch/detekt/api/Location : io/gitlab/arturbosch/detekt/api/Compactable {
public static final field Companion Lio/gitlab/arturbosch/detekt/api/Location$Companion;
public fun <init> (Lio/gitlab/arturbosch/detekt/api/SourceLocation;Lio/gitlab/arturbosch/detekt/api/SourceLocation;Lio/gitlab/arturbosch/detekt/api/TextLocation;Lio/github/detekt/psi/FilePath;)V
public fun <init> (Lio/gitlab/arturbosch/detekt/api/SourceLocation;Lio/gitlab/arturbosch/detekt/api/SourceLocation;Lio/gitlab/arturbosch/detekt/api/TextLocation;Ljava/nio/file/Path;)V
public fun compact ()Ljava/lang/String;
public fun compactWithSignature ()Ljava/lang/String;
public final fun getEndSource ()Lio/gitlab/arturbosch/detekt/api/SourceLocation;
public final fun getFilePath ()Lio/github/detekt/psi/FilePath;
public final fun getPath ()Ljava/nio/file/Path;
public final fun getSource ()Lio/gitlab/arturbosch/detekt/api/SourceLocation;
public final fun getText ()Lio/gitlab/arturbosch/detekt/api/TextLocation;
public fun toString ()Ljava/lang/String;
Expand Down Expand Up @@ -231,6 +224,8 @@ public final class io/gitlab/arturbosch/detekt/api/Notification$Level : java/lan
}

public abstract class io/gitlab/arturbosch/detekt/api/OutputReport : io/gitlab/arturbosch/detekt/api/Extension {
public static final field Companion Lio/gitlab/arturbosch/detekt/api/OutputReport$Companion;
public static final field DETEKT_OUTPUT_REPORT_BASE_PATH_KEY Ljava/lang/String;
public fun <init> ()V
public abstract fun getEnding ()Ljava/lang/String;
public fun getPriority ()I
Expand All @@ -240,6 +235,9 @@ public abstract class io/gitlab/arturbosch/detekt/api/OutputReport : io/gitlab/a
public final fun write (Ljava/nio/file/Path;Lio/gitlab/arturbosch/detekt/api/Detektion;)V
}

public final class io/gitlab/arturbosch/detekt/api/OutputReport$Companion {
}

public class io/gitlab/arturbosch/detekt/api/ProjectMetric {
public fun <init> (Ljava/lang/String;IIZI)V
public synthetic fun <init> (Ljava/lang/String;IIZIILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
Expand Up @@ -74,10 +74,4 @@ interface Compactable {
* Contract to format implementing object to a string representation.
*/
fun compact(): String

/**
* Same as [compact] except the content should contain a substring which represents
* this exact findings via a custom identifier.
*/
fun compactWithSignature(): String = compact()
}
@@ -1,15 +1,15 @@
package io.gitlab.arturbosch.detekt.api

import dev.drewhamilton.poko.Poko
import io.github.detekt.psi.FilePath
import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.getLineAndColumnInPsiFile
import io.github.detekt.psi.toFilePath
import org.jetbrains.kotlin.com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import java.nio.file.Path

/**
* Specifies a position within a source code fragment.
Expand All @@ -18,13 +18,13 @@ class Location(
val source: SourceLocation,
val endSource: SourceLocation,
val text: TextLocation,
val filePath: FilePath,
val path: Path,
) : Compactable {

override fun compact(): String = "${filePath.absolutePath}:$source"
override fun compact(): String = "$path:$source"

override fun toString(): String =
"Location(source=$source, endSource=$endSource, text=$text, filePath=$filePath)"
"Location(source=$source, endSource=$endSource, text=$text, path=$path)"

companion object {
/**
Expand All @@ -37,7 +37,7 @@ class Location(
val end = endLineAndColumn(element, offset)
val endSourceLocation = SourceLocation(end.line, end.column)
val textLocation = TextLocation(element.startOffset + offset, element.endOffset + offset)
return Location(sourceLocation, endSourceLocation, textLocation, element.containingFile.toFilePath())
return Location(sourceLocation, endSourceLocation, textLocation, element.containingFile.absolutePath())
}

/**
Expand Down
@@ -1,7 +1,7 @@
package io.gitlab.arturbosch.detekt.api

import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.createParentDirectories
import kotlin.io.path.extension
import kotlin.io.path.writeText

Expand All @@ -25,13 +25,16 @@ abstract class OutputReport : Extension {
assert(filePath.extension == ending) {
"The $id needs to have a file ending of type .$ending, but was ${filePath.fileName}."
}
filePath.parent?.createDirectories()
filePath.writeText(reportData)
filePath.createParentDirectories().writeText(reportData)
}
}

/**
* Defines the translation process of detekt's result into a string.
*/
abstract fun render(detektion: Detektion): String?

companion object {
const val DETEKT_OUTPUT_REPORT_BASE_PATH_KEY = "detekt.output.report.base.path"
}
}
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.api

import io.gitlab.arturbosch.detekt.test.fromRelative
import io.gitlab.arturbosch.detekt.test.location
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand All @@ -19,10 +18,7 @@ class CodeSmellSpec {
source = SourceLocation(1, 1),
endSource = SourceLocation(1, 1),
text = TextLocation(0, 0),
filePath = fromRelative(
Path("/").absolute().resolve("Users/tester/detekt/"),
Path("TestFile.kt"),
),
path = Path("/").absolute().resolve("Users/tester/detekt/TestFile.kt"),
),
ktElement = null
),
Expand All @@ -32,7 +28,7 @@ class CodeSmellSpec {
assertThat(codeSmell.toString()).isEqualTo(
"CodeSmell(entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"filePath=${codeSmell.location.filePath}), ktElement=null), message=TestMessage, " +
"path=${codeSmell.location.path}), ktElement=null), message=TestMessage, " +
"references=[])"
)
}
Expand Down
Expand Up @@ -18,8 +18,9 @@ class CorrectableCodeSmellSpec {
assertThat(codeSmell.toString()).isEqualTo(
"CorrectableCodeSmell(autoCorrectEnabled=true, " +
"entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, filePath=${codeSmell.location.filePath}), " +
"ktElement=null), message=TestMessage, references=[])"
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"path=${codeSmell.location.path}), ktElement=null), " +
"message=TestMessage, references=[])"
)
}
}
Expand Up @@ -9,14 +9,11 @@ import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.relativeToOrSelf
import kotlin.io.path.toPath
import kotlin.io.path.absolute

class EntitySpec {

private val path = Path("src/test/resources/EntitySpecFixture.kt").toAbsolutePath()
private val basePath = EntitySpec::class.java.getResource("/")!!.toURI().toPath()
private val relativePath = path.relativeToOrSelf(basePath)
private val path = Path("src/test/resources/EntitySpecFixture.kt").absolute()
private val code = compileForTest(path)

@Nested
Expand Down Expand Up @@ -56,7 +53,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=memberFun, signature=EntitySpecFixture.kt\$C\$private fun memberFun(): Int, " +
"location=Location(source=5:17, endSource=5:26, text=49:58, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=FUN)"
)
}
Expand All @@ -83,7 +80,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=C, signature=EntitySpecFixture.kt\$C : Any, " +
"location=Location(source=3:7, endSource=3:8, text=20:21, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=CLASS)"
)
}
Expand Down Expand Up @@ -114,7 +111,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=EntitySpecFixture.kt, signature=EntitySpecFixture.kt\$test.EntitySpecFixture.kt, " +
"location=Location(source=1:1, endSource=9:1, text=0:109, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=KtFile: EntitySpecFixture.kt)"
)
}
Expand Down
Expand Up @@ -47,8 +47,7 @@ class LocationSpec {

assertThat(location.toString()).isEqualTo(
"Location(source=2:5, endSource=2:11, text=22:28, " +
"filePath=FilePath(absolutePath=${location.filePath.absolutePath}, " +
"basePath=${location.filePath.basePath}, relativePath=${location.filePath.relativePath}))"
"path=${location.path})"
)
}
}
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.test

import io.github.detekt.psi.FilePath
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Location
Expand All @@ -10,7 +9,6 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import org.jetbrains.kotlin.psi.KtElement
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.absolute

Expand Down Expand Up @@ -71,7 +69,6 @@ fun createIssueForRelativePath(
basePath: String = "Users/tester/detekt/",
relativePath: String = "TestFile.kt"
): Issue {
require(!basePath.startsWith("/")) { "The path shouldn't start with '/'" }
return IssueImpl(
ruleInfo = ruleInfo,
entity = Entity(
Expand All @@ -81,7 +78,7 @@ fun createIssueForRelativePath(
source = SourceLocation(1, 1),
endSource = SourceLocation(1, 1),
text = TextLocation(0, 0),
filePath = fromRelative(Path("/").absolute().resolve(basePath), Path(relativePath))
path = Path("/").absolute().resolve(basePath).resolve(relativePath)
),
ktElement = null
),
Expand Down Expand Up @@ -112,8 +109,7 @@ fun createLocation(
source = SourceLocation(position.first, position.second),
endSource = SourceLocation(endPosition.first, endPosition.second),
text = TextLocation(text.first, text.last),
filePath = basePath?.let { fromRelative(Path("/").absolute().resolve(it), Path(path)) }
?: fromAbsolute(Path("/").absolute().resolve(path)),
path = basePath?.let { Path(it).absolute().resolve(path) } ?: Path(path).absolute(),
)
}

Expand All @@ -131,10 +127,3 @@ private data class IssueImpl(
override val description: String,
) : Issue.RuleInfo
}

fun fromAbsolute(path: Path) = FilePath(absolutePath = path.normalize())
fun fromRelative(basePath: Path, relativePath: Path) = FilePath(
absolutePath = basePath.resolve(relativePath).normalize(),
basePath = basePath.normalize(),
relativePath = relativePath
)
Expand Up @@ -34,9 +34,9 @@ internal class CliArgsSpec {
@Test
fun `the current working directory is used if parameter is not set`() {
val spec = parseArguments(emptyArray()).toSpec()
val workingDir = Path("").toAbsolutePath()
val workingDir = Path("").absolute()

assertThat(spec.projectSpec.inputPaths).allSatisfy { it.toAbsolutePath().startsWith(workingDir) }
assertThat(spec.projectSpec.inputPaths).allSatisfy { it.absolute().startsWith(workingDir) }
assertThat(spec.projectSpec.inputPaths).contains(pathBuildGradle)
assertThat(spec.projectSpec.inputPaths).contains(pathCliArgs)
assertThat(spec.projectSpec.inputPaths).contains(pathCliArgsSpec)
Expand Down
Expand Up @@ -32,7 +32,7 @@ fun Issue.renderAsCompilerWarningMessage(): Pair<String, CompilerMessageLocation

val sourceLocation = location?.let {
CompilerMessageLocation.create(
location.path,
entity.location.path.toString(),
entity.location.source.line,
entity.location.source.column,
location.lineContent
Expand Down
Expand Up @@ -5,7 +5,7 @@ import io.gitlab.arturbosch.detekt.api.ReportingExtension
import io.gitlab.arturbosch.detekt.api.SetupContext
import io.gitlab.arturbosch.detekt.api.getOrNull
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.createParentDirectories
import kotlin.io.path.exists
import kotlin.io.path.isRegularFile

Expand Down Expand Up @@ -59,7 +59,7 @@ class BaselineResultMapping : ReportingExtension {
val baselineFormat = BaselineFormat()
val baseline = baselineFormat.of(oldBaseline.manuallySuppressedIssues, ids)
if (oldBaseline != baseline) {
baselineFile.parent?.createDirectories()
baselineFile.createParentDirectories()
baselineFormat.write(baselineFile, baseline)
}
}
Expand Down
Expand Up @@ -27,7 +27,6 @@ const val BUILD = "build"
const val EXCLUDE_CORRECTABLE = "excludeCorrectable"

const val DETEKT_OUTPUT_REPORT_PATHS_KEY = "detekt.output.report.paths.key"
const val DETEKT_OUTPUT_REPORT_BASE_PATH_KEY = "detekt.output.report.base.path"

private const val REPORT_MESSAGE_SIZE_LIMIT = 80
private val messageReplacementRegex = Regex("\\s+")
Expand Down
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.core.reporting.console

import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.core.reporting.printIssues
import kotlin.io.path.absolutePathString

/**
* Contains all rule violations grouped by file location.
Expand All @@ -12,7 +13,7 @@ class FileBasedIssuesReport : AbstractIssuesReport() {
override val id: String = "FileBasedIssuesReport"

override fun render(issues: List<Issue>): String {
val issuesPerFile = issues.groupBy { it.entity.location.filePath.absolutePath.toString() }
val issuesPerFile = issues.groupBy { it.entity.location.path.absolutePathString() }
return printIssues(issuesPerFile)
}
}

0 comments on commit 7c76aa3

Please sign in to comment.