-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Change detection in organizeImports #57267
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 |
…/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