Skip to content

Commit

Permalink
Fixed TrailingCommaRule and StringTemplateRuleFixTest (#1742)
Browse files Browse the repository at this point in the history
- add checking children to support case with comma before comment
- reused ASTFactory
- removed copyElement in StringTemplateFormatRule
- added a warn test for StringTemplateFormatRule
  • Loading branch information
nulls committed Sep 21, 2023
1 parent 2e563f2 commit 05e7aa4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class StringTemplateFormatRule(configRules: List<RulesConfig>) : DiktatRule(

if (identifierName != null && node.treeParent.text.trim('"', '$') == identifierName) {
STRING_TEMPLATE_QUOTES.warnAndFix(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) {
val identifier = node.findChildByType(REFERENCE_EXPRESSION)!!.copyElement()
val identifier = node.findChildByType(REFERENCE_EXPRESSION)!!
// node.treeParent is String template that we need to delete
node.treeParent.treeParent.addChild(identifier, node.treeParent)
node.treeParent.treeParent.removeChild(node.treeParent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_IN_RANGE
import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_IS_PATTERN
import org.jetbrains.kotlin.KtNodeTypes.WHEN_ENTRY
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.LeafPsiElement
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC
import org.jetbrains.kotlin.lexer.KtTokens.BLOCK_COMMENT
import org.jetbrains.kotlin.lexer.KtTokens.COMMA
Expand Down Expand Up @@ -101,9 +101,13 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
}

private fun ASTNode.checkTrailingComma(config: Boolean) {
val shouldFix = this.siblings(true).toList().run {
!this.map { it.elementType }.contains(COMMA) && this.any { it.isWhiteSpaceWithNewline() || it.isPartOfComment() }
}
val noCommaInSiblings = siblings(true).toSet()
.let { siblings ->
siblings.none { it.elementType == COMMA } && siblings.any { it.isWhiteSpaceWithNewline() || it.isPartOfComment() }
}
val noCommaInChildren = children().none { it.elementType == COMMA }
val shouldFix = noCommaInSiblings && noCommaInChildren

if (shouldFix && config) {
// we should write type of node in warning, to make it easier for user to find the parameter
TRAILING_COMMA.warnAndFix(configRules, emitWarn, isFixMode, "after ${this.elementType}: ${this.text}", this.startOffset, this) {
Expand All @@ -116,9 +120,9 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
val comments = listOf(EOL_COMMENT, BLOCK_COMMENT, KDOC)
val firstCommentNodeOrNull = if (this.elementType == VALUE_PARAMETER) this.children().firstOrNull { it.elementType in comments } else null
firstCommentNodeOrNull?.let {
this.addChild(LeafPsiElement(COMMA, ","), firstCommentNodeOrNull)
this.addChild(ASTFactory.leaf(COMMA, ","), it)
}
?: parent.addChild(LeafPsiElement(COMMA, ","), this.treeNext)
?: parent.addChild(ASTFactory.leaf(COMMA, ","), this.treeNext)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import com.saveourtool.diktat.ruleset.rules.chapter3.StringTemplateFormatRule
import com.saveourtool.diktat.util.FixTestBase

import generated.WarningNames
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test

class StringTemplateRuleFixTest : FixTestBase("test/paragraph3/string_template", ::StringTemplateFormatRule) {
@Test
@Tag(WarningNames.STRING_TEMPLATE_CURLY_BRACES)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `should fix enum order`() {
fixAndCompare("StringTemplateExpected.kt", "StringTemplateTest.kt")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,20 @@ class StringTemplateRuleWarnTest : LintTestBase(::StringTemplateFormatRule) {
""".trimMargin()
)
}

@Test
@Tag(STRING_TEMPLATE_QUOTES)
fun `should trigger on long string template`() {
lintMethod(
"""
|class Some {
| fun some() {
| val x = "asd"
| val trippleQuotes = ""${'"'}${'$'}x""${'"'}
| }
|}
""".trimMargin(),
DiktatError(4, 31, ruleId, "${Warnings.STRING_TEMPLATE_QUOTES.warnText()} ${'$'}x", true)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class TrailingCommaFixTest : FixTestBase("test/paragraph3/trailing_comma", ::Tra

@Test
@Tag(WarningNames.TRAILING_COMMA)
@Disabled("https://github.com/saveourtool/diktat/issues/1737")
fun `should add all trailing comma`() {
fixAndCompare("TrailingCommaExpected.kt", "TrailingCommaTest.kt", config)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SomeClass {

val binExpr = "${foo as Foo}"

val trippleQuotes = """$x"""
val trippleQuotes = x

val test = """${'$'}"""

Expand Down

0 comments on commit 05e7aa4

Please sign in to comment.