microsoft / microsoft/TypeScript
Detect dead exports as well as per-module dead code
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
No se especifican archivos fuente ni pruebas. Empieza revisando los diagnósticos existentes del compilador para código no utilizado por módulo y cómo se procesan las opciones de tsconfig.json; después, investiga cómo podrían rastrearse los imports, exports y los imports dinámicos hardcodeados de todo el proyecto. Se considerará terminado cuando exista una opción documentada del compilador que informe de las declaraciones exportadas no utilizadas, respetando los puntos de entrada de la aplicación y las excepciones propuestas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 18/100