Skip to content

Commit

Permalink
Prevent class cast exception when retrieving ktlint_code_style proper…
Browse files Browse the repository at this point in the history
…ty from ".editorconfig"

Closes pinterest#1559
  • Loading branch information
paul-dingemans committed Aug 2, 2022
1 parent e0693a0 commit d108a8a
Showing 1 changed file with 20 additions and 4 deletions.
Expand Up @@ -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].
Expand All @@ -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 <T> EditorConfigProperties.getEditorConfigValue(
Expand Down

0 comments on commit d108a8a

Please sign in to comment.