PowerShell / PowerShell/PSScriptAnalyzer

UseConsistentWhitespace rule CheckOperator behavior is inconsistent across PowerShell versions & not well defined

未关闭
#1,606 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Area - Formatter Issue - Bug
主要语言
C#
星标
2.2k
派生
414
平均合并
13 小时 1 分钟
30 天内合并 PR
2

描述

UseConsistentWhitespace rule CheckOperator behavior is inconsistent across PowerShell versions & not well defined

In trying to figure out why the CheckOperator option of the UseConsistentWhitespace rule handles unary operators inconsistently (see #1239), I also discovered that the rule also behaves differently from version 5.1 to version 7.0 with some binary operators (at least the bitwise ones). Once I found that, I searched the code and the documentation to see what operators it is actually intended to handle, and found that to be inconclusive if not contradictory (see here, here, and here). Therefore I am proposing the following:

  • a decision should be made on exactly which operators CheckOperators is going to apply to
  • the documentation, code, and tests should be updated to reflect this, and to be consistent across PS versions that the module supports (hopefully at least 5.1 and current Core version)

Steps to reproduce

Invoke-Formatter {7-band3}

Expected behavior

7 -band 3

Actual behavior (PS 5.1)

7-band3

Actual behavior (PS 7.0)

7 -band 3

Environment data (PS 5.1)

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.1110
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.1110
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.1

Environment data (PS 7.0)

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.1

After looking at the code (see here, and here), my best guess is that the original intent was probably just to handle arithmetic (just +,-,/,*,%) and assignment operators. It looks like the intent of the IsOperator function was to check for:

  • Any assignment operator
  • Any operator with equal precedence to +
  • Any operator with equal precedence to *
  • Also && and ||. (I do not understand why these were specially included and other operators were not.)

So, still puzzled, I debugged it and found that TokenTraits.HasTrait does not behave as expected. (I opened https://github.com/PowerShell/PowerShell/issues/13820 to try and find out if there was a reason for this puzzling behaviour or if it would be worth changing.) It turns out that the function only does bitwise comparison and the BinaryPrecedence* values of TokenFlags are not actual bit flags, so code like TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd) returns true for more values than one would expect. It also behaves differently from PS 5.1 to PS 7.0 because the numeric values of the BinaryPrecedence* values were changed when Null Coalescing operator was implemented.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Rules/UseConsistentWhitespace.cs 及其 IsOperator 逻辑开始,然后比较 Rules/Strings.resx 和 RuleDocumentation/UseConsistentWhitespace.md 中的运算符描述。使用 Invoke-Formatter {7-band3} 在 PowerShell 5.1 和 7.0 下复现该行为。完成的标准是:受支持的运算符集合、代码、文档和测试在所有受支持的 PowerShell 版本之间保持一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, powershell
领域
tooling
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。