"Organize Imports" should adjust specifiers based on preferences

Đang mở
#58,642 3 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
typescript, vscode

Hướng nghiên cứu

Bắt đầu với lệnh TypeScript: Sort imports và hành vi quick-fix của language server được mô tả trong issue. Theo dõi cách typescript.preferences.importModuleSpecifier, tsconfig baseUrl và paths ảnh hưởng đến các import specifier được tạo. Hoàn tất khi các import được sắp xếp và các đường dẫn đủ điều kiện được điều chỉnh nhất quán theo các tùy chọn đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Domain: LS: Organize Imports In Discussion Suggestion
🔍 Search Terms

"sort imports", "organize imports"

✅ Viability Checklist
⭐ Suggestion

I have a feature request for the language server / it's commands.

So I have a large project, and I have not changed tsconfig.json since the beginning of the time.
Now I "discovered" what baseUrl does, what paths does, and that there is an option in vscode - ```

"typescript.preferences.importModuleSpecifier": "non-relative", // | "project-relative" | "relative" | "shortest"`.

So I have a file, deep inside the source - which imports from a file, close to the root.
Before I added 'baseUrl' - it did

import { bar } from "../../../../../foo/bar";`

After adding "baseUrl": "./src/" in tsconfig - this could be written as

import { bar } from "foo/bar";`

bar();

If I delete the import, and go to the bar() to fix the import issue - it provides me quick fix -

  • Add import from "foo/bar" if I have specified "typescript.preferences.importModuleSpecifier": "shortest" in settings., or
  • Add import from "../../../../../foo/bar" if I have specified "typescript.preferences.importModuleSpecifier": "relative" in settings.

I really don't like those endless "../../../../../" so I would like to fix them. The only "tool" I have for now - is just to delete the import, and resolve those imports manually - so it gives me the best import paths given my settings and my tsconfig's "baseUrl" or "paths".

This is tedious. And it doesn't work where I have used import * as foo from [..].

In VSCode - there is a command Organize imports (I think this comes not from TypeScript?) (In my optionion - wasted effort. since it doesn't organize them, it just sorts them, but this is besides the point (it does not come from TypeScript))

But TypeScript does come with a command "Typescript: Sort imports"!

What I would want - after executing the command, it would not only sort the imports, but also - change the paths for those imports matching my preferences of settings.json "typescript.preferences.importModuleSpecifier" + tsconfig.json "baseUrl" and "paths".

Or it could be a new command - Typescript: Organize imports? (maybe not..)

📃 Motivating Example

This feature would help automatically shorten imports / make the satisfy project setting for a file.

💻 Use Cases
  1. What do you want to use this for?
    To automatically organize imports
  2. What shortcomings exist with current approaches?
    You have to manually go over each import and edit them one by one
  3. What workarounds are you using in the meantime?
    Deleting the import and resolving them using quick fix.
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
106

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.