Skip to content

Commit

Permalink
Rules>PSAlignAssignmentStatement: Treat single kvp hashtables as bein…
Browse files Browse the repository at this point in the history
…g on a single line, and not checked for violations. (#1986)

Co-authored-by: Christoph Bergmeister <[email protected]>
  • Loading branch information
liamjpeters and bergmeister authored Feb 19, 2025
1 parent aa7a582 commit 20135fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Rules/AlignAssignmentStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ private static List<Tuple<IScriptExtent, IScriptExtent>> GetExtents(

private bool HasPropertiesOnSeparateLines(IEnumerable<Tuple<IScriptExtent, IScriptExtent>> 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<int>();
foreach (var kvp in tuples)
{
Expand Down
27 changes: 27 additions & 0 deletions Tests/Rules/AlignAssignmentStatement.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 20135fb

Please sign in to comment.