PowerShell / PowerShell/PSScriptAnalyzer

PSUseConsistentIndentation flattens the indentation of commented-out code

Open
#2,217 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the PSUseConsistentIndentation rule and its formatter tests, then reproduce the issue with Invoke-Formatter using the settings and input shown here. Add coverage for nested commented-out code with both tab and space indentation, and confirm that the relative indentation before # is preserved without breaking runs containing real code.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.