Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 27, 2020
Merged
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
@@ -53,6 +53,7 @@ foo |
bar |
baz
```
- None: Do not change any existing pipeline indentation.

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

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

// TODO make this configurable
@@ -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)
@@ -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;
22 changes: 22 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
@@ -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 = @'
@@ -246,6 +265,7 @@ function hello {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
@@ -264,6 +284,7 @@ function foo {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'
@@ -281,6 +302,7 @@ Describe 'describe' {
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@{ PipelineIndentation = 'NoIndentation' }
@{ PipelineIndentation = 'None' }
) {
param ($PipelineIndentation)
$idempotentScriptDefinition = @'