Skip to content

Commit

Permalink
Remove PsiFile.relativePath (#7238)
Browse files Browse the repository at this point in the history
* Remove psiFile.relativePath

* Stop passing basePath where it's no longer needed

* Remove basePath parameter from compileContentForTest
  • Loading branch information
3flex committed May 5, 2024
1 parent d021cc7 commit b01bc7a
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ typealias ParsingStrategy = (settings: ProcessingSettings) -> List<KtFile>
fun contentToKtFile(content: String, path: Path): ParsingStrategy = { settings ->
require(path.isRegularFile()) { "Given sub path ($path) should be a regular file!" }
listOf(
KtCompiler(settings.environment).createKtFile(content, settings.spec.projectSpec.basePath, path)
KtCompiler(settings.environment).createKtFile(content, path)
)
}

val inputPathsToKtFiles: ParsingStrategy = { settings ->
val compiler = KtCompiler(settings.environment)
val basePath = settings.spec.projectSpec.basePath
settings.spec.projectSpec.inputPaths.map { path ->
compiler.compile(basePath, path)
compiler.compile(path)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class AnalyzerSpec(val env: KotlinCoreEnvironment) {
return createProcessingSettings(config = config) { project { basePath = root } }
.use { settings ->
Analyzer(settings, listOf(CustomRuleSetProvider()), emptyList())
.run(listOf(compileContentForTest("", root, Path(path))))
.run(listOf(compileContentForTest("", Path(path))))
.isNotEmpty()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class IsActiveOrDefaultSpec {
class ShouldAnalyzeFileSpec {

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

@Test
fun `analyzes file with an empty config`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ private fun parseAll(parser: KtCompiler, root: Path): Collection<KtFile> =
Files.walk(root)
.asSequence()
.filter { it.extension == "kt" }
.map { parser.compile(root, it) }
.map { parser.compile(it) }
.toList()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.github.detekt.parser

import io.github.detekt.psi.absolutePath
import io.github.detekt.psi.lineSeparator
import io.github.detekt.psi.relativePath
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
Expand All @@ -20,20 +19,18 @@ open class KtCompiler(

protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false)

fun compile(basePath: Path, path: Path): KtFile {
fun compile(path: Path): KtFile {
require(path.isRegularFile()) { "Given path '$path' should be a regular file!" }
return createKtFile(path.readText(), basePath, path)
return createKtFile(path.readText(), path)
}

fun createKtFile(@Language("kotlin") content: String, basePath: Path, path: Path): KtFile {
fun createKtFile(@Language("kotlin") content: String, path: Path): KtFile {
val psiFile = psiFileFactory.createPhysicalFile(path.name, StringUtilRt.convertLineSeparators(content))

return psiFile.apply {
val normalizedAbsolutePath = path.absolute().normalize()
this.absolutePath = normalizedAbsolutePath
this.lineSeparator = content.determineLineSeparator()
val normalizedBasePath = basePath.absolute().normalize()
this.relativePath = normalizedBasePath.relativize(normalizedAbsolutePath)
}
}
}
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.lineSeparator
import io.github.detekt.psi.relativePath
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
Expand All @@ -20,40 +19,36 @@ class KtCompilerSpec {

@Test
fun `Kotlin file with LF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultLf.kt"))
val ktFile = ktCompiler.compile(path.resolve("DefaultLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\n")
assertThat(ktFile.relativePath)
.isEqualTo(Path("DefaultLf.kt"))
}

@Test
fun `Kotlin file with CRLF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultCrLf.kt"))
val ktFile = ktCompiler.compile(path.resolve("DefaultCrLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\r\n")
assertThat(ktFile.relativePath)
.isEqualTo(Path("DefaultCrLf.kt"))
}

@Test
fun `throws an exception for an invalid path`() {
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(path, path) }
.isThrownBy { ktCompiler.compile(path) }
.withMessage("Given path '$path' should be a regular file!")
}

@Test
fun `throws an exception for an non existent path`() {
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(Path(""), Path("nonExistent")) }
.isThrownBy { ktCompiler.compile(Path("nonExistent")) }
.withMessage("Given path 'nonExistent' should be a regular file!")
}

@Test
fun `parses with errors for non kotlin files`() {
val cssPath = resourceAsPath("css")
val ktFile = ktCompiler.compile(cssPath, cssPath.resolve("test.css"))
val ktFile = ktCompiler.compile(cssPath.resolve("test.css"))

val errors = mutableListOf<PsiErrorElement>()
ktFile.accept(object : KtTreeVisitorVoid() {
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 @@ -15,9 +15,7 @@ public final class io/github/detekt/psi/FunctionMatcher$Companion {

public final class io/github/detekt/psi/KeysKt {
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 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
}

public final class io/github/detekt/psi/KtFilesKt {
Expand Down
3 changes: 0 additions & 3 deletions detekt-psi-utils/src/main/kotlin/io/github/detekt/psi/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ package io.github.detekt.psi
import org.jetbrains.kotlin.com.intellij.openapi.util.Key
import org.jetbrains.kotlin.com.intellij.psi.PsiFile
import org.jetbrains.kotlin.psi.NotNullableUserDataProperty
import org.jetbrains.kotlin.psi.UserDataProperty
import java.nio.file.Path

var PsiFile.relativePath: Path? by UserDataProperty(Key("relativePath"))
var PsiFile.lineSeparator: String by NotNullableUserDataProperty(Key("lineSeparator"), System.lineSeparator())
4 changes: 2 additions & 2 deletions detekt-test-utils/api/detekt-test-utils.api
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public final class io/github/detekt/test/utils/CompileExtensionsKt {
public static final fun compileContentForTest (Ljava/lang/String;Ljava/lang/String;)Lorg/jetbrains/kotlin/psi/KtFile;
public static final fun compileContentForTest (Ljava/lang/String;Ljava/nio/file/Path;Ljava/nio/file/Path;)Lorg/jetbrains/kotlin/psi/KtFile;
public static synthetic fun compileContentForTest$default (Ljava/lang/String;Ljava/nio/file/Path;Ljava/nio/file/Path;ILjava/lang/Object;)Lorg/jetbrains/kotlin/psi/KtFile;
public static final fun compileContentForTest (Ljava/lang/String;Ljava/nio/file/Path;)Lorg/jetbrains/kotlin/psi/KtFile;
public static synthetic fun compileContentForTest$default (Ljava/lang/String;Ljava/nio/file/Path;ILjava/lang/Object;)Lorg/jetbrains/kotlin/psi/KtFile;
public static final fun compileForTest (Ljava/nio/file/Path;)Lorg/jetbrains/kotlin/psi/KtFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ fun compileContentForTest(
*/
fun compileContentForTest(
@Language("kotlin") content: String,
basePath: Path = Path("/").absolute(),
path: Path = basePath.resolve("Test.kt"),
path: Path = Path("/").absolute().resolve("Test.kt"),
): KtFile {
return KtTestCompiler.createKtFile(content, basePath, path)
return KtTestCompiler.createKtFile(content, path)
}

/**
* Use this method if you test a kt file/class in the test resources.
*/
fun compileForTest(path: Path) = KtTestCompiler.compile(resourceAsPath("/"), path)
fun compileForTest(path: Path) = KtTestCompiler.compile(path)

0 comments on commit b01bc7a

Please sign in to comment.