PowerShell / PowerShell/PSScriptAnalyzer

Formatting fails to pretty print a bad formatted document

未關閉
#1,709 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

Area - Formatter Issue - Bug
主要語言
C#
星號
2.2k
分支
415
平均合併
13 小時 1 分鐘
30 天內合併 PR
2

描述

Prerequisites
  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
Summary

I have some code copied from a pdf. Here it is when I paste it in VSCode :

function Stop-ProcessUsingWMI{    [CmdletBinding(SupportsShouldProcess=$True)]    param(      [parameter(mandatory=$true)] [regex] $pattern    )    foreach ($process in Get-WmiObject Win32_Process |        where { $_.Name -match $pattern })    {        if ($PSCmdlet.ShouldProcess(            "process $($process.Name) " +            " (id: $($process.ProcessId))" ,            "Stop Process"))        {            $process.Terminate()        }    }}

A long one-line code.

After formatting it, I get this :

function Stop-ProcessUsingWMI {
	[CmdletBinding(SupportsShouldProcess = $True)]    param(      [parameter(mandatory = $true)] [regex] $pattern    )    foreach ($process in Get-WmiObject Win32_Process | Where-Object { $_.Name -match $pattern }) {
		if ($PSCmdlet.ShouldProcess(            "process $($process.Name) " + " (id: $($process.ProcessId))" , 'Stop Process')) {
			$process.Terminate()        
		}    
	}
}

It is better but far from what one could expect from a well formatted code. There are multiple spaces everywhere and function body is not standard. Here is what I would expect :

function Stop-ProcessUsingWMI {
	[CmdletBinding(SupportsShouldProcess = $True)]
	param(
		[parameter(mandatory = $true)] [regex] $pattern 
	)
	
	foreach ($process in Get-WmiObject Win32_Process | Where-Object { $_.Name -match $pattern }) {
		if ($PSCmdlet.ShouldProcess( "process $($process.Name) " + " (id: $($process.ProcessId))" , 'Stop Process')) {
			$process.Terminate() 
		} 
	}
}
System Version
OS Win 10 x64 pro (french) 19043.1165
VS Code version 1.59.1
Extension version v2021.8.0
Powershell 7.1.4

PS extension custom settings :

	"powershell.buttons.showPanelMovementButtons": true,
	"powershell.codeFormatting.autoCorrectAliases": true,
	"powershell.codeFormatting.preset": "OTBS",
	"powershell.codeFormatting.trimWhitespaceAroundPipe": true,
	"powershell.codeFormatting.useCorrectCasing": true,
	"powershell.integratedConsole.focusConsoleOnExecute": false,
	"powershell.codeFormatting.useConstantStrings": true,
	"security.workspace.trust.untrustedFiles": "open",
	"editor.suggest.preview": true,
	"powershell.codeFormatting.ignoreOneLineBlock": false,
	"powershell.codeFormatting.whitespaceBetweenParameters": true
Proposed Design
  • remove multiple spaces when not needed
  • format function body, list of parameters and attributes on their respected line.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

使用提供的 PowerShell 函式和列出的 PSScriptAnalyzer 設定重現 issue 中的格式化結果。將輸出與預期範例進行比較,然後追蹤負責空白、函式主體、參數和屬性的格式化器行為。當格式錯誤的單行文件能夠像預期範例一樣進行格式化,且沒有不必要的重複空格時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
csharp, powershell
領域
tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。