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

Fixed TrailingCommaRule and StringTemplateRuleFixTest #1742

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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).toList()
nulls marked this conversation as resolved.
Show resolved Hide resolved
.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 @@ -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