PowerShell / PowerShell/PSScriptAnalyzer

Rule request: Warning if `Set`/`Get-Variable` is invoked with only the basic `-Name`/`-Value` parameters

オープン
#1,706 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

Area - Rules Issue - New Rule Up-for-Grabs
主要言語
C#
スター
2.2k
フォーク
414
平均マージ
13時間 1分
マージ済み PR(30日)
2

説明

Referring to StackOverflow questions along with: How do I automaticaly create and use variable names?.

The cmdlets:

Could completely mislead a novice PowerShell scripter in a wrong programming direction.

The general learning process of a scripter/programmer is playing with variables. Than it gets extended to list of variables like $Var1 = 'One', $Var2 = 'Two', .... The list extends further and they understand they need to automate this somehow but finding guidance for this is quiet a leap and if they ask for guidance at e.g. StackOverflow they often get a direct answer/comment in the direction of using one of these <verb>-Variable cmdlets which is not a good practice for these users and what they atempt to do or learn.

Afaik, there is no good reason to use one of these <verb>-Variable cmdlets which just -Name and -Value parameters.

e.g. Get-Variable MyVar can be replaced by $MyVar and Set-Variable MyVar 'Test' can be replaced by $MyVar = 'Test'

The only other reason to use just the -Name and -Value parameters is to create dynamic variable names (variable indirection) like:

Set-Variable "Var$i" 'Test'

Which is to my opinion a bad practice as they will be added to the same dictionary as the rest of the parameters and one might easily overwrite other general variables and lose the oversight.

Summary of the new feature

When it comes to creating a dynamic variables, a separated dictionary (in the form of a hashtable) should be created and a warning/hint could be given in that direction.

Visa verse, advanced users that know what they are doing, might simply avoid the warning by adding a specific (e.g. -Scope Script) parameter.

What is the latest version of PSScriptAnalyzer at the point of writing
1.19.1

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

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

はじめの一歩

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

調査の方向性

まず既存の PSScriptAnalyzer ルールの実装とテストを確認し、次に Get-Variable、Set-Variable、New-Variable の呼び出しがどのように解析されるかを追跡します。基本的な名前/値パラメーターのみを使用する呼び出しに対する警告条件と、明示的なスコープパラメーターに対する除外を定義します。ルールとそのテストでこれらのケースをカバーできれば完了です。

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

評価

技術スタック
powershell
領域
tooling
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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