Klint and spotless: com.pinterest.ktlint.core.ParseException: Expecting a parameter declaration

5.9k Views Asked by At

I'm getting weird exception when trying to run ./gradlew spotlessApply on my project in Kotlin.

Class causing the problem:

import io.realm.RealmObject
import io.realm.annotations.PrimaryKey

open class CurrentFluttering(
    @PrimaryKey var id: Long = 0,
    var currentCoinsHeap: Int = 0,
    var currentEarnedCoins: Int = 0,
    var startTime: Long = 0,
    var pauseTime: Long = 0,
    var time: Long = 0,
    var firstCycle: Boolean = true,
    var inBackground: Boolean = false,
    var currentMissedCoins: Int = 0,
    var isPaused: Boolean = false,
) : RealmObject()

Stack trace:

> Task :spotlessKotlin FAILED
Step 'ktlint' found problem in 'app/src/main/java/com/cfhero/android/model/state/CurrentFluttering.kt':
Expecting a parameter declaration
com.pinterest.ktlint.core.ParseException: Expecting a parameter declaration
        at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:357)
        at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.diffplug.spotless.kotlin.KtLintStep$State.lambda$createFormat$1(KtLintStep.java:173)
        at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:31)
        at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:78)
        at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:76)
        at com.diffplug.spotless.Formatter.compute(Formatter.java:230)
        at com.diffplug.spotless.Formatter.applyToAndReturnResultIfDirty(Formatter.java:192)
        at com.di

Have anybody experienced same or similar problem?

2

There are 2 best solutions below

2
On BEST ANSWER

I found it. The problem was dangling ',' after last parameter in constructor (which Kotlin allows ).

var isPaused: Boolean = false, <- here ლ(ಠ益ಠლ)
) : RealmObject()
0
On

This is caused due to the default ktlint version used by the plugin. As of this time it's private static final String DEFAULT_VERSION = "0.35.0" which does not support trailing commas, although kotlin 1.4 does. In fact they had updated it to 0.40.0 at some point but got some formatting issues whiplash and eventually reverted the changes.

If you want though you can manually point to the latest version with something like

spotless {
    kotlin {
        target '**/*.kt'
        ktlint("0.40.0")
    }
}