Skip to content

Commit

Permalink
Wrapping rule should not prevent comments for interfaces on separate …
Browse files Browse the repository at this point in the history
…lines (#1457)

* Wrapping rule should not prevent comments for interfaces on separate lines

Closes #1457

Co-authored-by: paul-dingemans
  • Loading branch information
hbmartin authored May 8, 2022
1 parent acf1188 commit 504162c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -222,6 +225,10 @@ public class WrappingRule : Rule(
}
}

private fun ASTNode.isFollowedByCommentOnSameLine() =
nextLeaf { !it.isWhiteSpaceWithoutNewline() }
?.isPartOfComment() == true

private fun rearrangeValueList(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 504162c

Please # to comment.