Skip to content

Commit 5dc3cd3

Browse files
committed
UseConsistentWhitespace - Create option to ignore assignment operator inside hash table
1 parent c78f5a6 commit 5dc3cd3

7 files changed

+134
-45
lines changed

Engine/Settings/CodeFormatting.psd1

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
}
3232

3333
PSUseConsistentWhitespace = @{
34-
Enable = $true
35-
CheckInnerBrace = $true
36-
CheckOpenBrace = $true
37-
CheckOpenParen = $true
38-
CheckOperator = $true
39-
CheckPipe = $true
40-
CheckPipeForRedundantWhitespace = $false
41-
CheckSeparator = $true
42-
CheckParameter = $false
34+
Enable = $true
35+
CheckInnerBrace = $true
36+
CheckOpenBrace = $true
37+
CheckOpenParen = $true
38+
CheckOperator = $true
39+
CheckPipe = $true
40+
CheckPipeForRedundantWhitespace = $false
41+
CheckSeparator = $true
42+
CheckParameter = $false
43+
IgnoreAssignmentOperatorInsideHashTable = $true
4344
}
4445

4546
PSAlignAssignmentStatement = @{

Engine/Settings/CodeFormattingAllman.psd1

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
}
3232

3333
PSUseConsistentWhitespace = @{
34-
Enable = $true
35-
CheckInnerBrace = $true
36-
CheckOpenBrace = $true
37-
CheckOpenParen = $true
38-
CheckOperator = $true
39-
CheckPipe = $true
40-
CheckPipeForRedundantWhitespace = $false
41-
CheckSeparator = $true
42-
CheckParameter = $false
34+
Enable = $true
35+
CheckInnerBrace = $true
36+
CheckOpenBrace = $true
37+
CheckOpenParen = $true
38+
CheckOperator = $true
39+
CheckPipe = $true
40+
CheckPipeForRedundantWhitespace = $false
41+
CheckSeparator = $true
42+
CheckParameter = $false
43+
IgnoreAssignmentOperatorInsideHashTable = $true
4344
}
4445

4546
PSAlignAssignmentStatement = @{

Engine/Settings/CodeFormattingOTBS.psd1

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
}
3232

3333
PSUseConsistentWhitespace = @{
34-
Enable = $true
35-
CheckInnerBrace = $true
36-
CheckOpenBrace = $true
37-
CheckOpenParen = $true
38-
CheckOperator = $true
39-
CheckPipe = $true
40-
CheckPipeForRedundantWhitespace = $false
41-
CheckSeparator = $true
42-
CheckParameter = $false
34+
Enable = $true
35+
CheckInnerBrace = $true
36+
CheckOpenBrace = $true
37+
CheckOpenParen = $true
38+
CheckOperator = $true
39+
CheckPipe = $true
40+
CheckPipeForRedundantWhitespace = $false
41+
CheckSeparator = $true
42+
CheckParameter = $false
43+
IgnoreAssignmentOperatorInsideHashTable = $true
4344
}
4445

4546
PSAlignAssignmentStatement = @{

Engine/Settings/CodeFormattingStroustrup.psd1

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@
3232
}
3333

3434
PSUseConsistentWhitespace = @{
35-
Enable = $true
36-
CheckInnerBrace = $true
37-
CheckOpenBrace = $true
38-
CheckOpenParen = $true
39-
CheckOperator = $true
40-
CheckPipe = $true
41-
CheckPipeForRedundantWhitespace = $false
42-
CheckSeparator = $true
43-
CheckParameter = $false
35+
Enable = $true
36+
CheckInnerBrace = $true
37+
CheckOpenBrace = $true
38+
CheckOpenParen = $true
39+
CheckOperator = $true
40+
CheckPipe = $true
41+
CheckPipeForRedundantWhitespace = $false
42+
CheckSeparator = $true
43+
CheckParameter = $false
44+
IgnoreAssignmentOperatorInsideHashTable = $true
4445
}
4546

4647
PSAlignAssignmentStatement = @{

RuleDocumentation/UseConsistentWhitespace.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
```powershell
1414
Rules = @{
1515
PSUseConsistentWhitespace = @{
16-
Enable = $true
17-
CheckInnerBrace = $true
18-
CheckOpenBrace = $true
19-
CheckOpenParen = $true
20-
CheckOperator = $true
21-
CheckPipe = $true
22-
CheckPipeForRedundantWhitespace = $false
23-
CheckSeparator = $true
24-
CheckParameter = $false
16+
Enable = $true
17+
CheckInnerBrace = $true
18+
CheckOpenBrace = $true
19+
CheckOpenParen = $true
20+
CheckOperator = $true
21+
CheckPipe = $true
22+
CheckPipeForRedundantWhitespace = $false
23+
CheckSeparator = $true
24+
CheckParameter = $false
25+
IgnoreAssignmentOperatorInsideHashTable = $false
2526
}
2627
}
2728
```
@@ -64,3 +65,7 @@ Checks if a pipe is surrounded by redundant whitespace (i.e. more than 1 whitesp
6465

6566
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.
6667
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.
68+
69+
#### IgnoreAssignmentOperatorInsideHashTable: bool (Default value is `$false`)
70+
71+
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.

Rules/UseConsistentWhitespace.cs

+26
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ private List<Func<TokenOperations, IEnumerable<DiagnosticRecord>>> violationFind
6363
[ConfigurableRuleProperty(defaultValue: false)]
6464
public bool CheckParameter { get; protected set; }
6565

66+
[ConfigurableRuleProperty(defaultValue: false)]
67+
public bool IgnoreAssignmentOperatorInsideHashTable { get; protected set; }
68+
6669
public override void ConfigureRule(IDictionary<string, object> paramValueMap)
6770
{
6871
base.ConfigureRule(paramValueMap);
@@ -530,6 +533,29 @@ private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tok
530533
continue;
531534
}
532535

536+
// exclude assignment operator inside of multi-line hash tables if requested
537+
if (IgnoreAssignmentOperatorInsideHashTable && tokenNode.Value.Kind == TokenKind.Equals)
538+
{
539+
var enclosingHashTables = tokenOperations.Ast.FindAll(a => a.Extent.StartOffset <= tokenNode.Value.Extent.StartOffset && a.Extent.EndOffset >= tokenNode.Value.Extent.EndOffset && a is HashtableAst, true);
540+
if (enclosingHashTables.Count() > 0)
541+
{
542+
Ast innermostEnclosingHashTable = enclosingHashTables.First();
543+
int smallestSizeSoFar = int.MaxValue;
544+
foreach (var hashTable in enclosingHashTables){
545+
int currentHashTableSize = hashTable.Extent.EndOffset - hashTable.Extent.StartOffset;
546+
if (currentHashTableSize < smallestSizeSoFar)
547+
{
548+
innermostEnclosingHashTable = hashTable;
549+
smallestSizeSoFar = currentHashTableSize;
550+
}
551+
}
552+
if (innermostEnclosingHashTable.Extent.StartLineNumber != innermostEnclosingHashTable.Extent.EndLineNumber)
553+
{
554+
continue;
555+
}
556+
}
557+
}
558+
533559
var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode);
534560
var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine
535561
|| IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next);

Tests/Rules/UseConsistentWhitespace.tests.ps1

+54
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function foo($param) {
127127
$ruleConfiguration.CheckOperator = $true
128128
$ruleConfiguration.CheckPipe = $false
129129
$ruleConfiguration.CheckSeparator = $false
130+
$ruleConfiguration.IgnoreAssignmentOperatorInsideHashTable = $false
130131
}
131132

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

184+
It "Should find violation if not asked to ignore assignment operator in hash table" {
185+
$def = @'
186+
$ht = @{
187+
variable = 3
188+
other = 4
189+
}
190+
'@
191+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
192+
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
193+
}
194+
}
195+
196+
Context "When asked to ignore assignment operator inside hash table" {
197+
BeforeAll {
198+
$ruleConfiguration.CheckInnerBrace = $false
199+
$ruleConfiguration.CheckOpenParen = $false
200+
$ruleConfiguration.CheckOpenBrace = $false
201+
$ruleConfiguration.CheckOperator = $true
202+
$ruleConfiguration.CheckPipe = $false
203+
$ruleConfiguration.CheckSeparator = $false
204+
$ruleConfiguration.IgnoreAssignmentOperatorInsideHashTable = $true
205+
}
206+
It "Should not find violation if assignment operator is in multi-line hash table" {
207+
$def = @'
208+
$ht = @{
209+
variable = 3
210+
other = 4
211+
}
212+
'@
213+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
214+
}
215+
216+
It "Should find violation if assignment operator has extra space in single-line hash table" {
217+
$def = @'
218+
$h = @{
219+
ht = @{a = 3; b = 4}
220+
eb = 33
221+
}
222+
'@
223+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
224+
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
225+
}
226+
227+
It "Should find violation for extra space around non-assignment operator inside hash table" {
228+
$def = @'
229+
$ht = @{
230+
variable = 3
231+
other = 4 + 7
232+
}
233+
'@
234+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
235+
Test-CorrectionExtentFromContent $def $violations 1 ' ' ' '
236+
}
183237
}
184238

185239
Context "When a comma is not followed by a space" {

0 commit comments

Comments
 (0)