Skip to content
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

Merged
merged 38 commits into from
Mar 15, 2024

Conversation

iisaduan
Copy link
Member

@iisaduan iisaduan commented Feb 1, 2024

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 by inline. 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:

  • Adds type-order detection.
  • Fixes #57030, so that outputs of running Organize Imports will be stable.
  • Removes need to resolve SortKind.
  • Runs in about the same time as organizeImports did with the previous detection method. Testing on a codespace, detecting on a version of checker.ts that is sorted and has some imports changed to type-only, tsserver logs report that organize imports takes:
    • On nightly 20240220, runs in ~3100ms. This PR runs in ~2700ms after this commit (that changes detection method to "is-sorted").
    • On nightly 20240221, runs in ~2000ms. This PR runs in ~1900ms after this commit.
    • (On a not-already-sorted version, runtime increases all around, due to the amount of time needed to write changes)
  • Cleans up the 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 or javascript.unstable in settings.json for organizeImports (example)

// these two are the only settings that if unspecified, will be detected on 
"organizeImportsIgnoreCase"?: "auto" | boolean;
"organizeImportsTypeOrder"?: "last" | "inline"  | "first";

// see #52115
"organizeImportsCollation"
"organizeImportsCaseFirst"
"organizeImportsLocale"
"organizeImportsNumericCollation"
"organizeImportsAccentCollation"

The latter settings are documented here: #52115

let i = 0, j = 0;
while (i < array1.length && j < array2.length) {
switch (comparer(array1[i], array2[j])) {
case Comparison.LessThan:
Copy link
Member

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.

Copy link
Member Author

@iisaduan iisaduan Feb 21, 2024

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.

@iisaduan
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 22, 2024

Heya @iisaduan, I've started to run the tarball bundle task on this PR at e3ccaf1. You can monitor the build here.

@iisaduan
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2024

Heya @iisaduan, I've started to run the tarball bundle task on this PR at 64f1468. You can monitor the build here.

@iisaduan
Copy link
Member Author

@typescript-bot pack this

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Mar 9, 2024
@iisaduan iisaduan added this to the TypeScript 5.5.0 milestone Mar 9, 2024
@iisaduan
Copy link
Member Author

iisaduan commented Mar 9, 2024

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 9, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 9, 2024

Hey @iisaduan, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/160250/artifacts?artifactName=tgz&fileId=4CC3954A4A018AEE5F144DEDD33C1552CCBD37F5E1E6A3D1B7390D6EA95933DC02&fileName=/typescript-5.5.0-insiders.20240309.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@jakebailey
Copy link
Member

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.

Copy link
Member

@jakebailey jakebailey left a 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)

@iisaduan iisaduan merged commit 0a671aa into microsoft:main Mar 15, 2024
26 checks passed
@iisaduan iisaduan changed the title Change detection to detection by diff in organizeImports Change detection in organizeImports Mar 16, 2024
@iisaduan iisaduan deleted the detect-organize-imports branch March 18, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

organizeImports needs to be called twice to be stable
4 participants