Skip to content

Commit

Permalink
Fix Signatures.kt:buildFunctionSignature (#4961)
Browse files Browse the repository at this point in the history
* Fix Signatures.kt:buildFunctionSignature

'buildFunctionSignature' fails when function body has KDoc.

* Simplify tests

* Skip comments
  • Loading branch information
VitalyVPinchuk committed Jun 27, 2022
1 parent 5170077 commit 2b068e0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
Expand Up @@ -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,}")
Expand Down Expand Up @@ -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
}
}
require(methodStart < methodEnd) {
"Error building function signature with range $methodStart - $methodEnd for element: ${element.text}"
element.valueParameterList?.endOffset ?: 0
} - element.startOffset

require(startOffset < endOffset) {
"Error building function signature with range $startOffset - $endOffset 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")
}
}

0 comments on commit 2b068e0

Please sign in to comment.