-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Change detection in organizeImports #57267
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
…etect-organize-imports
src/compiler/core.ts
Outdated
let i = 0, j = 0; | ||
while (i < array1.length && j < array2.length) { | ||
switch (comparer(array1[i], array2[j])) { | ||
case Comparison.LessThan: |
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.
This isn't something new, but I am somewhat nervous about switching on these values exactly; our Comparison type is unsound and we have cases where we actually return values that are not these three, but instead just positive/negative values. See also #52897.
Maybe it doesn't happen with what is passed to this new function (and the assert helps), of course.
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 encountered that bug while working on this function (fixed by using a better comparer
), so I put the default option there to prevent any loose ends from happening with unsound Comparisons.
@typescript-bot pack this |
@typescript-bot pack this |
…tect-organize-imports
…etect-organize-imports
@typescript-bot pack this |
], | ||
/*errorCode*/ undefined, | ||
{ organizeImportsTypeOrder : "last" }); | ||
goTo.marker("0a"); |
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.
What is with the extra navigation here? Aren't these all at the same positions?
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.
Sorry, just saw this comment. It helps to see which of the tests fail, as opposed to just reading "test failed at marker 0" for all of the tests. I can change it back if it's going to make the test much slower?
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.
No, that totally makes sense, I just haven't seen any examples of this.
Not for this PR, but this makes me wonder if we should have the fourslash harness keep track of how many times it runs each function to better indicate what it's doing, or include the line number, etc. I think some of that already happens in stack traces?
I feel like this is looking good, though my only overarching concern is the general feeling that we're going to see a lot of change in the way people's organize imports behave if it really is the case that we're going to start preferring insensitive more than we used to. Unless I'm still totally brainfarting and that's what we were doing before. |
…/TypeScript into detect-organize-imports
@typescript-bot pack this |
Hey @iisaduan, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
I don't want to let this one languish; besides how the defaults may have changed, is there anything else you need feedback on? The behavior does seem fine. |
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.
LGTM I think, but would be cool if @andrewbranch looked (I think he just went out of town, oops)
This PR changes the detection method in OrganizeImports to make a decision off of the sort order that will make the least changes to the file.
If there is a tie or not enough information to make a decision, then we default to case insensitive, and for type order,
last
followed byinline
. These defaults should line up with our previous behavior, though with this new detection method, we should be resorting to the default in fewer cases.The new detection has also been implemented in the codefixes that use any functions that organizeImports was previously supplying.
Some other features of this PR:
organizeImports
did with the previous detection method. Testing on a codespace, detecting on a version ofchecker.ts
that is sorted and has some imports changed to type-only, tsserver logs report that organize imports takes:organizeImports.ts
file by removing unused functions, and grouping functions (especially the ones that are only used for testing)The following settings can be specified under
typescript.unstable
orjavascript.unstable
insettings.json
fororganizeImports
(example)The latter settings are documented here: #52115