Skip to content

Commit

Permalink
PipelineIndentationStyle: Fix edge case where pipeline was incorrectl…
Browse files Browse the repository at this point in the history
…y detected to span mutliple lines due to backticks in the command leading up to the pipeline (PowerShell#1312)

* Fix logic to detect single-line pipeline

* combine statements to not break other cases

* Add test

* Apply suggestions from code review

Co-Authored-By: Robert Holt <rjmholt@gmail.com>
  • Loading branch information
bergmeister and rjmholt committed Aug 19, 2019
1 parent c5b0382 commit 171c81d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit 171c81d

Please sign in to comment.