Skip to content

PipelineIndentationStyle: Fix edge case where pipeline was incorrectly detected to span mutliple lines due to backticks in the command leading up to the pipeline #1312

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
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
6 changes: 4 additions & 2 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
{
continue;
}
bool pipelinesSpanOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber ==
matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber;
IScriptExtent firstPipelineElementExtent = matchingPipeLineAstEnd.PipelineElements[0].Extent;
IScriptExtent lastPipelineElementExtent = matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count - 1].Extent;
bool pipelinesSpanOnlyOneLine = firstPipelineElementExtent.EndLineNumber == lastPipelineElementExtent.EndLineNumber
|| firstPipelineElementExtent.StartLineNumber == lastPipelineElementExtent.StartLineNumber;
if (pipelinesSpanOnlyOneLine)
{
continue;
Expand Down
18 changes: 18 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ function foo {
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

It "Should preserve script when using PipelineIndentation <PipelineIndentation> for multi-line pipeline due to backtick" -TestCases @(
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
Describe 'describe' {
It 'it' {
{ 'To be,' -or `
-not 'to be' } | Should -Be 'the question'
}
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

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