Skip to content

Commit

Permalink
Bugfixing of single binary expression wrapped in parentheses (long li…
Browse files Browse the repository at this point in the history
…nes rule) (#1416)

### What's done:
- Fixing following binary expressions (selfRole.isHigherOrEqualThan(Role.OWNER) || userRole.isLowerThan(selfRole))
  • Loading branch information
Arrgentum authored Jun 30, 2022
1 parent cfd9eab commit 6c70495
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
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)
}
}

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`() {
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))

0 comments on commit 6c70495

Please sign in to comment.