Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum trailing comma #1633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}
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