PowerShell / PowerShell/PSScriptAnalyzer

Formatting fails to pretty print a bad formatted document

Đang mở
#1,709 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Area - Formatter Issue - Bug
Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện kết quả định dạng từ issue bằng cách sử dụng hàm PowerShell được cung cấp và các thiết lập PSScriptAnalyzer được liệt kê. So sánh đầu ra với ví dụ mong đợi, sau đó lần theo hoạt động của trình định dạng chịu trách nhiệm về khoảng trắng, thân hàm, tham số và thuộc tính. Hoàn thành khi tài liệu một dòng bị định dạng sai được định dạng giống như ví dụ mong đợi mà không có các khoảng trắng lặp lại không cần thiết.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, powershell
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.