PSUseConsistentIndentation flattens the indentation of commented-out code

Abierto
#2,217 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp, powershell
Área
tooling

Línea de trabajo

Comienza localizando la regla PSUseConsistentIndentation y sus pruebas del formateador; después, reproduce el problema con Invoke-Formatter usando la configuración y la entrada mostradas aquí. Añade cobertura para código anidado comentado con indentación mediante tabulaciones y espacios, y confirma que la indentación relativa antes de # se conserva sin romper ejecuciones que contienen código real.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
2.2k
Forks
415
Merge medio
13 h 1 min
PR fusionados (30 d)
2

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PowerShell/PSScriptAnalyzer

Todos los issues de PowerShell/PSScriptAnalyzer

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.