Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RuleDocumentation/UseConsistentIndentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ foo |
bar |
baz
```
- None: Do not change any existing pipeline indentation.

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

Expand Down
5 changes: 4 additions & 1 deletion Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ private enum PipelineIndentationStyle
{
IncreaseIndentationForFirstPipeline,
IncreaseIndentationAfterEveryPipeline,
NoIndentation
NoIndentation,
None
}

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

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

if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
// Check if the current token matches the end of a PipelineAst
var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst =>
PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst;
Expand Down
22 changes: 22 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,29 @@ baz
Test-CorrectionExtentFromContent @params
}

It 'Should preserve script when using PipelineIndentation None' -TestCases @(
@{ IdempotentScriptDefinition = @'
foo |
bar
'@
}
@{ IdempotentScriptDefinition = @'
foo |
bar
'@
}
) {
param ($IdempotentScriptDefinition)

$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
Expand All @@ -246,6 +265,7 @@ function hello {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
Expand All @@ -264,6 +284,7 @@ function foo {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
Expand All @@ -281,6 +302,7 @@ Describe 'describe' {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
Expand Down