diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt index 97f3f6b0b2..b3755c421a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt @@ -5,6 +5,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.ANNOTATION_ENTRY import com.pinterest.ktlint.rule.engine.core.api.ElementType.COLON import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST +import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_REFERENCE import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST @@ -107,10 +108,10 @@ public class ParameterListSpacingRule : } COMMA -> { - // Comma must be followed by whitespace + // Comma, except when it is the trailing comma, must be followed by whitespace el .nextLeaf() - ?.takeIf { it.elementType != WHITE_SPACE } + ?.takeUnless { it.elementType == WHITE_SPACE || it.elementType == RPAR } ?.let { addMissingWhiteSpaceAfterMe(el, emit) } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt index 214d8b3127..f19ee61711 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt @@ -511,6 +511,16 @@ class ParameterListSpacingRuleTest { .isFormattedAs(formattedCode) } + @Test + fun `Issue 2794 - Given a single line function signature and the last parameter is followed by a trailing comma`() { + val code = + """ + fun foo(bar: Int,) { + } + """.trimIndent() + parameterListSpacingRuleAssertThat(code).hasNoLintViolations() + } + private companion object { const val TOO_MANY_SPACES = " " }