Skip to content

Commit

Permalink
Ignore missing whitespace after trailing comma in single line paramet…
Browse files Browse the repository at this point in the history
…er value list

Closes #2794
  • Loading branch information
paul-dingemans committed Sep 18, 2024
1 parent a997d75 commit c0f608b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
}
Expand Down

0 comments on commit c0f608b

Please sign in to comment.