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

Bugfixing of single binary expression wrapped in parentheses (long lines rule) #1416

Merged
merged 4 commits into from
Jun 30, 2022
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 @@ -79,8 +79,9 @@ import java.net.URL

/**
* The rule checks for lines in the file that exceed the maximum length.
* Rule ignores URL in KDoc. Rule also can fix some cases.
* Rule can fix long binary expressions in condition inside `if` and in property declarations and one line functions
* Rule ignores URL in KDoc. This rule can also fix some particular corner cases.
* This inspection can fix long binary expressions in condition inside `if`,
* in property declarations and in single line functions.
*/
@Suppress("ForbiddenComment")
class LineLength(configRules: List<RulesConfig>) : DiktatRule(
Expand Down Expand Up @@ -541,7 +542,12 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
*/
private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) {
override fun fix() {
val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE)
val binNode = if (node.elementType == PARENTHESIZED) {
node.findChildByType(BINARY_EXPRESSION)
} else {
node
}
val nodeOperationReference = binNode?.findChildByType(OPERATION_REFERENCE)
val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) {
nodeOperationReference?.treeNext
} else {
Expand All @@ -551,7 +557,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
nodeOperationReference
}
}
node.appendNewlineMergingWhiteSpace(nextNode, nextNode)
binNode?.appendNewlineMergingWhiteSpace(nextNode, nextNode)
orchestr7 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
fun `fix bin expression first symbol last word`() {
fixAndCompare("LongBinaryExpressionLastWordExpected.kt", "LongBinaryExpressionLastWordTest.kt", rulesConfigListErrorLineLength1)
}

@Test
fun `fix bin 1expression first symbol last word`() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in test name

fixAndCompare("LongComplexExpressionExpected.kt", "LongComplexExpressionTest.kt", rulesConfigListErrorLineLength1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test.paragraph3.long_line

fun foo(){
val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) ||
userRole.isLowerThan(selfRole))
}

val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) ||
userRole.isLowerThan(selfRole))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test.paragraph3.long_line

fun foo(){
val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || userRole.isLowerThan(selfRole))
}

val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || userRole.isLowerThan(selfRole))