microsoft / microsoft/TypeScript

"Organize Imports" should adjust specifiers based on preferences

Aperta
#58,642 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: LS: Organize Imports In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il comando TypeScript: Sort imports e il comportamento di quick-fix del language server descritto nell’issue. Traccia come typescript.preferences.importModuleSpecifier, tsconfig baseUrl e paths influenzano gli specificatori di import generati. Il lavoro è completato quando gli import sono ordinati e i percorsi idonei vengono adattati in modo coerente con queste preferenze.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript, vscode
Ambito
developer-experience, tooling
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.