Ktlint: Kotlin 1.4 trailing commas marked as invalid syntax

Created on 22 Apr 2020  路  7Comments  路  Source: pinterest/ktlint

Expected Behavior

Kotlin 1.4 introduces the ability to have trailing commas (https://blog.jetbrains.com/kotlin/2019/12/what-to-expect-in-kotlin-1-4-and-beyond/#trailing-commas). ktlint currently marks this as invalid syntax

Observed Behavior

ktlint throws the following error:
Not a valid Kotlin file (expecting a parameter declaration) (cannot be auto-corrected)

Steps to Reproduce

Run ktlint on the following contents

data class Test(
    val test1: String,
    val test2: String,
) {
    fun test(
        t1: String,
        t2: String,
    ) {

    }
}

Your Environment

  • Version of ktlint used: 0.36.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): id("org.jlleitschuh.gradle.ktlint") version "9.2.1" (Gradle plugin)
  • Version of Gradle used (if applicable): 5.6.4
  • Operating System and version: macOS 10.15.2
  • Link to your project (if it's a public repository): https://github.com/airmap/AirMapSDK-Kotlin/

Most helpful comment

@yuesaka-m you don't need to wait for ktlint-gradle new release, just set ktlint version to 0.40.0 via ktlint-gradle configuration.

All 7 comments

Actually it is already supported in Kotlin 1.3.70, see https://youtrack.jetbrains.com/issue/KT-34743

Probably requires to set kotlinOptions.targetVersion to 1.4

Also related to #709

This is still not resolved on my code, what was the workaround?

@carbotaniuman

I have configured the version manually (because the plugin pulled in 0.35.0) and the issue went away:
in my build.gradle.kts:

kotlin {
  ktlint("0.40.0")
}

Thanks!

While what @rethab suggests most likely will work, if your project determines the ktlint version via
org.jlleitschuh.gradle:ktlint-gradle, then, there's currently a PR open in that repo that's attempting to update their default ktlint version to 0.40.0.

Once this PR goes in, the other option to fix this issue would be updating org.jlleitschuh.gradle:ktlint-gradle to the latest version.

This way there aren't two places where the dependencies for the ktlint version is defined in your project.

Cheers!

@yuesaka-m you don't need to wait for ktlint-gradle new release, just set ktlint version to 0.40.0 via ktlint-gradle configuration.

@Tapchicoma oh great, didn't know that configuration option existed, thanks!

Was this page helpful?
0 / 5 - 0 ratings