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

Bugfix in TRAILLING_COMMA rule: insert comma in a proper place when there are exists comments #1110

Merged
merged 8 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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)
petertrr marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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
petertrr marked this conversation as resolved.
Show resolved Hide resolved
)

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
)
}