Skip to content

Commit 171c81d

Browse files
bergmeisterrjmholt
andauthoredAug 19, 2019
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)
* 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>
1 parent c5b0382 commit 171c81d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎Rules/UseConsistentIndentation.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
224224
{
225225
continue;
226226
}
227-
bool pipelinesSpanOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber ==
228-
matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber;
227+
IScriptExtent firstPipelineElementExtent = matchingPipeLineAstEnd.PipelineElements[0].Extent;
228+
IScriptExtent lastPipelineElementExtent = matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count - 1].Extent;
229+
bool pipelinesSpanOnlyOneLine = firstPipelineElementExtent.EndLineNumber == lastPipelineElementExtent.EndLineNumber
230+
|| firstPipelineElementExtent.StartLineNumber == lastPipelineElementExtent.StartLineNumber;
229231
if (pipelinesSpanOnlyOneLine)
230232
{
231233
continue;

‎Tests/Rules/UseConsistentIndentation.tests.ps1

+18
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ function foo {
209209
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
210210
}
211211

212+
It "Should preserve script when using PipelineIndentation <PipelineIndentation> for multi-line pipeline due to backtick" -TestCases @(
213+
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
214+
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
215+
@{ PipelineIndentation = 'NoIndentation' }
216+
) {
217+
param ($PipelineIndentation)
218+
$idempotentScriptDefinition = @'
219+
Describe 'describe' {
220+
It 'it' {
221+
{ 'To be,' -or `
222+
-not 'to be' } | Should -Be 'the question'
223+
}
224+
}
225+
'@
226+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
227+
Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
228+
}
229+
212230
It "Should indent pipelines correctly using NoIndentation option" {
213231
$def = @'
214232
foo |

0 commit comments

Comments
 (0)
Please sign in to comment.