Skip to content

Commit

Permalink
Bump KtLint to 0.43.2 and add TrailingComma rule (#4227)
Browse files Browse the repository at this point in the history
* Bump KtLint to 0.43.0 and add TrailingComma rule

* KtLint to 0.43.1

* KtLint to 0.43.2

* Update detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/TrailingComma.kt

Co-authored-by: Matthew Haughton <3flex@users.noreply.github.com>

Co-authored-by: Chao Zhang <chao.zhang@instacart.com>
Co-authored-by: Matthew Haughton <3flex@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 3, 2022
1 parent fc51b53 commit 21a751a
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 2 deletions.
5 changes: 5 additions & 0 deletions detekt-core/src/main/resources/default-detekt-config.yml
Expand Up @@ -409,6 +409,11 @@ formatting:
StringTemplate:
active: true
autoCorrect: true
TrailingComma:
active: false
autoCorrect: true
allowTrailingComma: false
allowTrailingCommaOnCallSite: false

naming:
active: true
Expand Down
Expand Up @@ -47,6 +47,7 @@ import io.gitlab.arturbosch.detekt.formatting.wrappers.SpacingAroundUnaryOperato
import io.gitlab.arturbosch.detekt.formatting.wrappers.SpacingBetweenDeclarationsWithAnnotations
import io.gitlab.arturbosch.detekt.formatting.wrappers.SpacingBetweenDeclarationsWithComments
import io.gitlab.arturbosch.detekt.formatting.wrappers.StringTemplate
import io.gitlab.arturbosch.detekt.formatting.wrappers.TrailingComma
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.JavaDummyElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.JavaDummyHolder
Expand Down Expand Up @@ -103,6 +104,7 @@ class KtLintMultiRule(config: Config = Config.empty) : MultiRule() {
SpacingAroundUnaryOperator(config),
SpacingBetweenDeclarationsWithAnnotations(config),
SpacingBetweenDeclarationsWithComments(config),
TrailingComma(config),
)

override fun visit(root: KtFile) {
Expand Down
@@ -0,0 +1,33 @@
package io.gitlab.arturbosch.detekt.formatting.wrappers

import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.formatting.FormattingRule

/**
* See <a href="https://ktlint.github.io/#rule-spacing">ktlint-website</a> for documentation.
*/
@OptIn(FeatureInAlphaState::class)
@AutoCorrectable(since = "1.20.0")
class TrailingComma(config: Config) : FormattingRule(config) {

override val wrapping = TrailingCommaRule()
override val issue = issueFor("Rule to mandate/forbid trailing commas")

@Configuration("Defines whether a trailing comma (or no trailing comma) should be enforced on the defining side")
private val allowTrailingComma by config(false)

@Configuration("Defines whether a trailing comma (or no trailing comma) should be enforced on the calling side")
private val allowTrailingCommaOnCallSite by config(false)

override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> =
mapOf(
TrailingCommaRule.allowTrailingCommaProperty to allowTrailingComma.toString(),
TrailingCommaRule.allowTrailingCommaOnCallSiteProperty to allowTrailingCommaOnCallSite.toString(),
)
}
Expand Up @@ -5,11 +5,12 @@ import io.github.detekt.test.utils.compileContentForTest
import io.github.detekt.test.utils.compileForTest
import io.github.detekt.test.utils.resource
import io.gitlab.arturbosch.detekt.api.Finding
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
import java.io.File
import java.nio.file.Paths

fun FormattingRule.lint(content: String, fileName: String = "Test.kt"): List<Finding> {
fun FormattingRule.lint(@Language("kotlin") content: String, fileName: String = "Test.kt"): List<Finding> {
val root = compileContentForTest(content, fileName)
this.visit(root)
root.node.visit { node -> this.apply(node) }
Expand Down
@@ -0,0 +1,109 @@
package io.gitlab.arturbosch.detekt.formatting

import io.gitlab.arturbosch.detekt.formatting.wrappers.TrailingComma
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

private const val ALLOW_TRAILING_COMMA = "allowTrailingComma"
private const val ALLOW_TRAILING_COMMA_ON_CALL_SITE = "allowTrailingCommaOnCallSite"

/**
* Some test cases were used directly from KtLint to verify the wrapper rule:
*
* https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TrailingCommaRuleTest.kt
*/
class TrailingCommaSpec : Spek({

describe("unnecessary comma") {

it("reports unnecessary comma on function call") {
val code =
"""
val foo1 = listOf("a", "b",)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports unnecessary comma on constructor call") {
val code =
"""
val foo2 = Pair(1, 2,)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports unnecessary comma on generic type definition") {
val code =
"""
val foo3: List<String,> = emptyList()
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports unnecessary comma on array get") {
val code =
"""
val foo4 = Array(2) { 42 }
val bar4 = foo4[1,]
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports unnecessary comma on annotation") {
val code =
"""
@Foo5([1, 2,])
val foo5: Int = 0
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
}

describe("missing comma") {

it("reports missing comma on function call") {
val code =
"""
data class Foo1(val bar: Int)
data class Foo2(
val bar: Int
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to true))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports missing comma on function call") {
val code =
"""
val foo1 = listOf("a", "b")
val foo2 = listOf(
"a",
"b"
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA_ON_CALL_SITE to true))).lint(code)
assertThat(findings).hasSize(1)
}

it("reports missing comma on constructor call") {
val code =
"""
val foo2 = Pair(1, 2)
val foo2 = Pair(
1,
2
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA_ON_CALL_SITE to true))).lint(code)
assertThat(findings).hasSize(1)
}
}
})
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Expand Up @@ -2,7 +2,7 @@
dokka = "1.6.10"
jacoco = "0.8.7"
kotlin = "1.6.10"
ktlint = "0.42.1"
ktlint = "0.43.2"
spek = "2.0.17"

[libraries]
Expand Down

0 comments on commit 21a751a

Please sign in to comment.