This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.gitlab.arturbosch.detekt.api.* | |
import org.jetbrains.kotlin.psi.* | |
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType | |
class DoubleNegativeTakeUnless(config: Config = Config.empty) : Rule(config) { | |
override val issue = Issue( | |
"DoubleNegativeTakeUnless", | |
Severity.Style, | |
"Using double negatives with `takeUnless` can make the code harder to read.", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun compilation(vararg source: SourceFile) = KotlinCompilation().apply { | |
sources = source.toList() | |
symbolProcessorProviders = listOf(IntSummableProcessorProvider()) | |
workingDir = temporaryFolder.root | |
inheritClassPath = true | |
verbose = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.4") | |
testImplementation("com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.4") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `target is a data class`() { | |
val kotlinSource = SourceFile.kotlin( | |
"file1.kt", | |
""" | |
package com.tests.summable | |
import com.tsongkha.kspexample.annotation.IntSummable | |
@IntSummable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun KotlinCompilation.generatedSourceFor(fileName: String): String { | |
return kspSourcesDir.walkTopDown() | |
.firstOrNull { it.name == fileName } | |
?.readText() | |
?: throw IllegalArgumentException( | |
"Unable to find $fileName in ${ | |
kspSourcesDir.walkTopDown().filter { it.isFile }.toList() | |
}" | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// continued from above | |
if (summables.isEmpty()) { | |
return | |
} | |
val fileSpec = FileSpec.builder( | |
packageName = packageName, | |
fileName = classDeclaration.simpleName.asString() + "Ext" | |
).apply { | |
addFunction( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation("com.squareup:kotlinpoet:1.10.1") | |
implementation("com.squareup:kotlinpoet-ksp:1.10.1") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
kotlin("jvm") | |
id("com.google.devtools.ksp") version "1.5.31-1.0.0" | |
} | |
repositories { | |
mavenCentral() | |
google() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inner class Visitor : KSVisitorVoid() { | |
private lateinit var className: String | |
private lateinit var packageName: String | |
private val summables: MutableList<String> = mutableListOf() | |
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | |
val qualifiedName = classDeclaration.qualifiedName?.asString() | |
className = qualifiedName | |
packageName = classDeclaration.packageName.asString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inner class Visitor : KSVisitorVoid() { | |
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) { | |
val qualifiedName = classDeclaration.qualifiedName?.asString() | |
if (!classDeclaration.isDataClass()) { | |
logger.error( | |
"@IntSummable cannot target non-data class $qualifiedName", | |
classDeclaration | |
) |