Skip to content

Commit

Permalink
Merge pull request #2500 from pinterest/2499-argument-list-wrapping
Browse files Browse the repository at this point in the history
Prevent IllegalArgumentException in `argument-list-wrapping` rule
  • Loading branch information
paul-dingemans authored Jan 23, 2024
2 parents f7557d2 + 761728a commit cc1954d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.COLLECTION_LITERAL_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ELSE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EQ
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LPAR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT
Expand Down Expand Up @@ -173,7 +175,7 @@ public class ArgumentListWrappingRule :
editorConfigIndent
.indentLevelFrom(child.treeParent.indent(false))
.plus(indentLevelFix)
"\n" + editorConfigIndent.indent.repeat(indentLevel)
"\n" + editorConfigIndent.indent.repeat(maxOf(indentLevel, 0))
}

private fun wrapArgumentInList(
Expand Down Expand Up @@ -258,8 +260,14 @@ public class ArgumentListWrappingRule :
?.any { it.isWhiteSpaceWithNewline() } == true

private fun ASTNode.isPartOfDotQualifiedAssignmentExpression(): Boolean =
treeParent?.treeParent?.elementType == BINARY_EXPRESSION &&
treeParent?.treeParent?.children()?.find { it.elementType == DOT_QUALIFIED_EXPRESSION } != null
treeParent
?.treeParent
?.takeIf { it.elementType == BINARY_EXPRESSION }
?.let { binaryExpression ->
binaryExpression.firstChildNode.elementType == DOT_QUALIFIED_EXPRESSION &&
binaryExpression.findChildByType(OPERATION_REFERENCE)?.firstChildNode?.elementType == EQ
}
?: false

private fun ASTNode.prevWhiteSpaceWithNewLine(): ASTNode? {
var prev = prevLeaf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,15 @@ class ArgumentListWrappingRuleTest {
LintViolation(3, 48, "Missing newline before \")\""),
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 2499 - Given a function signature with expression body starting on same line as function signature then do not throw exception`() {
val code =
"""
fun foo(): List<Int> = listOf(
1,
) + listOf(2).map { it }
""".trimIndent()
argumentListWrappingRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit cc1954d

Please sign in to comment.