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
派生
414
平均合并
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 摘要。