PowerShell / PowerShell/PSScriptAnalyzer

PSUseConsistentIndentation flattens the indentation of commented-out code

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

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

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

説明

Summary

PSUseConsistentIndentation rewrites the whitespace before each comment's #, aligning every comment line in a block to the block indentation. When a run of comments is commented-out code (where the nested structure is expressed by the indentation before #), the rule flattens it, so the structure is lost. Whitespace after the # is preserved, so the two styles behave differently:

# preserved - the extra indentation is after the '#'
	# if ($b) {
	#     doThing
	# }

# flattened - the extra indentation is before the '#'
	# if ($b) {
		# doThing
	# }

Steps to reproduce

Only the one rule is enabled:

$settings = @{
	IncludeRules = @('PSUseConsistentIndentation')
	Rules        = @{
		PSUseConsistentIndentation = @{ Enable = $true; IndentationSize = 4; PipelineIndentation = 'NoIndentation'; Kind = 'tab' }
	}
}

Input:

if ($a) {
	# if ($b) {
		# doThing
	# }
}

Actual (Invoke-Formatter -ScriptDefinition $code -Settings $settings):

if ($a) {
	# if ($b) {
	# doThing
	# }
}

Expected: the commented-out block keeps its relative indentation (at most the whole comment run may be shifted to the code indentation).

The same happens with Kind = 'space' and when there is real code after the comment run.

Environment

  • PSScriptAnalyzer 1.25.0
  • PowerShell 7.6.6 on Windows

Why it matters

Commenting out a block is a common way to keep code around; flattening the indentation makes the commented-out block unreadable. Tooling that encodes directives as comments (e.g. sources that are uncommented by an external preprocessor) also relies on the indentation before # being preserved.

Related: #2151 asks for an ignore-range directive, but this is about the default behaviour of the indentation rule on ordinary comment runs.

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

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

はじめの一歩

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

調査の方向性

まず PSUseConsistentIndentation ルールとその formatter テストを見つけ、次にここに示した設定と入力を使用して Invoke-Formatter で問題を再現します。タブとスペースによるインデントを使用した、ネストされたコメントアウト済みコードのカバレッジを追加し、# の前の相対インデントが、実際のコードを含む連続部分を壊すことなく保持されることを確認します。

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

評価

技術スタック
csharp, powershell
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
明確に書かれている
初心者へのやさしさ
72/100

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

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