Skip to content

Commit 53ece47

Browse files
bergmeisterChristoph Bergmeisterrjmholt
authored
UseConsistentIndentation: When non-default options are used, cater for the case of a comment between pipe and newline (#1463)
* UseConsistentIndentation: When non-default options are used, cater for the case of a comment between pipe and newline * Update Rules/UseConsistentIndentation.cs Co-Authored-By: Robert Holt <[email protected]> * re-trigger ci * try split test template * Revert "try split test template" This reverts commit 226259d. * fix syntax due to missing brace (also: GitHub suggestions did not correctly insert whitespaces in there) Co-authored-by: Christoph Bergmeister <[email protected]> Co-authored-by: Robert Holt <[email protected]>
1 parent 26f34b5 commit 53ece47

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Rules/UseConsistentIndentation.cs

+32-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
155155

156156
case TokenKind.Pipe:
157157
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
158-
bool pipelineIsFollowedByNewlineOrLineContinuation = tokenIndex < tokens.Length - 1 && tokenIndex > 0 &&
159-
(tokens[tokenIndex + 1].Kind == TokenKind.NewLine || tokens[tokenIndex + 1].Kind == TokenKind.LineContinuation);
158+
bool pipelineIsFollowedByNewlineOrLineContinuation =
159+
PipelineIsFollowedByNewlineOrLineContinuation(tokens, tokenIndex);
160160
if (!pipelineIsFollowedByNewlineOrLineContinuation)
161161
{
162162
break;
@@ -254,6 +254,36 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
254254
return diagnosticRecords;
255255
}
256256

257+
private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens, int startIndex)
258+
{
259+
if (startIndex >= tokens.Length - 1)
260+
{
261+
return false;
262+
}
263+
264+
Token nextToken = null;
265+
for (int i = startIndex + 1; i < tokens.Length; i++)
266+
{
267+
nextToken = tokens[i];
268+
269+
switch (nextToken.Kind)
270+
{
271+
case TokenKind.Comment:
272+
continue;
273+
274+
case TokenKind.NewLine:
275+
case TokenKind.LineContinuation:
276+
return true;
277+
278+
default:
279+
return false;
280+
}
281+
}
282+
283+
// We've run out of tokens but haven't seen a newline
284+
return false;
285+
}
286+
257287
private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token)
258288
{
259289
int searchIndex = tokenIndex;

Tests/Rules/UseConsistentIndentation.tests.ps1

+12
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ foo |
156156
Invoke-FormatterAssertion $scriptDefinition $expected 3 $settings
157157
}
158158

159+
It "When a comment is after a pipeline and before the newline " {
160+
$scriptDefinition = @'
161+
foo | # comment
162+
bar
163+
'@
164+
$expected = @'
165+
foo | # comment
166+
bar
167+
'@
168+
Invoke-FormatterAssertion $scriptDefinition $expected 1 $settings
169+
}
170+
159171
It "Should find a violation if a pipleline element is not indented correctly" {
160172
$def = @'
161173
get-process |

0 commit comments

Comments
 (0)