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.relativePath #7238

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -11,14 +11,13 @@
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)

Check warning on line 14 in detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/tooling/ParsingStrategy.kt

View check run for this annotation

Codecov / codecov/patch

detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/tooling/ParsingStrategy.kt#L14

Added line #L14 was not covered by tests
)
}

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 @@ -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 @@ -19,12 +19,12 @@ 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +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")
}

@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")
}

@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
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ fun compileContentForTest(
basePath: Path = Path("/").absolute(),
path: Path = basePath.resolve("Test.kt"),
3flex marked this conversation as resolved.
Show resolved Hide resolved
): 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)