Skip to content

Commit

Permalink
Fix false positive when a single line contains multiple dot qualified…
Browse files Browse the repository at this point in the history
… expressions and/or safe expressions

Closes #1830
  • Loading branch information
paul-dingemans committed Mar 13, 2023
1 parent f37fdc9 commit 066cc7e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ Previously the default value for `.editorconfig` property `max_line_length` was
* Fix indentation of a multiline typealias `indent` ([#1788](https://github.com/pinterest/ktlint/issues/1788))
* Fix false positive when multiple KDOCs exists between a declaration and another annotated declaration `spacing-between-declarations-with-annotations` ([#1802](https://github.com/pinterest/ktlint/issues/1802))
* Fix false positive when a single line statement containing a block having exactly the maximum line length is preceded by a blank line `wrapping` ([#1808](https://github.com/pinterest/ktlint/issues/1808))
* Fix false positive when a single line contains multiple dot qualified expressions and/or safe expressions `indent` ([#1830](https://github.com/pinterest/ktlint/issues/1830))

### Changed
* Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ public class IndentationRule :
node.elementType == BINARY_WITH_TYPE ||
node.elementType == SUPER_TYPE_ENTRY ||
node.elementType == TYPE_ARGUMENT_LIST ||
node.elementType == TYPE_PARAMETER_LIST ->
node.elementType == TYPE_PARAMETER_LIST ||
node.elementType == USER_TYPE ->
startIndentContext(node)

node.elementType == DELEGATED_SUPER_TYPE_ENTRY ||
Expand Down Expand Up @@ -257,9 +258,7 @@ public class IndentationRule :
node.elementType == BINARY_EXPRESSION ->
visitBinaryExpression(node)

node.elementType == DOT_QUALIFIED_EXPRESSION ||
node.elementType == SAFE_ACCESS_EXPRESSION ||
node.elementType == USER_TYPE -> {
node.elementType in CHAINABLE_EXPRESSION -> {
if (codeStyle == ktlint_official &&
node.elementType == DOT_QUALIFIED_EXPRESSION &&
node.treeParent?.elementType == ARRAY_ACCESS_EXPRESSION &&
Expand All @@ -274,7 +273,9 @@ public class IndentationRule :
fromAstNode = node.treeParent,
toAstNode = node.treeParent.treeParent.lastChildLeafOrSelf(),
)
} else if (node.treeParent?.elementType != node.elementType) {
} else if (node.treeParent.elementType in CHAINABLE_EXPRESSION) {
// Multiple dot qualified expressions and/or safe expression on the same line should not increase the indent level
} else {
startIndentContext(node)
}
}
Expand Down Expand Up @@ -1068,6 +1069,7 @@ public class IndentationRule :
private companion object {
const val KDOC_CONTINUATION_INDENT = " "
const val TYPE_CONSTRAINT_CONTINUATION_INDENT = " " // Length of keyword "where" plus separating space
val CHAINABLE_EXPRESSION = setOf(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION)
}

private data class IndentContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3974,7 +3974,7 @@ internal class IndentationRuleTest {
).isFormattedAs(formattedCode)
}

@Test
@Test // user_type
fun `Issue 1210 - Given a supertype delegate`() {
val code =
"""
Expand Down Expand Up @@ -4921,6 +4921,19 @@ internal class IndentationRuleTest {
indentationRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 1830 - Given a dot qualified expression followed by an safe access expression on the same line`() {
val code =
"""
private fun test(): Boolean? =
runCatching { true }
.getOrNull()?.let { result ->
!result
}
""".trimIndent()
indentationRuleAssertThat(code).hasNoLintViolations()
}

private companion object {
val INDENT_STYLE_TAB =
INDENT_STYLE_PROPERTY to PropertyType.IndentStyleValue.tab
Expand Down

0 comments on commit 066cc7e

Please sign in to comment.