PSUseConsistentIndentation flattens the indentation of commented-out code
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 72/100
Piste de recherche
Commencez par localiser la règle PSUseConsistentIndentation et ses tests du formateur, puis reproduisez le problème avec Invoke-Formatter en utilisant les paramètres et l’entrée présentés ici. Ajoutez une couverture pour du code imbriqué commenté avec une indentation composée de tabulations et d’espaces, et confirmez que l’indentation relative avant # est préservée sans perturber les séquences contenant du code réel.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
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.
- Langage dominant
- C#
- Étoiles
- 2.2k
- Forks
- 415
- Merge moyen
- 13 h 1 min
- PR mergées (30 j)
- 2
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Difficulté 1/5 1-3 heures Accessibilité débutants 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 commentaires ·
-
PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 70/100
PowerShell/PSScriptAnalyzer#2216 · 2 commentaires ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
PowerShell/PSScriptAnalyzer#2211 ·
-
`PSPlaceOpenBrace` and `PSPlaceCloseBrace` leave trailing whitespace when expanding one-line blocks Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 70/100
PowerShell/PSScriptAnalyzer#2210 ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
PowerShell/PSScriptAnalyzer#2209 ·
Toutes les issues de PowerShell/PSScriptAnalyzer
Issues similaires
-
bug
Difficulté 1/5 Moins d'une heure Accessibilité débutants 75/100
sillsdev/languageforge-lexbox#2665 ·
-
bug documentation frontend
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
azurenoops/spin_agent#975 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulté 2/5 1-3 heures Accessibilité débutants 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
SubtitleEdit/subtitleedit#15108 · 1 commentaire ·