Skip to content

Commit

Permalink
Fix indentation before semi-colon when it is pushed down after insert…
Browse files Browse the repository at this point in the history
…ing a trailing comma

Closes #1609
  • Loading branch information
paul-dingemans committed Sep 3, 2022
1 parent 3c71f71 commit 6dc9f0f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
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
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
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)
}
}

0 comments on commit 6dc9f0f

Please sign in to comment.