Skip to content

Conversation

@ckwalsh
Copy link
Contributor

@ckwalsh ckwalsh commented May 2, 2025

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.

Stack created with Sapling. Best reviewed with ReviewStack.

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.
@vladislavarsenev vladislavarsenev merged commit a1d05d7 into trivago:v6 Oct 17, 2025
3 checks passed
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.

2 participants