-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Preserve leading trivia when organizing imports #22832
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
When organizing imports, we used to move the leading and trailing trivia of each individual import with that import as it was repositioned or deleted. This had the unfortunate effect of repositioning or deleting the header comment of the file. Our new approach is to leave the leading trivia of the first import ahead of the resulting block of imports (i.e. it no longer follows the first import as it is repositioned or deleted). Trailing trivia on the first import and trivia on subsequent imports are treated as before. Caveat: We had to hack around emitter limitation microsoft#22831. Fixes microsoft#22723 Fixes microsoft#22724 Fixes microsoft#22731
|
|
||
| // ==ORGANIZED== | ||
|
|
||
| /*A*/ import /*B*/ { /*C*/ F1 /*D*/ } /*G*/ from /*H*/ "lib" /*I*/; /*J*/ //K |
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.
I'm not sure why we lost the space, but it doesn't seem very problematic since this commenting pattern is very unusual.
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.
It looks like the space wasn't present in the input, so this is a good change.
| */ | ||
| /* @internal */ | ||
| export function suppressTrailingTrivia(node: Node) { | ||
| Debug.assertDefined(node); |
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.
Seems unnecessary as getOrCreateEmitNode will crash on undefined input anyway, and as of #22088 the type system should help with this.
|
|
||
| // ==ORGANIZED== | ||
|
|
||
| /*A*/ import /*B*/ { /*C*/ F1 /*D*/ } /*G*/ from /*H*/ "lib" /*I*/; /*J*/ //K |
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.
It looks like the space wasn't present in the input, so this is a good change.
|
I pulled this in and proposed a different fix in #22836 which I think is more appropriate (and less hacky) until we make our token comment emit more robust. |
When organizing imports, we used to move the leading and trailing trivia
of each individual import with that import as it was repositioned or
deleted. This had the unfortunate effect of repositioning or deleting
the header comment of the file.
Our new approach is to leave the leading trivia of the first import
ahead of the resulting block of imports (i.e. it no longer follows the
first import as it is repositioned or deleted). Trailing trivia on the
first import and trivia on subsequent imports are treated as before.
Caveat: We had to hack around emitter limitation #22831.
Fixes #22723
Fixes #22724
Fixes #22731