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

UseConsistentWhitespace - Create option to ignore assignment operator inside hash table #1566

Merged
merged 9 commits into from
Jan 5, 2021
19 changes: 10 additions & 9 deletions Engine/Settings/CodeFormatting.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
}

PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $true
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
}

PSAlignAssignmentStatement = @{
Expand Down
19 changes: 10 additions & 9 deletions Engine/Settings/CodeFormattingAllman.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
}

PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $true
}

PSAlignAssignmentStatement = @{
Expand Down
19 changes: 10 additions & 9 deletions Engine/Settings/CodeFormattingOTBS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
}

PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $true
}

PSAlignAssignmentStatement = @{
Expand Down
19 changes: 10 additions & 9 deletions Engine/Settings/CodeFormattingStroustrup.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
}

PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $true
}

PSAlignAssignmentStatement = @{
Expand Down
23 changes: 14 additions & 9 deletions RuleDocumentation/UseConsistentWhitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
```powershell
Rules = @{
PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $false
}
}
```
Expand Down Expand Up @@ -64,3 +65,7 @@ Checks if a pipe is surrounded by redundant whitespace (i.e. more than 1 whitesp

Checks if there is more than one space between parameters and values. E.g. `foo -bar $baz -bat` instead of `foo -bar $baz -bat`. This eliminates redundant whitespace that was probably added unintentionally.
The rule does not check for whitespace between parameter and value when the colon syntax `-ParameterName:$ParameterValue` is used as some users prefer either 0 or 1 whitespace in this case.

#### IgnoreAssignmentOperatorInsideHashTable: bool (Default value is `$false`)

When `CheckOperator` is set, ignore whitespace around assignment operators within multi-line hash tables. Set this option in order to use the `AlignAssignmentStatement` rule but still check whitespace around operators everywhere else.
26 changes: 26 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ private List<Func<TokenOperations, IEnumerable<DiagnosticRecord>>> violationFind
[ConfigurableRuleProperty(defaultValue: false)]
public bool CheckParameter { get; protected set; }

[ConfigurableRuleProperty(defaultValue: false)]
public bool IgnoreAssignmentOperatorInsideHashTable { get; protected set; }

public override void ConfigureRule(IDictionary<string, object> paramValueMap)
{
base.ConfigureRule(paramValueMap);
Expand Down Expand Up @@ -530,6 +533,29 @@ private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tok
continue;
}

// exclude assignment operator inside of multi-line hash tables if requested
if (IgnoreAssignmentOperatorInsideHashTable && tokenNode.Value.Kind == TokenKind.Equals)
{
var enclosingHashTables = tokenOperations.Ast.FindAll(a => a.Extent.StartOffset <= tokenNode.Value.Extent.StartOffset && a.Extent.EndOffset >= tokenNode.Value.Extent.EndOffset && a is HashtableAst, true);
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
if (enclosingHashTables.Count() > 0)
{
Ast innermostEnclosingHashTable = enclosingHashTables.First();
int smallestSizeSoFar = int.MaxValue;
foreach (var hashTable in enclosingHashTables){
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
int currentHashTableSize = hashTable.Extent.EndOffset - hashTable.Extent.StartOffset;
if (currentHashTableSize < smallestSizeSoFar)
{
innermostEnclosingHashTable = hashTable;
smallestSizeSoFar = currentHashTableSize;
}
}
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
if (innermostEnclosingHashTable.Extent.StartLineNumber != innermostEnclosingHashTable.Extent.EndLineNumber)
{
continue;
}
}
}
bergmeister marked this conversation as resolved.
Show resolved Hide resolved

var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode);
var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine
|| IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next);
Expand Down
54 changes: 54 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function foo($param) {
$ruleConfiguration.CheckOperator = $true
$ruleConfiguration.CheckPipe = $false
$ruleConfiguration.CheckSeparator = $false
$ruleConfiguration.IgnoreAssignmentOperatorInsideHashTable = $false
}

It "Should find a violation if no whitespace around an assignment operator" {
Expand Down Expand Up @@ -180,6 +181,59 @@ $x = $true -and
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should find violation if not asked to ignore assignment operator in hash table" {
$def = @'
$ht = @{
variable = 3
other = 4
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
}
}

Context "When asked to ignore assignment operator inside hash table" {
BeforeAll {
$ruleConfiguration.CheckInnerBrace = $false
$ruleConfiguration.CheckOpenParen = $false
$ruleConfiguration.CheckOpenBrace = $false
$ruleConfiguration.CheckOperator = $true
$ruleConfiguration.CheckPipe = $false
$ruleConfiguration.CheckSeparator = $false
$ruleConfiguration.IgnoreAssignmentOperatorInsideHashTable = $true
}
It "Should not find violation if assignment operator is in multi-line hash table" {
$def = @'
$ht = @{
variable = 3
other = 4
}
'@
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should find violation if assignment operator has extra space in single-line hash table" {
$def = @'
$h = @{
ht = @{a = 3; b = 4}
eb = 33
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
}

It "Should find violation for extra space around non-assignment operator inside hash table" {
$def = @'
$ht = @{
variable = 3
other = 4 + 7
}
'@
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
}
}

Context "When a comma is not followed by a space" {
Expand Down