diff --git a/CHANGELOG.md b/CHANGELOG.md index 71a32d2904..23fd898cf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Fixed * Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238) +* Fix indent of multiline object declaration inside class `indent` [#2257](https://github.com/pinterest/ktlint/issue/2257) * Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260) * Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt index e7e226b6c8..375f44e75b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt @@ -388,7 +388,8 @@ public class IndentationRule : } private fun ASTNode.isPartOfClassWithAMultilinePrimaryConstructor() = - parent { it.elementType == CLASS } + treeParent + .takeIf { it.elementType == CLASS } ?.findChildByType(PRIMARY_CONSTRUCTOR) ?.textContains('\n') == true diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt index b6a1bd07be..94efec9dc5 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt @@ -4651,7 +4651,7 @@ internal class IndentationRuleTest { val code = """ class FooBar : - Foox, + Foo, Bar { // Do something } @@ -4666,6 +4666,22 @@ internal class IndentationRuleTest { indentationRuleAssertThat(code).hasNoLintViolations() } + @Test + fun `Issue 2257 - Given a class containing an object declaration having a super type with parameters`() { + val code = + """ + class Foo( + foo: String, + ) { + object Bar : Baz( + baz = "baz", + bar = "bar", + ) + } + """.trimIndent() + indentationRuleAssertThat(code).hasNoLintViolations() + } + @Test fun `Given an if statement with multiple EOL comments between end of then and else`() { val code =