-
Notifications
You must be signed in to change notification settings - Fork 152
[Feature] importOrderIgnoreHeaderComments option #359
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
Conversation
|
cc @ayusharma and @byara, anything I should do to have this stack considered for merging? |
|
Ping? @ayusharma or @byara? |
|
Pinging again. @ayusharma or @byara? |
|
I'd really love to get this contributed, any chance @ayusharma or @byara? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the importOrderIgnoreHeaderComments option (and its companion importOrderIgnoreHeaderCommentTypes) to allow preserving and spacing license or header comments at the top of files without them being attached to imports. It updates AST extraction, comment adjustment, code assembly, plugin preprocessor, adds new tests/snapshots, and documents the feature.
- Add new config options (
importOrderIgnoreHeaderComments,importOrderIgnoreHeaderCommentTypes) insrc/index.ts - Update
extractASTNodes,getSortedNodes, andadjustCommentsOnSortedNodesto support ignoring header comments - Introduce
assembleUpdatedCodeand adjustgetCodeFromAst/preprocessor to inject sorted imports without disturbing preserved headers - Add new tests and snapshots covering the header‐comment behavior
- Update documentation (README, MIGRATION, TROUBLESHOOTING) to describe the new options
Reviewed Changes
Copilot reviewed 38 out of 44 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/index.ts | Register new CLI options |
| src/utils/extract-ast-nodes.ts | Filter out header comments based on new option |
| src/utils/adjust-comments-on-sorted-nodes.ts | Honor maintainFirstNodeComments when reattaching headers |
| src/utils/assemble-updated-code.ts | New utility to splice sorted imports back into source |
| src/utils/get-code-from-ast.ts | Switch to using assembleUpdatedCode and handle new params |
| src/preprocessors/preprocessor.ts | Integrate importOrderIgnoreHeaderComments into workflow |
| tests/HeaderComments* & snapshots | New/updated tests for header‐comment scenarios |
| README.md, docs/* | Document new options and Migration/Troubleshooting guidance |
README.md
Outdated
| "importOrderIgnoreHeaderComments": 1, | ||
| ``` | ||
|
|
||
| #### `importOrderIgnoreHeaderCommentTypess` |
Copilot
AI
Jul 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a typo in the heading: it reads importOrderIgnoreHeaderCommentTypess. It should be importOrderIgnoreHeaderCommentTypes.
| #### `importOrderIgnoreHeaderCommentTypess` | |
| #### `importOrderIgnoreHeaderCommentTypes` |
978d202 to
4955f1c
Compare
Summary: This commit changes how the sorted imports are combined with the original source. Prior to this commit, all ImportDeclaration nodes and their leading comments, plus any InterpreterDirective and Directive nodes, were extracted from the original code and re-rendered using babel. The rendered nodes were then concatenated with the original source with those nodes removed to produce the updated source. This approach safely protected against functional changes, but removed newlines around comments near the beginning of the file when the first node of the original source was an ImportDeclaration, as babel does not preserve whitespace when rendering content. If a user has configured a plugin that attempts to manage comments and/or whitespace near the top of the file, such as auto-inserting a license header (as I am trying to do), this results in conflicts / formatting churn. This commit does not directly resolve this incompatibility, however it better prepares the codebase for a plugin option to be added that can resolve the issue. Test Plan: `yarn install && yarn run test --all` Note that one snapshot was changed by this commit where a newline was changed, acting as an effective example of how the original approach could affect whitespace in the re-rendered portion of the file.
Summary: This commit adds the importOrderIgnoreHeaderComments options and all associated implementation. The importOrderIgnoreHeaderComments option allows the plugin to recognize that the first comments in code may be header files, and preserve their contents and whitespace. This can help avoid situations where this plugin and other tools conflict with expected whitespace, resulting in formatter cycles. Test Plan: `yarn install && yarn run test --all` Note: This diff adds new snapshot tests to cover the new functionality.
4955f1c to
56ff674
Compare
|
Thank you for the future implementation and effort! However, I find this option very niche and make options too complex. As I observed, currently plugin leaves first comments and don't attach them to first import, but doesn't preserve whitespace between them. Is it really often case, when file contains two or more header comments? |
Summary:
This commit adds the importOrderIgnoreHeaderComments options and all associated implementation.
The importOrderIgnoreHeaderComments option allows the plugin to
recognize that the first comments in code may be header files, and
preserve their contents and whitespace. This can help avoid
situations where this plugin and other tools conflict with expected
whitespace, resulting in formatter cycles.
Test Plan:
yarn install && yarn run test --allNote: This diff adds new snapshot tests to cover the new functionality.
Stack created with Sapling. Best reviewed with ReviewStack.
sort-imports-ignore#358