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

Support context receivers #1436

Closed
Kantis opened this issue Mar 22, 2022 · 2 comments · Fixed by #1438
Closed

Support context receivers #1436

Kantis opened this issue Mar 22, 2022 · 2 comments · Fixed by #1438
Milestone

Comments

@Kantis
Copy link
Contributor

Kantis commented Mar 22, 2022

Expected Behavior

Ktlint should handle Kotlin files where context receivers are used.

Current Behavior

Scanning a file which includes a function using context receivers from Kotlin 1.6.20 currently fails with

Not a valid Kotlin file (5:1 expecting a top level declaration) (cannot be auto-corrected)

Example declaration from Kotlin blog

interface LoggingContext {
    val log: Logger // This context provides a reference to a logger 
}

context(LoggingContext)
fun startBusinessOperation() {
    // You can access the log property since LoggingContext is an implicit receiver
    log.info("Operation has started")
}

fun test(loggingContext: LoggingContext) {
    with(loggingContext) {
        // You need to have LoggingContext in a scope as an implicit receiver
        // to call startBusinessOperation()
        startBusinessOperation()
    }
}

Additional information

  • Current version of ktlint: 0.45.1
paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Mar 23, 2022
@paul-dingemans paul-dingemans added this to the 0.46.0 milestone Mar 23, 2022
@romtsn
Copy link
Collaborator

romtsn commented Mar 24, 2022

Please give the snapshot a try while this is not released

@Kantis
Copy link
Contributor Author

Kantis commented Mar 24, 2022

Can confirm snapshot works as expected @romtsn. Thanks to you both, @paul-dingemans 🙌🏼

copybara-service bot pushed a commit to androidx/androidx that referenced this issue Jun 1, 2022
This snapshot version of ktlint supports Kotlin's context receivers syntax.
See: pinterest/ktlint#1436

Bug: 218707899
Bug: 231447570
Bug: 234161459
Test: ./gradlew ktlint
Change-Id: I61a1055693e79997fdcf75929f4572fb5488d419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants