Skip to content

Commit

Permalink
Bugfix in TRAILLING_COMMA rule: insert comma in a proper place when t…
Browse files Browse the repository at this point in the history
…here are exists comments (#1110)

### What's done:
* Bugfix in TRAILLING_COMMA: insert comma in a proper place when there are exists comments
* Expand tests
  • Loading branch information
kgevorkyan authored Nov 10, 2021
1 parent b188eab commit 0f1f8af
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings.TRAILING_COMMA
import org.cqfn.diktat.ruleset.rules.DiktatRule

import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.COLLECTION_LITERAL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.COMMA
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.INDICES
import com.pinterest.ktlint.core.ast.ElementType.KDOC
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.core.ast.ElementType.TYPE_ARGUMENT_LIST
Expand Down Expand Up @@ -102,7 +105,17 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
// 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) {
val parent = this.treeParent
parent.addChild(LeafPsiElement(COMMA, ","), this.treeNext)

// In case, when we got VALUE_PARAMETER, it may contain comments, which follows the actual parameter and all of them are actually in the same node
// Ex: `class A(val a: Int, val b: Int // comment)`
// `val b: Int // comment` --> the whole expression is VALUE_PARAMETER
// So, in this case we must insert comma before the comment, in other cases we will insert it after current node
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)
}
?: parent.addChild(LeafPsiElement(COMMA, ","), this.treeNext)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ fun isReferenceApplicable(myReference: KClass<*>,
else -> false
}

fun isReferenceApplicable(myReference: KClass<*> ,// comment
) = when (myReference) {
Comparable::class,
Iterable::class,
String::class,
-> true
else -> false
}

@ApplicableFor([
"serializer",
"balancer",
Expand Down Expand Up @@ -70,4 +79,45 @@ fun printMeanValue() {
meanValue += year
}
println(meanValue/cars.size)
}

enum class SomeEnum(
val a: Int, val b: Int ,// comment
)

enum class SomeEnum(
val a: Int, val b: Int,// comment
)

enum class SomeEnum(
val a: Int, val b: Int ,/* comment */
)

enum class SomeEnum(
val a: Int, val b: Int, /**
some comment */
)

fun foo() {
val sum: (Int, Int, Int,) -> Int = fun(
x,
y,
z ,// trailing comma
): Int {
return x + y + x
}
println(sum(8, 8, 8))
}

fun shift(x: Int, y: Int) {
shift(
25,
20, // trailing comma
)

val colors = listOf(
"red",
"green",
"blue", // trailing comma
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ fun isReferenceApplicable(myReference: KClass<*>
else -> false
}

fun isReferenceApplicable(myReference: KClass<*> // comment
) = when (myReference) {
Comparable::class,
Iterable::class,
String::class
-> true
else -> false
}

@ApplicableFor([
"serializer",
"balancer",
Expand Down Expand Up @@ -70,4 +79,45 @@ fun printMeanValue() {
meanValue += year
}
println(meanValue/cars.size)
}

enum class SomeEnum(
val a: Int, val b: Int // comment
)

enum class SomeEnum(
val a: Int, val b: Int// comment
)

enum class SomeEnum(
val a: Int, val b: Int /* comment */
)

enum class SomeEnum(
val a: Int, val b: Int /**
some comment */
)

fun foo() {
val sum: (Int, Int, Int,) -> Int = fun(
x,
y,
z // trailing comma
): Int {
return x + y + x
}
println(sum(8, 8, 8))
}

fun shift(x: Int, y: Int) {
shift(
25,
20 // trailing comma
)

val colors = listOf(
"red",
"green",
"blue" // trailing comma
)
}

0 comments on commit 0f1f8af

Please sign in to comment.