-
Notifications
You must be signed in to change notification settings - Fork 133
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
Only preserve directives already at the start of the program #198
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Good catch! Looks good to me. |
let's merge this! |
I was getting this issue as well, I thought it had to do with |
midrizi
approved these changes
Jan 29, 2023
Here's a quick patch, until it gets merged: Use diff --git a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/extract-ast-nodes.js b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/extract-ast-nodes.js
index 819a91c..54c1bb7 100644
--- a/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/extract-ast-nodes.js
+++ b/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/utils/extract-ast-nodes.js
@@ -10,11 +10,17 @@ function extractASTNodes(ast) {
var importNodes = [];
var directives = [];
traverse_1.default(ast, {
- Directive: function (_a) {
- var node = _a.node;
- directives.push(node);
- // Trailing comments probably shouldn't be attached to the directive
- node.trailingComments = null;
+ Directive: function (path) {
+ // Only capture directives if they are at the top scope of the source
+ // and their previous siblings are all directives
+ if (path.parent.type === 'Program' &&
+ path.getAllPrevSiblings().every(function (s) {
+ return s.type === 'Directive';
+ })) {
+ directives.push(path.node);
+ // Trailing comments probably shouldn't be attached to the directive
+ path.node.trailingComments = null;
+ }
},
ImportDeclaration: function (path) {
var tsModuleParent = path.findParent(function (p) { |
1 task
Good job :) |
byara
approved these changes
Feb 23, 2023
Released in v4.1.0. I appreciate your patience. ❤️ |
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
See: #182 (comment)
The previous logic of extracting all directives regardless of scope and moving them to the top of the program breaks various valid usages of directives.
This change updates the directive handling logic such that only the ones affected by import sorting are handled:
This ensures that only the block of directives at the top of a program are extracted + re-injected.
Previous behavior would transform this:
Into this: