Skip to content

Commit ea49400

Browse files
authored
Add PipelineIndentationStyle.None option for scenarios where indentation is custom, inconsistent or the user does not like any of the 3 pipeline indentation styles (#1399)
* Add PipelineIndentationStyle.None option for scenarios where indentation is custom, inconsistent or the user does not like any of the 3 pipeline indentation styles * re-trigger ci
1 parent fec0452 commit ea49400

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

RuleDocumentation/UseConsistentIndentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ foo |
5353
bar |
5454
baz
5555
```
56+
- None: Do not change any existing pipeline indentation.
5657

5758
#### Kind: string (Default value is `space`)
5859

Rules/UseConsistentIndentation.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ private enum PipelineIndentationStyle
9090
{
9191
IncreaseIndentationForFirstPipeline,
9292
IncreaseIndentationAfterEveryPipeline,
93-
NoIndentation
93+
NoIndentation,
94+
None
9495
}
9596

9697
// TODO make this configurable
@@ -152,6 +153,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
152153
break;
153154

154155
case TokenKind.Pipe:
156+
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
155157
bool pipelineIsFollowedByNewlineOrLineContinuation = tokenIndex < tokens.Length - 1 && tokenIndex > 0 &&
156158
(tokens[tokenIndex + 1].Kind == TokenKind.NewLine || tokens[tokenIndex + 1].Kind == TokenKind.LineContinuation);
157159
if (!pipelineIsFollowedByNewlineOrLineContinuation)
@@ -225,6 +227,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
225227
break;
226228
}
227229

230+
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
228231
// Check if the current token matches the end of a PipelineAst
229232
var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst =>
230233
PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst;

Tests/Rules/UseConsistentIndentation.tests.ps1

+22
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,29 @@ baz
225225
Test-CorrectionExtentFromContent @params
226226
}
227227

228+
It 'Should preserve script when using PipelineIndentation None' -TestCases @(
229+
@{ IdempotentScriptDefinition = @'
230+
foo |
231+
bar
232+
'@
233+
}
234+
@{ IdempotentScriptDefinition = @'
235+
foo |
236+
bar
237+
'@
238+
}
239+
) {
240+
param ($IdempotentScriptDefinition)
241+
242+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
243+
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
244+
}
245+
228246
It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
229247
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
230248
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
231249
@{ PipelineIndentation = 'NoIndentation' }
250+
@{ PipelineIndentation = 'None' }
232251
) {
233252
param ($PipelineIndentation)
234253
$idempotentScriptDefinition = @'
@@ -246,6 +265,7 @@ function hello {
246265
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
247266
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
248267
@{ PipelineIndentation = 'NoIndentation' }
268+
@{ PipelineIndentation = 'None' }
249269
) {
250270
param ($PipelineIndentation)
251271
$idempotentScriptDefinition = @'
@@ -264,6 +284,7 @@ function foo {
264284
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
265285
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
266286
@{ PipelineIndentation = 'NoIndentation' }
287+
@{ PipelineIndentation = 'None' }
267288
) {
268289
param ($PipelineIndentation)
269290
$idempotentScriptDefinition = @'
@@ -281,6 +302,7 @@ Describe 'describe' {
281302
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
282303
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
283304
@{ PipelineIndentation = 'NoIndentation' }
305+
@{ PipelineIndentation = 'None' }
284306
) {
285307
param ($PipelineIndentation)
286308
$idempotentScriptDefinition = @'

0 commit comments

Comments
 (0)