From a39904a47abddadacc1e3d025ae6c2e54dba55c6 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Fri, 6 Oct 2023 13:17:14 +0200 Subject: [PATCH] Improve violation message in `discouraged-comment-location` (#2293) * Improve violation message in `discouraged-comment-location` Closes #2292 * Fix lint violation --- CHANGELOG.md | 1 + .../rules/DiscouragedCommentLocationRule.kt | 3 +- .../DiscouragedCommentLocationRuleTest.kt | 57 ++++++++++++++----- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 129f6fdad3..26597d8495 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRule.kt index f837d11b05..314a10d99a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRule.kt @@ -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, ) } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRuleTest.kt index e1d21d399a..90335c5e3c 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/DiscouragedCommentLocationRuleTest.kt @@ -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() @@ -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"), ) } @@ -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."), ) } @@ -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"), @@ -636,7 +641,6 @@ class DiscouragedCommentLocationRuleTest { > class Foo2 """.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"), @@ -653,7 +657,6 @@ class DiscouragedCommentLocationRuleTest { Bar> class FooBar2 """.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"), @@ -715,7 +718,6 @@ class DiscouragedCommentLocationRuleTest { val fooBar2: FooBar """.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"), @@ -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."), + ) + } }