mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 00:01:19 +00:00
Fix Signatures.kt:buildFunctionSignature (#4961)
* Fix Signatures.kt:buildFunctionSignature 'buildFunctionSignature' fails when function body has KDoc. * Simplify tests * Skip comments
This commit is contained in:
committed by
GitHub
parent
5170077edc
commit
2b068e0042
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments
|
||||
import java.io.File
|
||||
|
||||
private val multipleWhitespaces = Regex("\\s{2,}")
|
||||
@@ -92,21 +93,15 @@ private fun buildClassSignature(classOrObject: KtClassOrObject): String {
|
||||
}
|
||||
|
||||
private fun buildFunctionSignature(element: KtNamedFunction): String {
|
||||
var methodStart = 0
|
||||
element.docComment?.let {
|
||||
methodStart = it.endOffset - it.startOffset
|
||||
}
|
||||
var methodEnd = element.endOffset - element.startOffset
|
||||
val typeReference = element.typeReference
|
||||
if (typeReference != null) {
|
||||
methodEnd = typeReference.endOffset - element.startOffset
|
||||
val startOffset = element.startOffsetSkippingComments - element.startOffset
|
||||
val endOffset = if (element.typeReference != null) {
|
||||
element.typeReference?.endOffset ?: 0
|
||||
} else {
|
||||
element.valueParameterList?.let {
|
||||
methodEnd = it.endOffset - element.startOffset
|
||||
}
|
||||
element.valueParameterList?.endOffset ?: 0
|
||||
} - element.startOffset
|
||||
|
||||
require(startOffset < endOffset) {
|
||||
"Error building function signature with range $startOffset - $endOffset for element: ${element.text}"
|
||||
}
|
||||
require(methodStart < methodEnd) {
|
||||
"Error building function signature with range $methodStart - $methodEnd for element: ${element.text}"
|
||||
}
|
||||
return element.text.substring(methodStart, methodEnd)
|
||||
return element.text.substring(startOffset, endOffset)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.gitlab.arturbosch.detekt.api.internal
|
||||
|
||||
import io.github.detekt.test.utils.compileContentForTest
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class SignaturesSpec {
|
||||
@Test
|
||||
fun `function with type reference`() {
|
||||
val result = compileContentForTest("fun data(): Int = 0")
|
||||
.findChildByClass(KtNamedFunction::class.java)!!
|
||||
.buildFullSignature()
|
||||
|
||||
assertThat(result).isEqualTo("Test.kt\$fun data(): Int")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `function without type reference`() {
|
||||
val result = compileContentForTest("fun data() = 0")
|
||||
.findChildByClass(KtNamedFunction::class.java)!!
|
||||
.buildFullSignature()
|
||||
|
||||
assertThat(result).isEqualTo("Test.kt\$fun data()")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `function with comments`() {
|
||||
val result = compileContentForTest("/* comments */ fun data() = 0")
|
||||
.findChildByClass(KtNamedFunction::class.java)!!
|
||||
.buildFullSignature()
|
||||
|
||||
assertThat(result).isEqualTo("Test.kt\$fun data()")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `function throws exception`() {
|
||||
assertThatThrownBy {
|
||||
compileContentForTest("{ fun data() = 0 }")
|
||||
.findChildByClass(KtNamedFunction::class.java)!!
|
||||
.buildFullSignature()
|
||||
}
|
||||
.isInstanceOf(IllegalArgumentException::class.java)
|
||||
.hasMessageContaining("Error building function signature")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user