From 7380bb3be5e1d1798cd4da68d0855dafeba0e57c Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 30 Aug 2022 22:58:24 +0100 Subject: [PATCH 1/3] Fix NullReferenceException when analysing partial script with InnerBrace option of UseConsistentWhitespace --- Rules/UseConsistentWhitespace.cs | 4 ++-- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 257fc1240..13613ae74 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -260,7 +260,7 @@ private IEnumerable FindInnerBraceViolations(TokenOperations t foreach (var lCurly in tokenOperations.GetTokenNodes(TokenKind.LCurly)) { if (lCurly.Next == null - || !IsPreviousTokenOnSameLine(lCurly) + || !(lCurly.Previous == null || IsPreviousTokenOnSameLine(lCurly)) || lCurly.Next.Value.Kind == TokenKind.NewLine || lCurly.Next.Value.Kind == TokenKind.LineContinuation || lCurly.Next.Value.Kind == TokenKind.RCurly @@ -616,7 +616,7 @@ private List GetCorrections( } - private bool IsPreviousTokenOnSameLine(LinkedListNode lparen) + private static bool IsPreviousTokenOnSameLine(LinkedListNode lparen) { return lparen.Previous.Value.Extent.EndLineNumber == lparen.Value.Extent.StartLineNumber; } diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 9719dbfcb..70695f492 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -508,6 +508,10 @@ if ($true) { Get-Item ` '@ Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null } + + It "Should not throw when analysing a partial scriptblock" { + { Invoke-ScriptAnalyzer -ScriptDefinition '{ }' -Settings $settings -ErrorAction Stop } | Should -Not -Throw + } } From f6ab625f4aa9b5b377fe4b01164bb4d5bf6685c9 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 30 Aug 2022 22:59:34 +0100 Subject: [PATCH 2/3] tweak test name --- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 70695f492..87351c81d 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -509,7 +509,7 @@ if ($true) { Get-Item ` Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null } - It "Should not throw when analysing a partial scriptblock" { + It "Should not throw when analysing a line starting with a scriptblock" { { Invoke-ScriptAnalyzer -ScriptDefinition '{ }' -Settings $settings -ErrorAction Stop } | Should -Not -Throw } } From 4b8445046eec9e8453d9918d03c7cf06f4f561a5 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 30 Aug 2022 22:59:57 +0100 Subject: [PATCH 3/3] formatting --- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 87351c81d..31442ad77 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -509,7 +509,7 @@ if ($true) { Get-Item ` Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null } - It "Should not throw when analysing a line starting with a scriptblock" { + It 'Should not throw when analysing a line starting with a scriptblock' { { Invoke-ScriptAnalyzer -ScriptDefinition '{ }' -Settings $settings -ErrorAction Stop } | Should -Not -Throw } }