Skip to content

Comment-only code blocks are removed when OrderedImports is enabled #1080

@a7medev

Description

@a7medev

Problem

swift-format (with OrderedImports enabled) turns the following code

import A

// Comment

import B

into this

import A
import B

Removing the comment in between.

Expected Behavior

I expect it to output the following code preserving the comment.

import A
import B

// Comment

Potential Solution

After looking into this issue, it happens in convertToCodeBlockItems(lines:) where the comment is added pendingLeadingTrivia but never added to the final output.

One way to fix this is to append the remaining pendingLeadingTrivia to the last item's trailingTrivia (probably trimming lines surrounding comments as well).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions