From 2e563f2c5f3a983726ee91076a6185036d12bc78 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 21 Sep 2023 14:19:41 +0300 Subject: [PATCH] Fixed StatelessClassesRule (#1741) - removed usage of copyElement - reused ASTFactory --- .../chapter6/classes/StatelessClassesRule.kt | 17 +++++++++-------- .../chapter6/StatelessClassesRuleFixTest.kt | 2 -- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter6/classes/StatelessClassesRule.kt b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter6/classes/StatelessClassesRule.kt index dea7417374..92ceadc57e 100644 --- a/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter6/classes/StatelessClassesRule.kt +++ b/diktat-rules/src/main/kotlin/com/saveourtool/diktat/ruleset/rules/chapter6/classes/StatelessClassesRule.kt @@ -13,9 +13,8 @@ import org.jetbrains.kotlin.KtNodeTypes.FUN import org.jetbrains.kotlin.KtNodeTypes.OBJECT_DECLARATION import org.jetbrains.kotlin.KtNodeTypes.SUPER_TYPE_ENTRY import org.jetbrains.kotlin.KtNodeTypes.SUPER_TYPE_LIST +import org.jetbrains.kotlin.com.intellij.lang.ASTFactory import org.jetbrains.kotlin.com.intellij.lang.ASTNode -import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement -import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.lexer.KtTokens.CLASS_KEYWORD import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER import org.jetbrains.kotlin.lexer.KtTokens.INTERFACE_KEYWORD @@ -50,14 +49,16 @@ class StatelessClassesRule(configRules: List) : DiktatRule( if (isClassExtendsValidInterface(node, interfaces) && isStatelessClass(node)) { OBJECT_IS_PREFERRED.warnAndFix(configRules, emitWarn, isFixMode, "class ${(node.psi as KtClass).name!!}", node.startOffset, node) { - val newObjectNode = CompositeElement(OBJECT_DECLARATION) + val newObjectNode = ASTFactory.composite(OBJECT_DECLARATION) + val children = node.children().toList() node.treeParent.addChild(newObjectNode, node) - node.children().forEach { - newObjectNode.addChild(it.copyElement(), null) + children.forEach { + if (it.elementType == CLASS_KEYWORD) { + newObjectNode.addChild(ASTFactory.leaf(OBJECT_KEYWORD, "object")) + } else { + newObjectNode.addChild(it) + } } - newObjectNode.addChild(LeafPsiElement(OBJECT_KEYWORD, "object"), - newObjectNode.getFirstChildWithType(CLASS_KEYWORD)) - newObjectNode.removeChild(newObjectNode.getFirstChildWithType(CLASS_KEYWORD)!!) node.treeParent.removeChild(node) } } diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter6/StatelessClassesRuleFixTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter6/StatelessClassesRuleFixTest.kt index ddde081b9f..cb7f4a3db3 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter6/StatelessClassesRuleFixTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/chapter6/StatelessClassesRuleFixTest.kt @@ -4,14 +4,12 @@ import com.saveourtool.diktat.ruleset.rules.chapter6.classes.StatelessClassesRul import com.saveourtool.diktat.util.FixTestBase import generated.WarningNames.OBJECT_IS_PREFERRED -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test class StatelessClassesRuleFixTest : FixTestBase("test/chapter6/stateless_classes", ::StatelessClassesRule) { @Test @Tag(OBJECT_IS_PREFERRED) - @Disabled("https://github.com/saveourtool/diktat/issues/1737") fun `fix class to object keyword`() { fixAndCompare("StatelessClassExpected.kt", "StatelessClassTest.kt") }