Skip to content

Inconsistent blankLinesBetweenScopesWhen combined with #if directives #2533

Description

@jshier

Given

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import LibC
#elseif canImport(FoundationEssentials)
// Platforms that don't have clock_gettime.
import FoundationEssentials
#endif

struct Instant {
    let value: Double

    #if canImport(Darwin) || canImport(Glibc)
    init() {
        var time = timespec()
        clock_gettime(CLOCK_MONOTONIC_RAW, &time)
        value = Double(time.tv_sec) + (Double(time.tv_nsec) / 1_000_000_000)
    }
    #elseif canImport(FoundationEssentials)
    init() {
        value = Date.now.timeIntervalSinceReferenceDate
    }
    #endif
}

When running with blankLinesBetweenScopes enabled, this is the output:

```swift
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import LibC
#elseif canImport(FoundationEssentials)
// Platforms that don't have clock_gettime.
import FoundationEssentials
#endif

struct Instant {
    let value: Double

    #if canImport(Darwin) || canImport(Glibc)
    init() {
        var time = timespec()
        clock_gettime(CLOCK_MONOTONIC_RAW, &time)
        value = Double(time.tv_sec) + (Double(time.tv_nsec) / 1_000_000_000)
    }

    #elseif canImport(FoundationEssentials)
    init() {
        value = Date.now.timeIntervalSinceReferenceDate
    }
    #endif
}

This is unexpected (to me) for two reasons:

  1. I wouldn't consider #if directives scopes for the purpose of the rule.
  2. If it is a scope for the rules, I would expect a similar newline added at the end of the other init, but it's not.

I'm not sure what the intention is here. If 1, then the rule should be fixed. If 2, then I'd appreciate a an option added to not add a newline when in #if directives and it should be fixed to add the newline for the second init.

Metadata

Metadata

Assignees

Labels

fixed in developbug/feature resolved in the develop branch

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions