microsoft / microsoft/TypeScript
Detect dead exports as well as per-module dead code
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
dead code
Suggestion
Add a feature to detect dead code not just within modules, but within the build as a whole.
I've looked at https://github.com/Microsoft/TypeScript/issues/16939 which is currently locked so can't comment there. This really does need to be part of the Typescript compiler as no other program is really going to understand all the subtleties; the emitted JS doesn't contain all the information about interfaces and that kind of thing. Furthermore, this is not a file level operation- you're talking about tracking each individual interface/function/etc and not files. With these two factors it's no surprise that there are no current tools that can accomplish this- you would have to reimplement the compiler.
This would probably not be useful for library projects, as by definition things they export are not dead, therefore a separate compiler switch would be useful that application developers can enable.
The exact behaviour should be that exported top-level declarations may be considered "dead" if they are not visibly imported, as well as if they are not used from within the current module. The compiler will error on unused exports. The compiler should detect dynamic imports if the module path is hardcoded- the compiler already supports proper type inference in this case so feels like the compiler can already figure this one out.
This does not have to extend to properties/members of classes or anything like that- that can be a future improvement.
The compiler should ignore exports with a leading underscore, offer a built-in decorator to override a declaration as used, or both to handle any weird cases or entry points.
Use Cases
We already detect dead code across individual files, but not across our whole project. We wish to do so across the entire project. We currently inspect runtime metadata of modules imported/exported as a unit test; unfortunately this is limited because TypeScript will emit modules that don't have any runtime code (e.g. just define an interface) but won't emit references to them. In addition, this approach can only detect whole modules as dead and not parts of them.
Examples
The user would simply turn this on in their tsconfig.json, mark up the entry point for their app, then run the build.
Checklist
My suggestion meets these guidelines:
- [*] This wouldn't be a breaking change in existing TypeScript/JavaScript code- existing users don't have to turn on the flag if they don't want to and it probably shouldn't be part of --strict either.
- [*] 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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non sono indicati file sorgente né test. Inizia esaminando le diagnostiche esistenti del compilatore per il codice inutilizzato a livello di modulo e il modo in cui vengono elaborate le opzioni di tsconfig.json; quindi verifica come potrebbero essere tracciati gli import, gli export e gli import dinamici hardcoded a livello di progetto. Il lavoro sarà considerato completato quando esisterà un’opzione documentata del compilatore che segnali le dichiarazioni esportate inutilizzate, rispettando i punti di ingresso dell’applicazione e le eccezioni proposte.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 18/100