diff --git a/CHANGELOG.md b/CHANGELOG.md index 7063160be7..5bfcffe5d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter ### Fixed - Fix check of spacing in the receiver type of an anonymous function ([#1440](https://github.com/pinterest/ktlint/issues/1440)) +- Allow comment on same line as super class in class declaration `wrapping` ([#1457](https://github.com/pinterest/ktlint/pull/1457)) ### Changed * Set Kotlin development version to `1.7.0-Beta` and Kotlin version to `1.6.21`. diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt index d4a3e6ff9b..568016f4dc 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt @@ -210,7 +210,10 @@ public class WrappingRule : Rule( // put entries on separate lines // TODO: group emit()s below with the one above into one (similar to ParameterListWrappingRule) for (c in node.children()) { - if (c.elementType == COMMA && !c.treeNext.isWhiteSpaceWithNewline()) { + if (c.elementType == COMMA && + !c.treeNext.isWhiteSpaceWithNewline() && + !c.isFollowedByCommentOnSameLine() + ) { requireNewlineAfterLeaf( nodeAfterWhichNewlineIsRequired = c, autoCorrect = autoCorrect, @@ -222,6 +225,10 @@ public class WrappingRule : Rule( } } + private fun ASTNode.isFollowedByCommentOnSameLine() = + nextLeaf { !it.isWhiteSpaceWithoutNewline() } + ?.isPartOfComment() == true + private fun rearrangeValueList( node: ASTNode, autoCorrect: Boolean, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt index 62894ae4e2..f707770346 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt @@ -317,6 +317,38 @@ internal class WrappingRuleTest { ).isEmpty() } + @Test + fun `test wrapping rule allows line comment`() { + val code = + """ + interface Foo1 {} + interface Foo2 {} + interface Foo3 {} + class Bar : + Foo1, // this comment should be legal + Foo2,// this comment should be legal + Foo3 { + } + """.trimIndent() + wrappingRuleAssertThat(code).hasNoLintViolations() + } + + @Test + fun `test wrapping rule allows block comment`() { + val code = + """ + interface Foo1 {} + interface Foo2 {} + interface Foo3 {} + class Bar : + Foo1, /* this comment should be legal */ + Foo2,/* this comment should be legal */ + Foo3 { + } + """.trimIndent() + wrappingRuleAssertThat(code).hasNoLintViolations() + } + @Test fun testLintNewlineAfterEqAllowed() { assertThat(