From 9874528fa667b20aa9eef5aa1f3a22bb4d3bf710 Mon Sep 17 00:00:00 2001 From: Artur Bosch Date: Mon, 20 Jul 2020 19:17:26 +0200 Subject: [PATCH] Simplify internal parsing to KtFile's (#2875) * Simplify parsing to KtFile's * Do not mark KtFiles as generated --- .../io/github/detekt/parser/KtCompiler.kt | 16 ++++++--------- .../io/github/detekt/parser/KtCompilerTest.kt | 20 ++++++++++++++----- detekt-parser/src/test/resources/css/test.css | 3 +++ .../detekt/test/utils/KtTestCompiler.kt | 10 ++++------ 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt index a4252a5e1..7d4e468e0 100644 --- a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt +++ b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt @@ -5,10 +5,8 @@ import io.github.detekt.psi.LINE_SEPARATOR import io.github.detekt.psi.RELATIVE_PATH import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt -import org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory -import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile -import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtPsiFactory import java.nio.file.Files import java.nio.file.Path @@ -16,7 +14,7 @@ open class KtCompiler( protected val environment: KotlinCoreEnvironment = createKotlinCoreEnvironment() ) { - protected val psiFileFactory: PsiFileFactory = PsiFileFactory.getInstance(environment.project) + protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false) fun compile(basePath: Path, path: Path): KtFile { require(Files.isRegularFile(path)) { "Given sub path ($path) should be a regular file!" } @@ -33,14 +31,12 @@ open class KtCompiler( val absolutePath = path.toAbsolutePath().normalize() val lineSeparator = content.determineLineSeparator() - val psiFile = psiFileFactory.createFileFromText( + val psiFile = psiFileFactory.createPhysicalFile( path.fileName.toString(), - KotlinLanguage.INSTANCE, - StringUtilRt.convertLineSeparators(content), - true, true, false, - LightVirtualFile(path.toString()) + StringUtilRt.convertLineSeparators(content) ) - return (psiFile as? KtFile ?: error("kotlin file expected")).apply { + + return psiFile.apply { putUserData(LINE_SEPARATOR, lineSeparator) putUserData(RELATIVE_PATH, relativePath.toString()) putUserData(ABSOLUTE_PATH, absolutePath.toString()) diff --git a/detekt-parser/src/test/kotlin/io/github/detekt/parser/KtCompilerTest.kt b/detekt-parser/src/test/kotlin/io/github/detekt/parser/KtCompilerTest.kt index cdd3c0333..a49040fcc 100644 --- a/detekt-parser/src/test/kotlin/io/github/detekt/parser/KtCompilerTest.kt +++ b/detekt-parser/src/test/kotlin/io/github/detekt/parser/KtCompilerTest.kt @@ -3,9 +3,11 @@ package io.github.detekt.parser import io.github.detekt.psi.LINE_SEPARATOR import io.github.detekt.psi.RELATIVE_PATH import io.github.detekt.test.utils.resource +import io.github.detekt.test.utils.resourceAsPath import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatIllegalArgumentException -import org.assertj.core.api.Assertions.assertThatIllegalStateException +import org.jetbrains.kotlin.com.intellij.psi.PsiErrorElement +import org.jetbrains.kotlin.psi.KtTreeVisitorVoid import org.spekframework.spek2.Spek import org.spekframework.spek2.style.specification.describe import java.nio.file.Paths @@ -32,10 +34,18 @@ class KtCompilerTest : Spek({ .withMessageEndingWith(") should be a regular file!") } - it("throws an exception for a css file") { - val cssPath = Paths.get(resource("css")) - assertThatIllegalStateException() - .isThrownBy { ktCompiler.compile(cssPath, cssPath.resolve("test.css")) } + it("parses with errors for non kotlin files") { + val cssPath = resourceAsPath("css") + val ktFile = ktCompiler.compile(cssPath, cssPath.resolve("test.css")) + + val errors = mutableListOf() + ktFile.accept(object : KtTreeVisitorVoid() { + override fun visitErrorElement(element: PsiErrorElement) { + errors.add(element) + } + }) + + assertThat(errors).isNotEmpty() } } diff --git a/detekt-parser/src/test/resources/css/test.css b/detekt-parser/src/test/resources/css/test.css index e69de29bb..987139b7a 100644 --- a/detekt-parser/src/test/resources/css/test.css +++ b/detekt-parser/src/test/resources/css/test.css @@ -0,0 +1,3 @@ +body { + margin: 1rem; +} diff --git a/detekt-test-utils/src/main/kotlin/io/github/detekt/test/utils/KtTestCompiler.kt b/detekt-test-utils/src/main/kotlin/io/github/detekt/test/utils/KtTestCompiler.kt index 0c7a1537c..9be3b8332 100644 --- a/detekt-test-utils/src/main/kotlin/io/github/detekt/test/utils/KtTestCompiler.kt +++ b/detekt-test-utils/src/main/kotlin/io/github/detekt/test/utils/KtTestCompiler.kt @@ -13,7 +13,6 @@ import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.CompilerConfiguration -import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.psi.KtFile import java.io.File import java.nio.file.Path @@ -28,12 +27,11 @@ internal object KtTestCompiler : KtCompiler() { fun compile(path: Path) = compile(root, path) fun compileFromContent(@Language("kotlin") content: String, filename: String = TEST_FILENAME): KtFile { - val file = psiFileFactory.createFileFromText( + val file = psiFileFactory.createPhysicalFile( filename, - KotlinLanguage.INSTANCE, - StringUtilRt.convertLineSeparators(content)) as? KtFile - file?.putUserData(ABSOLUTE_PATH, filename) - return file ?: error("kotlin file expected") + StringUtilRt.convertLineSeparators(content)) + file.putUserData(ABSOLUTE_PATH, filename) + return file } /**