Skip to content

Commit 128a9a2

Browse files
bergmeisterChristoph Bergmeister
and
Christoph Bergmeister
authored
Fix edge case of PSUseConsistentIndentation for non-default value (IncreaseIndentationForFirstPipeline/IncreaseIndentationAfterEveryPipeline) (#1423)
* When pipelineIndentationStyle is non-default setting: fix edge * add test cases * Apply test code simplification suggested by @JamesWTruher Co-authored-by: Christoph Bergmeister <[email protected]>
1 parent 975fcc6 commit 128a9a2

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Rules/UseConsistentIndentation.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
168168
}
169169
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
170170
{
171-
bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst => PositionIsEqual(((PipelineAst)pipelineAst).PipelineElements[0].Extent.EndScriptPosition, tokens[tokenIndex - 1].Extent.EndScriptPosition));
171+
bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst =>
172+
PositionIsEqual(LastPipeOnFirstLineWithPipeUsage((PipelineAst)pipelineAst).Extent.EndScriptPosition,
173+
tokens[tokenIndex - 1].Extent.EndScriptPosition));
172174
if (isFirstPipeInPipeline)
173175
{
174176
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
@@ -255,6 +257,21 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
255257
return diagnosticRecords;
256258
}
257259

260+
private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipelineAst)
261+
{
262+
CommandBaseAst lastPipeOnFirstLineWithPipeUsage = pipelineAst.PipelineElements[0];
263+
foreach (CommandBaseAst pipelineElement in pipelineAst.PipelineElements.Skip(1))
264+
{
265+
if (pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.StartLineNumber ||
266+
pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber ||
267+
pipelineElement.Extent.EndLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber)
268+
{
269+
lastPipeOnFirstLineWithPipeUsage = pipelineElement;
270+
}
271+
}
272+
return lastPipeOnFirstLineWithPipeUsage;
273+
}
274+
258275
private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2)
259276
{
260277
return position1.ColumnNumber == position2.ColumnNumber &&

Tests/Rules/UseConsistentIndentation.tests.ps1

+42
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,48 @@ baz
225225
Test-CorrectionExtentFromContent @params
226226
}
227227

228+
It "Should indent pipelines correctly using <PipelineIndentation> option" -TestCases @(
229+
@{
230+
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
231+
ExpectCorrection = $true
232+
},
233+
@{
234+
PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline'
235+
ExpectCorrection = $true
236+
},
237+
@{
238+
PipelineIndentation = 'NoIndentation'
239+
ExpectCorrection = $false
240+
}
241+
@{
242+
PipelineIndentation = 'None'
243+
ExpectCorrection = $false
244+
}
245+
) {
246+
Param([string] $PipelineIndentation, [bool] $ExpectCorrection)
247+
$def = @'
248+
foo | bar |
249+
baz
250+
'@
251+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
252+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
253+
if ($ExpectCorrection) {
254+
$violations.Count | Should -Be 1
255+
$params = @{
256+
RawContent = $def
257+
DiagnosticRecord = $violations[0]
258+
CorrectionsCount = 1
259+
ViolationText = "baz"
260+
CorrectionText = $indentationUnit * $indentationSize + 'baz'
261+
}
262+
Test-CorrectionExtentFromContent @params
263+
}
264+
else
265+
{
266+
$violations | Should -BeNullOrEmpty
267+
}
268+
}
269+
228270
It 'Should preserve script when using PipelineIndentation None' -TestCases @(
229271
@{ IdempotentScriptDefinition = @'
230272
foo |

0 commit comments

Comments
 (0)