PSUseConsistentIndentation flattens the indentation of commented-out code
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 72/100
Rechercherichtung
Beginne damit, die Regel PSUseConsistentIndentation und ihre Formatter-Tests zu finden, und reproduziere dann das Problem mit Invoke-Formatter unter Verwendung der hier gezeigten Einstellungen und Eingaben. Füge Abdeckung für verschachtelten auskommentierten Code mit Einrückungen aus Tabulatoren und Leerzeichen hinzu und bestätige, dass die relative Einrückung vor # erhalten bleibt, ohne Läufe zu beeinträchtigen, die echten Code enthalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- C#
- Sterne
- 2.2k
- Forks
- 415
- Ø Merge
- 13 Std. 1 Min.
- Gemergte PRs (30 T.)
- 2
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 Kommentare ·
-
PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 70/100
PowerShell/PSScriptAnalyzer#2216 · 2 Kommentare ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
PowerShell/PSScriptAnalyzer#2211 ·
-
`PSPlaceOpenBrace` and `PSPlaceCloseBrace` leave trailing whitespace when expanding one-line blocks Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 70/100
PowerShell/PSScriptAnalyzer#2210 ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 55/100
PowerShell/PSScriptAnalyzer#2209 ·
Alle Issues in PowerShell/PSScriptAnalyzer
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
nightscout/nocturne#1379 ·
-
priority-0
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
StackExchange/StackExchange.Redis#3249 ·
-
[Feat] 조합 영역 구분선 개선 Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100