Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Signatures.kt:buildFunctionSignature #4961

Merged
merged 4 commits into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
}