From 650ce55addeca4c083f22450ae630660c86b5475 Mon Sep 17 00:00:00 2001 From: bhoflack Date: Fri, 3 Jun 2022 08:35:24 +0200 Subject: [PATCH] Fix crash when indentSize isn't set to an Int. (#1486) * Fix crash when indentSize isn't set to an Int. * Check for both null as the type in the same line * Add test + changelog * Update changelog and test description Co-authored-by: paul-dingemans --- CHANGELOG.md | 3 +++ .../ktlint/core/api/UsesEditorConfigProperties.kt | 5 +++-- .../ktlint/core/UsesEditorConfigPropertiesTest.kt | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5d35e828e..17886e886f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,9 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter - Fix formatting of a property delegate with a dot-qualified-expression `indent` ([#1340](https://github.com/pinterest/ktlint/ssues/1340)) - Keep formatting of for-loop in sync with default IntelliJ formatter (`indent`) and a newline in the expression in a for-statement should not force to wrap it `wrapping` ([#1350](https://github.com/pinterest/ktlint/issues/1350)) - Fix indentation of property getter/setter when the property has an initializer on a separate line `indent` ([#1335](https://github.com/pinterest/ktlint/issues/1335)) +- When `.editorconfig` setting `indentSize` is set to value `tab` then return the default tab width as value for `indentSize` ([#1485](https://github.com/pinterest/ktlint/issues/1485)) + + ### Changed - Update Kotlin development version to `1.7.0-RC2` and Kotlin version to `1.6.21`. 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 c1115580bf..88308a8c3a 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 @@ -160,8 +160,9 @@ public object DefaultEditorConfigProperties { defaultValue = IndentConfig.DEFAULT_INDENT_CONFIG.tabWidth, propertyMapper = { property, _ -> when { - property == null -> IndentConfig.DEFAULT_INDENT_CONFIG.tabWidth - property.isUnset -> -1 + property?.isUnset == true -> -1 + property?.getValueAs() == null -> + IndentConfig.DEFAULT_INDENT_CONFIG.tabWidth else -> property.getValueAs() } } diff --git a/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/UsesEditorConfigPropertiesTest.kt b/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/UsesEditorConfigPropertiesTest.kt index 76bb44a67a..3605742806 100644 --- a/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/UsesEditorConfigPropertiesTest.kt +++ b/ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/UsesEditorConfigPropertiesTest.kt @@ -49,6 +49,21 @@ class UsesEditorConfigPropertiesTest { assertThat(actual).isEqualTo(-1) } + @Test + fun `Issue 1485 - Given that editor config property indent_size is set to value 'tab' then return tabWidth as value via the getEditorConfigValue of the node`() { + val testAstNode: ASTNode = DummyHolderElement("some-text") + testAstNode.putUserData( + KtLint.EDITOR_CONFIG_PROPERTIES_USER_DATA_KEY, + createPropertyWithValue( + DefaultEditorConfigProperties.indentSizeProperty, + "tab" + ) + ) + val actual = PropertyValueTester().testValue(testAstNode, DefaultEditorConfigProperties.indentSizeProperty) + + assertThat(actual).isEqualTo(IndentConfig.DEFAULT_INDENT_CONFIG.tabWidth) + } + @Test fun `Given that editor config property indent_size is not set then return the default tabWidth as value via the getEditorConfigValue of the node`() { val testAstNode: ASTNode = DummyHolderElement("some-text")