Skip to content

auto-import with alphabetized order by default #33839

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

Closed
5 tasks done
shiqimei opened this issue Oct 7, 2019 · 3 comments · Fixed by #39394
Closed
5 tasks done

auto-import with alphabetized order by default #33839

shiqimei opened this issue Oct 7, 2019 · 3 comments · Fixed by #39394
Assignees
Labels
Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone

Comments

@shiqimei
Copy link

shiqimei commented Oct 7, 2019

Search Terms

auto-import; import; auto import

Suggestion

Support auto-import with alphabetized order by default

Use Cases

image
Currently, Typescript auto-import by adding the new module at the end. But it does not obey tslint rule: ordered-imports It will be better if typescript can auto-import with alphabetized order by default :)

Examples

import { B } from 'moduleB';
...
const a = new A(); // Ctrl+. -> Do you want to import A?

then

import { A, B } from 'moduleB'; // A is auto-imported in front of B
...
const a = new a();
...
classs ModuleA implements IModuleA { // Ctrl+. -> Do you want to import IModuleA?
...

then

import { IModuleA } from 'moduleA'; // moduleA is auto-imported above moduleB
import { A, B } from 'moduleB';
...
const a = new a();
...
class ModuleA implements IModuleA {
...

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@shiqimei shiqimei changed the title auto-import auto-import with alphabetized order by default Oct 7, 2019
@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Oct 17, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 17, 2019
@mjbvz
Copy link
Contributor

mjbvz commented Jun 3, 2020

This issue should cover inserting imports in the right order for :

  • Auto imports
  • Add missing import quick fix
  • The add all missing imports quick fix

Marking as a VS Code polish item

@mjbvz mjbvz added the VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone label Jun 3, 2020
@andrewbranch
Copy link
Member

Sorting import specifiers within a single import clause seems fairly uncontroversial. When it comes to the order of import declarations, though, there are a million different rules that people follow. Just a few examples:

  • Separate into groups by default import, namespace import, and named imports, then sort alphabetically by the first alias name within those groups (eslint default)
  • Sort alphabetically by module specifier (tslint default)
  • Separate into first-party and third-party groups

And every parameter of this sorting is infinitely customizable. It seems like if you have a declaration sorting lint rule enabled, we’re bound to get it wrong more often than not. If I had to pick a ruleset to follow based on estimated adoption, I’d probably go with ESLint’s default sort-imports, but I’m almost reluctant to implicitly endorse such a specific and nitpicky linter rule.

@mjbvz
Copy link
Contributor

mjbvz commented Jun 10, 2020

From our discussion this morning, it sounds like we want to be fairly conservative:

  • If the existing imports in a file are sorted (according to TypeScript's default sorting behavior) then add any new imports to the end. If multiple imports are added, do not try sorting just those added imports

  • However if the existing imports in a file are sorted, then try to insert any added imports in their sorted position

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript VS Code Priority Critical issues that VS Code needs fixed in the current TypeScript milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants