diff --git a/CHANGELOG.md b/CHANGELOG.md index 4483995b43..b34b498804 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). * Ignore function naming in Kotest classes `function-naming` [#2289](https://github.com/pinterest/ktlint/issue/2289) * Prevent wrapping of nested multiline binary expression before operation reference as it results in a compilation error `multiline-expression-wrapping` [#2286](https://github.com/pinterest/ktlint/issue/2286) * Force blank line before object declaration if preceded by another declaration `blank-line-before-declaration` [#2284](https://github.com/pinterest/ktlint/issues/2284) -* Fix malformed AST when `&&` or `||` is at start of line `chain-wrapping` [#2297](https://github.com/pinterest/ktlint/issues/2297) +* Fix malformed AST when `&&` or `||` is at start of line `chain-wrapping` [#2297](https://github.com/pinterest/ktlint/issues/2297) +* Do not report false positives `type-argument-list-spacing` and `type-parameter-list-spacing` [#2299](https://github.com/pinterest/ktlint/issues/2299) ### Changed diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRule.kt index 5de1063e14..9848ea1197 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRule.kt @@ -12,6 +12,7 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPE import com.pinterest.ktlint.rule.engine.core.api.findCompositeParentElementOfType import com.pinterest.ktlint.rule.engine.core.api.isPartOfCompositeElementOfType import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace +import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline import com.pinterest.ktlint.rule.engine.core.api.nextLeaf import com.pinterest.ktlint.rule.engine.core.api.nextSibling import com.pinterest.ktlint.rule.engine.core.api.prevLeaf @@ -109,9 +110,13 @@ public class TypeArgumentListSpacingRule : ?.let { nextSibling -> if (multiline) { if (nextSibling.text != expectedIndent) { - emit(nextSibling.startOffset, "Expected newline", true) - if (autoCorrect) { - nextSibling.upsertWhitespaceAfterMe(expectedIndent) + if (nextSibling.isWhiteSpaceWithoutNewline()) { + emit(nextSibling.startOffset, "Expected newline", true) + if (autoCorrect) { + nextSibling.upsertWhitespaceAfterMe(expectedIndent) + } + } else { + // Let Indentation rule fix the indentation } } } else { @@ -129,9 +134,13 @@ public class TypeArgumentListSpacingRule : ?.let { prevSibling -> if (multiline) { if (prevSibling.text != expectedIndent) { - emit(prevSibling.startOffset, "Expected newline", true) - if (autoCorrect) { - prevSibling.upsertWhitespaceBeforeMe(expectedIndent) + if (prevSibling.isWhiteSpaceWithoutNewline()) { + emit(prevSibling.startOffset, "Expected newline", true) + if (autoCorrect) { + prevSibling.upsertWhitespaceBeforeMe(expectedIndent) + } + } else { + // Let Indentation rule fix the indentation } } } else { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRule.kt index 42d7968329..ece836f096 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRule.kt @@ -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.isWhiteSpaceWithoutNewline import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling import com.pinterest.ktlint.rule.engine.core.api.nextLeaf import com.pinterest.ktlint.rule.engine.core.api.nextSibling @@ -222,7 +223,7 @@ public class TypeParameterListSpacingRule : } } - expectedWhitespace.startsWith("\n") -> { + node.isWhiteSpaceWithoutNewline() && expectedWhitespace.startsWith("\n") -> { emit( node.startOffset, "Expected a newline", diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRuleTest.kt index 842b162961..37282d13a3 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeArgumentListSpacingRuleTest.kt @@ -82,4 +82,22 @@ class TypeArgumentListSpacingRuleTest { """.trimIndent() typeArgumentListSpacingRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2299 - Given a correctly indented multiline type argument list then do not report violations`() { + val code = + """ + class FooBar( + foo: String, + bar: Int, + ) : Baz< + String, + Int, + >( + foo, + bar, + ) + """.trimIndent() + typeArgumentListSpacingRuleAssertThat(code).hasNoLintViolations() + } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRuleTest.kt index df326e4904..425a20c603 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TypeParameterListSpacingRuleTest.kt @@ -283,7 +283,7 @@ class TypeParameterListSpacingRuleTest { } @Test - fun `Issue 1867 - Given a multiline type parameter list which is correctly formatted then do not report a violation`() { + fun `Issue 1867 - Given a function with multiline type parameter list which is correctly formatted then do not report a violation`() { val code = """ fun < @@ -291,8 +291,19 @@ class TypeParameterListSpacingRuleTest { Bar, > foobar() """.trimIndent() - typeParameterListSpacingRuleAssertThat(code) - .hasNoLintViolations() + typeParameterListSpacingRuleAssertThat(code).hasNoLintViolations() + } + + @Test + fun `Issue 2299 - Given a class with multiline type parameter list which is correctly formatted then do not report a violation`() { + val code = + """ + class FooBar< + Foo, + Bar, + > + """.trimIndent() + typeParameterListSpacingRuleAssertThat(code).hasNoLintViolations() } @Test