Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5be0d8
commit e564057
Showing
6 changed files
with
164 additions
and
26 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
27 changes: 27 additions & 0 deletions
27
...rc/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/TrailingCommaOnCallSite.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,27 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties | ||
import com.pinterest.ktlint.ruleset.standard.TrailingCommaOnCallSiteRule | ||
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 [ktlint docs](https://pinterest.github.io/ktlint/rules/standard/) for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.23.0") | ||
class TrailingCommaOnCallSite(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = TrailingCommaOnCallSiteRule() | ||
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 call side") | ||
private val allowTrailingComma by config(false) | ||
|
||
override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> = | ||
mapOf( | ||
TrailingCommaOnCallSiteRule.allowTrailingCommaOnCallSiteProperty to allowTrailingComma.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
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
112 changes: 112 additions & 0 deletions
112
.../test/kotlin/io/gitlab/arturbosch/detekt/formatting/TrailingCommaOnDeclarationSiteSpec.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,112 @@ | ||
package io.gitlab.arturbosch.detekt.formatting | ||
|
||
import io.gitlab.arturbosch.detekt.formatting.wrappers.TrailingCommaOnDeclarationSite | ||
import io.gitlab.arturbosch.detekt.test.TestConfig | ||
import io.gitlab.arturbosch.detekt.test.assertThat | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* 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 TrailingCommaOnDeclarationSiteSpec { | ||
|
||
@Nested | ||
inner class `unnecessary comma` { | ||
|
||
@Test | ||
fun `reports unnecessary comma on function call`() { | ||
val code = """ | ||
val foo1 = listOf("a", "b",) | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports unnecessary comma on constructor call`() { | ||
val code = """ | ||
val foo2 = Pair(1, 2,) | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports unnecessary comma on generic type definition`() { | ||
val code = """ | ||
val foo3: List<String,> = emptyList() | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports unnecessary comma on array get`() { | ||
val code = """ | ||
val foo4 = Array(2) { 42 } | ||
val bar4 = foo4[1,] | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports unnecessary comma on annotation`() { | ||
val code = """ | ||
@Foo5([1, 2,]) | ||
val foo5: Int = 0 | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class `missing comma` { | ||
|
||
@Test | ||
fun `reports missing comma on field definition`() { | ||
val code = """ | ||
data class Foo1(val bar: Int) | ||
data class Foo2( | ||
val bar: Int | ||
) | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to true))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports missing comma on function call`() { | ||
val code = """ | ||
val foo1 = listOf("a", "b") | ||
val foo2 = listOf( | ||
"a", | ||
"b" | ||
) | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to true))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
|
||
@Test | ||
fun `reports missing comma on constructor call`() { | ||
val code = """ | ||
val foo2 = Pair(1, 2) | ||
val foo2 = Pair( | ||
1, | ||
2 | ||
) | ||
""".trimIndent() | ||
val findings = TrailingCommaOnDeclarationSite(TestConfig(mapOf(ALLOW_TRAILING_COMMA to true))).lint(code) | ||
assertThat(findings).hasSize(1) | ||
} | ||
} | ||
|
||
private companion object { | ||
private const val ALLOW_TRAILING_COMMA = "allowTrailingComma" | ||
} | ||
} |
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