From f3da29122bf7d5547ba7a4f3f165391856dde908 Mon Sep 17 00:00:00 2001 From: Denis Kumar Date: Fri, 30 Oct 2020 15:27:07 +0300 Subject: [PATCH] BlockStructureBraces assertion failed (#461) * BlockStructureBraces assertion failed ### What's done: Fixed bug and added tests --- .../ruleset/rules/BlockStructureBraces.kt | 10 ++------ .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 6 +++++ .../block_brace/ClassBracesExpected.kt | 4 ++++ .../paragraph3/block_brace/ClassBracesTest.kt | 2 ++ .../smoke/src/main/kotlin/Bug1Expected.kt | 4 ++++ .../test/smoke/src/main/kotlin/Bug1Test.kt | 4 +++- .../smoke/src/main/kotlin/Example3Expected.kt | 23 +++++++++++++++++++ .../smoke/src/main/kotlin/Example3Test.kt | 19 +++++++++++++++ 8 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/BlockStructureBraces.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/BlockStructureBraces.kt index 3bb4e2d3e0..b8a4b9a6e9 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/BlockStructureBraces.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/BlockStructureBraces.kt @@ -1,6 +1,5 @@ package org.cqfn.diktat.ruleset.rules -import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BODY @@ -29,16 +28,11 @@ import com.pinterest.ktlint.core.ast.ElementType.WHILE import com.pinterest.ktlint.core.ast.ElementType.WHILE_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline -import java.lang.Exception 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.BRACES_BLOCK_STRUCTURE_ERROR -import org.cqfn.diktat.ruleset.utils.emptyBlockList -import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType -import org.cqfn.diktat.ruleset.utils.findLBrace -import org.cqfn.diktat.ruleset.utils.hasChildOfType -import org.cqfn.diktat.ruleset.utils.isBlockEmpty +import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl @@ -200,7 +194,7 @@ class BlockStructureBraces(private val configRules: List) : Rule("b BRACES_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "incorrect same line after opening brace", newNode.startOffset, newNode) { if (newNode.elementType != WHITE_SPACE) { - node.addChild(PsiWhiteSpaceImpl("\n"), newNode) + newNode.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newNode) } else { (newNode as LeafPsiElement).replaceWithText("\n") } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index d15573a3b2..c92801cee5 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -76,4 +76,10 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", fun `regression - shouldn't throw exception in cases similar to #371`() { fixAndCompare("Bug1Expected.kt", "Bug1Test.kt") } + + @Test + @Tag("DiktatRuleSetProvider") + fun `smoke test #3`() { + fixAndCompare("Example3Expected.kt", "Example3Test.kt") + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesExpected.kt index 3c5113ae03..6008c4bcee 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesExpected.kt @@ -13,3 +13,7 @@ class C{ private val id = 11 } } + +class D { +val x = 0 +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesTest.kt b/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesTest.kt index c01392b861..aebe620061 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/block_brace/ClassBracesTest.kt @@ -11,3 +11,5 @@ class C{ companion object{ private val id = 11} } + +class D {val x = 0} diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt index 3b6710ac3b..7fa743a166 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Expected.kt @@ -7,3 +7,7 @@ fun readFile(foo: Foo) { var bar: Bar } +class D { + val x = 0 +} + diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt index 28c2378fdf..41ddbf1625 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Bug1Test.kt @@ -2,4 +2,6 @@ package test.smoke.src.main.kotlin fun readFile(foo: Foo) { var bar: Bar -} \ No newline at end of file +} + +class D {val x = 0} diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt new file mode 100644 index 0000000000..6e371e65fa --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt @@ -0,0 +1,23 @@ +package org.cqfn.diktat + +/** + * @property name + */ +class HttpClient(var name: String) { + var url: String = "" + var port: String = "" + var timeout = 0 + + fun doRequest() {} +} + +fun mains() { + val httpClient = HttpClient("myConnection") + .apply { + url = "http://pushkin.com" + port = "8080" + timeout = 100 + } + .doRequest() +} + diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt new file mode 100644 index 0000000000..a53cb43d5d --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt @@ -0,0 +1,19 @@ +package test.smoke.src.main.kotlin + +class HttpClient(var name: String) { + var url: String = "" + var port: String = "" + var timeout = 0 + + fun doRequest() {} +} + +fun mains() { + val httpClient = HttpClient("myConnection") + .apply { + url = "http://pushkin.com" + port = "8080" + timeout = 100 + } + .doRequest() +} \ No newline at end of file