Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent IllegalArgumentException in argument-list-wrapping rule #2500

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}