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

PSUseConsistentWhitespace: Handle redirect operators which are not in stream order by liamjpeters · Pull Request #2001 · PowerShell/PSScriptAnalyzer · GitHub

Merged
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
11 changes: 10 additions & 1 deletion Rules/UseConsistentWhitespace.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 @@ -396,8 +396,17 @@ private IEnumerable<DiagnosticRecord> FindParameterViolations(Ast ast)
testAst => testAst is CommandAst, true);
foreach (CommandAst commandAst in commandAsts)
{
/// When finding all the command parameter elements, there is no guarantee that
/// we will read them from the AST in the order they appear in the script (in token
/// order). So we first sort the tokens by their starting line number, followed by
/// their starting column number.
List<Ast> commandParameterAstElements = commandAst.FindAll(
testAst => testAst.Parent == commandAst, searchNestedScriptBlocks: false).ToList();
testAst => testAst.Parent == commandAst, searchNestedScriptBlocks: false
).OrderBy(
e => e.Extent.StartLineNumber
).ThenBy(
e => e.Extent.StartColumnNumber
).ToList();
for (int i = 0; i < commandParameterAstElements.Count - 1; i++)
{
IScriptExtent leftExtent = commandParameterAstElements[i].Extent;
Expand Down
14 changes: 14 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.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 @@ -540,6 +540,12 @@ bar -h i `
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should not find a violation when redirect operators, spearated by 1 space, are used and not in stream order" {
# Related to Issue #2000
$def = 'foo 3>&1 1>$null 2>&1'
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should find 1 violation if there is 1 space too much before a parameter" {
$def = 'foo -bar'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Expand Down Expand Up @@ -578,5 +584,13 @@ bar -h i `
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
Should -Be "$expected"
}

It "Should fix script when redirects are involved and whitespace is not consistent" {
# Related to Issue #2000
$def = 'foo 3>&1 1>$null 2>&1'
$expected = 'foo 3>&1 1>$null 2>&1'
Invoke-Formatter -ScriptDefinition $def -Settings $settings |
Should -Be $expected
}
}
}

Back | FazBrowse Home | New Git URL