Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PsiFile.basePath #7237

Merged
merged 6 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ object CompilerTestUtils {
"detekt-compiler-plugin",
"useDefaultConfig",
useDefaultConfig.toString(),
),
PluginOption(
"detekt-compiler-plugin",
"rootDir",
workingDir.absolutePath
)
)
}.compile()
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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())