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" (#1564)

Closes #1559
  • Loading branch information
paul-dingemans authored Aug 3, 2022
1 parent 7bf39e5 commit bf1443e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
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 bf1443e

Please # to comment.