Skip to content

Instantly share code, notes, and snippets.

View drawers's full-sized avatar
🟩
In greenish twilight at the bottom of the Rhine

David Rawson drawers

🟩
In greenish twilight at the bottom of the Rhine
View GitHub Profile
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.",
private fun compilation(vararg source: SourceFile) = KotlinCompilation().apply {
sources = source.toList()
symbolProcessorProviders = listOf(IntSummableProcessorProvider())
workingDir = temporaryFolder.root
inheritClassPath = true
verbose = false
}
dependencies {
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.4")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing-ksp:1.4.4")
}
@Test
fun `target is a data class`() {
val kotlinSource = SourceFile.kotlin(
"file1.kt",
"""
package com.tests.summable
import com.tsongkha.kspexample.annotation.IntSummable
@IntSummable
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()
}"
)
}
// continued from above
if (summables.isEmpty()) {
return
}
val fileSpec = FileSpec.builder(
packageName = packageName,
fileName = classDeclaration.simpleName.asString() + "Ext"
).apply {
addFunction(
dependencies {
implementation("com.squareup:kotlinpoet:1.10.1")
implementation("com.squareup:kotlinpoet-ksp:1.10.1")
}
plugins {
kotlin("jvm")
id("com.google.devtools.ksp") version "1.5.31-1.0.0"
}
repositories {
mavenCentral()
google()
}
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()
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
)