Skip to content

Commit 20135fb

Browse files
Rules>PSAlignAssignmentStatement: Treat single kvp hashtables as being on a single line, and not checked for violations. (#1986)
Co-authored-by: Christoph Bergmeister <c.bergmeister@gmail.com>
1 parent aa7a582 commit 20135fb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Rules/AlignAssignmentStatement.cs

+5
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ private static List<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
314314

315315
private bool HasPropertiesOnSeparateLines(IEnumerable<Tuple<IScriptExtent, IScriptExtent>> tuples)
316316
{
317+
if (tuples.Count() == 1)
318+
{
319+
// If the hashtable has just a single key-value pair, it does not have properties on separate lines
320+
return false;
321+
}
317322
var lines = new HashSet<int>();
318323
foreach (var kvp in tuples)
319324
{

Tests/Rules/AlignAssignmentStatement.tests.ps1

+27
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ $x = @{ }
7575
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0
7676

7777
}
78+
79+
It "Should ignore if a hashtable has a single key-value pair on a single line" {
80+
$def = @'
81+
$x = @{ 'key'="value" }
82+
'@
83+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0
84+
85+
}
86+
87+
It "Should ignore if a hashtable has a single key-value pair across multiple lines" {
88+
$def = @'
89+
$x = @{
90+
'key'="value"
91+
}
92+
'@
93+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0
94+
95+
}
96+
97+
It "Should ignore if a hashtable has multiple key-value pairs on a single line" {
98+
$def = @'
99+
$x = @{ 'key'="value"; 'key2'="value2"; 'key3WithLongerName'="value3" }
100+
'@
101+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0
102+
103+
}
104+
78105
}
79106

80107
Context "When assignment statements are in DSC Configuration" {

0 commit comments

Comments
 (0)