Skip to content

Commit

Permalink
Fix Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR…
Browse files Browse the repository at this point in the history
… with empty block

### What's done:
* Fixed Conflicting TOO_MANY_BLANK_LINES and BRACES_BLOCK_STRUCTURE_ERROR with empty block in lambda
  • Loading branch information
Cheshiriks committed Mar 12, 2021
1 parent 8edfb4f commit 18ae1ab
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3
import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.constants.Warnings.EMPTY_BLOCK_STRUCTURE_ERROR
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.*
Expand Down Expand Up @@ -37,7 +36,7 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
checkEmptyBlock(newNode, configuration)
}

private fun isEmptyWhiteSpace(node: ASTNode) =
private fun isNewLine(node: ASTNode) =
node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: true

@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
Expand All @@ -46,21 +45,23 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
return
}
if (node.isBlockEmpty()) {
node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let {
// Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE)
if (!configuration.emptyBlockExist && isEmptyWhiteSpace(node)) {
val freeText = "do not put newlines in empty lambda"
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) {
val whiteSpaceNode = node.findChildByType(WHITE_SPACE)
whiteSpaceNode?.leaveExactlyNumNewLines(0)
}
}
return
}
if (!configuration.emptyBlockExist) {
EMPTY_BLOCK_STRUCTURE_ERROR.warn(configRules, emitWarn, isFixMode, "empty blocks are forbidden unless it is function with override keyword",
node.startOffset, node)
} else {
node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let {
// Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE)
if (isNewLine(node)) {
val freeText = "do not put newlines in empty lambda"
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) {
val whiteSpaceNode = node.findChildByType(WHITE_SPACE)
whiteSpaceNode?.let {
node.replaceChild(whiteSpaceNode, PsiWhiteSpaceImpl(" "))
}
}
}
return
}
val space = node.findChildByType(RBRACE)!!.treePrev
if (configuration.emptyBlockNewline && !space.text.contains("\n")) {
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class BlockStructureBracesWarnTest : LintTestBase(::BlockStructureBraces) {
lintMethod(
"""
|fun foo() {
| run { }
| run {
|
| }
|}
""".trimMargin()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) {
lintMethod(
"""
|fun foo() {
| run {
|
| }
| run { }
|}
""".trimMargin(),
LintError(2, 8, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} do not put newlines in empty lambda", true)
rulesConfigList = rulesConfigListEmptyBlockExist
)
}

Expand Down Expand Up @@ -134,14 +132,31 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) {
)
}

@Test
@Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
fun `check empty lambda with config`() {
lintMethod(
"""
|fun foo() {
| val y = listOf<Int>().map {
|
| }
|}
""".trimMargin(),
LintError(2, 30, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} do not put newlines in empty lambda", true),
rulesConfigList = rulesConfigListEmptyBlockExist
)
}

@Test
fun `check empty lambda`() {
lintMethod(
"""
|fun foo() {
| val y = listOf<Int>().map { }
|}
""".trimMargin()
""".trimMargin(),
LintError(2, 30, ruleId, "${EMPTY_BLOCK_STRUCTURE_ERROR.warnText()} empty blocks are forbidden unless it is function with override keyword", false)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class BlankLinesWarnTest : LintTestBase(::BlankLinesRule) {
)
}

@Test
@Tag(WarningNames.TOO_MANY_BLANK_LINES)
fun `check lambda with empty block`() {
lintMethod(
"""
|fun foo() {
| run {
|
| }
|}
""".trimMargin()
)
}

@Test
@Tag(WarningNames.TOO_MANY_BLANK_LINES)
fun `should prohibit usage of two or more consecutive blank lines`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin",
fixAndCompare("DefaultPackageExpected.kt", "DefaultPackageTest.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #8`() {
fixAndCompare("Example8Expected.kt", "Example8Test.kt")
}

@Test
@Tag("DiktatRuleSetProvider")
fun `smoke test #7`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun foo () {
fun goo () {
var x = 10
if (x == 10) return else println(10)
val y = listOf<Int>().map {}
val y = listOf<Int>().map { }
for(x in 0..10) println(x)
while (x > 0)
--x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ fun foo () {
fun goo () {
var x = 10
if (x == 10) return else println(10)
val y = listOf<Int>().map {}
val y = listOf<Int>().map {

}
for(x in 0..10) println(x)
while (x > 0)
--x
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 18ae1ab

Please sign in to comment.