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

New rule for complex expression #680

Merged
merged 14 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -68,12 +68,12 @@ class AnnotationNewLineRule(private val configRules: List<RulesConfig>) : Rule("
if (node == node.treeParent.getFirstChildWithType(node.elementType)) {
// Current node is ANNOTATION_ENTRY. treeParent(ModifierList) -> treeParent(PRIMARY_CONSTRUCTOR)
// Checks if there is a white space before grandparent node
val isParentWhiteSpace = node
val hasSpaceBeforeGrandparent = node
.treeParent
.treeParent
.treePrev
.isWhiteSpace()
if (isParentWhiteSpace) {
if (hasSpaceBeforeGrandparent) {
(node.treeParent.treeParent.treePrev as LeafPsiElement).replaceWithText("\n")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
}
val isIncorrect = (if (node.elementType == ELVIS) node.treeParent else node).run {
if (isCallsChain()) {
if (node.isInParentheses()) {
COMPLEX_EXPRESSION.warn(configRules, emitWarn, isFixMode, node.text, node.startOffset, node)
}
val isSingleLineIfElse = parent({ it.elementType == IF }, true)?.isSingleLineIfElse() ?: false
// to follow functional style these operators should be started by newline
(isFollowedByNewline() || !isBeginByNewline()) && !isSingleLineIfElse &&
Expand Down Expand Up @@ -444,17 +447,17 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
*
* @return true - if there is error, and false if there is no error
*/
private fun ASTNode.isCallsChain(isWithoutParentheses: Boolean = true) = getCallChain(isWithoutParentheses)?.isNotValidCalls(this) ?: false
private fun ASTNode.isCallsChain(dropLeadingBrackets: Boolean = true) = getCallChain(dropLeadingBrackets)?.isNotValidCalls(this) ?: false

private fun ASTNode.getCallChain(isWithoutParentheses: Boolean = true): List<ASTNode>? {
private fun ASTNode.getCallChain(dropLeadingBrackets: Boolean = true): List<ASTNode>? {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
val parentExpressionList = getParentExpressions()
.lastOrNull()
?.run {
mutableListOf<ASTNode>().also {
getOrderedCallExpressions(psi, it)
}
}
return if (isWithoutParentheses) {
return if (dropLeadingBrackets) {
// fixme: we can't distinguish fully qualified names (like java.lang) from chain of property accesses (like list.size) for now
parentExpressionList?.dropWhile { !it.treeParent.textContains('(') && !it.treeParent.textContains('{') }
} else {
Expand Down Expand Up @@ -521,7 +524,7 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
private fun ASTNode.isInParentheses() = parent({it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION})
?.treeParent
?.elementType
?.let { it in bracketsTypes }
?.let { it in parenthesesTypes }
?: false

/**
Expand All @@ -547,6 +550,6 @@ class NewlinesRule(private val configRules: List<RulesConfig>) : Rule("newlines"
private val expressionTypes = TokenSet.create(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, CALLABLE_REFERENCE_EXPRESSION, BINARY_EXPRESSION)
private val chainExpressionTypes = TokenSet.create(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION)
private val dropChainValues = TokenSet.create(EOL_COMMENT, WHITE_SPACE, BLOCK_COMMENT, KDOC)
private val bracketsTypes = TokenSet.create(CONDITION, WHEN, VALUE_ARGUMENT)
private val parenthesesTypes = TokenSet.create(CONDITION, WHEN, VALUE_ARGUMENT)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -977,4 +977,19 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
rulesConfigList = rulesConfigListShort
)
}

@Test
@Tags(Tag(WarningNames.WRONG_NEWLINES), Tag(WarningNames.COMPLEX_EXPRESSION))
fun `complex expression in condition with double warnings`() {
lintMethod(
"""
|fun foo() {
| if(a().b().c()) {}
|}
""".trimMargin(),
LintError(2, 14, ruleId, "${COMPLEX_EXPRESSION.warnText()} .", false),
LintError(2, 14, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
)
}
}
11 changes: 7 additions & 4 deletions info/guide/guide-chapter-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,20 @@ Note that all comparison operators, such as `==`, `>`, `<`, should not be split.
if (condition) list.map { foo(it) }.filter { bar(it) } else list.drop(1)
```

**Note:** If dot qualified expression is inside condition or passed as an argument - replace with new variable
**Note:** If dot qualified expression is inside condition or passed as an argument - it should be replaced with new variable.

**Invalid example**:
```kotlin
if (node.text.length.dec() != 0) {}
if (node.treeParent.treeParent.findChildByType(IDENTIFIER) != null) {}
```

**Valid example**:
```kotlin
val nodeLen = node.text.length.dec()
if (nodeLen != 0) {}
val grandIdentifier = node
.treeParent
.treeParent
.findChildByType(IDENTIFIER)
if (grandIdentifier != null) {}
```

2) Newlines should be placed after the assignment operator (`=`).
Expand Down