Skip to content

Commit

Permalink
Remove PsiFile.basePath (#7237)
Browse files Browse the repository at this point in the history
* Pass root path to detekt

* Pass proper basePath to analyzer spec tests

* Pass proper base path in perf monitor tests

* Unconditionally pass basePath to isIgnored check

* Remove PsiFile.basePath

* Convert provided base path to absolute path in CLI
  • Loading branch information
3flex committed May 5, 2024
1 parent 5fa82dc commit 1c12968
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 30 deletions.
Expand Up @@ -23,7 +23,7 @@ internal fun CliArgs.createSpec(output: Appendable, error: Appendable): Processi
}

project {
basePath = args.basePath
basePath = args.basePath.absolute()
val pathFilters = PathFilters.of(
args.excludes?.let(::asPatterns).orEmpty(),
args.includes?.let(::asPatterns).orEmpty(),
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.github.detekt.compiler.plugin.Keys
import io.github.detekt.tooling.api.spec.ProcessingSpec
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CompilerConfiguration
import kotlin.io.path.Path

internal fun CompilerConfiguration.toSpec(log: MessageCollector) = ProcessingSpec.invoke {
config {
Expand All @@ -18,6 +19,9 @@ internal fun CompilerConfiguration.toSpec(log: MessageCollector) = ProcessingSpe
outputChannel = AppendableAdapter { log.info(it) }
errorChannel = AppendableAdapter { log.error(it) }
}
project {
basePath = get(Keys.ROOT_PATH, Path(System.getProperty("user.dir")))
}
reports {
getMap(Keys.REPORTS).forEach {
report { it.key to it.value }
Expand Down
Expand Up @@ -32,6 +32,11 @@ object CompilerTestUtils {
"detekt-compiler-plugin",
"useDefaultConfig",
useDefaultConfig.toString(),
),
PluginOption(
"detekt-compiler-plugin",
"rootDir",
workingDir.absolutePath
)
)
}.compile()
Expand Down
Expand Up @@ -90,15 +90,15 @@ internal class Analyzer(
.map { it to settings.config.subConfig(it.ruleSetId.value) }
.filter { (_, ruleSetConfig) -> ruleSetConfig.isActiveOrDefault(true) }
.map { (provider, ruleSetConfig) -> provider.instance() to ruleSetConfig }
.filter { (_, ruleSetConfig) -> ruleSetConfig.shouldAnalyzeFile(file) }
.filter { (_, ruleSetConfig) -> ruleSetConfig.shouldAnalyzeFile(file, settings.spec.projectSpec.basePath) }

val (correctableRules, otherRules) = activeRuleSetsToRuleSetConfigs
.flatMap { (ruleSet, ruleSetConfig) ->
ruleSet.rules
.asSequence()
.map { (ruleId, ruleProvider) -> ruleProvider to ruleSetConfig.subConfig(ruleId.value) }
.filter { (_, config) -> config.isActiveOrDefault(false) }
.filter { (_, config) -> config.shouldAnalyzeFile(file) }
.filter { (_, config) -> config.shouldAnalyzeFile(file, settings.spec.projectSpec.basePath) }
.map { (ruleProvider, config) ->
val rule = ruleProvider(config)
rule.toRuleInfo(ruleSet.id) to rule
Expand Down
@@ -1,18 +1,18 @@
package io.gitlab.arturbosch.detekt.core.util

import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.basePath
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.internal.PathFilters
import org.jetbrains.kotlin.psi.KtFile
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.relativeTo

internal fun Config.isActiveOrDefault(default: Boolean): Boolean = valueOrDefault(Config.ACTIVE_KEY, default)

internal fun Config.shouldAnalyzeFile(file: KtFile): Boolean {
internal fun Config.shouldAnalyzeFile(file: KtFile, basePath: Path): Boolean {
val filters = createPathFilters()
return filters == null || !filters.isIgnored(file)
return filters == null || !filters.isIgnored(file, basePath)
}

private fun Config.createPathFilters(): PathFilters? {
Expand All @@ -22,15 +22,10 @@ private fun Config.createPathFilters(): PathFilters? {
}

/**
* Runs [isIgnored] against a [KtFile] based on its relative path, or based on its [absolutePath] if [basePath] is
* not set.
* Runs [isIgnored] against a [KtFile] based on its relative path.
*/
private fun PathFilters.isIgnored(ktFile: KtFile): Boolean {
ktFile.basePath?.let {
return isIgnored(Path(".", ktFile.absolutePath().relativeTo(it).toString()))
}
return isIgnored(ktFile.absolutePath())
}
private fun PathFilters.isIgnored(ktFile: KtFile, basePath: Path): Boolean =
isIgnored(Path(".", ktFile.absolutePath().relativeTo(basePath).toString()))

internal fun String.indentCompat(indent: Int): String {
val spaces = " ".repeat(indent)
Expand Down
Expand Up @@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import kotlin.io.path.Path
import kotlin.io.path.absolute

@KotlinCoreEnvironmentTest
class AnalyzerSpec(val env: KotlinCoreEnvironment) {
Expand Down Expand Up @@ -348,7 +349,7 @@ class AnalyzerSpec(val env: KotlinCoreEnvironment) {
path: String,
config: Config,
): Boolean {
val root = Path("/foo/bar/sample/")
val root = Path("").absolute()

return createProcessingSettings(config = config) { project { basePath = root } }
.use { settings ->
Expand Down
Expand Up @@ -9,6 +9,7 @@ import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolute

class IsActiveOrDefaultSpec {

Expand All @@ -34,25 +35,26 @@ class IsActiveOrDefaultSpec {
@Nested
class ShouldAnalyzeFileSpec {

private val file = compileContentForTest("", basePath = Path("/cases"), path = Path("/cases/Default.kt"))
private val basePath = Path("/cases").absolute()
private val file = compileContentForTest("", basePath = basePath, path = Path("/cases/Default.kt"))

@Test
fun `analyzes file with an empty config`() {
val config = Config.empty
assertThat(config.shouldAnalyzeFile(file)).isTrue()
assertThat(config.shouldAnalyzeFile(file, basePath)).isTrue()
}

@Test
@DisplayName("should not analyze file with **/*.kt excludes")
fun ignoreExcludedKt() {
val config = TestConfig(Config.EXCLUDES_KEY to listOf("**/*.kt"))
assertThat(config.shouldAnalyzeFile(file)).isFalse()
assertThat(config.shouldAnalyzeFile(file, basePath)).isFalse()
}

@Test
fun `Only check relative path`() {
val config = TestConfig(Config.EXCLUDES_KEY to listOf("**/cases/*.kt"))
assertThat(config.shouldAnalyzeFile(file)).isTrue()
assertThat(config.shouldAnalyzeFile(file, basePath)).isTrue()
}

@Test
Expand All @@ -61,6 +63,6 @@ class ShouldAnalyzeFileSpec {
Config.EXCLUDES_KEY to listOf("**/*.kt"),
Config.INCLUDES_KEY to listOf("**/*.kt"),
)
assertThat(config.shouldAnalyzeFile(file)).isFalse()
assertThat(config.shouldAnalyzeFile(file, basePath)).isFalse()
}
}
Expand Up @@ -14,6 +14,7 @@ class PerformanceMonitorSpec {
val actual = StringPrintStream()
val spec = ProcessingSpec {
project {
basePath = resourceAsPath("")
inputPaths = listOf(resourceAsPath("cases/Test.kt"))
}
logging {
Expand Down
@@ -1,7 +1,6 @@
package io.github.detekt.parser

import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.basePath
import io.github.detekt.psi.lineSeparator
import io.github.detekt.psi.relativePath
import org.intellij.lang.annotations.Language
Expand Down Expand Up @@ -34,7 +33,6 @@ open class KtCompiler(
this.absolutePath = normalizedAbsolutePath
this.lineSeparator = content.determineLineSeparator()
val normalizedBasePath = basePath.absolute().normalize()
this.basePath = normalizedBasePath.absolute()
this.relativePath = normalizedBasePath.relativize(normalizedAbsolutePath)
}
}
Expand Down
@@ -1,6 +1,5 @@
package io.github.detekt.parser

import io.github.detekt.psi.basePath
import io.github.detekt.psi.lineSeparator
import io.github.detekt.psi.relativePath
import io.github.detekt.test.utils.resourceAsPath
Expand All @@ -26,8 +25,6 @@ class KtCompilerSpec {
assertThat(ktFile.lineSeparator).isEqualTo("\n")
assertThat(ktFile.relativePath)
.isEqualTo(Path("DefaultLf.kt"))
assertThat(ktFile.basePath)
.endsWith(Path("cases"))
}

@Test
Expand All @@ -37,8 +34,6 @@ class KtCompilerSpec {
assertThat(ktFile.lineSeparator).isEqualTo("\r\n")
assertThat(ktFile.relativePath)
.isEqualTo(Path("DefaultCrLf.kt"))
assertThat(ktFile.basePath)
.endsWith(Path("cases"))
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions detekt-psi-utils/api/detekt-psi-utils.api
Expand Up @@ -14,10 +14,8 @@ public final class io/github/detekt/psi/FunctionMatcher$Companion {
}

public final class io/github/detekt/psi/KeysKt {
public static final fun getBasePath (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;)Ljava/nio/file/Path;
public static final fun getLineSeparator (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;)Ljava/lang/String;
public static final fun getRelativePath (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;)Ljava/nio/file/Path;
public static final fun setBasePath (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;Ljava/nio/file/Path;)V
public static final fun setLineSeparator (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;Ljava/lang/String;)V
public static final fun setRelativePath (Lorg/jetbrains/kotlin/com/intellij/psi/PsiFile;Ljava/nio/file/Path;)V
}
Expand Down
Expand Up @@ -7,5 +7,4 @@ import org.jetbrains.kotlin.psi.UserDataProperty
import java.nio.file.Path

var PsiFile.relativePath: Path? by UserDataProperty(Key("relativePath"))
var PsiFile.basePath: Path? by UserDataProperty(Key("basePath"))
var PsiFile.lineSeparator: String by NotNullableUserDataProperty(Key("lineSeparator"), System.lineSeparator())

0 comments on commit 1c12968

Please sign in to comment.