-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore surrounding spaces around the "CodeStyle" and "RuleExecution" …
…values in the '.editorconfig' file The default EnumValueParser of ec4j does not ignore such spaces which incorrectly leads to values not parsed correctly. Additional trailing space typically result from EOL comments in the '.editorconfig' file. For example: `codestyle = official # some comment`. The comment (including the hash sign) is ignored correctly but the space which separates the value and the hash sign is added to the value itself.
- Loading branch information
1 parent
c3e1431
commit b2e6c37
Showing
6 changed files
with
139 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
49 changes: 49 additions & 0 deletions
49
...nt-core/src/main/kotlin/com/pinterest/ktlint/core/api/editorconfig/SafeEnumValueParser.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,49 @@ | ||
package com.pinterest.ktlint.core.api.editorconfig | ||
|
||
import java.util.Locale | ||
import org.ec4j.core.model.PropertyType | ||
import org.ec4j.core.model.PropertyType.PropertyValueParser | ||
|
||
/** | ||
* A [PropertyValueParser] implementation that allows only members of a given [Enum] type. This class is almost | ||
* identical to the original [EnumValueParser] provided by ec4j. Difference is that values are trimmed before trying to | ||
* match the enum values. | ||
* | ||
* As the ec4j project has not provided any new release since version 1.0 (2019-08-01) a custom implementation has been | ||
* added. | ||
* | ||
* @param <T> the type of the value <T> | ||
* | ||
*/ | ||
internal class SafeEnumValueParser<T : Enum<*>?>(enumType: Class<out T?>) : PropertyValueParser<T> { | ||
private val enumType: Class<out Enum<*>?> | ||
|
||
init { | ||
this.enumType = enumType | ||
} | ||
|
||
override fun parse(name: String?, value: String?): PropertyType.PropertyValue<T> = | ||
if (value == null) { | ||
PropertyType.PropertyValue.invalid(value, "Cannot make enum " + enumType.name + " out of null") | ||
} else { | ||
try { | ||
PropertyType.PropertyValue.valid( | ||
value, | ||
java.lang.Enum.valueOf( | ||
enumType, | ||
value | ||
// In case an EOL comment (separated with a space) is appended after the value then the comment | ||
// itself is removed but not the space. This results in the enum value not being parsed | ||
// correctly. | ||
.trim() | ||
.lowercase(Locale.getDefault()), | ||
) as T, | ||
) | ||
} catch (e: IllegalArgumentException) { | ||
PropertyType.PropertyValue.invalid( | ||
value, | ||
"Unexpected parsed \"" + value + "\" for enum " + enumType.name, | ||
) | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...st/kotlin/com/pinterest/ktlint/core/api/editorconfig/CodeStyleEditorConfigPropertyTest.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,30 @@ | ||
package com.pinterest.ktlint.core.api.editorconfig | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
class CodeStyleEditorConfigPropertyTest { | ||
@ParameterizedTest(name = "Value: [{0}], result: [{1}]") | ||
@CsvSource( | ||
value = [ | ||
"official,official", | ||
" official,official", | ||
"official ,official", | ||
" official ,official", | ||
"android,android", | ||
" android,android", | ||
"android ,android", | ||
" android ,android", | ||
], | ||
ignoreLeadingAndTrailingWhitespace = false, | ||
) | ||
fun `Given a code style property`( | ||
value: String, | ||
expectedCodeStyleValue: CodeStyleValue, | ||
) { | ||
val actual = CODE_STYLE_PROPERTY.type.parse(value) | ||
|
||
assertThat(actual.parsed).isEqualTo(expectedCodeStyleValue) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...otlin/com/pinterest/ktlint/core/api/editorconfig/RuleExecutionEditorConfigPropertyTest.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,30 @@ | ||
package com.pinterest.ktlint.core.api.editorconfig | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
class RuleExecutionEditorConfigPropertyTest { | ||
@ParameterizedTest(name = "Value: [{0}], result: [{1}]") | ||
@CsvSource( | ||
value = [ | ||
"enabled,enabled", | ||
" enabled,enabled", | ||
"enabled ,enabled", | ||
" enabled ,enabled", | ||
"disabled,disabled", | ||
" disabled,disabled", | ||
"disabled ,disabled", | ||
" disabled ,disabled", | ||
], | ||
ignoreLeadingAndTrailingWhitespace = false, | ||
) | ||
fun `Given a rule execution property for which the value`( | ||
value: String, | ||
expectedRuleExecution: RuleExecution, | ||
) { | ||
val actual = RULE_EXECUTION_PROPERTY_TYPE.parse(value) | ||
|
||
assertThat(actual.parsed).isEqualTo(expectedRuleExecution) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ore/src/test/kotlin/com/pinterest/ktlint/core/api/editorconfig/SafeEnumValueParserTest.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,28 @@ | ||
package com.pinterest.ktlint.core.api.editorconfig | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.ec4j.core.model.PropertyType | ||
import org.junit.jupiter.api.Test | ||
|
||
class SafeEnumValueParserTest { | ||
@Test | ||
fun `Given a rule execution property for which the value`() { | ||
val propertyType = | ||
PropertyType.LowerCasingPropertyType( | ||
"some-property-type", | ||
null, | ||
SafeEnumValueParser(SomePropertyType::class.java), | ||
SomePropertyType.values().map { it.name }.toSet(), | ||
) | ||
|
||
val actual = propertyType.parse(" value2 ") | ||
|
||
assertThat(actual.parsed).isEqualTo(SomePropertyType.value2) | ||
} | ||
|
||
@Suppress("EnumEntryName") | ||
private enum class SomePropertyType { | ||
value1, | ||
value2, | ||
} | ||
} |