Skip to content

Commit

Permalink
Merge pull request #1633 from paul-dingemans/1609-enum-trailing-comma
Browse files Browse the repository at this point in the history
enum trailing comma
  • Loading branch information
paul-dingemans authored Sep 4, 2022
2 parents e8ac8f6 + 731238d commit 55b2048
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed
* Do not add trailing comma in empty parameter/argument list with comments (`trailing-comma-on-call-site`, `trailing-comma-on-declaration-site`) ([#1602](https://github.com/pinterest/ktlint/issue/1602))
* Fix class cast exception when specifying a non-string editorconfig setting in the default ".editorconfig" ([#1627](https://github.com/pinterest/ktlint/issue/1627))
* Fix class cast exception when specifying a non-string editorconfig setting in the default ".editorconfig" ([#1627](https://github.com/pinterest/ktlint/issue/1627))
* Fix indentation before semi-colon when it is pushed down after inserting a trailing comma ([#1609](https://github.com/pinterest/ktlint/issue/1609))

Do not show deprecation warning about property "disabled_rules" when using CLi-parameter `--disabled-rules` ([#1599](https://github.com/pinterest/ktlint/issues/1599))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ public class TrailingCommaOnDeclarationSiteRule :

if (inspectNode.treeParent.elementType == ElementType.ENUM_ENTRY) {
with(KtPsiFactory(prevNode.psi)) {
val parentIndent = (prevNode.psi.parent.prevLeaf() as? PsiWhiteSpace)?.text ?: "\n"
val parentIndent =
(prevNode.psi.parent.prevLeaf() as? PsiWhiteSpace)?.text
?: "\n${prevNode.lineIndent()}"
val newline = createWhiteSpace(parentIndent)
val enumEntry = inspectNode.treeParent.psi
enumEntry.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,4 +930,31 @@ class TrailingCommaOnDeclarationSiteRuleTest {
.withEditorConfigOverride(allowTrailingCommaProperty to true)
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 1609 - Given that a trailing comma is required then add trailing comma after last enum member and indent the semi-colon`() {
val code =
"""
enum class SomeEnum1(id: String) {
FOO("foo"),
BAR("bar");
fun doSomething(id: String) {}
}
""".trimIndent()
val formattedCode =
"""
enum class SomeEnum1(id: String) {
FOO("foo"),
BAR("bar"),
;
fun doSomething(id: String) {}
}
""".trimIndent()
ruleAssertThat(code)
.withEditorConfigOverride(allowTrailingCommaProperty to true)
.hasLintViolation(3, 15, "Missing trailing comma before \";\"")
.isFormattedAs(formattedCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal fun FileSystem.fileSequence(
return result.asSequence()
}

internal fun FileSystem.expand(
private fun FileSystem.expand(
patterns: List<String>,
rootDir: Path,
) =
Expand Down

0 comments on commit 55b2048

Please sign in to comment.