Skip to content

Commit

Permalink
## Whats Added:
Browse files Browse the repository at this point in the history
 * add analytic fully-qualified
 * add test
 * correct warning message
 * run fix mode

## Issue (#1403)
  • Loading branch information
Arrgentum committed Jun 28, 2022
1 parent d015c77 commit 591bdef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}.reversed()
if (listDot.size > 3) {
val without = listDot.filterIndexed { index, it ->
val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev
val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev
val firstElem = it.firstChildNode
(firstElem.textContains('(') || firstElem.textContains('{')) && (index > 0) && ((whiteSpaceBeforeDotOrSafeAccess?.elementType != WHITE_SPACE) ||
(whiteSpaceBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !whiteSpaceBeforeDotOrSafeAccess.textContains('\n')))
val isTextContainsParenthesized = firstElem.textContains('(') || firstElem.textContains('{')
val isWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE ||
(nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n'))
isTextContainsParenthesized && (index > 0) && isWhiteSpaceBeforeDotOrSafeAccessContainNewLine
}
if (without.isNotEmpty()) {
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access after first call expression",
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "wrong split long `dot qualified expression` or `safe access expression`",
node.startOffset, node) {
fixDotQualifiedExpression(listDot)
fixDotQualifiedExpression(without)
}
}
}
Expand All @@ -193,13 +195,10 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
* 2) If before first Dot or Safe Access node stay White Space node with \n - remove this node
*/
private fun fixDotQualifiedExpression(list: List<ASTNode>) {
list.forEachIndexed { index, astNode ->
list.forEach { astNode ->
val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS)
val nodeBeforeDot = dotNode?.treePrev
val firstElem = astNode.firstChildNode
if (index > 0 && (firstElem.textContains('(') || firstElem.textContains('{'))) {
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode)
}
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
mapOf("maxCallsInOneLine" to "1"))
)
private val ruleId = "$DIKTAT_RULE_SET_ID:${NewlinesRule.NAME_ID}"
private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} should be split before second and other dot/safe access after first call expression"
private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} wrong split long `dot qualified expression` or `safe access expression`"
private val shouldBreakAfter = "${WRONG_NEWLINES.warnText()} should break a line after and not before"
private val shouldBreakBefore = "${WRONG_NEWLINES.warnText()} should break a line before and not after"
private val functionalStyleWarn = "${WRONG_NEWLINES.warnText()} should follow functional style at"
Expand Down

0 comments on commit 591bdef

Please sign in to comment.