Skip to content

Commit fdbdf05

Browse files
authored
Fix edge case when PipelineIndentationStyle is not set to NoIndentation (#1261)
* Fix indentation problem when PipelineIndentationStyle is set to IncreaseIndentationForFirstPipeline or IncreaseIndentationAfterEveryPipeline The determination if the pipelineAst is on one line needs to be tweaked to include the case where pipelines are on the same line but a multi-line expressions follows * restart ci * Apply variables naming suggestion
1 parent 05f3fa8 commit fdbdf05

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: Rules/UseConsistentIndentation.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ 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-
224223
if (matchingPipeLineAstEnd == null)
225224
{
226225
continue;
227226
}
228-
229-
bool pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber;
230-
if (pipelineSpansOnlyOneLine)
227+
bool pipelinesSpanOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber ==
228+
matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber;
229+
if (pipelinesSpanOnlyOneLine)
231230
{
232231
continue;
233232
}

Diff for: Tests/Rules/UseConsistentIndentation.tests.ps1

+18
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ function hello {
191191
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
192192
}
193193

194+
It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
195+
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
196+
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
197+
@{ PipelineIndentation = 'NoIndentation' }
198+
) {
199+
param ($PipelineIndentation)
200+
$idempotentScriptDefinition = @'
201+
function foo {
202+
function bar {
203+
Invoke-Something | ForEach-Object {
204+
}
205+
}
206+
}
207+
'@
208+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
209+
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
210+
}
211+
194212
It "Should indent pipelines correctly using NoIndentation option" {
195213
$def = @'
196214
foo |

0 commit comments

Comments
 (0)