fix(formatter/sort-imports): Do not move leading comment if empty line found#16676
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Pull request overview
This PR fixes the import sorting behavior to correctly handle comments that are separated from imports by newlines. The key change ensures that comments followed by an empty line don't incorrectly attach to the subsequent import, but instead remain associated with the preceding import or become leading orphan lines if they appear before any imports.
Key changes:
- Enhanced orphan comment detection logic to distinguish between comments directly adjacent to imports versus those separated by empty lines
- Modified return signature of
into_sorted_import_unitsto include leading orphan lines as a separate output - Added comprehensive test cases covering various comment placement scenarios
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/oxc_formatter/tests/ir_transform/sort_imports.rs | Added test should_not_attach_comment_if_newline with three test cases validating correct comment handling when separated by newlines |
| crates/oxc_formatter/src/ir_transform/sort_imports/partitioned_chunk.rs | Implemented orphan comment detection logic using two pending buffers (orphan_pending for separated comments, current_pending for adjacent ones) and changed return type to 3-tuple including leading orphan lines |
| crates/oxc_formatter/src/ir_transform/sort_imports/mod.rs | Updated to handle new 3-tuple return value and output leading orphan lines before sorted imports |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
CodSpeed Performance ReportMerging #16676 will not alter performanceComparing Summary
Footnotes
|
e1bb39d to
ab35571
Compare
Merge activity
|
…e found (#16676) In short, this PR fixes #16655 Input: ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; import { useMutation, UseMutationOptions } from "@tanstack/react-query"; ``` 🆖 Before fix: Comment is attached to `import { apiClient` line. ```js import { useMutation, UseMutationOptions } from "@tanstack/react-query"; // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; ``` 🆗 After fix: Comment with empty line is not attached. ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { useMutation, UseMutationOptions } from "@tanstack/react-query"; import { apiClient } from "../../apiClient"; ``` However, the implementation itself was not straightforward; it required a concept to represent `OrphanContent`.
ab35571 to
2495a39
Compare
…e found (#16676) In short, this PR fixes #16655 Input: ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; import { useMutation, UseMutationOptions } from "@tanstack/react-query"; ``` 🆖 Before fix: Comment is attached to `import { apiClient` line. ```js import { useMutation, UseMutationOptions } from "@tanstack/react-query"; // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; ``` 🆗 After fix: Comment with empty line is not attached. ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { useMutation, UseMutationOptions } from "@tanstack/react-query"; import { apiClient } from "../../apiClient"; ``` However, the implementation itself was not straightforward; it required a concept to represent `OrphanContent`.
…e found (oxc-project#16676) In short, this PR fixes oxc-project#16655 Input: ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; import { useMutation, UseMutationOptions } from "@tanstack/react-query"; ``` 🆖 Before fix: Comment is attached to `import { apiClient` line. ```js import { useMutation, UseMutationOptions } from "@tanstack/react-query"; // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { apiClient } from "../../apiClient"; ``` 🆗 After fix: Comment with empty line is not attached. ```js // THIS IS A GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. import { useMutation, UseMutationOptions } from "@tanstack/react-query"; import { apiClient } from "../../apiClient"; ``` However, the implementation itself was not straightforward; it required a concept to represent `OrphanContent`.

In short, this PR fixes #16655
Input:
🆖 Before fix: Comment is attached to
import { apiClientline.🆗 After fix: Comment with empty line is not attached.
However, the implementation itself was not straightforward; it required a concept to represent
OrphanContent.