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

Prevent unwanted joining of KDoc with preceding type-parameter-list #2381

Merged
merged 1 commit into from
Nov 26, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}