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

NO_BRACES_IN_CONDITIONALS_AND_LOOPS fail within variable initialization block #1739

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -12,7 +12,9 @@ import com.saveourtool.diktat.ruleset.utils.prevSibling
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.BLOCK
import org.jetbrains.kotlin.KtNodeTypes.CALL_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.ELSE
import org.jetbrains.kotlin.KtNodeTypes.IF
import org.jetbrains.kotlin.KtNodeTypes.LAMBDA_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.REFERENCE_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.SAFE_ACCESS_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.WHEN
Expand Down Expand Up @@ -66,12 +68,7 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
val thenNode = ifPsi.then?.node
val elseKeyword = ifPsi.elseKeyword
val elseNode = ifPsi.`else`?.node
val indent = node
.prevSibling { it.elementType == WHITE_SPACE }
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
val indent = node.findIndentBeforeNode()

if (node.isSingleLineIfElse()) {
return
Expand Down Expand Up @@ -132,11 +129,8 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
if (loopBodyNode == null || loopBodyNode.elementType != BLOCK) {
NO_BRACES_IN_CONDITIONALS_AND_LOOPS.warnAndFix(configRules, emitWarn, isFixMode, node.elementType.toString(), node.startOffset, node) {
// fixme proper way to calculate indent? or get step size (instead of hardcoded 4)
val indent = node.prevSibling { it.elementType == WHITE_SPACE }!!
.text
.lines()
.last()
.count { it == ' ' }
val indent = node.findIndentBeforeNode()

loopBody?.run {
replaceWithBlock(indent)
}
Expand All @@ -152,6 +146,23 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
}
}

private fun ASTNode.findIndentBeforeNode(): Int {
val isElseIfStatement = treeParent.elementType == ELSE
val primaryIfNode = if (isElseIfStatement) treeParent.treeParent else this

val indentNode = if (primaryIfNode.treeParent?.treeParent?.treeParent?.elementType == LAMBDA_EXPRESSION) {
primaryIfNode.treeParent.prevSibling { it.elementType == WHITE_SPACE }
} else {
primaryIfNode.prevSibling { it.elementType == WHITE_SPACE }
}

return indentNode
?.text
?.lines()
?.last()
?.count { it == ' ' } ?: 0
}

@Suppress("UnsafeCallOnNullableType")
private fun checkWhenBranches(node: ASTNode) {
(node.psi as KtWhenExpression)
Expand Down Expand Up @@ -194,6 +205,6 @@ class BracesInConditionalsAndLoopsRule(configRules: List<RulesConfig>) : DiktatR
companion object {
private const val INDENT_STEP = 4
const val NAME_ID = "races-rule"
private val scopeFunctions = listOf("let", "run", "apply", "also")
private val scopeFunctions = listOf("let", "run", "with", "apply", "also")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ class BracesRuleFixTest : FixTestBase("test/paragraph3/braces", ::BracesInCondit
fixAndCompare("LoopsBracesExpected.kt", "LoopsBracesTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to if-else statements inside scope functions`() {
fixAndCompare("IfElseBracesInsideScopeFunctionsExpected.kt", "IfElseBracesInsideScopeFunctionsTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
fun `should add braces to loops inside scope functions`() {
fixAndCompare("LoopsBracesInsideScopeFunctionsExpected.kt", "LoopsBracesInsideScopeFunctionsTest.kt")
}

@Test
@Tag(WarningNames.NO_BRACES_IN_CONDITIONALS_AND_LOOPS)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) {
foo()
} else {
bar()
}
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else {
bar()
}
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) {
foo()
} else {
bar()
}
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) {
foo()
} else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) {
foo()
} else {
bar()
}
}
}
}

fun foo6() {
str.apply {
if (x > 0) {
foo()
} else if (y > 0) {
abc()
} else {
bar()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
if (x > 0) foo()
else bar()
}
}

fun foo2() {
str.let { if (x > 0) { foo() }
else bar()
}
}

fun foo3() {
str.run {
while (x > 0) {
if (x > 0) foo()
else bar()
}
}
}

fun foo4() {
str.with { while (x > 0) {
if (x > 0) foo()
else { bar() }
}
}
}

fun foo5() {
str.also {
while (x > 0) { if (x > 0) foo()
else bar()
}
}
}

fun foo6() {
str.apply {
if (x > 0) foo()
else if (y > 0) abc()
else bar()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
for (i in 1..100) {
println(i)
}
}
}

fun foo2() {
str.let { while (x > 0) {
println(i)
}
}
}

fun foo3() {
str.run {
do {
println(i)
}
while (x > 0)
}
}

fun foo4() {
str.with { do {
println(i)
}
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) {
while (x > 0)
{
println(i)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package test.paragraph3.braces

fun foo1() {
str.apply {
for (i in 1..100) println(i)
}
}

fun foo2() {
str.let { while (x > 0) println(i)
}
}

fun foo3() {
str.run {
do println(i)
while (x > 0)
}
}

fun foo4() {
str.with { do println(i)
while (x > 0)
}
}

fun foo5() {
str.also {
for (i in 1..100) {
while (x > 0)
println(i)
}
}
}
Loading