Skip to content

Fix complex case when PipelineIndentation is not set to default (NoIndentation) #1359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
var tokens = Helper.Instance.Tokens;
var diagnosticRecords = new List<DiagnosticRecord>();
var indentationLevel = 0;
var currentIndenationLevelIncreaseDueToPipelines = 0;
var onNewLine = true;
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true);
for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++)
Expand Down Expand Up @@ -160,6 +161,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
{
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
currentIndenationLevelIncreaseDueToPipelines++;
break;
}
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
Expand All @@ -168,6 +170,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
if (isFirstPipeInPipeline)
{
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
currentIndenationLevelIncreaseDueToPipelines++;
}
}
break;
Expand Down Expand Up @@ -238,13 +241,11 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
continue;
}

if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline ||
pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
{
indentationLevel = ClipNegative(indentationLevel - 1);
}
else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline)
{
indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1));
indentationLevel = ClipNegative(indentationLevel - currentIndenationLevelIncreaseDueToPipelines);
currentIndenationLevelIncreaseDueToPipelines = 0;
}
}

Expand Down
17 changes: 17 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ Describe 'describe' {
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}
It "Should preserve script when using PipelineIndentation <PipelineIndentation> for complex multi-line pipeline" -TestCases @(
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
function foo {
bar | baz {
Get-Item
} | Invoke-Item
$iShouldStayAtTheSameIndentationLevel
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

It "Should indent pipelines correctly using NoIndentation option" {
$def = @'
Expand Down