Skip to content

Commit

Permalink
Prevent unwanted joining of KDoc with preceding type-parameter-list (#…
Browse files Browse the repository at this point in the history
…2381)

Closes #2360
  • Loading branch information
paul-dingemans committed Nov 26, 2023
1 parent c2697f6 commit f7b6495
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
Expand Down Expand Up @@ -91,19 +92,34 @@ public class TypeParameterListSpacingRule :
?.takeIf { it.elementType == WHITE_SPACE && it.nextCodeSibling()?.elementType == PRIMARY_CONSTRUCTOR }
?.let { whiteSpace ->
if (whiteSpace.nextCodeSibling()?.findChildByType(CONSTRUCTOR_KEYWORD) != null) {
// Single space or newline expected before (modifier list of) constructor
// class Bar<T> constructor(...)
// class Bar<T> actual constructor(...)
// class Bar<T> @SomeAnnotation constructor(...)
if (whiteSpace.text != " " && whiteSpace.text != indentConfig.childIndentOf(node)) {
emit(
whiteSpace.startOffset,
"Expected a single space or newline (with indent)",
true,
)
if (autoCorrect) {
// If line is to be wrapped this should have been done by other rules before running this rule
whiteSpace.upsertWhitespaceBeforeMe(" ")
if (whiteSpace.isWhiteSpaceWithNewline()) {
// Newline is acceptable before (modifier list of) constructor
// class Bar<T>
// constructor(...)
// class Bar<T>
// actual constructor(...)
// class Bar<T>
// @SomeAnnotation constructor(...)
// class Bar<T>
// @SomeAnnotation1
// @SomeAnnotation2
// constructor(...)
// class Bar<T>
// /**
// * Some kdoc
// */
// constructor(...)
} else {
// Single space before (modifier list of) constructor
// class Bar<T> constructor(...)
// class Bar<T> actual constructor(...)
// class Bar<T> @SomeAnnotation constructor(...)
if (whiteSpace.text != " ") {
emit(whiteSpace.startOffset, "Expected a single space", true)
if (autoCorrect) {
// If line is to be wrapped this should have been done by other rules before running this rule
whiteSpace.upsertWhitespaceBeforeMe(" ")
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CODE_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleValue.android_studio
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -201,12 +203,12 @@ class TypeParameterListSpacingRuleTest {
""".trimIndent()
typeParameterListSpacingRuleAssertThat(code)
.hasLintViolations(
LintViolation(1, 16, "Expected a single space or newline (with indent)"),
LintViolation(2, 16, "Expected a single space or newline (with indent)"),
LintViolation(3, 16, "Expected a single space or newline (with indent)"),
LintViolation(4, 16, "Expected a single space or newline (with indent)"),
LintViolation(5, 16, "Expected a single space or newline (with indent)"),
LintViolation(6, 16, "Expected a single space or newline (with indent)"),
LintViolation(1, 16, "Expected a single space"),
LintViolation(2, 16, "Expected a single space"),
LintViolation(3, 16, "Expected a single space"),
LintViolation(4, 16, "Expected a single space"),
LintViolation(5, 16, "Expected a single space"),
LintViolation(6, 16, "Expected a single space"),
).isFormattedAs(formattedCode)
}

Expand Down Expand Up @@ -318,6 +320,21 @@ class TypeParameterListSpacingRuleTest {
.hasNoLintViolations()
}

@Test
fun `Issue 2360 - Given a class with a KDoc on an explicit constructor starting on a separate line then do not report a violation`() {
val code =
"""
class Foo<T>
/**
* some-comment
*/
constructor(param: T)
""".trimIndent()
typeParameterListSpacingRuleAssertThat(code)
.withEditorConfigOverride(CODE_STYLE_PROPERTY to android_studio)
.hasNoLintViolations()
}

@Test
fun `Given a class with an annotated constructor on same line as parameter type list then do not report a violation`() {
val code =
Expand All @@ -327,4 +344,14 @@ class TypeParameterListSpacingRuleTest {
typeParameterListSpacingRuleAssertThat(code)
.hasNoLintViolations()
}

@Test
fun `Issue 2360`() {
val code =
"""
class Foo<out T> @Bar constructor(val value: T?) : FooBar<T>()
""".trimIndent()
typeParameterListSpacingRuleAssertThat(code)
.hasNoLintViolations()
}
}

0 comments on commit f7b6495

Please sign in to comment.