Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users and rule authors to specify a reason per configured value (…
…#4611) * implement first idea on values with reasons * have ExplainedValues implement List by delegation * Parse explained value defaults * fix error message * [WIP] support ExplainedValues in yaml * print markdown value in DefaultValue directly * quote values in yaml maps * Update detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt Co-authored-by: Brais Gabín <braisgabin@gmail.com> * Update detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ConfigProperty.kt Co-authored-by: Brais Gabín <braisgabin@gmail.com> * add test case for missing reason * fail fast in case of invalid configuration * rename explainedValues to valuesWithReason * add api documentation * resolve and suppress code smells * suppress UNCHECKED_CAST * merge RuleCollectorSpec * reformat spec * fix indentation of test code Co-authored-by: Markus Schwarz <post@markus-schwarz.net> Co-authored-by: Brais Gabín <braisgabin@gmail.com>
- Loading branch information
1 parent
fe03b6d
commit 16b6658
Showing
18 changed files
with
719 additions
and
163 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
42 changes: 42 additions & 0 deletions
42
detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/ValuesWithReason.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,42 @@ | ||
package io.gitlab.arturbosch.detekt.api | ||
|
||
/** | ||
* This factory method can be used by rule authors to specify one or many configuration values along with an | ||
* explanation for each value. For example: | ||
* | ||
* ```kotlin | ||
* @Configuration("imports which should not be used") | ||
* private val imports: ValuesWithReason by config( | ||
* valuesWithReason("org.junit.Test" to "Do not use Junit4. Use org.junit.jupiter.api.Test instead.") | ||
* ) | ||
* ``` | ||
* | ||
* Note that the [config] property delegate only supports the factory methods when defining [ValuesWithReason]. | ||
*/ | ||
fun valuesWithReason(vararg values: Pair<String, String?>): ValuesWithReason { | ||
return valuesWithReason(values.map { ValueWithReason(it.first, it.second) }) | ||
} | ||
|
||
/** | ||
* This factory method can be used by rule authors to specify one or many configuration values along with an | ||
* explanation for each value. | ||
* | ||
* Note that the [config] property delegate only supports the factory methods when defining [ValuesWithReason]. | ||
*/ | ||
fun valuesWithReason(values: List<ValueWithReason>): ValuesWithReason { | ||
return ValuesWithReason(values) | ||
} | ||
|
||
/** | ||
* [ValuesWithReason] is essentially the same as [List] of [ValueWithReason]. Due to type erasure we cannot use the | ||
* list directly. Instances of this type should always created using the [valuesWithReason] factory method. | ||
*/ | ||
data class ValuesWithReason internal constructor(private val values: List<ValueWithReason>) : | ||
Iterable<ValueWithReason> by values | ||
|
||
/** | ||
* A ValueWithReason represents a single configuration value that may have an explanation as to why it is used. | ||
* @property value the actual value that is configured | ||
* @property reason an optional explanation for the configured value | ||
*/ | ||
data class ValueWithReason(val value: String, val reason: String? = null) |
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
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,15 @@ | ||
style: | ||
AsList: | ||
values: | ||
- a | ||
- b | ||
- c | ||
AsListOfMaps: | ||
values: | ||
- value: a | ||
reason: reason A | ||
- value: b | ||
- value: c | ||
reason: reason C | ||
EmptyListOfMaps: | ||
values: [] |
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
Oops, something went wrong.