From bf1443eb85c67ac33caa9d03b7fef292b2ee209d Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Wed, 3 Aug 2022 14:59:29 +0200 Subject: [PATCH] Prevent class cast exception when retrieving ktlint_code_style property from ".editorconfig" (#1564) Closes #1559 --- CHANGELOG.md | 2 +- .../core/api/UsesEditorConfigProperties.kt | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df10aa67f0..7c653a0e6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,9 +126,9 @@ The callback function provided as parameter to the format function is now called * When a glob is specified then ensure that it matches files in the current directory and not only in subdirectories of the current directory ([#1533](https://github.com/pinterest/ktlint/issue/1533)). * Execute `ktlint` cli on default kotlin extensions only when an (existing) path to a directory is given. ([#917](https://github.com/pinterest/ktlint/issue/917)). * Invoke callback on `format` function for all errors including errors that are autocorrected ([#1491](https://github.com/pinterest/ktlint/issues/1491)) +* Prevent class cast exception on ".editorconfig" property `ktlint_code_style` ([#1559](https://github.com/pinterest/ktlint/issues/1559)) * Handle trailing comma in enums `trailing-comma` ([#1542](https://github.com/pinterest/ktlint/pull/1542)) - ### Changed * Print an error message and return with non-zero exit code when no files are found that match with the globs ([#629](https://github.com/pinterest/ktlint/issue/629)). diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/UsesEditorConfigProperties.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/UsesEditorConfigProperties.kt index 2b87ef4053..8440757d73 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/UsesEditorConfigProperties.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/UsesEditorConfigProperties.kt @@ -47,10 +47,24 @@ public interface UsesEditorConfigProperties { require(editorConfigProperties.contains(editorConfigProperty)) { "EditorConfigProperty '${editorConfigProperty.type.name}' may only be retrieved when it is registered in the editorConfigProperties." } - val codeStyle = getEditorConfigValue(codeStyleSetProperty, official) - return getEditorConfigValue(editorConfigProperty, codeStyle) + + return getEditorConfigValue(editorConfigProperty, getEditorConfigCodeStyle()) } + /** + * The code style property does not need to be defined in the [editorConfigProperties] of the class that defines + * this interface. Those classed should not need to be aware of the different coding styles except when setting + * different default values. As the property is not defined in the [editorConfigProperties] the value needs to + * be parsed explicitly to prevent class cast exceptions. + */ + private fun EditorConfigProperties.getEditorConfigCodeStyle() = + codeStyleSetProperty + .type + .parse( + get(codeStyleSetProperty.type.name)?.sourceValue + ).parsed + ?: official + /** * Get the value of [EditorConfigProperty] based on loaded [EditorConfigProperties] content for the current * [ASTNode]. @@ -61,8 +75,10 @@ public interface UsesEditorConfigProperties { "EditorConfigProperty '${editorConfigProperty.type.name}' may only be retrieved when it is registered in the editorConfigProperties." } val editorConfigPropertyValues = getUserData(KtLint.EDITOR_CONFIG_PROPERTIES_USER_DATA_KEY)!! - val codeStyle = editorConfigPropertyValues.getEditorConfigValue(codeStyleSetProperty, official) - return editorConfigPropertyValues.getEditorConfigValue(editorConfigProperty, codeStyle) + return editorConfigPropertyValues.getEditorConfigValue( + editorConfigProperty, + editorConfigPropertyValues.getEditorConfigCodeStyle() + ) } private fun EditorConfigProperties.getEditorConfigValue(