Skip to content

Commit

Permalink
Fix class cast exception when specifying a non-string editorconfig se…
Browse files Browse the repository at this point in the history
…tting in the default ".editorconfig" (#1629)

Closes #1627
  • Loading branch information
paul-dingemans committed Sep 3, 2022
1 parent 8894ee9 commit 00d20a0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed
* Do not add trailing comma in empty parameter/argument list with comments (`trailing-comma-on-call-site`, `trailing-comma-on-declaration-site`) ([#1602](https://github.com/pinterest/ktlint/issue/1602))
* Fix class cast exception when specifying a non-string editorconfig setting in the default ".editorconfig" ([#1627](https://github.com/pinterest/ktlint/issue/1627))

Do not show deprecation warning about property "disabled_rules" when using CLi-parameter `--disabled-rules` ([#1599](https://github.com/pinterest/ktlint/issues/1599))

Expand Down
Expand Up @@ -108,7 +108,25 @@ public interface UsesEditorConfigProperties {
}
}

return property?.getValueAs()
val propertyValue =
when {
property == null -> null
property.type != null -> property.getValueAs()
else -> {
// In case the property was loaded from the default ".editorconfig" the type field is not known as
// the property could not yet be linked to a property type that is defined in a rule. To prevent
// class cast exceptions, lookup the property by name and convert to property type.
@Suppress("UNCHECKED_CAST")
this@UsesEditorConfigProperties
.editorConfigProperties
.find { it.type.name == property.name }
?.type
?.parse(property.sourceValue)
?.parsed as T?
}
}

return propertyValue
?: editorConfigProperty
.getDefaultValue(codeStyleValue)
.also {
Expand Down
Expand Up @@ -95,6 +95,20 @@ class EditorConfigDefaultsLoaderCLITest : BaseCLITest() {
}
}

@Test
fun `Issue 1627 - Given a default editorconfig containing a boolean setting then do not throw a class cast exception`() {
val projectDirectory = "$BASE_DIR_PLACEHOLDER/editorconfig-path/project"
runKtLintCliProcess(
"editorconfig-path",
listOf("--editorconfig=$projectDirectory/editorconfig-boolean-setting", "--debug"),
) {
assertErrorExitCode()

val assertThat = assertThat(errorOutput)
assertThat.doesNotContainLineMatching(Regex(".*java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean.*"))
}
}

private fun ListAssert<String>.containsLineMatching(regex: Regex) =
this.anyMatch {
it.matches(regex)
Expand Down
@@ -0,0 +1,5 @@
root = true

[*.{kt,kts}]
ij_kotlin_allow_trailing_comma=true
ij_kotlin_allow_trailing_comma_on_call_site=true

0 comments on commit 00d20a0

Please sign in to comment.