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

2

6

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?

Microphotograph answered 19/1, 2021 at 12:43 Comment(0)
M
27

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

var isPaused: Boolean = false, <- here ლ(ಠ益ಠლ)
) : RealmObject()
Microphotograph answered 19/1, 2021 at 12:43 Comment(2)
i think you just saved me from hours of debug and headaches 😂Johns
Also a very common issue when you have a list/map defined like this: listOf(1, 2, 3,)Countenance
C
4

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

Cambric answered 22/1, 2021 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.