From 60efc8f16a56840dfbcebc050861fb898949fddf Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 3 Sep 2024 18:41:13 +0200 Subject: [PATCH] Fix false positive in `class-signature` when EOL comment is between a class annotation and other class modifier (#2786) Closes #2755 --- .../ruleset/standard/rules/ClassSignatureRule.kt | 3 ++- .../standard/rules/ClassSignatureRuleTest.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt index 1635d5c1b9..381f96ad3d 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt @@ -149,7 +149,8 @@ public class ClassSignatureRule : currentNode = iterator.next() if (currentNode.elementType != ANNOTATION && currentNode.elementType != ANNOTATION_ENTRY && - currentNode.elementType != WHITE_SPACE + currentNode.elementType != WHITE_SPACE && + currentNode.elementType != EOL_COMMENT ) { return currentNode } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt index a0de8fdce4..4bb734041c 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt @@ -1849,6 +1849,20 @@ class ClassSignatureRuleTest { classSignatureWrappingRuleAssertThat(code).hasNoLintViolations() } + @Test + fun `Issue 2755 - Given a class preceded by an annotation, and EOL comment, and another class modifier then ignore the EOL comment in determination of class signature length`() { + val code = + """ + // $MAX_LINE_LENGTH_MARKER $EOL_CHAR + @Suppress("UnnecessaryAbstractClass") // some comment + abstract class Bar(val baz1: String, val baz2: String) + """.trimIndent() + classSignatureWrappingRuleAssertThat(code) + .setMaxLineLength() + .withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset") + .hasNoLintViolations() + } + private companion object { const val UNEXPECTED_SPACES = " " const val NO_SPACE = ""