FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

PSAlignAssignmentStatement: Ignore hashtables with a single key-value pair by liamjpeters · Pull Request #1983 · PowerShell/PSScriptAnalyzer · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (1) .ps1  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Rules/AlignAssignmentStatement.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL