[Refactor] Stop rerendering directives, inject imports instead #357
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
sort-imports-ignore
#358