Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump KtLint to 0.43.2 and add TrailingComma rule (#4227)
* 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
1 parent
fc51b53
commit 21a751a
Showing
6 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...rmatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/TrailingComma.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...kt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters