Skip to content

Commit

Permalink
Improve violation message in discouraged-comment-location (#2293)
Browse files Browse the repository at this point in the history
* Improve violation message in `discouraged-comment-location`

Closes #2292

* Fix lint violation
  • Loading branch information
paul-dingemans authored Oct 6, 2023
1 parent ea8df7e commit a39904a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Ignore override of function in rule `function-naming` [#2271](https://github.com/pinterest/ktlint/issue/2271)
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point 'function-expression-body' [#2269](https://github.com/pinterest/ktlint/issue/2269)
* Improve violation message in `discouraged-comment-location` [#2292](https://github.com/pinterest/ktlint/issue/2292)
* 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public class DiscouragedCommentLocationRule : StandardRule("discouraged-comment-
// of that type. However, if it would be possible, it is ok to emit the error analog to the VALUE_ARGUMENT
emit(
node.startOffset,
"A comment in a '${node.treeParentElementTypeName()}' is only allowed when placed on a new line before this element",
"A (block or EOL) comment inside or on same line after a '${node.treeParentElementTypeName()}' is not allowed. It " +
"may be placed on a separate line above.",
false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,21 @@ class DiscouragedCommentLocationRuleTest {
"""
class Foo1(
// some comment
val bar: Bar
val bar: Bar,
// some comment
val bar: Bar,
)
class Foo2(
/* some comment */
val bar: Bar
val bar: Bar,
/* some comment */
val bar: Bar,
)
class Foo3(
/** some comment */
val bar: Bar
val bar: Bar,
/** some comment */
val bar: Bar,
)
""".trimIndent()
discouragedCommentLocationRuleAssertThat(code).hasNoLintViolations()
Expand All @@ -531,11 +537,11 @@ class DiscouragedCommentLocationRuleTest {
val bar: /** some comment */ Bar
)
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
@Suppress("ktlint:standard:argument-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(3, 9, "A comment in a 'value_parameter' is only allowed when placed on a new line before this element"),
LintViolation(7, 14, "A comment in a 'value_parameter' is only allowed when placed on a new line before this element"),
LintViolation(3, 9, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
LintViolation(7, 14, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
LintViolation(10, 14, "A kdoc in a 'value_parameter' is only allowed when placed on a new line before this element"),
)
}
Expand All @@ -554,12 +560,12 @@ class DiscouragedCommentLocationRuleTest {
val bar: Bar /* some comment */
)
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
@Suppress("ktlint:standard:argument-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(2, 18, "A comment in a 'value_parameter' is only allowed when placed on a new line before this element"),
LintViolation(5, 18, "A comment in a 'value_parameter' is only allowed when placed on a new line before this element"),
LintViolation(8, 18, "A comment in a 'value_parameter' is only allowed when placed on a new line before this element"),
LintViolation(2, 18, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
LintViolation(5, 18, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
LintViolation(8, 18, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
)
}

Expand All @@ -574,7 +580,6 @@ class DiscouragedCommentLocationRuleTest {
val bar: Bar, /* some comment */
)
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(2, 19, "A comment in a 'value_parameter_list' is only allowed when placed on a separate line"),
Expand Down Expand Up @@ -636,7 +641,6 @@ class DiscouragedCommentLocationRuleTest {
>
class Foo2<Bar /* some comment */ >
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(2, 9, "A comment in a 'type_parameter_list' is only allowed when placed on a separate line"),
Expand All @@ -653,7 +657,6 @@ class DiscouragedCommentLocationRuleTest {
Bar>
class FooBar2<Foo, /* some comment */ Bar>
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(2, 10, "A comment in a 'type_parameter_list' is only allowed when placed on a separate line"),
Expand Down Expand Up @@ -715,7 +718,6 @@ class DiscouragedCommentLocationRuleTest {
val fooBar2: FooBar<Foo, /* some comment */
Bar>
""".trimIndent()
@Suppress("ktlint:standard:parameter-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(1, 26, "A comment in a 'type_argument_list' is only allowed when placed on a separate line"),
Expand Down Expand Up @@ -820,4 +822,31 @@ class DiscouragedCommentLocationRuleTest {
)
}
}

@Test
fun `Given value argument preceded by a KDOC plus an EOL or BLOCK comments on separate lines above`() {
val code =
"""
class Foo(
/** Some kdoc */
/* Some comment */
val bar1: Int,
/** Some kdoc */
// Some comment
val bar2: Int,
/* Some comment */
/** Some kdoc */
val bar3: Int,
// Some comment
/** Some kdoc */
val bar4: Int,
)
""".trimIndent()
@Suppress("ktlint:standard:argument-list-wrapping", "ktlint:standard:max-line-length")
discouragedCommentLocationRuleAssertThat(code)
.hasLintViolationsWithoutAutoCorrect(
LintViolation(3, 5, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
LintViolation(6, 5, "A (block or EOL) comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above."),
)
}
}

0 comments on commit a39904a

Please sign in to comment.