Skip to content

Commit

Permalink
Remove trailing comma if last two enum entries are on the same line a…
Browse files Browse the repository at this point in the history
…nd trailing commas are not allowed

Closes #1905
  • Loading branch information
paul-dingemans committed Mar 31, 2023
1 parent 75cfb75 commit b3208ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ Previously the default value for `.editorconfig` property `max_line_length` was
* Wrap each type parameter in a multiline type parameter list `wrapping` ([#1867](https://github.com/pinterest/ktlint/issues/1867))
* Allow value arguments with a multiline expression to be indented on a separate line `indent` ([#1217](https://github.com/pinterest/ktlint/issues/1217))
* When enabled, the ktlint rule checking is disabled for all code surrounded by the formatter tags (see [faq](https://pinterest.github.io/ktlint/faq/#are-formatter-tags-respected)) ([#670](https://github.com/pinterest/ktlint/issues/670))
* Remove trailing comma if last two enum entries are on the same line and trailing commas are not allowed. `trailing-comma-on-declaration-site` ([#1905](https://github.com/pinterest/ktlint/issues/1905))

### Changed
* Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_TYPE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHEN_ENTRY
Expand Down Expand Up @@ -184,6 +185,7 @@ public class TrailingCommaOnDeclarationSiteRule :
emit = emit,
)
}

private fun visitClass(
node: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
Expand All @@ -195,18 +197,30 @@ public class TrailingCommaOnDeclarationSiteRule :
node
.takeIf { psi.isEnum() }
?.findChildByType(ElementType.CLASS_BODY)
?.takeUnless {
it.noEnumEntries() || it.lastTwoEnumEntriesAreOnSameLine()
}?.let { classBody ->
?.takeUnless { it.noEnumEntries() }
?.let { classBody ->
classBody
.findNodeAfterLastEnumEntry()
?.let { nodeAfterLastEnumEntry ->
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = nodeAfterLastEnumEntry,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit,
)
when {
!node.isTrailingCommaAllowed() && nodeAfterLastEnumEntry.elementType == RBRACE -> {
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = nodeAfterLastEnumEntry,
isTrailingCommaAllowed = false,
autoCorrect = autoCorrect,
emit = emit,
)
}

!classBody.lastTwoEnumEntriesAreOnSameLine() -> {
node.reportAndCorrectTrailingCommaNodeBefore(
inspectNode = nodeAfterLastEnumEntry,
isTrailingCommaAllowed = node.isTrailingCommaAllowed(),
autoCorrect = autoCorrect,
emit = emit,
)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,16 +708,23 @@ class TrailingCommaOnDeclarationSiteRuleTest {
}

@Test
fun `Given that a trailing comma is not allowed then it is not removed for enums where last two entries are on same line`() {
fun `Given that a trailing comma is not allowed then remove it for enums where last two entries are on same line and followed by trailing comma`() {
val code =
"""
enum class Shape {
SQUARE, TRIANGLE,
}
""".trimIndent()
val formattedCode =
"""
enum class Shape {
SQUARE, TRIANGLE
}
""".trimIndent()
trailingCommaOnDeclarationSiteRuleAssertThat(code)
.withEditorConfigOverride(TRAILING_COMMA_ON_DECLARATION_SITE_PROPERTY to false)
.hasNoLintViolations()
.hasLintViolation(2, 21, "Unnecessary trailing comma before \"}\"")
.isFormattedAs(formattedCode)
}

@Nested
Expand Down

0 comments on commit b3208ca

Please sign in to comment.