microsoft / microsoft/TypeScript

Detect dead exports as well as per-module dead code

オープン
#30,517 コメント 5 件 リアクション 17 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ソースファイルやテストは指定されていません。まず、コンパイラに既存のモジュール単位の未使用コード診断と、tsconfig.json のオプションがどのように処理されるかを確認し、次に、プロジェクト全体の imports、exports、ハードコードされた動的 imports を追跡する方法を調査してください。完了の条件は、アプリケーションのエントリポイントと提案された例外を尊重しながら、未使用のエクスポートされた宣言を報告する、文書化されたコンパイラオプションです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
18/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。