Repository: pinterest/ktlint
Issue: Comma on newline preceded by // comment will move code into comment

I got a when phrase. The case is


before
```java
when (type) {
            IHomeLivingConstant.LIVING_TYPE_GUESS_YOU_LIKE //a
                , IHomeLivingConstant.Live_MODULE_CONTENT_CODE // b
                , IHomeLivingConstant.LIVE_MODULE_AUTO_PLAY  //c
-> {
    // do something
}
```

after formatting, I turn out
```java
when (type) {
            IHomeLivingConstant.LIVING_TYPE_GUESS_YOU_LIKE //a, IHomeLivingConstant.Live_MODULE_CONTENT_CODE // b, IHomeLivingConstant.LIVE_MODULE_AUTO_PLAY  //c
-> {
    //do something
} 

```

This lead to bugs cause it will not invoke compile error, but is wrong logically.  And it is hard to find the bug in the hundreds of formatted files.

Changed files:
ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRule.kt
ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRuleTest.kt

diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRule.kt
index 5a947817..17485ea7 100644
--- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRule.kt
+++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRule.kt
@@ -2,10 +2,14 @@ package com.pinterest.ktlint.ruleset.standard
 
 import com.pinterest.ktlint.core.Rule
 import com.pinterest.ktlint.core.ast.isPartOfString
+import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
 import com.pinterest.ktlint.core.ast.nextLeaf
+import com.pinterest.ktlint.core.ast.nextSibling
+import com.pinterest.ktlint.core.ast.prevCodeLeaf
 import com.pinterest.ktlint.core.ast.prevLeaf
 import com.pinterest.ktlint.core.ast.upsertWhitespaceAfterMe
 import org.jetbrains.kotlin.com.intellij.lang.ASTNode
+import org.jetbrains.kotlin.com.intellij.psi.PsiComment
 import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
 import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
 
@@ -21,7 +25,20 @@ class SpacingAroundCommaRule : Rule("comma-spacing") {
             if (prevLeaf is PsiWhiteSpace) {
                 emit(prevLeaf.startOffset, "Unexpected spacing before \\"${node.text}\\"", true)
                 if (autoCorrect) {
-                    prevLeaf.node.treeParent.removeChild(prevLeaf.node)
+                    val isPrecededByComment = prevLeaf.prevLeaf { it !is PsiWhiteSpace } is PsiComment
+                    if (isPrecededByComment && prevLeaf.isWhiteSpaceWithNewline()) {
+                        // If comma is on new line and preceded by a comment, it should be moved before this comment
+                        // https://github.com/pinterest/ktlint/issues/367
+                        val previousStatement = node.prevCodeLeaf()!!
+                        previousStatement.treeParent.addChild(node.clone(), previousStatement.nextSibling { true })
+                        val nextLeaf = node.nextLeaf()
+                        if (nextLeaf is PsiWhiteSpace) {
+                            nextLeaf.treeParent.removeChild(nextLeaf)
+                        }
+                        node.treeParent.removeChild(node)
+                    } else {
+                        prevLeaf.treeParent.removeChild(prevLeaf)
+                    }
                 }
             }
             if (node.nextLeaf() !is PsiWhiteSpace) {
diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRuleTest.kt
index 4a8db107..ee148da6 100644
--- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRuleTest.kt
+++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundCommaRuleTest.kt
@@ -47,6 +47,8 @@ class SpacingAroundCommaRuleTest {
     fun testFormat() {
         assertThat(SpacingAroundCommaRule().format("fun main() { x(1,3); x(1, 3) }"))
             .isEqualTo("fun main() { x(1, 3); x(1, 3) }")
+        assertThat(SpacingAroundCommaRule().format("fun main() { x(1 /* comment */ , 3); x(1 /* comment */, 3) }"))
+            .isEqualTo("fun main() { x(1 /* comment */, 3); x(1 /* comment */, 3) }")
         assertThat(
             SpacingAroundCommaRule().format(
                 """
@@ -69,5 +71,28 @@ class SpacingAroundCommaRuleTest {
             ) = Unit
             """.trimIndent()
         )
+        assertThat(
+            SpacingAroundCommaRule().format(
+                """
+                fun fn(
+                    arg1: Int ,
+                    arg2: Int // some comment
+                    , arg3: Int
+                    // other comment
+                    , arg4: Int
+                ) = Unit
+                """.trimIndent()
+            )
+        ).isEqualTo(
+            """
+            fun fn(
+                arg1: Int,
+                arg2: Int, // some comment
+                arg3: Int,
+                // other comment
+                arg4: Int
+            ) = Unit
+            """.trimIndent()
+        )
     }
 }