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

Organize type imports #55269

Merged
merged 17 commits into from
Jan 10, 2024
Merged

Organize type imports #55269

merged 17 commits into from
Jan 10, 2024

Conversation

iisaduan
Copy link
Member

@iisaduan iisaduan commented Aug 4, 2023

  • Adds a configuration to change where named type imports are sorted. organizeImportsTypeOrder can be specified as "first", "last", or "inline", with default behavior being type imports are sorted last.
  • Autoimports will place the new named import at the end of the list of named imports unless the imports are sorted as specified
  • Fixes a bug in verifyImportFixAtPosition (function used for testing)

fixes #52820

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Aug 4, 2023
@typescript-bot
Copy link
Collaborator

Thanks for the PR! It looks like you've changed the TSServer protocol in some way. Please ensure that any changes here don't break consumers of the current TSServer API. For some extra review, we'll ping @sheetalkamat, @mjbvz, @zkat, and @joj for you. Feel free to loop in other consumers/maintainers if necessary.

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

* Indicates where named type-only imports should sort. The default order ("inline") sorts imports regardless of if imports are
* type-only.
*
* Default: `inline`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current default is effectively "last"; for backwards compat I'm thinking we should probaby keep that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me, and is inline with the past changes to organize imports

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, changing the default to inline was what I was doing when I was attempting to make this change, was very helpful in debugging.

@jakebailey
Copy link
Member

This same code should also be affecting auto-imports; do we have enough test coverage for that? I'm not seeing any of those tests change which is suspicious as either it means that it's not working, or we're missing tests for auto-imports with type imports.

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me! Just a couple notes

src/services/organizeImports.ts Outdated Show resolved Hide resolved
tests/cases/fourslash/autoImportTypeImport3.ts Outdated Show resolved Hide resolved
tests/cases/fourslash/importNameCodeFix_importType10.ts Outdated Show resolved Hide resolved
Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@@ -3643,6 +3643,13 @@ export interface UserPreferences {
* Default: `false`
*/
readonly organizeImportsCaseFirst?: "upper" | "lower" | false;
/**
* Indicates where named type-only imports should sort. "inline" sorts imports without regard to if the import is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you add the VS Code option for this, make sure to say “type-only named imports” or “type-only import specifiers” since “type-only imports” could also refer to the relative order of these:

import type a from "./a";
import b from "./b";

which IIUC is unaffected by this preference.

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.

Some of the orderings seem weird, but is that because they aren't sorted?

goTo.marker("");

verify.importFixAtPosition([
`import { A, D, type C, type B } from './foo';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this one turn out to have C first? The next one doesn't which feels odd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's adding type B and it's determined that the types are unsorted (since it specified inline), it adds it on to the end. If the imports were unsorted before, it would always add onto the end, otherwise it might add the import somewhere weird in the middle, since we're not changing the order of the rest of the imports

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.

I've stared at the tests and I think everything is in order.

Note that the other new import sorting options are not actually exposed in VS Code yet (you have to use the "unstable" field IIRC, see settings.template.json), so I don't think we need to do that quite yet.

One thing I'm not sure I'm seeing is detection, in that we detect that imports are sorted "inline" and then use it. Am I missing that (there's a lot of tests...) or is that coming later?

@iisaduan
Copy link
Member Author

iisaduan commented Jan 9, 2024

@jakebailey

One thing I'm not sure I'm seeing is detection, in that we detect that imports are sorted "inline" and then use it. Am I missing that (there's a lot of tests...) or is that coming later?

Coming later!

@iisaduan
Copy link
Member Author

iisaduan commented Jan 9, 2024

@andrewbranch

Note that the other new import sorting options are not actually exposed in VS Code yet (you have to use the "unstable" field IIRC, see settings.template.json), so I don't think we need to do that quite yet.

is organizeImportsIgnoreCase exposed to VScode?

@andrewbranch
Copy link
Member

I thought it was, but I guess it’s not.

@iisaduan
Copy link
Member Author

iisaduan commented Jan 9, 2024

Note that the other new import sorting options are not actually exposed in VS Code yet [...]

briefly discussed offline, and were reminded of #52115 (comment), so there's some more thinking to be done about how to expose the setting before we do

@jakebailey
Copy link
Member

Yep, that's my understanding.

@iisaduan iisaduan merged commit 7800978 into microsoft:main Jan 10, 2024
19 checks passed
@iisaduan iisaduan deleted the organize-type-imports branch January 10, 2024 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support customization of where type imports are placed in organize/sort imports
4 participants