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

Wrapping rule should not prevent comments for interfaces on separate lines #1457

Merged
merged 5 commits into from
May 8, 2022
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
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.6.21` 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()
}

paul-dingemans marked this conversation as resolved.
Show resolved Hide resolved
@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