Skip to content

Commit 8389997

Browse files
authored
Fix formatting regression in 1.18.0 whereby single-line pipeline reduces indentation level incorrectly (#1191)
* Fix issue whereby single-line pipeline reduces indentation level incorrectly (problem only showed up when there was already existing indentation due to ClipNegative * Apply suggestions from code review Co-Authored-By: bergmeister <[email protected]> * Fix tests by replacing break with continue (forgot I was inside for loop and not switch statement), simple mistake
1 parent 7f05870 commit 8389997

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Rules/UseConsistentIndentation.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,25 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
220220
// Check if the current token matches the end of a PipelineAst
221221
var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst =>
222222
PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst;
223-
if (matchingPipeLineAstEnd != null)
223+
224+
if (matchingPipeLineAstEnd == null)
224225
{
225-
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
226-
{
227-
indentationLevel = ClipNegative(indentationLevel - 1);
228-
}
229-
else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
230-
{
231-
indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1));
232-
}
226+
continue;
227+
}
228+
229+
bool pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber;
230+
if (pipelineSpansOnlyOneLine)
231+
{
232+
continue;
233+
}
234+
235+
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
236+
{
237+
indentationLevel = ClipNegative(indentationLevel - 1);
238+
}
239+
else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
240+
{
241+
indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1));
233242
}
234243
}
235244

Tests/Rules/UseConsistentIndentation.tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ baz
174174
Test-CorrectionExtentFromContent @params
175175
}
176176

177+
It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
178+
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
179+
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
180+
@{ PipelineIndentation = 'NoIndentation' }
181+
) {
182+
param ($PipelineIndentation)
183+
$idempotentScriptDefinition = @'
184+
function hello {
185+
if ($true) {
186+
"hello" | Out-Host
187+
}
188+
}
189+
'@
190+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
191+
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
192+
}
193+
177194
It "Should indent pipelines correctly using NoIndentation option" {
178195
$def = @'
179196
foo |

0 commit comments

Comments
 (0)