Skip to content

Commit 8e6182f

Browse files
bergmeisterChristoph Bergmeisterrjmholt
authored
Fix bug with PipelineIndentationStyle.None where break instead of continue statement was used (#1497)
* Fix bug with PipelineIndentationStyle.None where break instead of continue statement was used. TODO: Fix test that starts to fail because of this now * fix last failing test to make None not touch existing indendation * empty commit since Azure Pipelines did not trigger * empty commit since Azure Pipelines did not trigger * Apply suggestions from code review Co-authored-by: Robert Holt <[email protected]> * Apply suggestions from code review Co-authored-by: Christoph Bergmeister <[email protected]> Co-authored-by: Robert Holt <[email protected]>
1 parent a23af17 commit 8e6182f

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

Rules/UseConsistentIndentation.cs

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;
@@ -220,14 +220,19 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
220220
}
221221
}
222222

223-
bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token);
223+
if (pipelineIndentationStyle == PipelineIndentationStyle.None && PreviousLineEndedWithPipe(tokens, tokenIndex, token))
224+
{
225+
continue;
226+
}
224227

228+
bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token);
225229
AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken);
226230
}
227231
break;
228232
}
229233

230-
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
234+
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { continue; }
235+
231236
// Check if the current token matches the end of a PipelineAst
232237
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token);
233238
if (matchingPipeLineAstEnd == null)
@@ -284,6 +289,35 @@ private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens
284289
return false;
285290
}
286291

292+
private static bool PreviousLineEndedWithPipe(Token[] tokens, int tokenIndex, Token token)
293+
{
294+
if (tokenIndex < 2 || token.Extent.StartLineNumber == 1)
295+
{
296+
return false;
297+
}
298+
299+
int searchIndex = tokenIndex - 2;
300+
int searchLine;
301+
do
302+
{
303+
searchLine = tokens[searchIndex].Extent.StartLineNumber;
304+
if (tokens[searchIndex].Kind == TokenKind.Comment)
305+
{
306+
searchIndex--;
307+
}
308+
else if (tokens[searchIndex].Kind == TokenKind.Pipe)
309+
{
310+
return true;
311+
}
312+
else
313+
{
314+
break;
315+
}
316+
} while (searchLine == token.Extent.StartLineNumber - 1 && searchIndex >= 0);
317+
318+
return false;
319+
}
320+
287321
private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token)
288322
{
289323
int searchIndex = tokenIndex;

Tests/Rules/UseConsistentIndentation.tests.ps1

+34
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,40 @@ baz
239239
Test-CorrectionExtentFromContent @params
240240
}
241241

242+
It "Should indent hashtable correctly using <PipelineIndentation> option" -TestCases @(
243+
@{
244+
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
245+
},
246+
@{
247+
PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline'
248+
},
249+
@{
250+
PipelineIndentation = 'NoIndentation'
251+
}
252+
@{
253+
PipelineIndentation = 'None'
254+
}
255+
) {
256+
Param([string] $PipelineIndentation)
257+
$scriptDefinition = @'
258+
@{
259+
foo = "value1"
260+
bar = "value2"
261+
}
262+
'@
263+
$settings = @{
264+
IncludeRules = @('PSUseConsistentIndentation')
265+
Rules = @{ PSUseConsistentIndentation = @{ Enable = $true; PipelineIndentation = $PipelineIndentation } }
266+
}
267+
Invoke-Formatter -Settings $settings -ScriptDefinition $scriptDefinition | Should -Be @'
268+
@{
269+
foo = "value1"
270+
bar = "value2"
271+
}
272+
'@
273+
274+
}
275+
242276
It "Should indent pipelines correctly using <PipelineIndentation> option" -TestCases @(
243277
@{
244278
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'

0 commit comments

Comments
 (0)