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 1 commit
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 @@ -92,21 +92,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.startOffset
val endOffset = if (element.typeReference != null) {
element.typeReference?.endOffset ?: 0
} else {
element.valueParameterList?.let {
methodEnd = it.endOffset - element.startOffset
}
element.valueParameterList?.endOffset ?: 0
}
require(methodStart < methodEnd) {
"Error building function signature with range $methodStart - $methodEnd for element: ${element.text}"

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(0, endOffset - startOffset)
}
@@ -0,0 +1,128 @@
package io.gitlab.arturbosch.detekt.api.internal

import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.test.compileAndLint
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 {
private var result = ""
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
private val subject = TestRule { result = it }

@Test
fun `function with type reference`() {
subject.compileAndLint(functionWithTypeReference())
assertThat(result).isEqualTo(signatureWithTypeReference())
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun `function without type reference`() {
subject.compileAndLint(functionWithoutTypeReference())
assertThat(result).isEqualTo(signatureWithoutTypeReference())
}

@Test
fun `function throws exception`() {
assertThatThrownBy {
subject.compileAndLint(functionWontCompile())
}
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("Error building function signature")
}
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
}

private class TestRule(val block: (String) -> Unit) : Rule() {
override val issue = Issue(javaClass.simpleName, Severity.CodeSmell, "", Debt.TWENTY_MINS)

override fun visitNamedFunction(function: KtNamedFunction) {
block(function.buildFullSignature())
}
}

private fun signatureWithTypeReference() = """
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data(): List<Array<out Any?>>
""".trimIndent()

private fun signatureWithoutTypeReference() = """
Test.kt${'$'}TestClass.Companion${'$'}@JvmStatic @Parameterized.Parameters(name = "parameterName") /** * Function KDoc */ fun data()
""".trimIndent()

private fun functionWithTypeReference() = """
@RunWith(Parameterized::class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data(): List<Array<out Any?>> =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()

private fun functionWithoutTypeReference() = """
@RunWith(Parameterized::class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data() =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()

private fun functionWontCompile() = """
@RunWith(Parameterized.class)
class TestClass {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "parameterName")
/**
* Function KDoc
*/
fun data() =
/**
* more description
* more description
* more description
* more description
* more description
*/
listOf(
arrayOf(1, 2, 3, 4),
arrayOf(11, 22, 33, 44)
)
}
}
""".trimIndent()