@@ -28,8 +28,8 @@ import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_EXPRESSION
28
28
import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_IN_RANGE
29
29
import org.jetbrains.kotlin.KtNodeTypes.WHEN_CONDITION_IS_PATTERN
30
30
import org.jetbrains.kotlin.KtNodeTypes.WHEN_ENTRY
31
+ import org.jetbrains.kotlin.com.intellij.lang.ASTFactory
31
32
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
32
- import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
33
33
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens.KDOC
34
34
import org.jetbrains.kotlin.lexer.KtTokens.BLOCK_COMMENT
35
35
import org.jetbrains.kotlin.lexer.KtTokens.COMMA
@@ -101,9 +101,13 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
101
101
}
102
102
103
103
private fun ASTNode.checkTrailingComma (config : Boolean ) {
104
- val shouldFix = this .siblings(true ).toList().run {
105
- ! this .map { it.elementType }.contains(COMMA ) && this .any { it.isWhiteSpaceWithNewline() || it.isPartOfComment() }
106
- }
104
+ val noCommaInSiblings = siblings(true ).toList()
105
+ .let { siblings ->
106
+ siblings.none { it.elementType == COMMA } && siblings.any { it.isWhiteSpaceWithNewline() || it.isPartOfComment() }
107
+ }
108
+ val noCommaInChildren = children().none { it.elementType == COMMA }
109
+ val shouldFix = noCommaInSiblings && noCommaInChildren
110
+
107
111
if (shouldFix && config) {
108
112
// we should write type of node in warning, to make it easier for user to find the parameter
109
113
TRAILING_COMMA .warnAndFix(configRules, emitWarn, isFixMode, " after ${this .elementType} : ${this .text} " , this .startOffset, this ) {
@@ -116,9 +120,9 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
116
120
val comments = listOf (EOL_COMMENT , BLOCK_COMMENT , KDOC )
117
121
val firstCommentNodeOrNull = if (this .elementType == VALUE_PARAMETER ) this .children().firstOrNull { it.elementType in comments } else null
118
122
firstCommentNodeOrNull?.let {
119
- this .addChild(LeafPsiElement (COMMA , " ," ), firstCommentNodeOrNull )
123
+ this .addChild(ASTFactory .leaf (COMMA , " ," ), it )
120
124
}
121
- ? : parent.addChild(LeafPsiElement (COMMA , " ," ), this .treeNext)
125
+ ? : parent.addChild(ASTFactory .leaf (COMMA , " ," ), this .treeNext)
122
126
}
123
127
}
124
128
}
0 commit comments