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

PSAlignAssignmentStatement: Ignore hashtables with a single key-value pair #1983

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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