PowerShell / PowerShell/PSScriptAnalyzer

Rule request: Prefix parameters with an unambiguous (ASCII) hyphen-minus

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

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

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

説明

Hyphen-minus characters are sometimes unintendedly replaced when passed by word processors as Microsoft Office applications as MSWord and Outlook. It is recommended to prefix parameters with an unambiguous hyphen-minus as Unicode dashes might cause parsing errors in earlier versions of PowerShell.
Besides it unnecessary triggers the UseBOMForUnicodeEncodedFile rule with no clear indication of the location.

PowerShell 7
# .\Test.ps1
Write-Host “test” –ForegroundColor ‘Red’ -BackgroundColor ‘Green’
Invoke-ScriptAnalyzer .\Test.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingWriteHost               Warning      Test.ps1   1     File 'Test.ps1' uses Write-Host. Avoid using
                                                                  Write-Host because it might not work in all hosts,
                                                                  does not work when there is no host, and (prior to
                                                                  PS 5.0) cannot be suppressed, captured, or
                                                                  redirected. Instead, use Write-Output,
                                                                  Write-Verbose, or Write-Information.
PSUseBOMForUnicodeEncodedFile       Warning      Test.ps1         Missing BOM encoding for non-ASCII encoded file
                                                                  'Test.ps1'
Windows PowerShell (5.1)

Windows PowerShell (5.1) still has issue with EM-dashes:

.\test.ps1
At C:\Users\Gebruiker\downloads\Test.ps1:1 char:25
+ ...  “test” –ForegroundColor ‘Red’ -BackgroundColor ‘Green’
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Invoke-ScriptAnalyzer .\Test.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
TerminatorExpectedAtEndOfString     ParseError   Test.ps1   1     The string is missing the terminator: ".
PSAvoidUsingWriteHost               Warning      Test.ps1   1     File 'Test.ps1' uses Write-Host. Avoid using
                                                                  Write-Host because it might not work in all hosts,
                                                                  does not work when there is no host, and (prior to
                                                                  PS 5.0) cannot be suppressed, captured, or
                                                                  redirected. Instead, use Write-Output,
                                                                  Write-Verbose, or Write-Information.
PSUseBOMForUnicodeEncodedFile       Warning      Test.ps1         Missing BOM encoding for non-ASCII encoded file
                                                                  'Test.ps1'

Proposed technical implementation details (optional)

See (PowerShell based) prototype UseHyphenMinusForParameter at: https://github.com/iRon7/PSRules

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

1.21.0

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

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

はじめの一歩

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

調査の方向性

Issue にある PowerShell の例から始め、PSRules にあるリンク先の UseHyphenMinusForParameter プロトタイプを確認します。提案されている動作を、既存の PSScriptAnalyzer ルールおよび UseBOMForUnicodeEncodedFile の出力と比較します。完了の条件は、パラメーターの接頭辞として使用されている Unicode のダッシュ文字が、示されている PowerShell のケースを含め、明確な位置情報と診断情報付きで検出されることです。

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

評価

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

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

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