diff --git a/Rules/AlignAssignmentStatement.cs b/Rules/AlignAssignmentStatement.cs index d8b1623d6..1d79870f2 100644 --- a/Rules/AlignAssignmentStatement.cs +++ b/Rules/AlignAssignmentStatement.cs @@ -314,6 +314,11 @@ private static List> GetExtents( private bool HasPropertiesOnSeparateLines(IEnumerable> tuples) { + if (tuples.Count() == 1) + { + // If the hashtable has just a single key-value pair, it does not have properties on separate lines + return false; + } var lines = new HashSet(); foreach (var kvp in tuples) { diff --git a/Tests/Rules/AlignAssignmentStatement.tests.ps1 b/Tests/Rules/AlignAssignmentStatement.tests.ps1 index 9a94f48ce..7558abf88 100644 --- a/Tests/Rules/AlignAssignmentStatement.tests.ps1 +++ b/Tests/Rules/AlignAssignmentStatement.tests.ps1 @@ -75,6 +75,33 @@ $x = @{ } Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0 } + + It "Should ignore if a hashtable has a single key-value pair on a single line" { + $def = @' +$x = @{ 'key'="value" } +'@ + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0 + + } + + It "Should ignore if a hashtable has a single key-value pair across multiple lines" { + $def = @' +$x = @{ + 'key'="value" +} +'@ + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0 + + } + + It "Should ignore if a hashtable has multiple key-value pairs on a single line" { + $def = @' +$x = @{ 'key'="value"; 'key2'="value2"; 'key3WithLongerName'="value3" } +'@ + Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Get-Count | Should -Be 0 + + } + } Context "When assignment statements are in DSC Configuration" {