microsoft / microsoft/TypeScript
Typescript should prevent invalid variable assigning in if conditions
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Suggestion
🔍 Search Terms
"assigning if", I don't know what else to look for
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code (would only detect errors that are already occurring)
- 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
I believe a lot of JavaScript programmers have accidentally written code that assigns a new value to a variable instead of comparing it to something else by doing the following
let variable = "text"
if (variable = "value") // rest of the code
When they actually meant to do this
let variable = "text"
if (variable === "value") // rest of the code
And this code is invalid and an error should be thrown by TypeScript before runtime because it is essentially creating an invalid if condition that will never run. A possible problem that could come from creating an error in this would be situations where variables are correctly reassigned and then compared to something, like in the following example:
let matches;
while ((matches = this.constructor.CHANNELS_PATTERN.exec(this._content)) !== null) {
const chan = this.client.channels.cache.get(matches[1]);
if (chan) this._channels.set(chan.id, chan);
}
Even though the above example uses a while, it's still a valid use case where an error should not be thrown since it is being compared later.
📃 Motivating Example
TypeScript will now be able to detect invalid if conditions and help you improve them before runtime so that they aren't ignored.
💻 Use Cases
Basically, whenever you're coding while tired and accidentally write this, TS would alert you like all other TS features.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue には実装ファイル、テスト、エントリーポイントが記載されていません。まず、TypeScript checker が if 条件内の代入と報告された例をどのように処理しているかを確認し、有効な while ループのケースも含めて検討してください。Done では、誤った代入と有効な代入と比較のパターンを区別する診断動作を定義し、それらのケースをカバーするテストを用意する必要があります。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100