Skip to content

Commit

Permalink
Fixed WRONG_NEWLINES rule to take into account maxCallsInOneLine (#…
Browse files Browse the repository at this point in the history
…1858)

### What's done:
- Fixed bug related to case when the number of allowed extension function calls in one line did not match `maxCallsInOneLine` configuration.
- Fixed several warning tests.
- Added new warning test.

Closes #1705
  • Loading branch information
DrAlexD authored Dec 12, 2023
1 parent fd85628 commit 73cd9a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
) {
isDotQuaOrSafeAccessOrPostfixExpression(it) && it.elementType != POSTFIX_EXPRESSION
}.reversed()
if (listDot.size > 3) {
if (listDot.size > configuration.maxCallsInOneLine) {
val without = listDot.filterIndexed { index, it ->
val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev
val firstElem = it.firstChildNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
RulesConfig(WRONG_NEWLINES.name, true,
mapOf("maxCallsInOneLine" to "1"))
)
private val rulesConfigListLong: List<RulesConfig> = listOf(
RulesConfig(WRONG_NEWLINES.name, true,
mapOf("maxCallsInOneLine" to "10"))
)
private val ruleId = "$DIKTAT_RULE_SET_ID:${NewlinesRule.NAME_ID}"
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"
Expand Down Expand Up @@ -682,7 +686,8 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| }
|}
""".trimMargin(),
DiktatError(19, 20, ruleId, "${WRONG_NEWLINES.warnText()} should follow functional style at .", true),
DiktatError(16, 9, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(19, 20, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
)
}
Expand Down Expand Up @@ -829,9 +834,9 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
|}
""".trimMargin(),
DiktatError(2, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(3, 22, ruleId, "${WRONG_NEWLINES.warnText()} should follow functional style at .", true),
DiktatError(3, 22, ruleId, "$functionalStyleWarn .", true),
DiktatError(13, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(13, 23, ruleId, "${WRONG_NEWLINES.warnText()} should follow functional style at .", true)
DiktatError(13, 23, ruleId, "$functionalStyleWarn .", true)
)
}

Expand Down Expand Up @@ -893,7 +898,9 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| .few()
|}
""".trimMargin(),
DiktatError(2, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(4, 10, ruleId, "$functionalStyleWarn .", true),
DiktatError(8, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(9, 11, ruleId, "$functionalStyleWarn .", true),
DiktatError(9, 17, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
Expand Down Expand Up @@ -930,7 +937,9 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
|}
""".trimMargin(),
DiktatError(2, 7, ruleId, "$functionalStyleWarn .", true),
DiktatError(15, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(16, 10, ruleId, "$functionalStyleWarn .", true),
DiktatError(21, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(21, 7, ruleId, "$functionalStyleWarn .", true),
DiktatError(22, 10, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
Expand Down Expand Up @@ -1051,6 +1060,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
""".trimMargin(),
DiktatError(2, 14, ruleId, "$functionalStyleWarn ?:", true),
DiktatError(4, 8, ruleId, "$functionalStyleWarn ?:", true),
DiktatError(4, 11, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(4, 22, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
)
Expand All @@ -1066,7 +1076,9 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| a().b.c()!!
|}
""".trimMargin(),
DiktatError(2, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(2, 11, ruleId, "$functionalStyleWarn .", true),
DiktatError(3, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(3, 9, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
)
Expand All @@ -1092,6 +1104,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| .qwe()
|}
""".trimMargin(),
DiktatError(7, 11, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(7, 22, ruleId, "$functionalStyleWarn .", true),
DiktatError(9, 15, ruleId, "$functionalStyleWarn ?:", true),
DiktatError(10, 16, ruleId, "$functionalStyleWarn ?:", true),
Expand Down Expand Up @@ -1152,6 +1165,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| if(a().b().c()) {}
|}
""".trimMargin(),
DiktatError(2, 7, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
DiktatError(2, 14, ruleId, "${COMPLEX_EXPRESSION.warnText()} .", false),
DiktatError(2, 14, ruleId, "$functionalStyleWarn .", true),
rulesConfigList = rulesConfigListShort
Expand Down Expand Up @@ -1195,4 +1209,17 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
fileName = "build.gradle.kts"
)
}

@Test
@Tags(Tag(WarningNames.WRONG_NEWLINES), Tag(WarningNames.COMPLEX_EXPRESSION))
fun `shouldn't trigger on allowed many calls in one line`() {
lintMethod(
"""
|fun foo() {
| if(a().b().c().d().e()) {}
|}
""".trimMargin(),
rulesConfigList = rulesConfigListLong
)
}
}

0 comments on commit 73cd9a7

Please sign in to comment.