Skip to content

Commit e0507b9

Browse files
authored
Fix complex case when PipelineIndentation is not set to default (NoIndentation) (#1359)
* Fix complex case when PipelineIndentation is not set to default (NoIndentation) * re-trigger ci
1 parent ca1b7c0 commit e0507b9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Rules/UseConsistentIndentation.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
129129
var tokens = Helper.Instance.Tokens;
130130
var diagnosticRecords = new List<DiagnosticRecord>();
131131
var indentationLevel = 0;
132+
var currentIndenationLevelIncreaseDueToPipelines = 0;
132133
var onNewLine = true;
133134
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true);
134135
for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++)
@@ -160,6 +161,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
160161
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
161162
{
162163
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
164+
currentIndenationLevelIncreaseDueToPipelines++;
163165
break;
164166
}
165167
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
@@ -168,6 +170,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
168170
if (isFirstPipeInPipeline)
169171
{
170172
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
173+
currentIndenationLevelIncreaseDueToPipelines++;
171174
}
172175
}
173176
break;
@@ -238,13 +241,11 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
238241
continue;
239242
}
240243

241-
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
244+
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline ||
245+
pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
242246
{
243-
indentationLevel = ClipNegative(indentationLevel - 1);
244-
}
245-
else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
246-
{
247-
indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1));
247+
indentationLevel = ClipNegative(indentationLevel - currentIndenationLevelIncreaseDueToPipelines);
248+
currentIndenationLevelIncreaseDueToPipelines = 0;
248249
}
249250
}
250251

Tests/Rules/UseConsistentIndentation.tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,23 @@ Describe 'describe' {
277277
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
278278
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
279279
}
280+
It "Should preserve script when using PipelineIndentation <PipelineIndentation> for complex multi-line pipeline" -TestCases @(
281+
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
282+
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
283+
@{ PipelineIndentation = 'NoIndentation' }
284+
) {
285+
param ($PipelineIndentation)
286+
$idempotentScriptDefinition = @'
287+
function foo {
288+
bar | baz {
289+
Get-Item
290+
} | Invoke-Item
291+
$iShouldStayAtTheSameIndentationLevel
292+
}
293+
'@
294+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
295+
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
296+
}
280297

281298
It "Should indent pipelines correctly using NoIndentation option" {
282299
$def = @'

0 commit comments

Comments
 (0)