Skip to content

Conversation

@ckwalsh
Copy link
Contributor

@ckwalsh ckwalsh commented May 2, 2025

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.

Stack created with Sapling. Best reviewed with ReviewStack.

@ckwalsh
Copy link
Contributor Author

ckwalsh commented May 8, 2025

cc @ayusharma and @byara, anything I should do to have this stack considered for merging?

@ckwalsh
Copy link
Contributor Author

ckwalsh commented May 13, 2025

Ping? @ayusharma or @byara?

@ckwalsh
Copy link
Contributor Author

ckwalsh commented May 28, 2025

Pinging again. @ayusharma or @byara?

@ckwalsh
Copy link
Contributor Author

ckwalsh commented Jun 30, 2025

I'd really love to get this contributed, any chance @ayusharma or @byara?

@byara byara requested review from Copilot and vladislavarsenev July 9, 2025 11:54
Copy link

Copilot AI left a 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) in src/index.ts
  • Update extractASTNodes, getSortedNodes, and adjustCommentsOnSortedNodes to support ignoring header comments
  • Introduce assembleUpdatedCode and adjust getCodeFromAst/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`
Copy link

Copilot AI Jul 9, 2025

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.

Suggested change
#### `importOrderIgnoreHeaderCommentTypess`
#### `importOrderIgnoreHeaderCommentTypes`

Copilot uses AI. Check for mistakes.
@ckwalsh ckwalsh force-pushed the pr359 branch 2 times, most recently from 978d202 to 4955f1c Compare July 18, 2025 19:45
@vladislavarsenev vladislavarsenev changed the base branch from main to v6 October 17, 2025 13:38
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.
@vladislavarsenev
Copy link
Collaborator

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?

@byara byara deleted the branch trivago:v6 October 28, 2025 15:46
@byara byara closed this Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants